5 ms·
I was wondering why the animation was so stuttery and then I saw they are using setInterval for the animation... Jesus it's 2020 at least use requestAnimationF
by pier25 7y ago
I was wondering why the animation was so stuttery and then I saw they are using setInterval for the animation...
Jesus it's 2020 at least use requestAnimationFrame!
https://flaviocopes.com/requestanimationframe/ https://flaviocopes.com/requestanimationframe/
- kamikaz1k 7y agoIt's a pretty simple page, setTimeout should work well enough. Is the interval too high?
- winrid 7y agoThe issue's not setTimeout. Even with requestAnimationFrame, you'd combine it with setTimeout to control framerate and cpu usage. The issue is the css properties manipulated, as commented by another person.
- kroltan 7y agoIt's not just about speed, it's about timing. With setInterval the alignment of the screen refresh and the code to update will quickly become out of sync and cause periods where 2 frames are the same.
- aphextron 7y ago>I was wondering why the animation was so stuttery and then I saw they are using setInterval for the animation... setInterval isn't the problem. He's animating the CSS position (top, left), which triggers a full reflow and causes the stuttering. Always animate with translate3d, which is GPU accelerated.
- tantalor 7y agoOoh you could even animate with a css transition from one bounce to the next with a linear curve; then you don't run any JS per frame, only at the bounces.
- augusto-moura 7y agoThe problem I think is that transitions have fixed amount of time, both long and short travels finish with the same amount of time giving a different velocity of movement. Also executing Javascript every frame is not that heavy
- jbroman 7y agoExecuting JavaScript every frame is not that heavy, but if the main thread is blocked because of something that is, which may even be in another tab, the animation will stutter. The animations built into the platform can run and remain smooth under a broader range of conditions, which is why they should be preferred if you don't need the level of control that running script via requestAnimationFrame gives you.
- deleted 7y ago[deleted]
- ajflores1604 7y agoAs someone who's new to front end and css is there a good resource on what properties are more costly, or which functions are newer and meant to be more performant like translate3d is mentioned as being gpu accelerated? I've seen mentions of reflow, repaint, and layout thrashing but not a comprehensive list of what functions are meant to replace what other ones, or relative cost of each function in common use cases. I've managed to grasp javascript fairly easily coming from python, but css seems like this obscure black box that I thought would be much easier to get a handle on.
- XaspR8d 7y agoOr alternatively, write it as a CSS animation and modify it on collision.
- Cyykratahk 7y agoExactly. This is what CSS Animations [1] were made for. And you don't even need to do collision detection, as CSS can already alternate directions and loop infinitely. https://bouncing-logo-css.glitch.me/ https://bouncing-logo-css.glitch.me/ Just needed a tiny bit of JS to set the sizes and randomise the logo colour. [1] https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animati...
- XaspR8d 7y agoNice demo. I was curious how you were going to avoid collision -- my assumption was since the pattern wasn't a "single cycle" loop (whose total length presumably depends on the LCM of the width and height) that the animation setting would either need to be very complicated or changed periodically, but you cleverly sidestepped the whole problem by applying X and Y translations to different wrappers so they can each have their own fixed period: smart!
- tengbretson 7y agosetTimeout would probably be fine if they used the diff in elapsed time between calls to derive the position deltas rather than assuming it is always going to be 30ms.
- echeese 7y agoI wrote my own version - seems to run smoother https://speets.ca/dvd/ https://speets.ca/dvd/
- deleted 7y ago[deleted]
- herpderperator 7y agoIt stutters for me, but less than the original.
- gruez 7y agoIt stutters on iGPU graphics but not on dGPU graphics, both on firefox.
- deleted 7y ago[deleted]
- n_u_l_l 7y agoThe source image is also pixelated[1]. Could've used a vector instead[2] 1. https://www.bouncingdvdlogo.com/logos/dvdlogo-01.png https://www.bouncingdvdlogo.com/logos/dvdlogo-01.png 2. https://www.brandsoftheworld.com/logo/dvd-video-0 https://www.brandsoftheworld.com/logo/dvd-video-0