> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.tLtghOnlQqT/rev.3
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * CellGrid (v2.0)
 * by GoToLoop (2015/Jul/09)
 *
 * forum.Processing.org/two/discussion/11605/100x100-grid-draw-at-2-fps
 * studio.ProcessingTogether.com/sp/pad/export/ro.9KbaSot4iSjhZ/latest
 */

const W = 640, H = 480, FPS = 30,
      COLS = 100, ROWS = 80,
      WIDE = W/COLS, TALL = H/ROWS,
      ON = -1, OFF = 0,
      RESHUFFLE = true, // change it to false to stop auto-reshuffle.
      cells = new Uint8Array(COLS*ROWS);

void setup() {
  size(W, H, JAVA2D);
  noSmooth(); noStroke(); rectMode(CORNER); frameRate(FPS);
  RESHUFFLE || randomCellRefill();
}

void draw() {
  RESHUFFLE && randomCellRefill();
  println(frameRate);

  for (var col = 0, row = 0, cur = 0, tall = 0, now, last; row != ROWS; 
       cur = ++row*COLS, tall = TALL*row, col = 0)
    while (col != COLS) {
      (now = cells[cur + col]) != last && fill(last = now? ON : OFF);
      rect(WIDE*col++, tall, WIDE + 1, TALL);
    }
}

void mousePressed() {
  RESHUFFLE || randomCellRefill();
}
 
function randomCellRefill() {
  for (var idx = cells.length; idx--; cells[idx] = random(1) < .5);
}