xxxxxxxxxx
82
function makeGrating() {
return {
amp: random(20, 60),
period: random(20, 100),
dphase: random(PI),
leading: random(0.85, 1.5),
ori: random(PI),
};
}
function inGrating(x, y, g) {
const c = cos(-g.ori);
const s = sin(-g.ori);
let rx = c * x - s * y;
let ry = s * x + c * y;
let res = false;
let p = 0;
if (ry >= sin((rx / g.period) * TWO_PI) * g.amp) {
// Count up
while (ry >= sin((rx / g.period) * TWO_PI + p) * g.amp) {
ry -= g.leading * g.amp * 2;
p -= g.dphase;
res = !res;
}
} else {
// Count down
res = !res;
while (ry <= sin((rx / g.period) * TWO_PI + p) * g.amp) {
ry += g.leading * g.amp * 2;
p += g.dphase;
res = !res;
}
}
return res;
}
function makeColour()
{
colorMode( HSB, 255 );
return color( random(255), random(100,150), random(180,250) );
}
function setup() {
createCanvas(1024, 1024);
}
function draw() {
background(220);
const g1 = makeGrating();
const g2 = makeGrating();
const cols = [makeColour(),makeColour(),makeColour(),makeColour()];
const d = pixelDensity();
loadPixels();
for (let y = 0; y < height * d; ++y) {
for (let x = 0; x < width * d; ++x) {
const idx = (y * width * d + x) * 4;
const a = (inGrating(x/d,y/d,g1) ? 2 : 0) + (inGrating(x/d,y/d,g2) ? 1 : 0);
const col = cols[a];
pixels[idx] = red(col);
pixels[idx+1] = green(col);
pixels[idx+2] = blue(col);
}
}
updatePixels();
noLoop();
}
function keyPressed()
{
if( key == 's' ) {
save( 'output.png' );
} else if( key == ' ' ) {
loop();
}
}