particles-0
2012. 04. 30.

Click and drag on the screen to create particles. Press enter key to clear the screen.

» launch particles-0

p5        = processing
particles = []
zrand     = (r) -> random(r) - r / 2

class Particle
  constructor: (@x, @y, @vx, @vy) ->
    @r    = random 50, 255
    @g    = random 0, @r
    @b    = random 0, @g
    @a    = 255
    @dead = false

  move: ->
    @x += @vx
    @y += @vy

    if @x > p5.width or @x < 0 or @y > p5.height
      @dead = true

    @vx *= 0.995
    @vy += 0.1
    @a  *= 0.98

  draw: ->
    stroke @r, @g, @b, @a
    line   @x, @y, @x - @vx, @y - @vy

  alive: -> !@dead

setup = ->
  size $(window).width(), $(window).height()
  background 255

draw = ->
  if __mousePressed() and particles.length < 300
    for i in [0...5]
      particles.push(
        new Particle(
          p5.mouseX + zrand(30),
          p5.mouseY + zrand(30),
          constrain(p5.mouseX - p5.pmouseX, -5, 5) -
              zrand(10),
          constrain(p5.mouseY - p5.pmouseY, -5, 5) -
              zrand(10) - 2))

  for p in particles
    p.move()
    p.draw()

  particles = (p for p in particles when p.alive())

keyPressed = ->
  if keyCode() == ENTER
    particles = []
    background 255
» capture | close