Published
Edited
May 3, 2019
6 stars
Insert cell
Insert cell
Insert cell
{
const options = {
width: 900,
height: 400,
centerX: .5, // Horizontal center (ratio)
centerY: .95, // Vertical center (ratio)
seedSize: 5, // Size of the innermost square (not rendered)
count: 40, // Number of squares
rotationGlobal: .0, // Global rotation
rotationMin: .01, // Min initial square rotation (1 = full rotation)
rotationMax: .2, // Max initial square rotation (1 = full rotation)
rotationSpeed: .0005, // Max rotation speed per square (rotations cascade)
rotationShift: 0, // Power applied to normalized square index
// Fill lightness
lightInner: .1, // Inner square lightness
lightOuter: 1, // Outer square lightness
lightShift: 1.05, // Power applied to normalized square index
lineWidth: 2,
lineLight: 1, // Line lightness
lineOpacity: .03,
colorFn: colorAlternate, // Color callback
rng: Math.random,
};
reset;
const {width: w, height: h, rng} = options;
const clearColor = `hsl(0,0%,${options.lightOuter}%)`;
const color = options.colorFn(options);
const cx = w * options.centerX, cy = h * options.centerY;
const c = DOM.context2d(w, h);
c.canvas.style.maxWidth = '100%';
const {PI} = Math;
const randAngle = (min, max) => mix(min, max, rng()) * PI * 2;
const angles = Array(options.count).fill().map((v,i) => {
// Alternate directions.
return mix(-1, 1, i % 2) * randAngle(options.rotationMin, options.rotationMax);
});
c.strokeStyle = `hsla(0,0%,${options.lineLight * 100}%,${options.lineOpacity})`;
c.lineWidth = options.lineWidth;

let t = 0;
while(true) {
clear();
draw(angles, t);
yield c.canvas;
t += options.rotationGlobal;
angles.forEach((a, i, arr) => {
const t = (i+1)/arr.length ** options.rotationShift;
arr[i] += Math.sign(a) * t * options.rotationSpeed;
});
}
function draw(angles, t = 0) {
let s = options.seedSize, aGlobal = t * PI * 2;
const squares = angles.map((aLocal, i, arr) => ({
size: (s = s * (Math.abs(Math.cos(aLocal)) + Math.abs(Math.sin(aLocal)))),
aGlobal: (aGlobal += aLocal),
aLocal,
}));

for(let i = squares.length - 1; i >= 0; i--) {
const square = squares[i];
c.beginPath();
drawSquare(square);
c.fillStyle = color(square, i);
c.fill();
if(c.lineWidth > 0) c.stroke();
}
}
function drawSquare({size, aGlobal}) {
c.save();
c.translate(cx, cy);
c.rotate(aGlobal);
c.translate(-cx, -cy);
c.rect(cx - size/2, cy - size/2, size, size);
c.restore();
}

function clear() {
c.save();
c.fillStyle = clearColor;
c.fillRect(0, 0, w, h);
c.restore();
}
}
Insert cell
Insert cell
function colorFromSize(options) {
const {width, height, lightInner, lightOuter, lightShift} = options;
const max = Math.max(width, height);
return (square, i) => {
const t = mix(lightInner, lightOuter, square.size / max ** options.lightShift);
return `hsl(0, 0%, ${t*100}%)`;
};
}
Insert cell
function colorFromAngle(options) {
return (square, i) => {
const t = Math.sin(square.aGlobal) * .5 + .5;
return `hsl(0, 0%, ${t*100}%)`;
};
}
Insert cell
function colorAlternate(options) {
const {lightInner, lightOuter} = options;
return (square, i) => {
const t = mix(lightInner, lightOuter, i%2);
return `hsl(0, 0%, ${t*100}%)`;
};
}
Insert cell
Insert cell
function mix(a, b, t) {
return a + (b - a) * t;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more