import { mountButton } from './ui/button'

interface Particle {
  x: number
  y: number
  vx: number
  vy: number
  life: number
}

const particles: Particle[] = []

function spawn(x: number, y: number): void {
  for (let i = 0; i < 80; i++) {
    const a = (Math.PI * 2 * i) / 80
    particles.push({ x, y, vx: Math.cos(a) * 3, vy: Math.sin(a) * 3, life: 1 })
  }
}

function tick(ctx: CanvasRenderingContext2D): void {
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
  for (const p of particles) {
    p.x += p.vx
    p.y += p.vy
    p.vy += 0.05
    p.life -= 0.01
    ctx.globalAlpha = Math.max(p.life, 0)
    ctx.fillRect(p.x, p.y, 2, 2)
  }
  requestAnimationFrame(() => tick(ctx))
}

const canvas = document.querySelector('canvas')!
const ctx = canvas.getContext('2d')!
mountButton(() => spawn(canvas.width / 2, canvas.height / 2))
tick(ctx)
