This is a Part 3 of a tutorial post HTML5 Canvas Particles System (Pt. 1). In this tutorial I will cover:
- Initialising Canvas (covered in Pt. 1)
- Drawing circles in Canvas (covered in Pt. 2)
- Simple animation for particle movement using
SetInterval()
(covered in Pt. 2) - Improving the code using
RequestAnimationFrame()
Using RequestAnimationFrame() instead of SetInterval()
Instead of using SetInterval()
we can animate only when we need to using RequestAnimationFrame()
. This helps improve performance as we re-draw only when something has changed. We can define to call spawn()
, draw()
and update()
on load, after which we would have to modify the update()
method to trigger the RequestAnimationFrame()
.
Here is what we are using instead of SetInterval()
:
1 2 3 4 5 6 7 8 |
|
And adding an on load function:
1 2 3 4 5 6 7 8 |
|
We can now modify update()
to look like this which now:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
And that is it!
🔥 If you enjoyed this post then you can keep up to date with my latests blogs posts & courses by following me on Twitter and checking out my code school for some hands on guided coding practice.