Move the lamp, move the shadows: build a small study with SVG and JavaScript
An SVG study-room demo with day/night, lamp and blind controls. Learn how state updates change colors, positions and opacity, and how to animate the shadows.
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.
Create a study room for your favorite time by moving the lights and shadows.
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.
Switch between day and night. Confirm that the appearance of walls, sky, and light changes.
Move the lamp from side to side and watch the shadow of the desk move in the opposite direction.
Change the conditions one by one by turning off the lamp or opening the blinds and compare.
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/study/ 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==='study').
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
Collect the two states night and lamp and the values of the slider and selection field in draw(). By gathering the drawings in one place, you can avoid discrepancies between the button labels and the pictures.
Point 2
The center of the shadow is 350-x*.65 and the width is 65+Math.abs(x)*.3. The shadow is shifted in the opposite direction according to the amount of movement of the lamp, and spread out by the distance.
Point 3
The triangle of light expresses strength with opacity. The blinds use a combination of translate and scale to create an open look.
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 study experiment's draw function, change the expression for the shadow's center from 350-x*.65 to 350-x*1.0.
Result changes:Even if you move the lamp the same amount, the shadow will move more left and right. Since RX, which determines the size of the shadow, is not changed, you can notice the difference in position.
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.
It is not a rendering that traces light rays, but a 2D expression that uses formulas to make it look like it. Actual shadow shape and light occlusion are not calculated.
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.