Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active October 22, 2021 20:43
Show Gist options
  • Save BretCameron/f022d5b4e6205da822ed0f8006cbcf1e to your computer and use it in GitHub Desktop.
Save BretCameron/f022d5b4e6205da822ed0f8006cbcf1e to your computer and use it in GitHub Desktop.
Boilerplate code for creating animated, interactive artwork using HTML5 canvas
const cvs = document.querySelector('canvas');
const c = cvs.getContext('2d');
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
window.addEventListener('resize', function () {
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
});
let mouse = {
x: undefined,
y: undefined
};
window.addEventListener('mousemove', function (e) {
mouse.x = event.x;
mouse.y = event.y;
});
class Shape {
constructor(x, y) {
this.x = x;
this.y = y;
this.initialX = x;
this.initialY = y;
};
draw = () => {
// this is where we control the shape's appearance
};
update = () => {
// this is where we control movement and interactivity
this.draw();
};
};
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, window.innerWidth, window.innerHeight);
/* this is where we call our animation methods, such as
Shape.draw() */
};
animate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment