The Hidden World of requestAnimationFrame

  Рет қаралды 31,538

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

Күн бұрын

There is a great method called window.requestAnimationFrame which lets you work with the natural rhythm of the browser to improve your UI updates and animations. It will let you call a function each time it is about to do a screen paint and update the interface.
Inspiration for this video: gomakethings.com/how-to-use-r...
Code from video: gist.github.com/prof3ssorSt3v...
MDN reference for requestAnimationFrame: developer.mozilla.org/en-US/d...

Пікірлер: 63
@mykalimba
@mykalimba 4 жыл бұрын
For anyone wondering why most frames are spaced around 16.6 milliseconds apart, that is roughly equal to 60 FPS.
@Baargaa
@Baargaa 3 жыл бұрын
and if u wonder why there is 60FPS - it's coming from 60 Hz ;) - Refresh rate of monitors ;)
@techsyndrome3291
@techsyndrome3291 2 жыл бұрын
@@Baargaa nobody wondered that
@biswamohandwari780
@biswamohandwari780 2 жыл бұрын
really wonderful thank you
@retagainez
@retagainez 2 ай бұрын
As chaotic as web development is, this has been a very straight-forward and useful resource for me to begin puzzling out how I could possibly start writing some 2-D physics engine in JavaScript.
@maxtayebwa8987
@maxtayebwa8987 4 жыл бұрын
I dare anyone to tell me any better explanation for this thing! It's okay, youtube has billions of videos, feel free to try to tell me which channel even dares to compare!! Thanks prof, this was awesome, as usual of course!!!
@TheD2D21
@TheD2D21 7 ай бұрын
Well, you know, while the best explanaition I myself have come to so far as well, there is no video response feature anymore on KZbin for anybody to post a challenge to your dare.
@mrSargi7
@mrSargi7 2 жыл бұрын
This channel is my MDN on youtube, the best teacher with soothing voice and content that is always on point
@dajuanmcdonald3861
@dajuanmcdonald3861 3 жыл бұрын
This really helped me out with Cellular Automaton simulations and understanding exactly what double buffering is and how to do it.
@patils22
@patils22 3 жыл бұрын
Dodging the punch analogy !! It made the difference between setInterval and requestAnimationFrame crystal clear. Thanks.
@maelstrom57
@maelstrom57 3 жыл бұрын
Finally an understandable explanation for this somewhat obscure method. Thanks a lot :)
@angladephil
@angladephil 4 жыл бұрын
Nice and useful ! I guess the CSS use this function behind the scenes for transitions.Thank you !
@shankarghimire4492
@shankarghimire4492 3 жыл бұрын
Wow! wonderful explanation and demonstration! Best video I got so far! Thanks a lot!
@Baargaa
@Baargaa 3 жыл бұрын
Thx! Good introduction to the topic. I just transormed my setInteval functions to requestAnimationFrame. Now all browsers has the same effect. Earlier animations on FF were little bit too lazy ;)
@georgebockari289
@georgebockari289 2 жыл бұрын
I'm less than 3 minutes in and I'm going to sub. I can only imagine how you teach other topics, this was exceptional
@Giregar
@Giregar 2 жыл бұрын
Thanks you so much for this easy to understand introduction to Animation Frame.
@rajkiran4873
@rajkiran4873 3 жыл бұрын
Your explanation is simply awesome
@muegaschneemann
@muegaschneemann 2 жыл бұрын
YES, exactly what i was searching for!! Saved the vid!! Thanks a lot!
@sleeplessdev7204
@sleeplessdev7204 3 жыл бұрын
I find that, oftentimes, when people try to name things to be more "friendly", it ends up making things more confusing. This was overall a great explanation though!
@mohammedhayari28
@mohammedhayari28 2 жыл бұрын
thank you so much from morocco for your easy explanation
@dejandjosic1258
@dejandjosic1258 Жыл бұрын
Very useful and well explained. Thanks!
@arnav_0397
@arnav_0397 3 жыл бұрын
Amazing man, I just love the way you explain it,
@smaragd_
@smaragd_ 3 жыл бұрын
Your voice is like political radio host. Which goes quite well with these teachings as well. Thanks for simple examples and hints. :)
@bisratiskool2193
@bisratiskool2193 2 жыл бұрын
I don't think requestAnimationFrame has ever been better explained
@OmarOnAWave
@OmarOnAWave 2 жыл бұрын
Amazing explanation!!
@mouradelcadi
@mouradelcadi Жыл бұрын
Thanks for the beautiful video
@durgeshgupta6647
@durgeshgupta6647 3 жыл бұрын
awesome bro...clear explanation......from india
@DanishKhan-lh3ob
@DanishKhan-lh3ob 2 жыл бұрын
Super Clean Explanation :)
@saifeddine6479
@saifeddine6479 3 жыл бұрын
The way I explain things is just wonderful
@danielChibuogwu
@danielChibuogwu Жыл бұрын
Thank you for this video
@BrettCooper4702
@BrettCooper4702 4 жыл бұрын
Thanks, that was interesting.
@suvendudas5391
@suvendudas5391 4 жыл бұрын
Thank you again.
@ryandsouza7100
@ryandsouza7100 4 жыл бұрын
Hi Steve. Great video as always. Just one question. For which use-cases do you think it would be best to use this as an alternative for CSS animation?
@suvendudas5391
@suvendudas5391 4 жыл бұрын
Same here
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
When animating, stick to using transform and opacity as much as possible to get the best performance. Most things can be done in CSS but if you need to calculate position or property values then you will using JS. If you use JS then use requestAnimationFrame instead of setTimeout or setInterval.
@ryandsouza7100
@ryandsouza7100 4 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Cool. Noted. Thank you. 👍
@delcarmat
@delcarmat Жыл бұрын
thank you
@marcinkalmar9964
@marcinkalmar9964 2 жыл бұрын
Very good explanation , Sometimes i think that my sucess in programming dependence of teacher who teach me than of my brain capacity 😅
@stapler942
@stapler942 3 жыл бұрын
So I'm making a small grid-based game project in the JS canvas. I am needing a fixed time-step sort of mechanism for various updates. Would you say the easiest method is to call my draw() and update() functions after a certain count of frames in the requestAnimationFrame loop? Can I assume that this framerate will not be too different on different browsers/machines?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
The frame rate will definitely vary depending on the machine. But with requestAnimationFrame and highRes timestamps you can calculate things to run smoothly. You can also look at the Web Animations API.
@Jinxxeh
@Jinxxeh Жыл бұрын
Hi Steve, is there any benefit to wrapping something in window.requestAnimationFrame if you aren't repeatedly/recursively calling the callback? I've noticed people doing that sometimes and I'm not sure what the purpose is
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
It uses a different part of the event loop for scheduling things. So, if you need to have tasks, and microtasks and raf calls in a specific order, then it might be something you want. kzbin.info/www/bejne/rV7Nhn-OZalomck - video about the event loop etc.
@universalworthy2169
@universalworthy2169 Жыл бұрын
Hello Steve, thank you very much for your helpful video. I have a question: if I have multiple box elements and access them via an array const boxes = document.querySelectorAll(".box"); I would like to apply the box.style.transform ... for every item in the array when clicking with forEach, like boxes.forEach(item =>{item.addEventListener("click", ()=>handleClick()} ? I keep getting the error: Uncaught TypeError: Cannot set properties of undefined (setting 'transform'). I don't really understand this error. Please help. Thank you very much.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
Whenever you see an error that say "Cannot" do something "of undefined (setting x)" It means that the thing you wrote in front of x is undefined. Usually a misspelled variable or missing variable or wrong scope of variable.
@tails_the_god
@tails_the_god 2 жыл бұрын
Aw yes pretty smooth indeed.. I was getting smooth movements but now i get jerky movement in my windows programming when im programming my win32 NES emulator soooo it's about timing that causes it?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 жыл бұрын
Timing is a big part of it. You might find this interesting / helpful - kzbin.info/www/bejne/g3TMZ3SQgtCrmbc
@abhim6380
@abhim6380 4 жыл бұрын
How do you change the speed of the animation?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
The function runs automatically each time the screen is being painted. Most of the time this should be a fairly stable time increment with smooth animations. You can use the time passed between frames or time passed since the page loaded to calculate things.
@Barnardrab
@Barnardrab 2 жыл бұрын
The motion still looks jittery, so it's not smooth. Is this the best we can get with JavaScript?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 жыл бұрын
Animation with JS is a complex topic with many layers, many techniques, and many approaches. requestAnimationFrame is just one tool in the toolbox. There are many factors that go into making animations run smoothly and many things that can interfere with that.
@azealot7112
@azealot7112 3 жыл бұрын
How do I change the FPS?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
You can use the Performance Timing API to calculate a High Res time difference and calculate the distance you want to move each time you repaint. -developer.mozilla.org/en-US/docs/Web/API/User_Timing_API/Using_the_User_Timing_API Another option is to use the Web Animations API - developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
@allanimeworld2898
@allanimeworld2898 3 жыл бұрын
Sir i request to make a tut on making a finger swiper with vanilla js
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
You can make tutorial requests in the comments here - But I have several videos about that already: kzbin.info/www/bejne/nZLChH-wqLqXirs kzbin.info/www/bejne/qJnUloFnZcd-Z5I kzbin.info/www/bejne/gGGZhq1topJ9pJo
@allanimeworld2898
@allanimeworld2898 3 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 yeah sir First thank you for reply You taught them i am talking about touch image slider. Then we will get how to use them.
@necronomicon-xmortis9362
@necronomicon-xmortis9362 Ай бұрын
😢my numbers count but my box dont move 🤔🤷‍♂️
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Ай бұрын
did you type or copy and paste? It's a very simple code sample that should run anywhere.
@Ivan-dt9mc
@Ivan-dt9mc 2 жыл бұрын
thank you
@elkhanhamet2561
@elkhanhamet2561 3 жыл бұрын
thank you
100+ Linux Things you Need to Know
12:23
Fireship
Рет қаралды 34 М.
IntersectionObserver API
15:33
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 19 М.
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 93 МЛН
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 46 МЛН
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 134 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,6 МЛН
requestAnimationFrame() - Beau teaches JavaScript
4:22
freeCodeCamp.org
Рет қаралды 49 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 197 М.
Animation Loops in JavaScript using requestAnimationFrame
12:52
How to Use the Resize Observer API
9:24
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 8 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 137 М.
How To Create Performant CSS Animations
12:05
Web Dev Simplified
Рет қаралды 60 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 152 М.
Using FormData Objects Effectively
13:14
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 36 М.
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 93 МЛН