5 ms·
Dumb question: In the code[1], the requestAnimationFrame() function is overwritten with a custom implementation using setTimeout(). From my understanding, this
by xg15 7y ago
Dumb question: In the code[1], the requestAnimationFrame() function is overwritten with a custom implementation using setTimeout(). From my understanding, this is done unconditionally, even if the browser provides the function (the reason is not a polyfill but having better control over the frame rate).
However, wasn't the whole idea of requestAnimationFrame() that it provides better timing guarantees than setTimeout()? So wouldn't overwriting with setTimeout kind of defeat the purpose of using it?
[1] https://github.com/anvaka/atree/blob/58680a99113cc40cd07c3716dd81491c0f263486/index.js#L140 https://github.com/anvaka/atree/blob/58680a99113cc40cd07c371...
- jagged-chisel 7y agoOverridden, overriding
- techbio 7y agoIt’s Christmas, so being generous, “overwritten” applies by the third definition here (https://www.dictionary.com/browse/overwritten https://www.dictionary.com/browse/overwritten) Also, conferring correct usage, ie. actionable information, may be a gift as well, as the fourth definition here (https://www.dictionary.com/browse/overridden https://www.dictionary.com/browse/overridden) I did not find either on (http://catb.org/jargon/html/ http://catb.org/jargon/html/)
- jagged-chisel 7y agoLanguage evolves and changes over time. I don't know whether the popular/common usage for the intended meaning (provide an alternative implementation of a function or method) has begun using "overwritten," but I'd argue that the underlying runtime implementation matters. Typically, one's own implementations doesn't have the ability to literally overwrite the original - the original exists in some place and continues to exist in that place. You can point original symbols at a new implementation, but that old implementation isn't "overwritten" in memory. In OO runtimes, you can override in a subclass (but not overwrite), and the super class implementation is still right there, available as needed. In the discussed case, if anything is truly "overwritten," it's the value (pointer?) at the symbol "requestAnimationFrame," but I'm doubting that's even that case - I'd wager (without being a deep JS expert) that the original is available in another scope. Even given all that, if popular usage wants to go with "overwritten," fine - I'm not a linguistic purist.
- anvaka 7y agoThe idea was to match animation speed of the gif and JavaScript implementation (which was 24 frames per second) requestAnimationFrame() (RAF) is more likely to fire approximately 60 times per second than setTimeout(..., 1000 / 60) would. But I needed 1000 / 24, and setTimeout worked very well, so I used it. If I were doing it all over again, I'd probably use RAF with window.performance, or simple counting variable as a timer. RAF is indeed much smoother than setTimeout and on top of it, RAF saves battery life when a user doesn't have your page open. Good question!
- exikyut 7y agoWhat about using normal RAF and slowing the animation itself down to match the original GIF as best as possible?
- bathtub365 7y agoI think that’s what they were suggesting with > If I were doing it all over again, I'd probably use RAF with window.performance Since RAF doesn’t give a guaranteed framerate you’d need to use metrics from window.performance to make the animation framerate-independent, or people on different browsers or devices could see the animation at different speeds.
- thinkloop 7y agoA use-case where slower framerate is an upgrade - rare, but they exist.
- tripzilch 7y agoMy use case was doing a metaball animation using procedural scribbling[0], because the scribbles are random each frame, I needed a much slower framerate (10 or 15fps, IIRC), otherwise they'd look like a twitchy mess :) [0] https://www.instagram.com/p/B3FWPi4o4Vq/ https://www.instagram.com/p/B3FWPi4o4Vq/
- modeless 7y agoYes, if you want to use setTimeout you should just use it directly. It's bad practice to override built in functions like requestAnimationFrame to do something different. If the intent is to throttle the frame rate, then you should still use requestAnimationFrame, but simply return immediately if it's not time for a new frame yet. This can get you more consistent frame spacing than using setTimeout. But the difference is probably small. Throttling the frame rate is a legitimate artistic choice that can evoke the feeling of watching a film projection; however it seems like the intent here may have been to slow down the speed of the dots, not necessarily to use a slower frame rate for its artistic effect. The animation speed is tied to the frame rate because the animation is advanced a fixed amount each frame. This is bad practice as well; instead the animation should be advanced based on the timestamp passed to requestAnimationFrame. Then the speed of the dots would be constant no matter the frame rate, and users of high frame rate displays would enjoy smoother motion. All that said, it's a pretty animation that serves its purpose perfectly and none of these concerns are important in this context.