Prompt specification
Build or customize the Three.js 3D scene "Straight and Curved Toy Free Kick" 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: Straight and Curved Toy Free Kick
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/straight-and-curved-toy-free-kick-x3658
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing ball spin and qualitative sideways deflection. Compare two ball trajectories under a clearly labeled qualitative lateral-force model. Show the added force direction and avoid claiming an accurate real soccer prediction. Stylized solid forms.
Working Three.js Starter (straight-and-curved-toy-free-kick-x3658):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Qualitative Spin & Side-Force Comparison</title>
<style>
:root {
--bg: #09101e;
--panel: rgba(15, 24, 42, 0.78);
--panel-strong: rgba(11, 19, 34, 0.92);
--border: rgba(171, 205, 255, 0.16);
--text: #eef5ff;
--muted: #9caec5;
--cyan: #58d8ff;
--amber: #ffb454;
--orange: #ff7f50;
--spin: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 48%,
transparent 36%,
rgba(2, 7, 17, 0.26) 75%,
rgba(2, 6, 15, 0.68) 100%
);
}
.topbar {
position: fixed;
z-index: 10;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.intro {
width: min(530px, 48vw);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 12px;
color: #b8c9df;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 15px rgba(255, 127, 80, 0.75);
}
h1 {
margin: 0;
font-size: clamp(32px, 4.2vw, 58px);
line-height: 0.99;
letter-spacing: -0.045em;
font-weight: 780;
text-shadow: 0 8px 35px rgba(0, 0, 0, 0.3);
}
h1 em {
color: var(--amber);
font-style: normal;
}
.intro-copy {
max-width: 510px;
margin: 15px 0 18px;
color: #b8c8dc;
font-size: 14px;
line-height: 1.55;
}
.comparison {
display: flex;
flex-wrap: wrap;
gap: 9px;
}
.compare-item {
display: flex;
align-items: center;
gap: 10px;
min-width: 184px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 13px;
background: rgba(12, 21, 37, 0.68);
backdrop-filter: blur(12px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
}
.swatch {
width: 27px;
height: 7px;
flex: 0 0 auto;
border-radius: 999px;
box-shadow: 0 0 13px currentColor;
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.swatch.amber {
color: var(--amber);
background: var(--amber);
}
.compare-item b,
.compare-item small {
display: block;
}
.compare-item b {
font-size: 12px;
letter-spacing: 0.01em;
}
.compare-item small {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.model-card {
width: min(340px, 34vw);
padding: 17px;
border: 1px solid rgba(255, 180, 84, 0.22);
border-radius: 17px;
background:
linear-gradient(
145deg,
rgba(31, 32, 43, 0.88),
rgba(11, 20, 36, 0.84)
);
backdrop-filter: blur(16px);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
}
.model-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.model-heading span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
}
.model-heading strong {
padding: 5px 8px;
border: 1px solid rgba(255, 180, 84, 0.3);
border-radius: 999px;
color: var(--amber);
background: rgba(255, 180, 84, 0.08);
font-size: 9px;
letter-spacing: 0.1em;
}
.model-card p {
margin: 12px 0 0;
color: #c4d2e3;
font-size: 12px;
line-height: 1.5;
}
.force-key {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
padding: 9px 10px;
border-radius: 10px;
color: #ffd8c6;
background: rgba(255, 127, 80, 0.09);
border: 1px solid rgba(255, 127, 80, 0.18);
font-size: 11px;
font-weight: 700;
}
.force-key .arrow {
color: var(--orange);
font-size: 20px;
line-height: 1;
}
.warning {
padding-top: 11px;
border-top: 1px solid var(--border);
color: #91a2b8 !important;
font-size: 10px !important;
}
.world-label {
position: fixed;
z-index: 7;
pointer-events: none;
will-change: left, top, opacity;
}
.lane-tag {
display: grid;
width: 30px;
height: 30px;
place-items: center;
border: 1px solid rgba(88, 216, 255, 0.42);
border-radius: 9px;
color: var(--cyan);
background: rgba(7, 17, 31, 0.88);
box-shadow: 0 0 18px rgba(88, 216, 255, 0.13);
font-size: 12px;
font-weight: 850;
transform: translate(-50%, -50%);
}
.lane-tag.alt {
color: var(--amber);
border-color: rgba(255, 180, 84, 0.45);
box-shadow: 0 0 18px rgba(255, 180, 84, 0.14);
}
.force-caption {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border: 1px solid rgba(255, 127, 80, 0.42);
border-radius: 10px;
color: #fff3ea;
background: rgba(30, 18, 19, 0.88);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -115%);
white-space: nowrap;
}
.force-caption .mini-arrow {
color: var(--orange);
font-size: 19px;
line-height: 1;
}
.force-caption b,
.force-caption small {
display: block;
}
.force-caption b {
font-size: 9px;
letter-spacing: 0.08em;
}
.force-caption small {
margin-top: 2px;
color: #c7a89b;
font-size: 8px;
}
.controls {
position: fixed;
z-index: 12;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
width: min(920px, calc(100vw - 36px));
padding: 11px 13px;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel-strong);
backdrop-filter: blur(18px);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.34);
transform: translateX(-50%);
}
.control-button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(184, 211, 244, 0.16);
border-radius: 10px;
color: var(--text);
background: #17243a;
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.control-button:hover {
background: #22334d;
border-color: rgba(184, 211, 244, 0.28);
transform: translateY(-1px);
}
.control-button.primary {
min-width: 94px;
color: #07121e;
background: var(--cyan);
border-color: transparent;
}
.control-button.primary:hover {
background: #83e4ff;
}
.divider {
width: 1px;
height: 30px;
flex: 0 0 auto;
background: var(--border);
}
.slider-block {
display: grid;
grid-template-columns: 1fr auto;
gap: 4px 12px;
min-width: 260px;
flex: 1 1 320px;
}
.slider-block label {
color: #d7e3f2;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.025em;
}
.slider-block output {
color: var(--amber);
font-size: 10px;
font-weight: 800;
}
.slider-block input {
grid-column: 1 / -1;
width: 100%;
height: 5px;
margin: 5px 0 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
var(--amber) 0%,
var(--amber) var(--slider-fill, 72%),
#25344c var(--slider-fill, 72%),
#25344c 100%
);
cursor: pointer;
}
.slider-block input::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid #0d1728;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 0 1px var(--amber);
}
.slider-block input::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #0d1728;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 0 1px var(--amber);
}
.camera-hint {
position: fixed;
z-index: 8;
right: 27px;
bottom: 100px;
color: rgba(202, 218, 237, 0.58);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.lane-note {
position: fixed;
z-index: 8;
left: 28px;
bottom: 99px;
max-width: 350px;
color: rgba(190, 205, 224, 0.55);
font-size: 9px;
line-height: 1.45;
pointer-events: none;
}
@media (max-width: 780px) {
.topbar {
top: 15px;
left: 15px;
right: 15px;
display: block;
}
.intro {
width: min(100%, 470px);
}
h1 {
font-size: clamp(28px, 8vw, 43px);
}
.intro-copy {
display: none;
}
.eyebrow {
margin-bottom: 8px;
font-size: 9px;
}
.comparison {
margin-top: 12px;
}
.compare-item {
min-width: 0;
padding: 8px 10px;
}
.model-card {
width: min(330px, calc(100vw - 30px));
margin-top: 11px;
padding: 13px;
}
.model-card p {
font-size: 10px;
}
.warning {
font-size: 9px !important;
}
.controls {
bottom: 12px;
flex-wrap: wrap;
gap: 8px;
width: calc(100vw - 24px);
padding: 10px;
}
.divider {
display: none;
}
.slider-block {
order: 4;
flex-basis: 100%;
min-width: 0;
}
.camera-hint,
.lane-note {
display: none;
}
.force-caption {
transform: translate(-50%, -110%) scale(0.84);
transform-origin: bottom center;
}
}
</style>
</head>
<body>
<canvas
id="scene"
role="img"
aria-label="Three-dimensional comparison of a straight qualitative flight path and a spinning ball with an exaggerated sideways force."
></canvas>
<div class="vignette"></div>
<header class="topbar">
<section class="intro">
<div class="eyebrow">
<span class="eyebrow-dot"></span>
Interactive flight-path explainer
</div>
<h1>
Spin bends the path.<br />
<em>Qualitatively speaking.</em>
</h1>
<p class="intro-copy">
Compare a reference arc with a stylized spin-linked lateral bias.
The second path and force arrow are deliberately exaggerated to make
direction easy to see.
</p>
<div class="comparison">
<div class="compare-item">
<span class="swatch cyan"></span>
<span>
<b>A · No side bias</b>
<small>Reference flight arc</small>
</span>
</div>
<div class="compare-item">
<span class="swatch amber"></span>
<span>
<b>B · Spin-linked side bias</b>
<small>Exaggerated qualitative bend</small>
</span>
</div>
</div>
</section>
<aside class="model-card">
<div class="model-heading">
<span>MODEL</span>
<strong>QUALITATIVE ONLY</strong>
</div>
<p>
Both paths use the same illustrative flight arc. Path B adds one
smooth sideways bias in the orange arrow’s direction.
</p>
<div class="force-key">
<span class="arrow">➜</span>
<span>Orange arrow = added force direction</span>
</div>
<p class="warning">
This is not a calibrated Magnus model. It omits drag, speed, spin-axis
details, Reynolds number, and real soccer-ball aerodynamics. It must
not be used to predict an actual kick, match, or trajectory.
</p>
</aside>
</header>
<div id="laneA" class="world-label lane-tag">A</div>
<div id="laneB" class="world-label lane-tag alt">B</div>
<div id="forcePin" class="world-label force-caption">
<span class="mini-arrow">➜</span>
<span>
<b>ADDED LATERAL FORCE</b>
<small>direction shown by the 3D arrow</small>
</span>
</div>
<div class="lane-note">
Paths are separated sideways for readability. Their launch shape and
forward motion are conceptually matched.
</div>
<div class="camera-hint">
Drag to orbit · scroll to zoom · double-click to reset
</div>
<section class="controls" aria-label="Animation controls">
<button id="playButton" class="control-button primary" type="button">
❚❚ Pause
</button>
<button id="replayButton" class="control-button" type="button">
↻ Replay
</button>
<div class="divider"></div>
<div class="slider-block">
<label for="forceSlider">Spin-linked side-bias amount</label>
<output id="forceValue" for="forceSlider">72% visual</output>
<input
id="forceSlider"
type="range"
min="0"
max="100"
value="72"
aria-label="Illustrative spin-linked lateral force amount"
/>
</div>
<div class="divider"></div>
<button id="cameraButton" class="control-button" type="button">
Reset view
</button>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09101e);
scene.fog = new THREE.Fog(0x09101e, 20, 42);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 1.0, 1.0);
const defaultCamera = {
radius: 16.6,
theta: 0.50,
phi: 1.12
};
const cameraState = { ...defaultCamera };
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
Object.assign(cameraState, defaultCamera);
updateCamera();
}
updateCamera();
scene.add(
new THREE.HemisphereLight(0xc9e4ff, 0x111827, 2.15)
);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(-5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x66aaff, 1.1);
rimLight.position.set(8, 5, -8);
scene.add(rimLight);
// ------------------------------------------------------------
// Stylized solid environment
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x070d18,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.27;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(21, 0.22, 14),
new THREE.MeshStandardMaterial({
color: 0x101a2b,
roughness: 0.9,
metalness: 0.05
})
);
platform.position.set(0, -0.14, 1.2);
platform.receiveShadow = true;
scene.add(platform);
const gridMaterial = new THREE.MeshStandardMaterial({
color: 0x1b2a41,
roughness: 0.95
});
for (let x = -8; x <= 8; x += 2) {
const rib = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.025, 12.8),
gridMaterial
);
rib.position.set(x, -0.006, 1.2);
scene.add(rib);
}
for (const z of [-4.6, -1.6, 1.4, 4.4, 7.0]) {
const rib = new THREE.Mesh(
new THREE.BoxGeometry(19.8, 0.025, 0.035),
gridMaterial
);
rib.position.set(0, -0.006, z);
scene.add(rib);
}
const CYAN = 0x58d8ff;
const AMBER = 0xffb454;
const ORANGE = 0xff7f50;
const SPIN = 0xffd166;
function addLane(laneZ, color) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(14.4, 0.025, 1.08),
new THREE.MeshStandardMaterial({
color,
transparent: true,
opacity: 0.065,
roughness: 1
})
);
strip.position.set(0, 0.012, laneZ);
scene.add(strip);
const startPad = new THREE.Mesh(
new THREE.CylinderGeometry(0.58, 0.68, 0.08, 32),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.7
})
);
startPad.position.set(-6.25, 0.025, laneZ);
startPad.receiveShadow = true;
scene.add(startPad);
const endPad = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.38, 0.06, 24),
new THREE.MeshStandardMaterial({
color: 0x263850,
roughness: 0.85
})
);
endPad.position.set(6.25, 0.018, laneZ);
endPad.receiveShadow = true;
scene.add(endPad);
}
addLane(-1.85, CYAN);
addLane(1.85, AMBER);
// ------------------------------------------------------------
// Qualitative trajectory curves
//
// The model intentionally uses simple stylized arcs. The second
// path adds a smooth lateral displacement to represent a bias
// in one chosen direction. It is not a physical soccer prediction.
// ------------------------------------------------------------
function makeTrajectory(laneZ, lateralOffset = 0) {
const points = [];
for (let i = 0; i <= 70; i++) {
const t = i / 70;
const x = -6.2 + 12.4 * t;
const y =
0.56 +
1.62 * Math.sin(Math.PI * t) -
0.13 * t;
const z = laneZ + lateralOffset * t * t;
points.push(new THREE.Vector3(x, y, z));
}
return new THREE.CatmullRomCurve3(
points,
false,
"centripetal"
);
}
const curveA = makeTrajectory(-1.85, 0);
let curveB = makeTrajectory(1.85, 1.9);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function disposePathGroup(group) {
group.traverse((object) => {
if (object.geometry) {
object.geometry.dispose();
}
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
}
});
}
function buildPath(curve, color) {
const group = new THREE.Group();
// A slightly lowered, darker tube acts as a stylized solid shadow.
const shadowPoints = curve
.getPoints(70)
.map((point) => point.clone().add(new THREE.Vector3(0, -0.05, 0)));
const shadowCurve = new THREE.CatmullRomCurve3(
shadowPoints,
false,
"centripetal"
);
const shadowTube = new THREE.Mesh(
new THREE.TubeGeometry(shadowCurve, 90, 0.078, 8, false),
new THREE.MeshStandardMaterial({
color: 0x030914,
roughness: 0.95
})
);
group.add(shadowTube);
const mainTube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 90, 0.043, 10, false),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.23,
roughness: 0.55
})
);
group.add(mainTube);
const end = curve.getPointAt(1);
const tangent = curve.getTangentAt(1).normalize();
const directionHead = new THREE.Mesh(
new THREE.ConeGeometry(0.15, 0.36, 16),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.18,
roughness: 0.55
})
);
directionHead.position.copy(end);
directionHead.quaternion.setFromUnitVectors(Y_AXIS, tangent);
group.add(directionHead);
scene.add(group);
return group;
}
let pathGroupA = buildPath(curveA, CYAN);
let pathGroupB = buildPath(curveB, AMBER);
// ------------------------------------------------------------
// Stylized soccer balls assembled from solid geometry
// ------------------------------------------------------------
const patchGeometry = (() => {
const shape = new THREE.Shape();
const radius = 0.115;
for (let i = 0; i < 5; i++) {
const angle = -Math.PI / 2 + (i / 5) * Math.PI * 2;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) {
shape.moveTo(x, y);
} else {
shape.lineTo(x, y);
}
}
shape.closePath();
return new THREE.ShapeGeometry(shape);
})();
const patchMaterial = new THREE.MeshStandardMaterial({
color: 0x111722,
roughness: 0.8,
metalness: 0.02
});
const patchNormals = [
[0, 1, 0],
[0, -1, 0],
[1, 0, 0],
[-1, 0, 0],
[0, 0, 1],
[0, 0, -1],
[1, 1, 1],
[-1, 1, 1],
[1, -1, 1],
[-1, -1, 1],
[1, 1, -1],
[-1, 1, -1],
[1, -1, -1],
[-1, -1, -1]
].map((values) => new THREE.Vector3(...values).normalize());
function createBall(accentColor) {
const root = new THREE.Group();
const shell = new THREE.Mesh(
new THREE.SphereGeometry(0.42, 28, 18),
new THREE.MeshStandardMaterial({
color: 0xf5f1e8,
roughness: 0.7,
metalness: 0.01
})
);
shell.castShadow = true;
shell.receiveShadow = true;
root.add(shell);
for (const normal of patchNormals) {
const patch = new THREE.Mesh(patchGeometry, patchMaterial);
patch.position.copy(normal).multiplyScalar(0.436);
patch.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
normal
);
root.add(patch);
}
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.424, 0.022, 8, 48),
new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.2,
roughness: 0.5
})
);
band.rotation.x = Math.PI / 2;
root.add(band);
return root;
}
function createSpinRing() {
const group = new THREE.Group();
const radius = 0.68;
const points = [];
const startAngle = 0.38;
const sweep = Math.PI * 1.7;
for (let i = 0; i <= 40; i++) {
const angle = startAngle + (i / 40) * sweep;
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
)
);
}
const ringCurve = new THREE.CatmullRomCurve3(points);
const material = new THREE.MeshBasicMaterial({
color: SPIN,
transparent: true,
opacity: 0.9
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(ringCurve, 64, 0.023, 8, false),
material
);
group.add(tube);
const tip = points[points.length - 1];
const tangent = tip.clone().sub(points[points.length - 2]).normalize();
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.23, 12),
material
);
arrowHead.position.copy(tip);
arrowHead.quaternion.setFromUnitVectors(Y_AXIS, tangent);
group.add(arrowHead);
return group;
}
const ballA = createBall(CYAN);
const ballB = createBall(AMBER);
const spinRing = createSpinRing();
ballB.add(spinRing);
scene.add(ballA, ballB);
function createBlobShadow() {
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(0.4, 32),
new THREE.MeshBasicMaterial({
color: 0x02050b,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
shadow.rotation.x = -Math.PI / 2;
scene.add(shadow);
return shadow;
}
const blobA = createBlobShadow();
const blobB = createBlobShadow();
// ------------------------------------------------------------
// Solid added-force arrow
// ------------------------------------------------------------
const forceArrow = new THREE.Group();
const forceMaterial = new THREE.MeshBasicMaterial({
color: ORANGE,
depthTest: false,
transparent: true,
opacity: 0.98
});
const forceShaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 1, 12),
forceMaterial
);
forceShaft.rotation.x = Math.PI / 2;
forceShaft.renderOrder = 20;
forceArrow.add(forceShaft);
const forceHead = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.3, 16),
forceMaterial
);
forceHead.rotation.x = Math.PI / 2;
forceHead.renderOrder = 20;
forceArrow.add(forceHead);
scene.add(forceArrow);
let forceLength = 2.1;
function updateForceArrow() {
forceShaft.scale.y = forceLength;
forceShaft.position.z = forceLength * 0.5;
forceHead.position.z = forceLength + 0.15;
forceArrow.visible = sideStrength > 0.005;
spinRing.visible = sideStrength > 0.005;
}
// ------------------------------------------------------------
// DOM labels projected into the 3D scene
// ------------------------------------------------------------
const laneLabelA = document.querySelector("#laneA");
const laneLabelB = document.querySelector("#laneB");
const forcePin = document.querySelector("#forcePin");
function placeWorldLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.opacity = visible ? "1" : "0";
}
const laneAnchorA = new THREE.Vector3(-6.25, 0.2, -1.85);
const laneAnchorB = new THREE.Vector3(-6.25, 0.2, 1.85);
const forceTip = new THREE.Vector3();
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
const forceSlider = document.querySelector("#forceSlider");
const forceValue = document.querySelector("#forceValue");
const playButton = document.querySelector("#playButton");
const replayButton = document.querySelector("#replayButton");
const cameraButton = document.querySelector("#cameraButton");
let sideStrength = Number(forceSlider.value) / 100;
let playing = true;
let timeline = 0;
let spinPhase = 0;
const duration = 6.8;
function updateSliderUI() {
const value = Number(forceSlider.value);
forceValue.textContent = `${value}% visual`;
forceSlider.style.setProperty(
"--slider-fill",
`${value}%`
);
sideStrength = value / 100;
scene.remove(pathGroupB);
disposePathGroup(pathGroupB);
curveB = makeTrajectory(
1.85,
2.7 * sideStrength
);
pathGroupB = buildPath(curveB, AMBER);
forceLength = 0.85 + 1.55 * sideStrength;
updateForceArrow();
}
forceSlider.addEventListener("input", updateSliderUI);
updateSliderUI();
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "❚❚ Pause" : "▶ Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
}
playButton.addEventListener("click", () => {
if (timeline >= duration) {
timeline = 0;
setPlaying(true);
} else {
setPlaying(!playing);
}
});
replayButton.addEventListener("click", () => {
timeline = 0;
spinPhase = 0;
setPlaying(true);
});
cameraButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) {
return;
}
if (event.code === "Space") {
event.preventDefault();
if (timeline >= duration) {
timeline = 0;
setPlaying(true);
} else {
setPlaying(!playing);
}
}
if (event.key.toLowerCase() === "r") {
timeline = 0;
spinPhase = 0;
setPlaying(true);
}
});
// ------------------------------------------------------------
// Direct camera controls: no addons or OrbitControls
// ------------------------------------------------------------
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.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.42,
1.42
);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (
event &&
typeof event.pointerId === "number" &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
9.5,
27
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = width < 760 ? 51 : 40;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", onResize);
onResize();
let lastTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (playing && timeline < duration) {
timeline = Math.min(duration, timeline + delta);
spinPhase +=
delta *
(2.2 + sideStrength * 5.4) *
Math.PI;
}
const progress = THREE.MathUtils.clamp(
timeline / duration,
0,
1
);
const positionA = curveA.getPointAt(progress);
const positionB = curveB.getPointAt(progress);
ballA.position.copy(positionA);
ballB.position.copy(positionB);
// The patch pattern and spin ring make the spin legible.
ballB.rotation.y = spinPhase;
forceArrow.position.copy(positionB);
forceArrow.position.y += 0.015;
updateForceArrow();
blobA.position.set(positionA.x, 0.034, positionA.z);
blobB.position.set(positionB.x, 0.034, positionB.z);
const shadowScale =
0.82 + Math.max(0, positionA.y - 0.42) * 0.22;
blobA.scale.setScalar(shadowScale);
blobB.scale.setScalar(shadowScale);
forceTip
.copy(positionB)
.add(new THREE.Vector3(0, 0.015, forceLength + 0.32));
placeWorldLabel(laneLabelA, laneAnchorA);
placeWorldLabel(laneLabelB, laneAnchorB);
placeWorldLabel(forcePin, forceTip);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```