Skip to content

Instantly share code, notes, and snippets.

@b-aleksei
Last active June 19, 2020 13:07
Show Gist options
  • Save b-aleksei/0a104b5ed5f528177bf2d970380143f8 to your computer and use it in GitHub Desktop.
Save b-aleksei/0a104b5ed5f528177bf2d970380143f8 to your computer and use it in GitHub Desktop.
requestAnimationFrame
// <progress id="elem" style="width: 5%;" onclick="anim.start()"></progress>
class Animation {
constructor({draw, duration}) {
this.draw = draw;
this.duration = duration;
}
animate(time) {
let timeFraction = (time - this.startAnition) / this.duration;
if (timeFraction > 1) timeFraction = 1;
this.draw(timeFraction);
if (timeFraction < 1) {
requestAnimationFrame(this.animate.bind(this))
}
}
start() {
this.startAnition = performance.now()
requestAnimationFrame(this.animate.bind(this))
}
}
let anim = new Animation({
draw: function(progress) {
document.querySelector("#elem").style.width = progress * 100 + "%";
},
duration: 1000
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment