How to make Canvas draw animation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How to make Canvas draw animation

If we have lineTo(50, 100);, How do we animate It drawing ... (50, 100)? Here I have a line drawn already, but it's not animated (don't know how). https://code.sololearn.com/WL7sS6c3HaHJ/?ref=app

8th Sep 2019, 5:52 PM
Ginfio
Ginfio - avatar
9 Answers
+ 5
Use window.requestAnimationFrame method https://code.sololearn.com/WIRUZtbG0bGG/?ref=app
9th Sep 2019, 1:28 AM
Calviղ
Calviղ - avatar
+ 7
https://code.sololearn.com/WtPI9miO20ig/?ref=app I hope this code will be helpful for setinterval.
9th Sep 2019, 8:30 AM
R💠🇮🇳
R💠🇮🇳 - avatar
+ 4
Requet animation frame indeed will help
9th Sep 2019, 12:02 AM
Qudusayo
Qudusayo - avatar
10th Sep 2019, 2:00 AM
SUJAN
SUJAN - avatar
+ 3
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations
9th Sep 2019, 12:50 AM
Gordon
Gordon - avatar
+ 3
Use of setInterval or setTimeout method https://code.sololearn.com/WLNx6w5n923l/?ref=app https://code.sololearn.com/WZLu4j4XT1S8/?ref=app However you should always use requestAnimationFrame method in canvas animation in order to get smoother movement.
9th Sep 2019, 2:44 AM
Calviղ
Calviղ - avatar
+ 3
setInterval(1000). might work better in your case. you can increment the lines x and y position every second in a for loop. if the drawing needs to slow down you can say like setInterval (1000 / 5)
9th Sep 2019, 5:42 AM
Anthony Johnson
Anthony Johnson - avatar
+ 2
Qudusayo , I know this is offtopic, but did you get your profle from somewhere, or did you make it? (I saw it on Youtube). Also, can you please show me how to do the request animation frame indeed (in the code)? Thank you
9th Sep 2019, 12:11 AM
Ginfio
Ginfio - avatar
+ 2
Coder , Can you show me with setInterval (in the code) please?
9th Sep 2019, 2:03 AM
Ginfio
Ginfio - avatar