Creating an Interactive Gear Simulation
This guide demonstrates how to build an interactive web simulation that shows the rotation behavior of interlocking gears. Users can modify the number of teeth on three gears (A, B, and C) and observe the resulting motion. The central question is: "How many times must Gear A turn to make all dots align again?"
![]() |
Objective
The goal is to simulate three interlocking gears (A, B, C), where turning Gear A causes Gears B and C to turn based on their teeth ratios. Each gear has a reference dot. The user can adjust the number of teeth and rotate Gear A to determine how many turns it takes for all dots to line up again.
1. HTML Structure (index.html
)
The HTML structure is essential for building the layout:
-
Title: An
<h1>
tag to present the question. -
Gear Elements: Three
div
elements (id="gearA"
,id="gearB"
,id="gearC"
) to represent the gears, each containing:-
A
div
for the radius line (class="radius-line"
). -
A
div
for the reference dot (class="dot"
).
-
-
Controls:
-
Buttons for "Turn" (
id="turnButton"
) and "Reset" (id="resetButton"
).
-
-
Inputs & Labels:
-
Number input fields (
id="teethA"
,id="teethB"
,id="teethC"
) for setting the teeth count. -
Labels for each gear and span elements to display the rotation count.
-
-
Answer Section:
-
An input field (
id="answerA"
) for the user’s guess. -
A button (
id="checkAnswerButton"
) for checking the answer. -
A
p
tag (id="feedbackMessage"
) for feedback.
-
-
External Resources:
<link>
forstyle.css
and<script>
forscript.js
.
2. CSS Styling (style.css
)
CSS handles the appearance and animation:
-
Gear Style: Use
border-radius: 50%
to make gears circular and style them with a background color and border. Use a::before
pseudo-element to represent the axle. -
Dot and Radius Line: Style the
.dot
as a small circle and.radius-line
as a thin line from the center of the gear to the edge. -
Positioning: Use absolute positioning for gears and relative positioning for the container. JavaScript will handle gear positioning.
-
Rotation Animation: Smooth rotation is achieved using
transition: transform 0.5s linear;
for the.gear
class. -
Layout: Use Flexbox to arrange controls and labels.
-
Responsive Design: Use
@media
queries for mobile responsiveness.
3. JavaScript Logic (script.js
)
JavaScript controls the interactive behavior:
-
Initialization: Reference HTML elements and set initial values.
-
updateGearVisuals()
: Adjust the size and position of each gear based on the number of teeth, ensuring they mesh properly. -
applyRotation()
: Triggered by the "Turn" button, it rotates the gears based on the inputted teeth ratios. It updates the rotation angles and modifies their visual transformations. -
resetGears()
: Resets the rotation and inputs to default values. -
Answer Calculation: Use the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) to calculate the number of turns for alignment.
-
checkAnswer()
: Compares the user's answer with the calculated answer and provides feedback. -
Event Listeners: Attach functions to relevant buttons and input changes.
Replication Steps
-
Save the HTML structure in
index.html
. -
Save the CSS in
style.css
. -
Save the JavaScript in
script.js
. -
Open
index.html
in your browser.
Now, you can experiment with different gear configurations and see how the gears interact and align.
Conclusion
This interactive gear simulation combines HTML for structure, CSS for styling, and JavaScript for dynamic behavior and calculations.
![]() |
gear_simulation/ gear_simulation_20250421-155059.zip gear_simulation_sls_20250423-082511.zip |
The core mathematical concept involves using the Least Common Multiple (LCM) based on gear teeth ratios to find the point of simultaneous realignment.
For Teachers
[SIMU_TEACHER]
Software Requirements
[SIMU_SWREQ]
Translation
[text]
Research
[text]
Video
[text]
Credits
[SIMU_CREDITS]
Version:
https://weelookang.blogspot.com/2025/04/creating-interactive-gear-simulation.html
Other Resources
[text]