Public
Edited
Feb 21, 2023
Importers
1 star
Insert cell
Insert cell
canvas = sketch(draw, { width, height, setup, update })
Insert cell
Insert cell
Insert cell
Insert cell
draw = (state) => {
p8g.background(220);
p8g.rect(50, 50, 100, 100);
}
Insert cell
setup = () => ({})
Insert cell
update = (state) => state
Insert cell
mutable state = ({})
Insert cell
Insert cell
p8g = import("https://unpkg.com/p8g.js")
Insert cell
sketch = {
var canvas, request;
return (draw = (state) => {}, options = {}) => {
const {
setup = () => ({}),
update = (state) => state,
width = 900,
height = 450
} = options || {};
if (!canvas) canvas = html`${p8g.createCanvas(width, height)}`;
if (request) cancelAnimationFrame(request);
mutable state = setup(mutable state || {}) || mutable state;
request = requestAnimationFrame(async function tick() {
mutable state = (await update(mutable state)) || mutable state;
mutable state = (await draw(mutable state)) || mutable state;
request = requestAnimationFrame(tick);
});
invalidation.then(() => cancelAnimationFrame(request));
return canvas;
};
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more