artworld/examples/spiral.js
2025-03-12 23:51:24 -05:00

52 lines
1.0 KiB
JavaScript

import {
World,
Pico8,
Group,
Line,
Random,
Vector2,
degToRad,
} from "../artworld/index.js";
const world = new World("mainCanvas");
window.world = world;
function* spirals() {
while (true) {
let g = new Group(world);
let rc = Random.choice(Object.values(Pico8));
for (let d = 0; d < 180; d += 10) {
if (Random.chance(0.9)) {
new Line(g).to(Vector2.polar(190 - d, degToRad(d))).stroke(rc);
}
}
yield g;
}
}
function makeGrid(iterable, rows, cols, opts) {
opts = opts || {};
let width = opts.width || 50;
let height = opts.height || 50;
let xOff = opts.xOff || 0;
let yOff = opts.yOff || 0;
for (let c = 0; c < cols; c++) {
for (let r = 0; r < rows; r++) {
let result = iterable.next().value;
if (result === undefined) {
return;
} else {
result.pos(new Vector2(width * c + xOff, height * r + yOff));
}
}
}
}
makeGrid(spirals(), 4, 3, {
width: 220,
height: 120,
xOff: 80,
yOff: 10,
});
world.draw();