Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
fix color not working, add confetti example
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Oct 11, 2023
1 parent 53ca2e3 commit 68285df
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v3000.1.12
- fix `color()` and `rgb()` not working

### v3000.1.11
- added option `kaboom({ focus: false })` to disable focus on start
- fixed `rand()` typing for numbers
Expand Down
55 changes: 55 additions & 0 deletions examples/confetti.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const DEF_COUNT = 80
const DEF_GRAVITY = 800
const DEF_AIR_DRAG = 0.9
const DEF_VELOCITY = [1000, 4000]
const DEF_ANGULAR_VELOCITY = [-200, 200]
const DEF_FADE = 0.3
const DEF_SPREAD = 60
const DEF_SPIN = [2, 8]
const DEF_SATURATION = 0.7
const DEF_LIGHTNESS = 0.6

kaboom()

function addConfetti(opt = {}) {
const sample = (s) => typeof s === "function" ? s() : s
for (let i = 0; i < (opt.count ?? DEF_COUNT); i++) {
const p = add([
pos(sample(opt.pos ?? vec2(0, 0))),
choose([
rect(rand(5, 20), rand(5, 20)),
circle(rand(3, 10)),
]),
color(sample(opt.color ?? hsl2rgb(rand(0, 1), DEF_SATURATION, DEF_LIGHTNESS))),
opacity(1),
lifespan(4),
scale(1),
anchor("center"),
rotate(rand(0, 360)),
])
const spin = rand(DEF_SPIN[0], DEF_SPIN[1])
const gravity = opt.gravity ?? DEF_GRAVITY
const airDrag = opt.airDrag ?? DEF_AIR_DRAG
const heading = (opt.heading ?? 0) - 90
const spread = opt.spread ?? DEF_SPREAD
const head = heading + rand(-spread / 2, spread / 2)
const fade = opt.fade ?? DEF_FADE
const vel = sample(opt.velocity ?? rand(DEF_VELOCITY[0], DEF_VELOCITY[1]))
let velX = Math.cos(deg2rad(head)) * vel
let velY = Math.sin(deg2rad(head)) * vel
const velA = sample(opt.angularVelocity ?? rand(DEF_ANGULAR_VELOCITY[0], DEF_ANGULAR_VELOCITY[1]))
p.onUpdate(() => {
velY += gravity * dt()
p.pos.x += velX * dt()
p.pos.y += velY * dt()
p.angle += velA * dt()
p.opacity -= fade * dt()
velX *= airDrag
velY *= airDrag
p.scale.x = wave(-1, 1, time() * spin)
})
}
}

onKeyPress("space", () => addConfetti({ pos: mousePos() }))
onMousePress(() => addConfetti({ pos: mousePos() }))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kaboom",
"description": "kaboom.js is a JavaScript library that helps you make games fast and fun!",
"version": "3000.1.11",
"version": "3000.1.12",
"license": "MIT",
"homepage": "https://kaboomjs.com/",
"repository": "github:replit/kaboom",
Expand Down
2 changes: 1 addition & 1 deletion src/kaboom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const VERSION = "3000.1.11"
const VERSION = "3000.1.12"

import initApp from "./app"

Expand Down
3 changes: 2 additions & 1 deletion src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export class Color {

return new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255))


}

static RED = new Color(255, 0, 0)
Expand Down Expand Up @@ -322,6 +321,8 @@ export class Color {

export function rgb(...args): Color {
if (args.length === 0) {
return new Color(255, 255, 255)
} else if (args.length === 1) {
if (args[0] instanceof Color) {
return args[0].clone()
} else if (typeof args[0] === "string") {
Expand Down

0 comments on commit 68285df

Please sign in to comment.