<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 that draws a black background and a white disk
canvasAnimater.draw = function (t) {
  
    // get interaction variables for mouse position
    let x_pos = canvasAnimater.mouseX; 
    let y_pos = canvasAnimater.mouseY; 
    
    let W = canvasAnimater.width;
    let H = canvasAnimater.height;
    
    // draw a black background
    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, W, H);  
  
    // draw a white disk at mouse 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(`mouseX : ${canvasAnimater.mouseX}`, 20, 20);
    ctx.fillText(`mouseY : ${canvasAnimater.mouseY}`, 160, 20);
}

// enable interaction for 'mousedownmove'
canvasAnimater.interact('mousedownmove', true);

// 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();
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://github.com/smtsjhr/CanvasAnimater.js/blob/6ecb4d5ea3f5d36d3abcdd6c8338d9ccf42638e4/CanvasAnimater.js