2024.js/island.ts

60 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-01-13 11:34:25 -05:00
import { IslandGrid } from "./island/grid";
import { renderIslands } from "./island/render";
2024-01-13 00:29:49 -05:00
import { canvas2d, h } from "./lib/html";
2024-01-12 20:46:09 -05:00
2024-01-12 23:58:12 -05:00
const BLOWUP = 4;
const WIDTH = 240;
const HEIGHT = 135;
2024-01-12 20:21:58 -05:00
const DEFAULT_SEED = 128;
2024-01-12 20:21:58 -05:00
export function IslandApplet() {
2024-01-13 10:09:33 -05:00
// STATE
let timerId: number;
2024-01-13 14:46:04 -05:00
let ticks = 0;
2024-01-13 10:09:33 -05:00
let islands = new IslandGrid(WIDTH, HEIGHT, DEFAULT_SEED);
// UI
2024-01-12 20:46:09 -05:00
const [canvas, cx] = canvas2d({
2024-01-12 20:21:58 -05:00
width: WIDTH * BLOWUP,
height: HEIGHT * BLOWUP,
2024-01-12 20:46:09 -05:00
});
2024-01-12 20:21:58 -05:00
cx.scale(BLOWUP, BLOWUP);
const seedInput = h("input", { type: "number", valueAsNumber: DEFAULT_SEED });
2024-01-13 00:29:49 -05:00
const seedLabel = h("label", {}, "Seed:", seedInput);
const generateButton = h(
"button",
{
onclick: () => {
clearInterval(timerId);
2024-01-13 14:46:04 -05:00
ticks = 0;
2024-01-13 00:29:49 -05:00
islands = new IslandGrid(WIDTH, HEIGHT, seedInput.valueAsNumber);
timerId = setInterval(function tick() {
islands.step();
islands.step();
islands.step();
2024-01-13 14:46:04 -05:00
ticks += 3;
if (islands.done) {
clearInterval(timerId);
}
renderIslands(islands, cx);
}, 1000 / 30);
2024-01-13 00:29:49 -05:00
},
},
"Generate"
);
2024-01-12 22:31:48 -05:00
2024-01-13 00:29:49 -05:00
renderIslands(islands, cx);
2024-01-12 20:21:58 -05:00
2024-01-13 00:29:49 -05:00
return [canvas, seedLabel, generateButton];
2024-01-12 20:21:58 -05:00
}
(globalThis as any).IslandApplet = IslandApplet;