by geenat · 3y ago · 0 comments
You can do non-blocking animation timelines without any library if you use async javascript.
<html>
<body>
<div>Click me to change color every second, then remove.
<script>
// Locality of Behavior
me = document.currentScript.parentElement;
me.addEventListener("click", async event => {
me = event.target
me.style.transition = "background 1s"
await sleep(1000)
me.style.background = "red"
await sleep(1000)
me.style.background = "green"
await sleep(1000)
me.style.background = "blue"
await sleep(1000)
me.style.background = "none"
await sleep(1000)
me.remove()
})
const sleep = ms => new Promise(r => setTimeout(r, ms));
</script>
</div>
</body>
</html>
If you like this, you may also want to check out: https://github.com/gnat/surrealSo how is anime.js obsolete?
the optional "lib" is just aliases to make vanilla js less verbose.
await is only used to build the non-blocking timeline: Think keyframes, not frames.
Because `await setStyle` ain't it.
i'm so f#####g done with web dev.
You can do most things natively, if you have the time and luxury of reinventing the wheel.
https://developer.mozilla.org/en-US/docs/Web/API/window/requ...
https://stackoverflow.com/questions/38709923/why-is-requesta...
...and based on that I'm afraid you shouldn't be giving out advice on doing animations
Whether you use requestAnimationFrame which calls your code at every draw step or whether you set a CSS animation directive, you are delegating the animation work to the browser. In either case, the resultant behavior is dependent on browser-code, not your own js. This is very different than setting up your own manual draw loop with setTimeout.