Last active
October 13, 2018 02:43
-
-
Save GoSubRoutine/78bcd2ae59f6db72d5e6736589d2856b to your computer and use it in GitHub Desktop.
Toggle Rects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height: 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script async src=https://CDN.JSDelivr.net/npm/p5></script> | |
<script defer src=sketch.js></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Toggle Rects (v1.1.3) | |
* GoToLoop (2017-Dec-26) | |
* | |
* Forum.Processing.org/two/discussion/25722/click-event-on-rect-object#Item_2 | |
* | |
* Bl.ocks.org/GoSubRoutine/78bcd2ae59f6db72d5e6736589d2856b | |
* Alpha.Editor.p5js.org/zusamarehan/sketches/B1GpXWlQf | |
*/ | |
"use strict"; | |
const ROWS = 3, COLS = 3, GRID = ROWS*COLS, rects = Array(GRID); | |
function setup() { | |
createCanvas(400, 400).mousePressed(toggleRect); | |
rectMode(CORNER).colorMode(RGB).noLoop(); | |
stroke(Rect.STROKE).strokeWeight(Rect.BOLD); | |
createRectGrid(rects); | |
} | |
function draw() { | |
background(Rect.BG); | |
for (const b of rects) b.display(); | |
} | |
function toggleRect() { | |
//function toggleRect(evt) { | |
// _onmousedown(evt); // workaround for mouseButton, mouseX & mouseY! | |
// evt.preventDefault(); | |
// evt.stopPropagation(); | |
switch (mouseButton) { | |
case RIGHT: // clear all | |
for (const b of rects) b.fills = false; | |
return redraw(); | |
case CENTER: // toggle all | |
for (const b of rects) b.fills ^= true; | |
return redraw(); | |
default: // toggle clicked at | |
for (const b of rects) if (b.isMouseInside()) return redraw(b.fills ^= true); | |
} | |
} | |
function createRectGrid(arr=Array(GRID)) { | |
for (let idx = 0, y = 0; y < ROWS; ++y) for (let x = 0; x < COLS; ++x, ++idx) | |
arr[idx] = new Rect(x*Rect.W, y*Rect.H); | |
return arr; | |
} | |
class Rect { | |
static get BG() { | |
delete this.BG; | |
return this.BG = color(0); | |
} | |
static get ON() { | |
delete this.ON; | |
return this.ON = color('lime'); | |
} | |
static get OFF() { | |
delete this.OFF; | |
return this.OFF = color(0o340); | |
} | |
static get STROKE() { | |
delete this.STROKE; | |
return this.STROKE = color(150); | |
} | |
static get BOLD() { | |
delete this.BOLD; | |
return this.BOLD = 5; | |
} | |
static get W() { | |
delete this.W; | |
return this.W = width/3 | 0; | |
} | |
static get H() { | |
delete this.H; | |
return this.H = height>>2; | |
} | |
constructor(x=0, y=0) { | |
this.x = x, this.y = y, this.fills = false; | |
} | |
display() { | |
const {x, y, fills} = this, {W, H, ON, OFF} = this.constructor; | |
fill(fills && ON || OFF).rect(x, y, W, H); | |
return this; | |
} | |
isMouseInside() { | |
const {x, y} = this, {W, H} = this.constructor; | |
return mouseX >= x && mouseX < x+W && mouseY >= y && mouseY < y+H; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment