trail-1
2013. 09. 23.

It's been a while. This is a little variation of an-year-old trail-0.

Press enter key to clear the screen and toggle the background color.

» launch trail-1

p5        = processing
num_bikes = 10
bikes     = []
bg        = 1
crange    =
  hue: [0.0, 1.0],
  sat: [0.4, 1.0],
  bri: [0.5, 1.0]

# Penner equation :)
easeInOut = (t, b, c, d) ->
  if (t /= d / 2) < 1
    c / 2 * pow(t, 3) + b
  else
    return c / 2 * (pow(t - 2, 3) + 2) + b

class Bike
  constructor: ->
    @init()

  init_struct: (x) ->
    x.t = x.d = 0
    x.c = x.s = x.e = x.i()

  init: ->
    @x = random(0, p5.width)
    @y = random(0, p5.height)
    @r =
      i: -> random(- PI, PI)
      r: -> random(10, 200)
      p: random(1, 10)
    @init_struct @r

    @w =
      i: -> random(5, 30)
      r: -> random(10, 200)
      p: random(1, 10)
    @init_struct @w

    @v = random(1, 3)
    @c =
      if random(100) < 15
        color bg
      else
        color random(crange.hue...),
              random(crange.sat...),
              random(crange.bri...)

  move: ->
    step = 1
    for _ in [0..@v] by step
      @x += step * cos(@r.c)
      @y += step * sin(@r.c)

      stroke @c, 0.1
      w = 1
      for s in [1..10]
        w = easeInOut(s, w, @w.c, 10)
        strokeWeight w
        point @x, @y
        break if w >= @w.c
      stroke @c, 0.7
      for _ in [1..random(8)]
        strokeWeight random(1.0, @w.c / 4)
        point @x + random(@w.c * 2) - @w.c, @y + random(@w.c * 2) - @w.c

      if @x > p5.width or @x < 0 or @y < 0 or @y > p5.height
        @init()

    @apply(@r)
    @apply(@w)
    @turn(@r, random(100))
    @turn(@w, random(200))

  apply: (x) ->
    if x.t < x.d
      x.c = easeInOut(x.t, x.s, x.e - x.s, x.d)
      x.t += 1

  turn: (x, r) ->
    if r < x.p
      x.s = x.c
      x.e = x.i()
      x.t = 0
      x.d = x.r()

setup = ->
  size       $(window).width(), $(window).height()
  frameRate  60
  colorMode  HSB, 1.0
  background bg

  for t in [0...num_bikes]
    bikes.push new Bike()

draw = ->
  for b in bikes
    b.move()

keyPressed = ->
  if keyCode() == ENTER || keyCode() == RETURN
    bg = (bg + 1) % 2
    background bg
    for b in bikes
      b.init()
» capture | close