Let's look at animating a sprite sheet, on an HTML5 canvas, using JavaScript.
A Little Setup
First, let's create the canvas element.
...
For further actions, you may consider blocking this person and/or reporting abuse
Yes, Marty!
I literally started building something yesterday and needed to use sprite sheets but I got a bit stuck and just decided to animate a round div instead! Lol
This is literally gold for me right now, man! Thanks for writing this up!
If I get this finished I'll show you the final product!
Thanks again! ✌🏻😁
Glad I could help! I look forward to seeing your project!
Great tut Martin. Am just learning.
Can I ask... how to stop the looping? Say you want the animation to complete only one cycle?
requestAnimationFrame
returns a long integer. If you assign that to a variable, you can then callcancelAnimationFrame(id)
once the required condition is met to stop the cycle.Using the
step
function as an example, something like this:Another way could be to return early to break out of the
step
function. If only one cycle is needed, once the counter reaches the end of the array, return before calling the nextrequestAnimationFrame
(instead of reseting the counter to 0 and continuing).Here are the MDN pages for requestAnimationFrame and cancelAnimationFrame.
Thank you... I really appreciate your help.
Thank you so much for this tutorial. It was my first exposure to making sprite animation but your walk through/examples was very clear. :)
CSS3 animations using steps(n) will work in most modern browsers, too.
Good to know. I haven't done much with CSS animations yet. Thanks!
This is incredible! Thanks for the super clear and thorough explanation.
Nice thanks
Thank you so much mr Himmel.
Nice article, I also cover this topic if someone want another point of view.
HTML5 canvas - part 1: Drawing
Guillaume Martigny