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

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



/**
 * CellGrid (v4.1)
 * 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, BYTES = 5,
      WIDE = W/COLS, WIDE_PLUS = WIDE + 1, TALL = H/ROWS,
      ON = -1, OFF = 0,
      RESHUFFLE = true, // change it to false to stop auto-reshuffle.
      cells = new DataView(new ArrayBuffer(COLS*ROWS*BYTES));

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 < cells.byteLength; idx += BYTES)  cells.getInt8(idx) &&
    rect(cells.getInt16(idx + 1, true), cells.getInt16(idx + 3, true), WIDE_PLUS, TALL);
}

void mousePressed() {
  RESHUFFLE || randomCellRefill();
}

function randomCellRefill() {
  for (var idx = 0; idx < cells.byteLength; idx += BYTES)
    cells.setInt8(idx, random(1) < .5);
}

function setCellCoords() {
  RESHUFFLE || randomCellRefill();
  for (var x, y, idx = 0; idx < cells.byteLength; idx += BYTES) {
    x = idx/BYTES%COLS * WIDE; y = (idx/BYTES/COLS | 0) * TALL;
    cells.setInt16(idx + 1, x, true); cells.setInt16(idx + 3, y, true);
  }
}