Skip to content

gitflarePublicPartly agent-ready40

sketch-rush

Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.

Add the Apache-2.0 licencedbe1e99
JavaScript37 lines1.4 KB
// Pooled confetti particles: no allocation after creation.const N = 220;const COLORS = ["#ff3b3b", "#ff9f1c", "#ffe14d", "#35d96b", "#2f8bff", "#b44dff", "#ffffff"];export function createParticles() {  const x = new Float32Array(N), y = new Float32Array(N), vx = new Float32Array(N), vy = new Float32Array(N);  const life = new Float32Array(N), size = new Float32Array(N), col = new Uint8Array(N);  let head = 0, seed = 12345;  const rnd = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296);  return {    count: N,    life,    burst(cx, cy, n, power) {      for (let k = 0; k < n; k++) {        const i = head; head = (head + 1) % N;        const a = rnd() * 6.2832, s = (0.3 + rnd()) * power;        x[i] = cx; y[i] = cy; vx[i] = Math.cos(a) * s; vy[i] = Math.sin(a) * s - power * 0.5;        life[i] = 1 + rnd() * 0.8; size[i] = 6 + rnd() * 10; col[i] = (rnd() * COLORS.length) | 0;      }    },    update(dt) {      for (let i = 0; i < N; i++) {        if (life[i] <= 0) continue;        life[i] -= dt; vy[i] += 900 * dt; x[i] += vx[i] * dt; y[i] += vy[i] * dt;      }    },    draw(ctx) {      for (let i = 0; i < N; i++) {        if (life[i] <= 0) continue;        ctx.globalAlpha = life[i] > 1 ? 1 : life[i];        ctx.fillStyle = COLORS[col[i]];        ctx.fillRect(x[i], y[i], size[i], size[i] * 0.6);      }      ctx.globalAlpha = 1;    },  };}