Last active
June 19, 2020 13:07
-
-
Save b-aleksei/0a104b5ed5f528177bf2d970380143f8 to your computer and use it in GitHub Desktop.
requestAnimationFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <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