> 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.17
 * 
 * authors: 
 *   GoToLoop

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



/**
 * CellGrid (v5.2)
 * 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
 */
 
const W = 640, H = 480, FPS = 30,
      COLS = 100, ROWS = 80, ELEMS = 3, DIM = COLS*ROWS*ELEMS,
      WIDE = W/COLS, TALL = H/ROWS,
      WIDE_ADJ = WIDE + .5, TALL_ADJ = TALL + .5,
      ON = -1, OFF = 0,
      RESHUFFLE = true, // change it to false to stop auto-reshuffle.
      cells = new Int16Array(DIM);
 
void setup() {
  size(W, H, JAVA2D);
  noSmooth(); noStroke(); rectMode(CORNER); frameRate(FPS);
  fill(ON);
  setCellCoords();
}
 
void draw() {
  RESHUFFLE && randomCellRefill();
  println(frameRate);
 
  background(OFF);
  for (var idx = 0; idx < DIM; idx += ELEMS)  cells[idx] &&
    rect(cells[idx+1], cells[idx+2], WIDE_ADJ, TALL_ADJ);
}
 
void mousePressed() {
  RESHUFFLE || randomCellRefill();
}
 
function randomCellRefill() {
  for (var idx = 0; idx < DIM; idx += ELEMS)
    cells[idx] = random(1) < .5;
}
 
function setCellCoords() {
  RESHUFFLE || randomCellRefill();
  for (var x, y, idx = 0; idx < DIM; idx += ELEMS) {
    x = idx/ELEMS%COLS * WIDE; y = (idx/ELEMS/COLS | 0) * TALL;
    cells[idx+1] = round(x); cells[idx+2] = round(y);
  }
}