Animation Loops in JavaScript using requestAnimationFrame

  Рет қаралды 64,270

KIRUPA

KIRUPA

Күн бұрын

Create really smooth animations in JavaScript by having your animation use the requestAnimationFrame function.
Learn more at: www.kirupa.com

Пікірлер: 116
@devinwarrick3660
@devinwarrick3660 4 жыл бұрын
Here in 2020, catching some useful info from the video for the first time. Thank you!
@markm8090
@markm8090 5 жыл бұрын
I'm only just learning jq and js and found this really useful and detailed enough for me to understand. Thanks.
@ben-cb5er
@ben-cb5er 4 жыл бұрын
oh my god!!!!! man i really hope you achieve anything that you desire in life because this video is the ONLY video I've seen that actually explains requestAnimationFrame so simple and easy to grasp and yet in detail and to a point that i can comfortably use it!!!!! bro you got talent! dont stop teaching....you got yourself a new subscriber!! A like! and even bookmarked your website lol thank you again and keep it up
@kirupa
@kirupa 4 жыл бұрын
Thanks for the nice comment, Ben! I'm glad you liked this video. raF is still one of my favorite functions...even 7 years after this recorded :-)
@nileshinkane6034
@nileshinkane6034 5 жыл бұрын
This video explains it all, awesome man. Thanks
@VeeraVikis
@VeeraVikis 8 жыл бұрын
Thanks a lot.Works very fine. Please,do zoom in while you're either typing or explaining the code blocks.
@raultabajara
@raultabajara 7 жыл бұрын
Thanks! :) Helps a lot!
@jandeswart1378
@jandeswart1378 9 ай бұрын
10 years old, but still good information.
@kirupa
@kirupa 9 ай бұрын
I do need to revise this a bit! I will do so in the near future, for there have been some changes that are worth calling out 😀
@amudhavigneshwarankmp7571
@amudhavigneshwarankmp7571 3 жыл бұрын
I understood rAF finally after watching your video in 2021. Thanks for the content :)
@kirupa
@kirupa 3 жыл бұрын
Great to hear! :P
@valikonen
@valikonen 10 жыл бұрын
Thank you Kirupa.
@real-manoj-manoharan
@real-manoj-manoharan Жыл бұрын
Your content is awesome. Your blogs are awesome too. Thanks for sharing
@onelook1870
@onelook1870 3 ай бұрын
Will have a look at this course
@Bliss..
@Bliss.. 9 жыл бұрын
Loved it.
@General_Aladeen
@General_Aladeen 7 жыл бұрын
Great video for using native JS.
@CJ-pd7xg
@CJ-pd7xg 7 жыл бұрын
Well done. Thank you for sharing.
@McGavel1
@McGavel1 9 жыл бұрын
Cool, thanks for the info!
@arturomuller4816
@arturomuller4816 6 жыл бұрын
great tutorial!
@icetarrantula
@icetarrantula 2 жыл бұрын
Wow, amazing explanation. Thank you
@kirupa
@kirupa 2 жыл бұрын
Thanks for the nice comment! Glad you found it useful 😀
@amenslemirii7556
@amenslemirii7556 5 жыл бұрын
How can you do this if you want to change images, say you have 5 images and want to make them loop?
@SaveTheWhales5
@SaveTheWhales5 4 жыл бұрын
very good tutorial thank you!
@prasadsawant22
@prasadsawant22 3 жыл бұрын
It's 2020 the video is 7 years old but still useful
@winstonsmith1086
@winstonsmith1086 8 жыл бұрын
UR amazing as usual. Is there some way to use this to call an animation (function) after the animation has completed? I'm using jquery and the damned thing keeps resetting. Apparently anim-fill property forwards (in css) isn't working 4 me. :( Many thanks Kirupa) --You rock!
@kirupa
@kirupa 8 жыл бұрын
Can you post a snippet of the code in question? It could be a bunch of things. A better place to post your snippet would be on the forums at forum.kirupa.com where you have more formatting options! :-)
@muhammedraseen8505
@muhammedraseen8505 4 жыл бұрын
Thank you for the video
@AdemarSantana
@AdemarSantana 6 жыл бұрын
Awesome!
@taospencer1403
@taospencer1403 6 жыл бұрын
very well explained.
@ronaldurbina8233
@ronaldurbina8233 3 жыл бұрын
thank you
@Jimmy-lu6pr
@Jimmy-lu6pr 4 жыл бұрын
Thank you!
@rock_0075
@rock_0075 4 жыл бұрын
Thanks for the video, i am still a bit confused with the return value it produces , like in the below code .function gameLoop(timestamp){ let deltaTime=timestamp- lastTime; lastTime = timestamp; ctx.clearRect(0,0,800,600); paddle1.update(deltaTime); paddle1.draw(ctx); requestAnimationFrame(gameLoop); } gameLoop(); so when the gameloop() fuction is called first, it will go through the gamLoop function with no parameter, so will timestamp will be zero initially? and also how come requestAnimationFrame(gameLoop); will return a timestamp? really appreciate your help
@kirupa
@kirupa 4 жыл бұрын
Rock_007 Can you please post this on the forums at forum.kirupa.com This will require some code formatting as part of the response 😀
@rock_0075
@rock_0075 4 жыл бұрын
@@kirupa Thanks for the reply :) , i have just created an account and posted at forum.kirupa.com/t/timestamp-in-callback-requestanimationframe-function/642777 sorry i am new to coding so not sure about the code formatting.
@bullygram
@bullygram 4 жыл бұрын
Small Bro u r great
@donutbedum9837
@donutbedum9837 3 жыл бұрын
thanks mate
@Toontic
@Toontic 3 жыл бұрын
Thanks for this well presented video. How would I change the image randomly from an ever growing image folder like a gallery every time the animation starts.
@kirupa
@kirupa 3 жыл бұрын
Hi Toontic! Can you please ask this question on the forums at forum.kirupa.com ? I can share some snippets that may help.
@Toontic
@Toontic 3 жыл бұрын
@@kirupa Will do. Thanks
@scikick
@scikick 7 жыл бұрын
Wouldn't animations on a 30fps screen be half as slow than on a screen with 60fps? What if you don't want your animation to be just frame by frame, but there's a time factor to it? For example, I want a car moving at a fixed speed across the screen. Without any extra inputs like 'time passed since', how would you attempt to do this? Thanks for the awesome video btw. Cheers :)
@kirupa
@kirupa 7 жыл бұрын
Sorry for the delay in getting back to you. Can you post this question on forum.kirupa.com? The answer is going to be a bit more involved, and posting with the limited formatting options here simply won't work :P
@scikick
@scikick 7 жыл бұрын
Kirupa Chinnathambi Done. Thanks. :)
@taospencer1403
@taospencer1403 5 жыл бұрын
What device do you have that works at 30fps?
@durchschnittlich
@durchschnittlich 8 жыл бұрын
I have seen other people hate on setInterval and setTimeout as well. But a question nobody could answer so far, or I didn't listen is: How do you make it wait?
@kirupa
@kirupa 8 жыл бұрын
+4E2010XD There is no built-in way, but you can do what I've described here by using an extra variable that determines the play state: forum.kirupa.com/t/pausing-requestanimationframe/633995 :-)
@mledesma64
@mledesma64 4 жыл бұрын
Thank you
@MrBosiwollig
@MrBosiwollig 10 жыл бұрын
GREAT! :D
@juancarlosgallegos3902
@juancarlosgallegos3902 11 ай бұрын
Good video :) I think it would've helped if you started with an implementation of the animation loop that did NOT use rAF because that would've helped to explain the purpose of rAF
@kirupa
@kirupa 11 ай бұрын
That is good feedback, Juan. I will take that into consideration when I re-record these videos in the future.
@chunk1978
@chunk1978 6 жыл бұрын
What advantages, if any, are there in using requestAnimationFrame instead of using a CSS Animation?
@kirupa
@kirupa 6 жыл бұрын
Because it is JavaScript, you aren't limited by the CSS animation syntax, keyframes, and only the properties whose values you can set. You are free to create more freeform animations such as particle effects and so on. For simple animations like revealing a menu, page transitions, and so on, both are similar.
@chunk1978
@chunk1978 6 жыл бұрын
Thanks for the explanation. Also, I ordered the 2nd edition of your React book and I'm looking forward to reading it when it arrives. Thanks again :)
@Berkana
@Berkana 8 жыл бұрын
With this technique, how do you set frame rate? The only way to set the frame rate using this that I can find on the web is to wrap it in a setInterval inside your animation function.
@kirupa
@kirupa 8 жыл бұрын
+Berkana The setInterval approach is basically it. There is no good alternative solution for setting the frame rate! :(
@Berkana
@Berkana 8 жыл бұрын
+Kirupa Chinnathambi is there some way to determine the rate of animation besides messing with the frame rate? I would like to calculate the animation details on-demand of requestAnimationFrame, independent of the frame rate.
@lizbanach3684
@lizbanach3684 7 жыл бұрын
Would this animation loop work with a tween engine like TweenNano or TweenLite?
@kirupa
@kirupa 7 жыл бұрын
You wouldn't use both requestAnimationFrame and TweenNano/TweenLite. Both of those come with their own looping implementation under the covers that take care of all this for you :-)
@lizbanach3684
@lizbanach3684 7 жыл бұрын
I see. I know this is random, but do you by any chance know of a way to auto loop with TweenNano or TweenLite? I have been looking everywhere and can't find a solution for this.
@kirupa
@kirupa 7 жыл бұрын
Unfortunately, I do not. You could try posting on forum.kirupa.com and seeing if somebody there knows :-)
@FitnessChaos
@FitnessChaos 4 жыл бұрын
good video
@orenmizr
@orenmizr 9 жыл бұрын
what would you do if you needed to support ie9 ?? (for the enterprise)
@kirupa
@kirupa 9 жыл бұрын
I would use a polyfill that uses the built-in timers on older browsers: gist.github.com/paulirish/1579671 :P
@Geert890
@Geert890 8 жыл бұрын
+Kirupa Chinnathambi I copied your code letter by letter and it didnt work... WHY?
@kirupa
@kirupa 8 жыл бұрын
+Geert Boer Can you share your code? I'd be happy to take a look and see what may be wrong. If you need a place to post that has good code formatting, please post your code snippet here: forum.kirupa.com
@Geert890
@Geert890 8 жыл бұрын
+Kirupa Chinnathambi thanks for the response. I already deleted the snippets unfortunately... I have a big exam tomorrow where I have to make a bucket and there will be 5 different fruits falling from the air. The bucket has to 'catch' the right fruit to get points. It's very difficult and I'm panicking a bit now because I have no idea how to do it.....
@kirupa
@kirupa 8 жыл бұрын
+Geert Boer What you are trying to do is a bit tricky - it involves understanding a lot of things (only a part of which is working with animations). If you have working (or partially working) examples, I can see where things are wrong.
@marqimoth6987
@marqimoth6987 2 жыл бұрын
how could i use this to make a repeating animation like a question mark box in mario?
@kirupa
@kirupa 2 жыл бұрын
There are several ways to do that! Can you repost your question on forum.kirupa.com where we can dive into this in more detail?
@marqimoth6987
@marqimoth6987 2 жыл бұрын
@@kirupa sure
@beau6077
@beau6077 7 жыл бұрын
//using translate3d() prop with .js //posting here for future me who has seen this vid a dozen times and forgets stuff. var currentPos = 0; var thing = document.querySelector("#thing") var requestAnimationFrame = window.requestAnimationFrame; function moveThing () { currentPos+= 5; thing.style.transform = "translate3d(" + currentPos + "px, 30px, 0px)"; if (Math.abs(currentPos) >= 900) { currentPos = - 500; } requestAnimationFrame(moveThing); } moveThing();
@NewTechGenius
@NewTechGenius 7 жыл бұрын
How would I apply this to a sprite in a canvas?
@kirupa
@kirupa 7 жыл бұрын
This will help you out: www.kirupa.com/html5/creating_simple_html5_canvas_animation.htm :-)
@NewTechGenius
@NewTechGenius 7 жыл бұрын
Thanks for the quick response :)
@mikaelnilsson8041
@mikaelnilsson8041 7 жыл бұрын
Great video! At kzbin.info/www/bejne/qH_WdGSMfp5mbpom25s the animation is stuttering considerably. How do you remove this stuttering?
@kirupa
@kirupa 7 жыл бұрын
Mikael - that is a result of my video recording/encoding. If you view it in the browser, it looks really smooth :-)
@bhaveshgohel
@bhaveshgohel 10 жыл бұрын
Thanks. (y)
@danielgolden6991
@danielgolden6991 Жыл бұрын
This guy was using VS code before it was cool.
@kirupa
@kirupa Жыл бұрын
Haha! I wish that were the case with other tools and technologies that I had bet on.
@widheg
@widheg 7 жыл бұрын
How can you be 100% certain that the variable requestAnimationFrame was actually initialized? Before using it (i.e. to call a function), you should test that it has been initialized.
@kirupa
@kirupa 7 жыл бұрын
That may be unnecessary these days, for requestAnimationFrame is built-in and fully supported by almost every browser out there :P
@widheg
@widheg 7 жыл бұрын
As you say ... "ALMOST every browser"... so what happens in an older browser? Ever heard of defensive programming? ;-)
@kirupa
@kirupa 7 жыл бұрын
Fair point. If I update this video, I'll be sure to make a mention of providing a polyfill for the users on old browsers :-)
@widheg
@widheg 7 жыл бұрын
Outline: var raf=null; /*Try to initialize raf: */ ... ... if (raf !== null) { /* Initialization succeeded. */ Allowed to use 'raf' here i.e. if-and-only-if browser supports it. ... ... } /* Do not use 'raf' here. */
@joshiprabhas
@joshiprabhas 6 жыл бұрын
that keystroke noise is killing me
@qwe123727
@qwe123727 6 жыл бұрын
Hey you are mixing transition with animation
@kirupa
@kirupa 6 жыл бұрын
Thanks! I'll need to revise this at some point to be more accurate haha.
@qwe123727
@qwe123727 6 жыл бұрын
Kirupa Chinnathambi imean, idea is right, example is wrong. Don't worry, you are far better than a normal Indian. Region matters
@kirupa
@kirupa 6 жыл бұрын
Ethashamuddin Mohammed I glanced through the video. Where is the mistake?
@qwe123727
@qwe123727 6 жыл бұрын
Kirupa Chinnathambi you are appending hash symbol to an element. This is transition but not animation because you are changing the state of an element per unit time. In animation states does not change.
@kirupa
@kirupa 6 жыл бұрын
Ethashamuddin Mohammed Appending a hash tag has no bearing on whether the output is an animation or transition. Animations and transitions are similar, but CSS has the concept of a transition defined as a property. Anything that isn’t a CSS Transition is considered an animation, especially in JavaScript where you don’t have any other concept. Also, states change in both animation and transition. That is the fundamental definition of an animation - state changing over a period of time.
@Nexaman18
@Nexaman18 Жыл бұрын
i can't understand any thing
@kirupa
@kirupa Жыл бұрын
That's totally normal as part of learning. Can you be more specific on what you are stuck on?
@zwhitchcox
@zwhitchcox 8 жыл бұрын
The first half of the video was just explaining what animation was. We know what animation is. We know that we're not going to notice it moving one pixel, but over time, moving several pixels, we will notice. We know that it's good to be cross browser compatible. That was another like two minutes. We are web developers. You don't need to explain every little thing to us. Having to stop watching this video, now, because it's already wasted 6 minutes of my time.
@kirupa
@kirupa 8 жыл бұрын
+Zane Hitchcox That's good feedback. I'll revise this video in the near future to be more concise!
@sheilferzepeda1124
@sheilferzepeda1124 8 жыл бұрын
+Kirupa Chinnathambi It's only partial feedback. I found the introduction very helpful since I'm only just starting to use animation in code. I would personally suggest how to operate youtube timers - it's not old school tv so in that regard maybe annotated times could help people track around first
@kirupa
@kirupa 8 жыл бұрын
+Sheilfer Zepeda That's a great idea! I have gone ahead and added a "Skip intro" link to the intro slides that jumps you to the 3 minute mark where we start talking about the actual API itself :-)
@timbrown7681
@timbrown7681 8 жыл бұрын
+Zane Hitchcox thats what she tells you, doesn't she? :D be grateful...
@Geert890
@Geert890 8 жыл бұрын
+Kirupa Chinnathambi i was happy about the attention to little details dont listen to zane hitchcox it is wat makes this video good
Creating Buttery Smooth Animations in CSS
11:34
KIRUPA
Рет қаралды 42 М.
The Hidden World of requestAnimationFrame
9:22
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 31 М.
Пробую самое сладкое вещество во Вселенной
00:41
Was ist im Eis versteckt? 🧊 Coole Winter-Gadgets von Amazon
00:37
SMOL German
Рет қаралды 37 МЛН
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 4,3 МЛН
когда повзрослела // EVA mash
00:40
EVA mash
Рет қаралды 4,3 МЛН
DOM vs. Canvas
10:44
KIRUPA
Рет қаралды 25 М.
requestAnimationFrame() - Beau teaches JavaScript
4:22
freeCodeCamp.org
Рет қаралды 49 М.
How To Create Performant CSS Animations
12:05
Web Dev Simplified
Рет қаралды 60 М.
What is rAF - How to Use requestAnimationFrame()
8:09
Fullstack Academy
Рет қаралды 9 М.
JavaScript Pro Tips - Code This, NOT That
12:37
Fireship
Рет қаралды 2,5 МЛН
ПОКУПКА ТЕЛЕФОНА С АВИТО?🤭
1:00
Корнеич
Рет қаралды 3,8 МЛН
Красиво, но телефон жаль
0:32
Бесполезные Новости
Рет қаралды 444 М.
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 32 МЛН