<script src="https://assets.codepen.io/3560077/CanvasAnimater.js"></script>
<canvas id="myCanvas"></canvas>
<script src="./script.js"></script>
// create a CanvasAnimater object that targets a canvas element with id='myCanvas'
let canvasAnimater = new CanvasAnimater('myCanvas');
// get the '2D' rendering canvas context
let ctx = canvasAnimater.context;
// set the canvas width and height dimensions to 300x300
// NOTE: this setting is bypassed if canvas is flexed
canvasAnimater.size(300,300)
// flex the canvas to fill the entire window
canvasAnimater.flex(true);
// define and set a draw function that draws a black background and a white disk
canvasAnimater.draw = function (t) {
let W = canvasAnimater.width;
let H = canvasAnimater.height;
// get interaction variables for scroll position and bound their range
let x_pos = Math.min(W,canvasAnimater.scrollX);
let y_pos = Math.min(H, canvasAnimater.scrollY);
// draw a black background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, W, H);
// draw a white disk at scroll position
ctx.beginPath();
ctx.arc(x_pos, y_pos , 10, 0, 2*Math.PI);
ctx.closePath();
ctx.fillStyle = 'white';
ctx.fill();
// draw reference text displaying interaction variables
displayInteractionText();
}
// draws reference text displaying interaction variables
function displayInteractionText() {
ctx.font = '14px Monospace';
ctx.fillText(`scrollX : ${canvasAnimater.scrollX}`, 20, 20);
ctx.fillText(`scrollY : ${canvasAnimater.scrollY}`, 160, 20);
}
// enable interaction for 'scroll'
canvasAnimater.interact('scroll', true);
// set the scroll size
canvasAnimater.scrollSize(2000, 2000);
// set a time evolution range
canvasAnimater.evolve(0);
// loop the animation over the evolution time range
canvasAnimater.loop(false);
// set the render rate in frames-per-second
canvasAnimater.FPS(50);
// set animation mode to 'rate' with 'animation_rate' = 0.03
canvasAnimater.mode('rate', 0.03);
// render the animation
canvasAnimater.animate();
This Pen doesn't use any external CSS resources.