<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 with a time parameter that draws a black background and a white disk moving in a circle
canvasAnimater.draw = function (t) {
let W = canvasAnimater.width;
let H = canvasAnimater.height;
// draw a black background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, W, H);
// define a radius for the circular path the disk follows
let R = Math.min(W,H)/4;
// draw a white disk that moves in a circle
ctx.beginPath();
ctx.arc(W/2 + R*Math.sin(t), H/2 - R*Math.cos(t), R/4, 0, 2*Math.PI);
ctx.closePath();
ctx.fillStyle = 'white';
ctx.fill();
}
// set a time evolution range
canvasAnimater.evolve(0, 2*Math.PI);
// loop the animation over the evolution time range
canvasAnimater.loop(false);
// set the render rate to 1 frames-per-second
canvasAnimater.FPS(1);
// set animation mode to 'frames' with 'animation_frames' = 12
canvasAnimater.mode('frames', 12);
// render the animation
canvasAnimater.animate();
This Pen doesn't use any external CSS resources.