treehouse/static/js/sandbox.js

36 lines
746 B
JavaScript
Raw Normal View History

2024-02-18 12:10:02 +01:00
export const internals = {
body: document.createElement("body"),
2024-02-18 23:37:31 +01:00
resetBody() {
this.body.replaceChildren();
}
2024-02-18 12:10:02 +01:00
};
export function body() {
return internals.body;
}
export function addElement(element) {
body().appendChild(element);
}
2024-02-18 00:29:58 +01:00
export class Sketch {
constructor(width, height) {
this.canvas = document.createElement("canvas");
this.canvas.width = width;
this.canvas.height = height;
this.ctx = this.canvas.getContext("2d");
2024-02-18 12:10:02 +01:00
addElement(this.canvas);
2024-02-18 00:29:58 +01:00
}
2024-02-18 23:37:31 +01:00
animate(draw) {
let animationCallback;
animationCallback = () => {
draw();
requestAnimationFrame(animationCallback);
};
animationCallback();
}
2024-02-18 00:29:58 +01:00
}