xxxxxxxxxx
166
let w = 720;
let h = w;
let x0 = 0;
let y0 = 0;
let x1 = w;
let y1 = 0;
let x2 = w;
let y2 = h;
let x3 = 0;
let y3 = h;
let squares = [];
let maxIterations = 10000;
let counter = 0;
let epsilon = 0.05;
let highlightIndex = 0;
let changeDelay = 0.05;
let changeTimer = 0;
function setup()
{
createCanvas(w, h);
noFill();
do
{
PushSquare();
} while(TiltedSquare(0.0625) && counter <= maxIterations);
}
function draw()
{
blendMode(BLEND);
background(40);
// highlightIndex = squares.length - 5;
let highlightCount = 10;
for (let i = 0; i < squares.length; ++i)
{
let square = squares[i];
if (highlightIndex <= i && i < highlightIndex + highlightCount ||
highlightIndex - squares.length <= i && i < highlightIndex - squares.length +
highlightCount)
DrawLitSquare(square);
else
DrawUnlitSquare(square);
}
// changeTimer += deltaTime / 1000;
// if (changeTimer >= changeDelay)
// {
// highlightIndex = (highlightIndex + 1) % squares.length;
// changeTimer = changeTimer % changeDelay;
// }
highlightIndex = (highlightIndex + 7) % squares.length;
}
function TiltedSquare(f)
{
let tx = x1 - x0;
let ty = y1 - y0;
let dx = tx * f;
let dy = ty * f;
let dSq = dx * dx + dy * dy;
if (dSq <= epsilon)
return false;
x0 += dx;
y0 += dy;
x1 -= dy;
y1 += dx;
x2 -= dx;
y2 -= dy;
x3 += dy;
y3 -= dx;
++counter;
return true;
}
function PushSquare()
{
squares.push([x0, y0, x1, y1, x2, y2, x3, y3]);
}
function DrawUnlitSquare(square)
{
blendMode(BLEND);
strokeWeight(1);
stroke(255, 8);
quad(square);
}
function DrawLitSquare(square)
{
blendMode(ADD);
let maxSpread = 200;
let count = 8;
let p = 8;
for (let i = count; i > 0; --i)
{
let t = i / count;
let internal = (1 - cos(PI * t)) / 2;
let r = 1 - pow(internal, p);
let g = 1 - pow(internal, p);
let b = pow(1 - internal, p);
strokeWeight(t * maxSpread);
stroke(255 * r, 255 * g, 255 * b, 255 * b * 0.12);
quad(square);
}
}
function keyPressed()
{
if (keyCode === 32)
TogglePause();
if (keyCode === ENTER)
Step();
}
function TogglePause()
{
if (isLooping())
Pause();
else
Unpause();
}
function Pause()
{
noLoop();
}
function Unpause()
{
loop();
}
function Step()
{
if (isLooping())
Pause();
redraw();
}