Prompt specification
Build or customize the Three.js 3D scene "Eccentric Cam and Guided Follower" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Eccentric Cam and Guided Follower
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/eccentric-cam-and-guided-follower-x2013
Scene Description Prompt:
Eccentric cam and guided follower. Model an eccentric circular cam beneath a vertically guided follower. Show the offset axis and contact relationship before animating the mechanism. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (eccentric-cam-and-guided-follower-x2013):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Eccentric Cam and Guided Follower</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(12, 18, 34, 0.86);
--border: rgba(255, 255, 255, 0.13);
--text: #eef4ff;
--muted: #a8b5cc;
--cyan: #59d8ff;
--orange: #ff914d;
--lime: #b9ff66;
--purple: #c59cff;
--yellow: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #080d19;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(14px);
}
#title-card {
top: 22px;
left: 22px;
padding: 17px 20px;
max-width: 390px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#status {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 13px;
padding: 7px 10px;
border: 1px solid rgba(185, 255, 102, 0.3);
border-radius: 999px;
background: rgba(185, 255, 102, 0.08);
color: var(--lime);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
content: "";
box-shadow: 0 0 12px currentColor;
}
#status.running {
color: var(--cyan);
border-color: rgba(89, 216, 255, 0.35);
background: rgba(89, 216, 255, 0.09);
}
#status.paused {
color: var(--yellow);
border-color: rgba(255, 209, 102, 0.35);
background: rgba(255, 209, 102, 0.09);
}
#construction-card {
bottom: 22px;
left: 22px;
width: 355px;
padding: 17px 18px;
}
#construction-card h2 {
margin: 0 0 8px;
font-size: 17px;
}
#construction-card p {
margin: 0 0 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.relation {
display: grid;
grid-template-columns: 18px 1fr;
gap: 7px 9px;
align-items: start;
margin: 10px 0 13px;
font-size: 12px;
line-height: 1.35;
}
.swatch {
width: 10px;
height: 10px;
margin-top: 4px;
border-radius: 3px;
}
.formula {
display: inline-block;
margin-left: 4px;
color: #fff;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
}
.readout {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.readout div {
padding: 9px 8px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.readout span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 3px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
#controls {
right: 22px;
bottom: 22px;
width: 295px;
padding: 16px;
}
.button-row {
display: flex;
gap: 9px;
}
button {
min-height: 42px;
padding: 0 14px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 10px;
background: #202b43;
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #2a3854;
}
#start {
flex: 1;
border-color: rgba(89, 216, 255, 0.45);
background: var(--cyan);
color: #07111b;
}
#start:hover {
background: #7ee3ff;
}
#reset {
flex: 1;
}
.speed-row {
display: flex;
align-items: center;
gap: 11px;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: var(--muted);
font-size: 11px;
}
.toggle-row label {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
}
input[type="checkbox"] {
accent-color: var(--cyan);
}
#camera-hint {
position: absolute;
top: 22px;
right: 22px;
padding: 10px 13px;
color: var(--muted);
font-size: 11px;
}
#camera-hint button {
min-height: 30px;
margin-left: 9px;
padding: 0 10px;
font-size: 10px;
}
.tag {
position: absolute;
z-index: 5;
display: none;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 8px;
background: rgba(8, 13, 25, 0.87);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.28);
color: #fff;
font-size: 10px;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(7px);
}
.tag b {
font-size: 12px;
}
.tag span {
color: var(--muted);
}
#label-O {
border-color: rgba(89, 216, 255, 0.5);
}
#label-O b {
color: var(--cyan);
}
#label-e {
border-color: rgba(255, 255, 255, 0.35);
}
#label-C {
border-color: rgba(255, 255, 255, 0.3);
}
#label-Q {
border-color: rgba(185, 255, 102, 0.55);
}
#label-Q b {
color: var(--lime);
}
#label-P {
border-color: rgba(255, 209, 102, 0.55);
}
#label-P b {
color: var(--yellow);
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
}
#construction-card {
display: none;
}
#controls {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
}
#camera-hint {
display: none;
}
.subtitle {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive eccentric cam and guided roller follower"></canvas>
<div id="overlay">
<section id="title-card" class="panel">
<div class="eyebrow">Kinematic construction</div>
<h1>Eccentric Cam &<br>Guided Roller Follower</h1>
<p class="subtitle">
Inspect the offset pivot and common-normal contact before starting the motion.
</p>
<div id="status" aria-live="polite">Inspection view · motion locked</div>
</section>
<section id="construction-card" class="panel">
<div class="eyebrow">Step 1 — inspect</div>
<h2>Contact construction</h2>
<p>
The circular cam rotates about fixed axis O, while its geometric center C
travels on an eccentric circle. The roller center P remains on the
vertical guide.
</p>
<div class="relation">
<div class="swatch" style="background: var(--cyan)"></div>
<div>Fixed rotation axis through <b>O</b></div>
<div class="swatch" style="background: #f5f7ff"></div>
<div>
Eccentric offset
<span class="formula">|OC| = e = 0.46</span>
</div>
<div class="swatch" style="background: var(--orange)"></div>
<div>
Cam radius
<span class="formula">|CQ| = R = 1.20</span>
</div>
<div class="swatch" style="background: var(--lime)"></div>
<div>
Contact at Q and roller radius
<span class="formula">|QP| = r = 0.32</span>
</div>
<div class="swatch" style="background: var(--purple)"></div>
<div>
Tangent at Q is perpendicular to the common normal
<span class="formula">C–Q–P</span>
</div>
</div>
<div class="readout">
<div>
<span>Cam angle</span>
<strong id="angle-readout">35.0°</strong>
</div>
<div>
<span>Roller y</span>
<strong id="height-readout">0.00</strong>
</div>
<div>
<span>Travel Δ</span>
<strong id="travel-readout">0.00</strong>
</div>
</div>
</section>
<section id="controls" class="panel">
<div class="button-row">
<button id="start" type="button">▶ Start animation</button>
<button id="reset" type="button">Reset inspection</button>
</div>
<div class="speed-row">
<span>Speed</span>
<input id="speed" type="range" min="0.2" max="1.5" step="0.05" value="0.65">
<strong id="speed-value">0.65×</strong>
</div>
<div class="toggle-row">
<label>
<input id="guides" type="checkbox" checked>
Show construction guides
</label>
<span>Low-poly model</span>
</div>
</section>
<div id="camera-hint" class="panel">
Drag to orbit · wheel to zoom · double-click to frame
<button id="camera-reset" type="button">Reset view</button>
</div>
<div id="label-O" class="tag"><b>O</b><span>fixed axis</span></div>
<div id="label-e" class="tag"><b>e</b><span>offset</span></div>
<div id="label-C" class="tag"><b>C</b><span>cam center</span></div>
<div id="label-Q" class="tag"><b>Q</b><span>contact</span></div>
<div id="label-P" class="tag"><b>P</b><span>roller center</span></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene
// ------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d19);
scene.fog = new THREE.Fog(0x080d19, 10, 24);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
let cameraYaw = 0.56;
let cameraPitch = 1.14;
let cameraRadius = 8.7;
function updateCamera() {
const sinPitch = Math.sin(cameraPitch);
camera.position.set(
cameraTarget.x + cameraRadius * sinPitch * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.cos(cameraPitch),
cameraTarget.z + cameraRadius * sinPitch * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9d9ff, 0x182033, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4fc3ff, 1.4);
rimLight.position.set(-5, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9e57, 16, 10, 2);
warmLight.position.set(2.5, 4, 3);
scene.add(warmLight);
// ------------------------------------------------------------
// Materials and low-poly geometry helpers
// ------------------------------------------------------------
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x111a2d,
roughness: 0.88,
metalness: 0.18,
flatShading: true
}),
baseEdge: new THREE.MeshStandardMaterial({
color: 0x263653,
roughness: 0.72,
metalness: 0.42,
flatShading: true
}),
cam: new THREE.MeshStandardMaterial({
color: 0x28bfa2,
roughness: 0.58,
metalness: 0.28,
flatShading: true
}),
camDetail: new THREE.MeshStandardMaterial({
color: 0x74e8d0,
emissive: 0x0b493e,
emissiveIntensity: 0.28,
roughness: 0.48,
metalness: 0.32
}),
orange: new THREE.MeshStandardMaterial({
color: 0xff8a3d,
roughness: 0.52,
metalness: 0.28,
flatShading: true
}),
follower: new THREE.MeshStandardMaterial({
color: 0xf4aa24,
roughness: 0.56,
metalness: 0.38,
flatShading: true
}),
roller: new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x4a3000,
emissiveIntensity: 0.22,
roughness: 0.45,
metalness: 0.42,
flatShading: true
}),
guide: new THREE.MeshStandardMaterial({
color: 0x71819e,
roughness: 0.62,
metalness: 0.64,
flatShading: true
}),
guideDark: new THREE.MeshStandardMaterial({
color: 0x26334a,
roughness: 0.72,
metalness: 0.5,
flatShading: true
}),
bearing: new THREE.MeshStandardMaterial({
color: 0x34445e,
roughness: 0.56,
metalness: 0.6,
flatShading: true
})
};
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderZ(
parent,
radius,
depth,
position,
material,
segments = 24
) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
depth,
segments,
1,
false
);
// CylinderGeometry is Y-aligned; rotate its axis to Z.
geometry.rotateX(Math.PI / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addTorus(
parent,
radius,
tube,
position,
material,
radialSegments = 6,
tubularSegments = 28
) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(
radius,
tube,
radialSegments,
tubularSegments
),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Base, bearing support, and guide structure
// ------------------------------------------------------------
const base = addBox(
scene,
new THREE.Vector3(7.4, 0.25, 3.5),
new THREE.Vector3(0, -0.43, 0),
materials.base
);
addBox(
scene,
new THREE.Vector3(7.0, 0.08, 3.1),
new THREE.Vector3(0, -0.265, 0),
materials.baseEdge
);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x080d18,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.575;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(16, 32, 0x355172, 0x1a2941);
grid.position.y = -0.565;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// Fixed bearing pedestal behind the eccentric cam.
addBox(
scene,
new THREE.Vector3(0.78, 1.68, 0.38),
new THREE.Vector3(0, 0.50, -0.52),
materials.guideDark
);
const O = new THREE.Vector3(0, 1.55, 0);
const CAM_RADIUS = 1.20;
const ROLLER_RADIUS = 0.32;
const ECCENTRICITY = 0.46;
const CONTACT_DISTANCE = CAM_RADIUS + ROLLER_RADIUS;
addCylinderZ(
scene,
0.30,
0.24,
new THREE.Vector3(O.x, O.y, -0.36),
materials.bearing,
16
);
addTorus(
scene,
0.22,
0.045,
new THREE.Vector3(O.x, O.y, -0.225),
materials.guide,
6,
20
);
// Fixed pivot shaft.
addCylinderZ(
scene,
0.085,
1.45,
new THREE.Vector3(O.x, O.y, 0),
materials.guide,
16
);
// Vertical follower guides.
const guideHeight = 2.25;
const guideCenterY = 4.20;
for (const x of [-0.70, 0.70]) {
addBox(
scene,
new THREE.Vector3(0.105, guideHeight, 0.12),
new THREE.Vector3(x, guideCenterY, -0.18),
materials.guide
);
addBox(
scene,
new THREE.Vector3(0.34, 0.12, 0.46),
new THREE.Vector3(x, 3.08, -0.18),
materials.guideDark
);
}
addBox(
scene,
new THREE.Vector3(1.62, 0.12, 0.46),
new THREE.Vector3(0, 5.34, -0.18),
materials.guideDark
);
// ------------------------------------------------------------
// Eccentric cam
// ------------------------------------------------------------
const camRotor = new THREE.Group();
camRotor.position.copy(O);
scene.add(camRotor);
// The geometric disk center is offset from the pivot by e.
addCylinderZ(
camRotor,
CAM_RADIUS,
0.40,
new THREE.Vector3(ECCENTRICITY, 0, 0),
materials.cam,
28
);
addTorus(
camRotor,
0.82,
0.025,
new THREE.Vector3(ECCENTRICITY, 0, 0.218),
materials.camDetail,
5,
28
);
// Visible eccentric arm makes the rotation readable.
addBox(
camRotor,
new THREE.Vector3(ECCENTRICITY, 0.105, 0.07),
new THREE.Vector3(ECCENTRICITY * 0.5, 0, 0.235),
materials.orange
);
// Pivot hub.
addCylinderZ(
camRotor,
0.205,
0.16,
new THREE.Vector3(0, 0, 0.285),
materials.orange,
16
);
// Geometric-center plug.
addCylinderZ(
camRotor,
0.095,
0.08,
new THREE.Vector3(ECCENTRICITY, 0, 0.245),
materials.camDetail,
14
);
// A radial face mark rotates with the cam.
addBox(
camRotor,
new THREE.Vector3(0.58, 0.055, 0.035),
new THREE.Vector3(ECCENTRICITY + 0.28, 0, 0.23),
materials.camDetail
);
// ------------------------------------------------------------
// Vertically guided roller follower
// ------------------------------------------------------------
const follower = new THREE.Group();
scene.add(follower);
addCylinderZ(
follower,
ROLLER_RADIUS,
0.48,
new THREE.Vector3(0, 0, 0),
materials.roller,
20
);
addTorus(
follower,
0.205,
0.025,
new THREE.Vector3(0, 0, 0.255),
materials.orange,
5,
20
);
addCylinderZ(
follower,
0.085,
0.58,
new THREE.Vector3(0, 0, 0.015),
materials.guide,
14
);
// Vertical follower stem.
addBox(
follower,
new THREE.Vector3(0.145, 1.15, 0.16),
new THREE.Vector3(0, 0.56, 0),
materials.follower
);
// Guided crosshead.
addBox(
follower,
new THREE.Vector3(1.48, 0.38, 0.48),
new THREE.Vector3(0, 1.17, -0.02),
materials.follower
);
for (const x of [-0.70, 0.70]) {
addBox(
follower,
new THREE.Vector3(0.24, 0.46, 0.58),
new THREE.Vector3(x, 1.17, -0.02),
materials.guideDark
);
}
// ------------------------------------------------------------
// Construction guides
// ------------------------------------------------------------
const annotations = new THREE.Group();
scene.add(annotations);
function createLine(color, dashed = false, opacity = 1) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(6), 3)
);
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.11,
gapSize: 0.075,
depthTest: false
})
: new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthTest: false
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
line.renderOrder = 10;
annotations.add(line);
return line;
}
function setLinePoints(line, a, b) {
const positions = line.geometry.getAttribute("position");
positions.setXYZ(0, a.x, a.y, a.z);
positions.setXYZ(1, b.x, b.y, b.z);
positions.needsUpdate = true;
if (line.material.isLineDashedMaterial) {
line.computeLineDistances();
}
}
const axisLine = createLine(0x59d8ff, false, 0.95);
const offsetLine = createLine(0xf5f7ff, true, 0.90);
const guideLine = createLine(0xffd166, true, 0.72);
const camRadiusLine = createLine(0xff914d, false, 1);
const rollerRadiusLine = createLine(0xb9ff66, false, 1);
const tangentLine = createLine(0xc59cff, false, 0.95);
setLinePoints(
axisLine,
new THREE.Vector3(O.x, O.y, -1.05),
new THREE.Vector3(O.x, O.y, 1.28)
);
setLinePoints(
guideLine,
new THREE.Vector3(0, 1.10, 0.34),
new THREE.Vector3(0, 5.25, 0.34)
);
// Arrowhead indicating the positive rotation-axis direction.
const axisArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.18, 8),
new THREE.MeshBasicMaterial({
color: 0x59d8ff,
depthTest: false
})
);
axisArrow.rotation.x = Math.PI / 2;
axisArrow.position.set(O.x, O.y, 1.31);
axisArrow.renderOrder = 11;
annotations.add(axisArrow);
const contactMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.058, 10, 7),
new THREE.MeshBasicMaterial({
color: 0xb9ff66,
depthTest: false
})
);
contactMarker.renderOrder = 12;
annotations.add(contactMarker);
// ------------------------------------------------------------
// Kinematic relationship
//
// C = cam center
// P = roller center, constrained to x = 0
// Q = common contact point
//
// |CP| = R + r
// Q lies between C and P
// ------------------------------------------------------------
const C = new THREE.Vector3();
const P = new THREE.Vector3();
const Q = new THREE.Vector3();
const Cline = new THREE.Vector3();
const Pline = new THREE.Vector3();
const Qline = new THREE.Vector3();
let angle = THREE.MathUtils.degToRad(35);
let initialFollowerHeight = 0;
let running = false;
let hasStarted = false;
let speed = 0.65;
function updateMechanism(theta) {
const cos = Math.cos(theta);
const sin = Math.sin(theta);
C.set(
O.x + ECCENTRICITY * cos,
O.y + ECCENTRICITY * sin,
0
);
// Intersect the circle of radius R+r about C with the
// vertical follower guide x = 0. Choose the upper solution.
const horizontalDistance = P.x - C.x;
const verticalDistance = Math.sqrt(
CONTACT_DISTANCE * CONTACT_DISTANCE -
horizontalDistance * horizontalDistance
);
P.set(
0,
C.y + verticalDistance,
0
);
Q.lerpVectors(C, P, CAM_RADIUS / CONTACT_DISTANCE);
camRotor.rotation.z = theta;
follower.position.set(P.x, P.y, P.z);
const overlayZ = 0.305;
Cline.set(C.x, C.y, overlayZ);
Pline.set(P.x, P.y, overlayZ);
Qline.set(Q.x, Q.y, overlayZ);
setLinePoints(offsetLine, Cline, Pline.clone().lerp(Cline, 1));
setLinePoints(
offsetLine,
new THREE.Vector3(O.x, O.y, overlayZ),
Cline
);
setLinePoints(camRadiusLine, Cline, Qline);
setLinePoints(rollerRadiusLine, Qline, Pline);
// Tangent to both circles at Q is perpendicular to C-Q-P.
const normalX = (P.x - C.x) / CONTACT_DISTANCE;
const normalY = (P.y - C.y) / CONTACT_DISTANCE;
const tangentX = -normalY;
const tangentY = normalX;
const tangentLength = 0.34;
setLinePoints(
tangentLine,
new THREE.Vector3(
Q.x - tangentX * tangentLength,
Q.y - tangentY * tangentLength,
overlayZ
),
new THREE.Vector3(
Q.x + tangentX * tangentLength,
Q.y + tangentY * tangentLength,
overlayZ
)
);
contactMarker.position.set(Q.x, Q.y, overlayZ + 0.025);
const degrees = ((THREE.MathUtils.radToDeg(theta) % 360) + 360) % 360;
document.getElementById("angle-readout").textContent =
degrees.toFixed(1) + "°";
document.getElementById("height-readout").textContent =
P.y.toFixed(2);
const travel = P.y - initialFollowerHeight;
document.getElementById("travel-readout").textContent =
(travel >= 0 ? "+" : "") + travel.toFixed(2);
}
// Establish the inspection reference height.
updateMechanism(angle);
initialFollowerHeight = P.y;
updateMechanism(angle);
// ------------------------------------------------------------
// DOM labels projected from 3D positions
// ------------------------------------------------------------
const labelElements = {
O: document.getElementById("label-O"),
e: document.getElementById("label-e"),
C: document.getElementById("label-C"),
Q: document.getElementById("label-Q"),
P: document.getElementById("label-P")
};
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, dx = 0, dy = 0) {
if (!annotations.visible) {
element.style.display = "none";
return;
}
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
if (!visible) {
element.style.display = "none";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + dx;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + dy;
element.style.display = "flex";
element.style.left = x + "px";
element.style.top = y + "px";
}
function updateLabels() {
placeLabel(
labelElements.O,
new THREE.Vector3(O.x, O.y + 0.10, 1.30),
-24,
-12
);
placeLabel(
labelElements.e,
new THREE.Vector3(
(O.x + C.x) * 0.5,
(O.y + C.y) * 0.5,
0.34
),
18,
-18
);
placeLabel(
labelElements.C,
new THREE.Vector3(C.x, C.y - 0.08, 0.34),
-28,
17
);
placeLabel(
labelElements.Q,
new THREE.Vector3(Q.x, Q.y, 0.36),
32,
-18
);
placeLabel(
labelElements.P,
new THREE.Vector3(P.x, P.y + 0.20, 0.36),
28,
-14
);
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
const startButton = document.getElementById("start");
const resetButton = document.getElementById("reset");
const status = document.getElementById("status");
const speedInput = document.getElementById("speed");
const speedValue = document.getElementById("speed-value");
const guidesInput = document.getElementById("guides");
function updateStatus() {
status.classList.toggle("running", running);
status.classList.toggle("paused", !running && hasStarted);
if (running) {
status.textContent = "Running · kinematic model";
startButton.textContent = "Ⅱ Pause";
} else if (hasStarted) {
status.textContent = "Paused · construction visible";
startButton.textContent = "▶ Resume";
} else {
status.textContent = "Inspection view · motion locked";
startButton.textContent = "▶ Start animation";
}
}
startButton.addEventListener("click", () => {
running = !running;
hasStarted = true;
updateStatus();
});
resetButton.addEventListener("click", () => {
running = false;
hasStarted = false;
angle = THREE.MathUtils.degToRad(35);
updateMechanism(angle);
updateStatus();
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = speed.toFixed(2) + "×";
});
guidesInput.addEventListener("change", () => {
annotations.visible = guidesInput.checked;
if (!guidesInput.checked) {
Object.values(labelElements).forEach((label) => {
label.style.display = "none";
});
}
});
document.getElementById("camera-reset").addEventListener("click", () => {
cameraYaw = 0.56;
cameraPitch = 1.14;
cameraRadius = 8.7;
updateCamera();
});
// Direct camera controls; no external controls addon is used.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch - dy * 0.005,
0.32,
1.48
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.006,
5.0,
14.0
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraYaw = 0.56;
cameraPitch = 1.14;
cameraRadius = 8.7;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (running) {
angle = (angle + dt * speed) % (Math.PI * 2);
updateMechanism(angle);
}
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateStatus();
requestAnimationFrame(animate);
</script>
</body>
</html>
```