Skip to content

Instantly share code, notes, and snippets.

@guidoschmidt
Created April 24, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidoschmidt/00849a4cda43530c49e0b5a7767b211d to your computer and use it in GitHub Desktop.
Save guidoschmidt/00849a4cda43530c49e0b5a7767b211d to your computer and use it in GitHub Desktop.
thi.ng/voronoi-foam
import { srgb } from "@thi.ng/color";
import type { Color, css } from "@thi.ng/color";
import { randMinMax2 } from "@thi.ng/vectors";
import type { Vec } from "@thi.ng/vectors";
import { repeatedly } from "@thi.ng/transducers";
import { draw } from "@thi.ng/hiccup-canvas";
import type { IToHiccup } from "@thi.ng/api";
const NUM_CELLS = 10;
const WIDTH = window.innerWidth;
const HEIGHT = window.innerHeight;
interface Cell extends IToHiccup {
pos: Vec;
r: number;
color: Color;
}
class Cell implements Cell {
color: Color;
constructor(public pos: Vec, public r: number) {
this.color = srgb.random();
}
toHiccup() {
return ["circle", { fill: this.color }, this.pos, this.r];
}
}
const cells = [
...repeatedly(
() =>
new Cell(
randMinMax2([], [0, 0], [WIDTH, HEIGHT]),
Math.round(Math.random() * 10)
),
NUM_CELLS
),
];
const canvas = document.createElement("canvas") as HTMLCanvasElement;
document.body.appendChild(canvas);
canvas.width = WIDTH;
canvas.height = HEIGHT;
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
draw(ctx, ["g", {}, ...cells]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment