Canvas - Good practices? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Canvas - Good practices?

Hi there, I'm writting my first canvas code and after some headach I'm finally closing to my final target but my script start to slow down the animation. That's why I want to ask if you guys know some common tips to increase framerates?

16th Mar 2017, 6:04 AM
Geoffrey L
Geoffrey L - avatar
4 Answers
+ 2
window.requestAnimationFrame() This method may help you. https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations
16th Mar 2017, 6:13 AM
K.C. Leung
K.C. Leung - avatar
+ 1
Oh thanks I didn't see this one, I'll try it and tell you the result!
16th Mar 2017, 6:28 AM
Geoffrey L
Geoffrey L - avatar
+ 1
Hi K.C Leung, I have test your suggestion and I didn't see any better but I understand your advice is the good one, people recommend your idea everywhere but I think I have been to far in my program and would need to change my animation system (setInterval mixed with a system to manage F.P.S depending params settings). By the way I found what dropped my frame per seconds so low... shadow blur propriety! I turned off that and I have been able to display 60 times more geometric shapes compare with before!
17th Mar 2017, 6:00 AM
Geoffrey L
Geoffrey L - avatar
+ 1
context.beginPath(); and clearRect(); usually you don't need to re-draw all area of the canvas everytime. I think it can improve the performance. And there is some advice for optimize https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Optimizing_canvas
17th Mar 2017, 9:23 AM
K.C. Leung
K.C. Leung - avatar