JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas" width="512" height="512"></canvas>
JavaScript
/*
* A simple keyframe-tweening animation module for 2D
* canvas elements.
*/
var KeyframeTweener = {
// The module comes with a library of common easing functions.
linear: function (currentTime, start, distance, duration) {
var percentComplete = currentTime / duration;
return distance * percentComplete + start;
},
quadEaseIn: function (currentTime, start, distance, duration) {
var percentComplete = currentTime / duration;
return distance * percentComplete * percentComplete + start;
},
quadEaseOut: function (currentTime, start, distance, duration) {
var percentComplete = currentTime / duration;
return -distance * percentComplete * (percentComplete - 2) + start;
},
quadEaseInAndOut: function (currentTime, start, distance, duration) {
var percentComplete = currentTime / (duration / 2);
return (percentComplete < 1) ?
(distance / 2) * percentComplete * percentComplete + start :
(-distance / 2) * ((percentComplete - 1) * (percentComplete - 3) - 1) + start;
},
// The big one: animation initialization. The settings parameter
// is expected to be a JavaScript object with the following
// properties:
//
// - renderingContext: the 2D canvas rendering context to use
// - width: the width of the canvas element
// - height: the height of the canvas element
// - sprites: the array of sprites to animate
// - frameRate: number of frames per second (default 24)
//
// In turn, each sprite is a JavaScript object with the following
// properties:
//
// - draw: the function that draws the sprite
// - keyframes: the array of keyframes that the sprite should follow
//
// Finally, each keyframe is a JavaScript object with the following
// properties. Unlike the other objects, defaults are provided in
// case a property is not present:
//
// - frame: the global...