Breaking down a ripple animation in JavaScript(bryanbraun.com) |
Breaking down a ripple animation in JavaScript(bryanbraun.com) |
const anim = () => {
drawRipple();
requestAnimationFrame(anim);
}
anim();Now that I think of it, it might be worth adding an asterisk and footnote about requestAnimationFrame to the post.
Edit: I wonder why was this downvoted. Is setTimeout not more reliable time wise than setInterval?
Edit: looking into this some more, it seems like the recursive methods might not actually grow the call stack due to being asynchronous. Their only added baggage is the extra closure which would be negligible. Anyone know if this is actually the case?
<canvas id=c><svg onload=setInterval("for(c.width=64,i=4096;i--;)c.getContext('2d').fillRect(X=i&63,Y=i/64|0,1,Math.sin(Math.hypot(X-32,Y-32)/2-++t/4e4)/4+.5)",t=8)> void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 center = iMouse.xy/iResolution.xy;
float aspect = iResolution.x/iResolution.y;
vec2 uv = fragCoord/iResolution.xy;
uv.x *= aspect;
center.x *= aspect;
float r = distance(uv, center);
float h = (sin((r*50.0)-(iTime*2.0))+1.0)/2.0;
h *= 1.0 - min(1.0, r);
fragColor = vec4(h,h,h,1.0);
}Quick GLSL tutorial: https://web.archive.org/web/20210119091116/http://www.bidoui...
let distance = hypotenuseLength(reIndexedX, reIndexedY);
function hypotenuseLength(x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
The above can be replaced with: let distance = Math.hypot(reIndexedX, reIndexedY);
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...First learning how to code, then learning the math was a hard way to do the interesting stuff with computers. The other way around is much more straight forward.
In a similar vein to the ripple, here’s[1] the Batman Curve in FormulaGraph.
You described generating a ripple still-frame then completely glossed over the “animation” part.
https://www.iquilezles.org/www/articles/distfunctions/distfu...
Usually if you want to do any kind of posteriori procedural animation i.e real-time physics, you need a constant interval, and RAF does not give you this...
RAF usually runs at 60FPS but it can drop to 30 or increase to 120 depending on the browser, hardware and power saving modes - Which means whatever you are running in that function can get called at drastically different intervals for different users.
In this case the animation is a priori, meaning we don't have to simulate intermediate steps, so it's possible to correct for this inside the RAF alone by using Date.now() as the time parameter for shifting the sine wave... however this is not always the case. And in those cases setInterval can be far simpler when timing is important, but if you want to continue using RAF ultimately you would need to decouple rendering from "physics" or any timing related code that cannot be calculated a priori, and run the latter at a constant interval.
OTOH whether you want your animation to advance in real time is a separate issue, regardless of whether the animation is interpolated. If you do want real-time animations, and you're using an interpolated physics engine or similar, there really aren't any good options besides decoupling the interpolation from the rendering. And if you've done that, then it doesn't matter much whether the interpolation is called from RAF or setInterval (personally I use both, so that physics ticks can happen between RAF events if the CPU load is heavy).
But as a footnote, setInterval definitely does not give you a "constant" interval. Besides the normal variance, browsers will throttle the events (e.g. if the document is in a background tab).
RAF also stops executing when tab is inactive. Sometimes we don't want our loop halted and that's where setTimeout comes in as the better alternative to setInterval.
Your condescending responses are hardly genuine attempts at finding a plausible explanation for me being downvoted by people who can't think that pointing out thing A is better than thing B even though thing C exists and is the correct approach most of the time.
If anything your code will be much worse for it and likely to contain more bugs.
For each browser you add support for, x more browsers and access limitations you've never even heard of also become supported.