WEB LAB 07 · JavaScript / SVG / Collision detection
Letters fall and pile up. Create physical animation of characters with JavaScript and SVG
A tactile web demo in which characters are dropped as building blocks. We will introduce the detection of gravity, floor repulsion, and collision between rectangles using JavaScript, and how to make adjustments.
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.
Drag the text or select it with Tab and move it with the arrow keys. Maximum 16 characters.
Recipe for how to play
Ready.
The device's "Reduce parallax effect" setting suppresses movement when displaying images.
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.
Enter a short word or phrase and press “Drop the letters”. A block for each character will fall into the box.
Lift a block and let go, then shake the box to rearrange the stack.
You can select building blocks using Tab and move them using the arrow keys. Try using short words of your choice instead of your full name.
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/letters/ 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==='letters').
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
Creates an SVG group for each character and maintains position, speed, and whether or not it is being dragged. Adds gravity to the downward velocity every frame.
Point 2
On the floor, reverse the speed and weaken it. Blocks are checked for overlap as rectangles along the axis, and pushed back to the axis where there is a small overlap.
Point 3
Repeat the push back four times in one frame to adjust the stacking. In order to examine all combinations, the number of characters is limited to 16.
Try changing one thing and compare.
Open src/scripts/experiments.ts in your editor and find the branch for this experiment.
What to change:In the letters experiment's step function, change 1000 in b.vy+=1000*dt to 400.
Result changes:The blocks now fall more slowly. Use “Drop the letters” to restart from the initial state and compare the fall with the original version.
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.
This is a simple simulation that does not handle rotations or complex shapes. It is not a substitute for a full-fledged physics engine that handles large numbers of objects reliably.
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.