weave-0
2012. 06. 24.

Basic weaving pattern. Click on the screen to randomize the pattern. Press spacebar to pause and resume drawing. Press enter key to toggle background color.

» launch weave-0

rint          = (a, b) -> parseInt random(a, b)
Array::sample = -> this[rint(0, this.length)]
p5  = processing
bgs = [(-> color(random(), random(), random(0, 0.2))), (-> 1.0), (-> 0.0)]

colors    = undefined
thickness = undefined
grid_size = undefined
randness  = undefined
slots     = undefined
ncomp     = undefined
vtx       = undefined
paused    = false

setup = ->
  frameRate 60
  noFill()
  randomize()
  reset()

reset = ->
  background bgs[0]()
  paused = false
  p5.loop()

randomize = ->
  size window.p5w or $(window).width(), window.p5h or $(window).height()
  colorMode HSB, 1.0
  colors = for i in [0...rint(5, 10)]
      color(random(1.0), random(0.0, 1.0), random(0.3, 1.0))
  thickness = p5.width * random(0.01, 0.02)
  grid_size = thickness * random(1, 3)
  randness  = random(0.1, 0.5)

  nx1 = random(0.0001, 0.005)
  ny1 = random(0.0001, 0.005)
  nx2 = random(0.001, 0.05)
  ny2 = random(0.001, 0.05)
  nm1 = random(1, 32)
  nm2 = random(1, 8)
  noiseSeed random()

  ncomp = (x, y) ->
    (noise(x * nx1, y * ny1) - 0.5) * nm1 +
    (noise(x * nx2, y * ny2) - 0.5) * nm2

  vtx = (x, y) ->
    x += ncomp(x, y) * grid_size
    y += ncomp(x, y) * grid_size
    curveVertex x, y
    for t in [0...3]
      point x + random(-0.5, 0.5) * grid_size,
            y + random(-0.5, 0.5) * grid_size

  xslots = for i in [0..parseInt(p5.width / grid_size)]
    {x: i, c: colors.sample()}
  yslots = for i in [0..parseInt(p5.height / grid_size)]
    {y: i, c: colors.sample()}
  slots = xslots.concat yslots

draw = ->
  alpha = 0.1

  slot = slots.sample()
  stroke slot.c, alpha
  strokeWeight random() * 4
  for t in [0...5]
    beginShape()
    if slot.x?
      x = (slot.x + 0.5) * grid_size
      for y in [-grid_size*4..p5.height+grid_size*4] by grid_size
        vtx x + randness * thickness * random(-1, 1) / 5,
            y + randness * thickness * random(-1, 1) / 5
        vtx x + randness * thickness * random(-1, 1),
            y + grid_size / 2
    else
      y = (slot.y + 0.5) * grid_size
      for x in [-grid_size*4..p5.width+grid_size*4] by grid_size
        vtx x + randness * thickness * random(-1, 1) / 5,
            y + randness * thickness * random(-1, 1) / 5
        vtx x + grid_size / 2,
            y + randness * thickness * random(-1, 1)
    endShape()

mousePressed = ->
  randomize()
  reset()

keyPressed = ->
  if keyCode() == ENTER || keyCode() == RETURN
    bgs.push bgs.shift()
    reset()
  else if keyCode() == 32
    if !paused
      noLoop()
      paused = true
    else
      paused = false
      p5.loop()
» capture | close