WEB LAB 03 · JavaScript / Web Animations API / SVG
One push and it moves one after another. Create chain animation with JavaScript
A demonstration that connects movements from balls to dominoes to drawers. We will introduce how to control the order, speed change, and reset processing using the Web Animations API and async/await.
It's fun to touch. You can understand by making it. A small laboratory for the web. Run it first, and if you feel like it, open the code and try changing one thing.
First, try touching it.
When you press the switch, they start moving in order.
Recipe for how to play
Ready.
The device's "Reduce parallax effect" setting suppresses movement when displaying images.
See the code for this trick
// 前の動きが終わってから次へ
await ball.animate(path, timing).finished;
for (const domino of dominoes) {
await domino.animate(fall, timing).finished;
}
await drawer.animate(open, timing).finished;
Here is an excerpt showing how it works. Click here for the entire process including drawing, input operations, and stop processing.
For a playful CTA. If you transform using transform, the position of the surrounding text will not change. Even if you hit repeatedly, the animation will not overlap.
For feedback on successful operations, such as copy completion. This model only increases the number of marks when you press it, but does not save or send it. There can be up to 6 marks.
status.textContent = 'コピーしました';
None of these demos plays sound. Animations are skipped when your device requests reduced motion.
Try them in this order and you'll see the difference.
Press the start button. Make sure the ball moves, then the domino, and then the drawer.
Change the speed and run it again. The shape remains the same, but the speed of movement changes.
Reset in the middle. If the old movements stop and you can start over, you have succeeded.
To run it on your own computer.
The entire demo on this page is in a public repository. Below are the steps to copy and run the site. The short code snippet above won't work.
1. Check the execution environment
Open a terminal and run it line by line. Requires Git and Node.js.
git --version
node --version
npm --version
Results to check: Each command should display a version number. This project requires Node.js 22.12.0 or later. If a command is missing, use Git official and Node.js official to set up your environment.
2. Get the code and launch
Move to the folder you want to save it to, then execute the following line by line. If a folder with the same name already exists, choose a different location.
git clone https://github.com/R-Koda/ryu-koda.com.git
cd ryu-koda.com
npm ci
npm run dev -- --host 127.0.0.1 --port 4321
Results to check:Finally, the local server URL will be displayed and the terminal will remain open. If an error occurs, do not proceed and check the output of the previous command.
3. Open the experiment page
Open http://127.0.0.1:4321/playground/chain/ in your browser. If the same demo appears and responds as described above, it is running successfully. If Terminal shows a different port, use that port instead. Press Ctrl+C in Terminal to stop the server.
Which file should I read?
src/components/Experiment.astro:SVG, HTML, CSS for appearance.
src/scripts/experiments.ts contains the experiment interactions. Look for if(kind==='chain').
src/pages/playground/[slug].astro: Common layout for this explanation page.
If the screen is white, check your terminal for errors and your browser's developer tools. If the movement stops, also check the "Reduce parallax effect" setting on your device. This demo will suppress movement according to that setting.
How does it work?
Point 1
Element.animate() returns an Animation. Await its finished promise to wait for the current animation to finish before starting the next step.
Point 2
Await each domino in a for...of loop. Switching to Promise.all makes them fall together, rather than one after another.
Point 3
On reset, cancel the active Animation and update generation. This prevents an older sequence from resuming later even if its pending work completes.
Try changing one thing and compare.
Open src/scripts/experiments.ts in your editor and find the branch for this experiment.
What to change:Change the coefficient 1.5 at the end of the chain ball's animate call to 3.
Result changes:Only the section where the ball rolls will take about twice as long, and the speed of the section where the dominoes fall will remain the same.
Once saved, return to your browser and reload if necessary. Make a note of the original values so you can quickly go back and compare.
before incorporating it into your site.
Chaining is not a calculation of collisions, but a sequential playback of prepared animations. This code is not used for physical simulation.
Instead of conveying information through movement alone, it can also be used with keyboard operations and settings that reduce movement. First, incorporate it into one part and make sure that it does not interfere with the readability of articles and menus.