Prompt specification
Build or customize the Three.js 3D scene "Two-cord force ring" 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: Two-cord force ring
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-cord-force-ring-x1437
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring force vectors and their resultant. Build a ring pulled by two cords with visible force arrows and a separate resultant arrow; compute the resultant from components rather than adding arrow lengths directly. Low-poly.
Working Three.js Starter (two-cord-force-ring-x1437):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Two Cords — Force Resultant Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 23, 39, 0.84);
--border: rgba(148, 184, 211, 0.17);
--text: #eaf4ff;
--muted: #8fa8bd;
--cyan: #55d8ff;
--coral: #ff776d;
--gold: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #122b42 0%, #091727 45%, #050b14 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#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:
linear-gradient(180deg, rgba(2, 8, 15, 0.2), transparent 25%),
radial-gradient(circle, transparent 48%, rgba(2, 7, 13, 0.48) 100%);
}
.hero {
position: fixed;
top: 28px;
left: 32px;
z-index: 5;
width: min(430px, calc(100vw - 64px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(85, 216, 255, 0.65);
}
h1 {
margin: 10px 0 8px;
font-size: clamp(28px, 4vw, 48px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.hero p {
max-width: 390px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: fixed;
top: 28px;
right: 30px;
z-index: 5;
display: grid;
gap: 9px;
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 13px;
background: rgba(7, 17, 31, 0.65);
backdrop-filter: blur(12px);
pointer-events: none;
font-size: 12px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #bed0df;
}
.swatch {
width: 19px;
height: 4px;
border-radius: 10px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.swatch.cyan {
color: var(--cyan);
}
.swatch.coral {
color: var(--coral);
}
.swatch.gold {
color: var(--gold);
}
.hint {
position: fixed;
top: 142px;
right: 30px;
z-index: 5;
color: #7992a8;
font-size: 11px;
letter-spacing: 0.02em;
pointer-events: none;
}
.panel {
position: fixed;
z-index: 6;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
}
.controls {
left: 30px;
bottom: 28px;
width: 330px;
padding: 18px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
color: #a8bfd1;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
color: #70e5ad;
}
.live::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: #70e5ad;
box-shadow: 0 0 10px rgba(112, 229, 173, 0.9);
}
.control {
margin-bottom: 17px;
}
.control:last-of-type {
margin-bottom: 14px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font-size: 12px;
}
.control-name {
display: flex;
align-items: center;
gap: 8px;
color: #c8d8e5;
font-weight: 650;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.dot.cyan {
color: var(--cyan);
}
.dot.coral {
color: var(--coral);
}
output {
min-width: 58px;
padding: 4px 8px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
text-align: right;
color: #f3f8fc;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
outline: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 2px solid #07111f;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 1px currentColor, 0 0 14px currentColor;
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid #07111f;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 1px currentColor, 0 0 14px currentColor;
}
#tension1 {
color: var(--cyan);
accent-color: var(--cyan);
}
#tension2 {
color: var(--coral);
accent-color: var(--coral);
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 13px;
border-top: 1px solid var(--border);
}
.scale-note {
color: #7891a7;
font-size: 10px;
line-height: 1.4;
}
button {
padding: 8px 11px;
border: 1px solid rgba(255, 209, 102, 0.24);
border-radius: 9px;
background: rgba(255, 209, 102, 0.08);
color: var(--gold);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
background: rgba(255, 209, 102, 0.15);
}
.math-panel {
right: 30px;
bottom: 28px;
width: 390px;
padding: 18px;
font-variant-numeric: tabular-nums;
}
.equations {
display: grid;
gap: 8px;
color: #8fa8bd;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.35;
}
.equation {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 7px 9px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.028);
}
.equation b {
color: #e6f1fa;
font-weight: 600;
}
.result-main {
display: flex;
align-items: baseline;
gap: 7px;
margin-top: 14px;
padding: 13px 0 10px;
border-top: 1px solid var(--border);
}
.result-main span {
color: var(--gold);
font-size: 12px;
font-weight: 800;
}
.result-main strong {
color: #fff3c9;
font-size: 31px;
line-height: 1;
letter-spacing: -0.04em;
}
.result-main small {
color: var(--gold);
font-size: 12px;
font-weight: 800;
}
.direction {
color: #9eb5c7;
font-size: 11px;
}
.direction b {
color: var(--text);
}
.method-note {
margin-top: 12px;
color: #6f899f;
font-size: 10px;
line-height: 1.5;
}
.vector-label {
position: fixed;
top: 0;
left: 0;
z-index: 4;
display: flex;
align-items: center;
gap: 7px;
min-width: 88px;
padding: 7px 10px;
border: 1px solid currentColor;
border-radius: 10px;
background: rgba(5, 14, 25, 0.88);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
pointer-events: none;
will-change: transform;
}
.vector-label strong {
font-size: 12px;
letter-spacing: 0.01em;
}
.vector-label small {
display: block;
margin-top: 1px;
color: #b8c9d7;
font-size: 10px;
}
#label-t1 {
color: var(--cyan);
}
#label-t2 {
color: var(--coral);
}
#label-result {
color: var(--gold);
}
@media (max-width: 900px) {
.hero {
top: 20px;
left: 20px;
}
.hero p {
display: none;
}
.legend,
.hint {
display: none;
}
.controls {
left: 18px;
bottom: 18px;
width: 300px;
}
.math-panel {
right: 18px;
bottom: 18px;
width: 340px;
}
}
@media (max-width: 680px) {
h1 {
max-width: 270px;
font-size: 31px;
}
.controls {
right: 12px;
left: 12px;
bottom: 12px;
width: auto;
padding: 14px;
}
.control {
margin-bottom: 12px;
}
.math-panel {
top: 105px;
right: 12px;
bottom: auto;
width: min(330px, calc(100vw - 24px));
max-height: 43vh;
overflow: auto;
padding: 14px;
}
.equations {
font-size: 10px;
}
.vector-label {
transform-origin: center;
scale: 0.9;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly ring pulled by two cords"></canvas>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">Vector Mechanics Lab</div>
<h1>Two Cords.<br />One Resultant.</h1>
<p>
Adjust the cord tensions and inspect the resultant vector. The resultant
is rebuilt from summed x/y components—not by adding arrow lengths.
</p>
</header>
<aside class="legend" aria-label="Vector legend">
<div class="legend-row">
<span class="swatch cyan"></span>
<span>First cord tension</span>
</div>
<div class="legend-row">
<span class="swatch coral"></span>
<span>Second cord tension</span>
</div>
<div class="legend-row">
<span class="swatch gold"></span>
<span>Separate resultant</span>
</div>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<section class="panel controls" aria-label="Tension controls">
<div class="panel-title">
<span>Cord tensions</span>
<span class="live">Live</span>
</div>
<div class="control">
<div class="control-head">
<label class="control-name" for="tension1">
<span class="dot cyan"></span>
First cord, T₁
</label>
<output id="tension1-value" for="tension1">120 N</output>
</div>
<input
id="tension1"
type="range"
min="0"
max="240"
step="1"
value="120"
aria-label="First cord tension in newtons"
/>
</div>
<div class="control">
<div class="control-head">
<label class="control-name" for="tension2">
<span class="dot coral"></span>
Second cord, T₂
</label>
<output id="tension2-value" for="tension2">150 N</output>
</div>
<input
id="tension2"
type="range"
min="0"
max="240"
step="1"
value="150"
aria-label="Second cord tension in newtons"
/>
</div>
<div class="panel-footer">
<div class="scale-note">Arrow length ∝ vector magnitude</div>
<button id="reset-view" type="button">Reset view</button>
</div>
</section>
<section class="panel math-panel" aria-label="Resultant component calculation">
<div class="panel-title">
<span>Component solution</span>
<span class="live">Vector math</span>
</div>
<div class="equations">
<div class="equation">
<span>F₁ components</span>
<b id="force1-components">—</b>
</div>
<div class="equation">
<span>F₂ components</span>
<b id="force2-components">—</b>
</div>
<div class="equation">
<span>ΣFₓ = F₁ₓ + F₂ₓ</span>
<b id="sum-x">—</b>
</div>
<div class="equation">
<span>ΣFᵧ = F₁ᵧ + F₂ᵧ</span>
<b id="sum-y">—</b>
</div>
</div>
<div class="result-main" aria-live="polite">
<span>|R|</span>
<strong id="result-magnitude">—</strong>
<small>N</small>
</div>
<div class="direction">
Direction from +x:
<b id="result-angle">—</b>
·
R = (<b id="result-vector">—</b>) N
</div>
<div class="method-note">
The gold arrow length is calculated independently from
√(Rₓ² + Rᵧ²), where Rₓ and Rᵧ are the summed force components.
</div>
</section>
<div id="label-t1" class="vector-label">
<span class="dot cyan"></span>
<div><strong>T₁</strong><small>120 N</small></div>
</div>
<div id="label-t2" class="vector-label">
<span class="dot coral"></span>
<div><strong>T₂</strong><small>150 N</small></div>
</div>
<div id="label-result" class="vector-label">
<span class="dot gold"></span>
<div><strong>R</strong><small>Resultant</small></div>
</div>
<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.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x07111f, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111f, 19, 36);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(
0xa8ddff,
0x18202b,
1.65
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d5, 3.2);
keyLight.position.set(-6, 10, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ac7ff, 2.1);
rimLight.position.set(8, 4, -7);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xff9e5e, 18, 18, 2);
warmFill.position.set(4, -1, 5);
scene.add(warmFill);
// ------------------------------------------------------------
// Geometry and physical setup
// ------------------------------------------------------------
const ringCenter = new THREE.Vector3(0, -0.65, 0);
const attachmentA = new THREE.Vector3(-0.58, -0.07, 0);
const attachmentB = new THREE.Vector3(0.58, -0.07, 0);
const anchorA = new THREE.Vector3(-5.0, 4.75, -0.08);
const anchorB = new THREE.Vector3(5.15, 4.35, -0.08);
const directionA = anchorA.clone().sub(attachmentA).normalize();
const directionB = anchorB.clone().sub(attachmentB).normalize();
// All arrows use the same force-to-world scale. The resultant still
// gets its own magnitude, rather than being made from arrow lengths.
const FORCE_SCALE = 0.016;
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(38, 38),
new THREE.MeshStandardMaterial({
color: 0x0b1826,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -4.2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
30,
30,
0x1b4961,
0x102b3c
);
grid.position.y = -4.18;
grid.material.transparent = true;
grid.material.opacity = 0.38;
grid.material.depthWrite = false;
scene.add(grid);
// Faceted low-poly ring.
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.82, 0.145, 6, 16),
new THREE.MeshStandardMaterial({
color: 0xe5a83f,
roughness: 0.3,
metalness: 0.72,
flatShading: true
})
);
ring.position.copy(ringCenter);
ring.castShadow = true;
ring.receiveShadow = true;
scene.add(ring);
// Small facets on the inner rim add a hand-built, low-poly character.
const innerRim = new THREE.Mesh(
new THREE.TorusGeometry(0.655, 0.026, 4, 16),
new THREE.MeshStandardMaterial({
color: 0xffd98a,
roughness: 0.28,
metalness: 0.8,
flatShading: true
})
);
innerRim.position.copy(ringCenter);
innerRim.position.z = 0.035;
scene.add(innerRim);
const cordMaterial = new THREE.MeshStandardMaterial({
color: 0x80644e,
roughness: 0.92,
metalness: 0.02,
flatShading: true
});
const attachmentMaterial = new THREE.MeshStandardMaterial({
color: 0xb48a4c,
roughness: 0.55,
metalness: 0.48,
flatShading: true
});
const anchorMaterial = new THREE.MeshStandardMaterial({
color: 0x526a7b,
roughness: 0.48,
metalness: 0.62,
flatShading: true
});
function makeCylinderBetween(start, end, radius, material, segments = 6) {
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
makeCylinderBetween(
attachmentA,
anchorA,
0.038,
cordMaterial
);
makeCylinderBetween(
attachmentB,
anchorB,
0.038,
cordMaterial
);
function makeAttachment(point, color) {
const knot = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.17, 0),
new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.45,
flatShading: true
})
);
knot.position.copy(point);
knot.scale.set(0.85, 1.15, 0.85);
knot.castShadow = true;
scene.add(knot);
}
makeAttachment(attachmentA, 0x39b9dc);
makeAttachment(attachmentB, 0xdf5b55);
function makeAnchor(point) {
const fixture = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.24, 0),
anchorMaterial
);
fixture.position.copy(point);
fixture.castShadow = true;
scene.add(fixture);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.042, 4, 10),
new THREE.MeshStandardMaterial({
color: 0x7893a7,
roughness: 0.45,
metalness: 0.65,
flatShading: true
})
);
halo.position.copy(point);
halo.position.z += 0.01;
scene.add(halo);
}
makeAnchor(anchorA);
makeAnchor(anchorB);
// Gold marker at the point where the resultant is applied.
const resultOriginMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.105, 0),
new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x7a4f00,
emissiveIntensity: 0.45,
roughness: 0.3,
metalness: 0.65,
flatShading: true
})
);
resultOriginMarker.position.copy(ringCenter);
resultOriginMarker.position.z = 0.32;
scene.add(resultOriginMarker);
// ------------------------------------------------------------
// Low-poly arrow construction
// ------------------------------------------------------------
function createArrow(color, shaftRadius, headRadius, headLength) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.27,
roughness: 0.38,
metalness: 0.16,
flatShading: true
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
shaftRadius,
shaftRadius,
1,
6,
1,
false
),
material
);
const head = new THREE.Mesh(
new THREE.ConeGeometry(headRadius, 1, 6, 1, false),
material
);
shaft.castShadow = true;
head.castShadow = true;
group.add(shaft);
group.add(head);
scene.add(group);
return {
group,
shaft,
head,
headLength
};
}
const forceArrowA = createArrow(0x55d8ff, 0.047, 0.15, 0.32);
const forceArrowB = createArrow(0xff776d, 0.047, 0.15, 0.32);
const resultArrow = createArrow(0xffd166, 0.073, 0.205, 0.42);
const yAxis = new THREE.Vector3(0, 1, 0);
const arrowQuaternion = new THREE.Quaternion();
function updateArrow(arrow, origin, direction, length) {
arrow.group.visible = length > 0.035;
if (!arrow.group.visible) {
return;
}
const safeLength = Math.max(0.001, length);
const headLength = Math.min(
arrow.headLength,
safeLength * 0.42
);
const shaftLength = Math.max(0.001, safeLength - headLength);
arrow.group.position.copy(origin);
arrowQuaternion.setFromUnitVectors(yAxis, direction);
arrow.shaft.quaternion.copy(arrowQuaternion);
arrow.shaft.scale.set(1, shaftLength, 1);
arrow.shaft.position.copy(direction).multiplyScalar(
shaftLength * 0.5
);
arrow.head.quaternion.copy(arrowQuaternion);
arrow.head.scale.set(1, headLength, 1);
arrow.head.position.copy(direction).multiplyScalar(
shaftLength + headLength * 0.5
);
}
// ------------------------------------------------------------
// Component-based force calculation
// ------------------------------------------------------------
const tension1Input = document.getElementById("tension1");
const tension2Input = document.getElementById("tension2");
const tension1Value = document.getElementById("tension1-value");
const tension2Value = document.getElementById("tension2-value");
const force1Components = document.getElementById(
"force1-components"
);
const force2Components = document.getElementById(
"force2-components"
);
const sumXElement = document.getElementById("sum-x");
const sumYElement = document.getElementById("sum-y");
const resultMagnitude = document.getElementById(
"result-magnitude"
);
const resultAngle = document.getElementById("result-angle");
const resultVector = document.getElementById("result-vector");
const labelT1 = document.getElementById("label-t1");
const labelT2 = document.getElementById("label-t2");
const labelResult = document.getElementById("label-result");
const force1 = new THREE.Vector3();
const force2 = new THREE.Vector3();
const resultant = new THREE.Vector3();
const resultantDirection = new THREE.Vector3();
const labelPositionA = new THREE.Vector3();
const labelPositionB = new THREE.Vector3();
const labelPositionResult = new THREE.Vector3();
function signed(value) {
return `${value >= 0 ? "+" : "−"}${Math.abs(value).toFixed(1)}`;
}
function updatePhysics() {
const tension1 = Number(tension1Input.value);
const tension2 = Number(tension2Input.value);
tension1Value.value = `${tension1} N`;
tension2Value.value = `${tension2} N`;
// Each tension is resolved into world-space x/y components.
force1.copy(directionA).multiplyScalar(tension1);
force2.copy(directionB).multiplyScalar(tension2);
// The resultant is calculated by summing components first.
resultant.set(
force1.x + force2.x,
force1.y + force2.y,
force1.z + force2.z
);
const magnitude = resultant.length();
if (magnitude > 0.0001) {
resultantDirection.copy(resultant).normalize();
} else {
resultantDirection.set(0, 1, 0);
}
const angle =
(Math.atan2(resultant.y, resultant.x) * 180) / Math.PI;
const normalizedAngle = (angle + 360) % 360;
// Tension arrows originate at their physical cord attachments.
updateArrow(
forceArrowA,
attachmentA.clone().setZ(0.17),
directionA,
tension1 * FORCE_SCALE
);
updateArrow(
forceArrowB,
attachmentB.clone().setZ(0.17),
directionB,
tension2 * FORCE_SCALE
);
// The resultant is a separate vector applied at the ring center.
updateArrow(
resultArrow,
ringCenter.clone().setZ(0.32),
resultantDirection,
magnitude * FORCE_SCALE
);
force1Components.textContent =
`(${signed(force1.x)}, ${signed(force1.y)}) N`;
force2Components.textContent =
`(${signed(force2.x)}, ${signed(force2.y)}) N`;
sumXElement.textContent = `${signed(resultant.x)} N`;
sumYElement.textContent = `${signed(resultant.y)} N`;
resultMagnitude.textContent = magnitude.toFixed(1);
resultAngle.textContent = `${normalizedAngle.toFixed(1)}°`;
resultVector.textContent =
`${signed(resultant.x)}, ${signed(resultant.y)}`;
labelT1.querySelector("small").textContent =
`${tension1.toFixed(0)} N`;
labelT2.querySelector("small").textContent =
`${tension2.toFixed(0)} N`;
labelResult.querySelector("small").textContent =
`${magnitude.toFixed(1)} N`;
// Projected label anchors.
labelPositionA
.copy(attachmentA)
.addScaledVector(directionA, tension1 * FORCE_SCALE + 0.38);
labelPositionA.z = 0.2;
labelPositionB
.copy(attachmentB)
.addScaledVector(directionB, tension2 * FORCE_SCALE + 0.38);
labelPositionB.z = 0.2;
labelPositionResult
.copy(ringCenter)
.addScaledVector(
resultantDirection,
magnitude * FORCE_SCALE + 0.48
);
labelPositionResult.z = 0.34;
}
tension1Input.addEventListener("input", updatePhysics);
tension2Input.addEventListener("input", updatePhysics);
updatePhysics();
// ------------------------------------------------------------
// Direct camera controls — no external control addons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
const defaultView = {
theta: 0.18,
phi: 1.43,
radius: 15.7
};
const desiredView = { ...defaultView };
const currentView = { ...defaultView };
let dragging = false;
let previousX = 0;
let previousY = 0;
function resetView() {
desiredView.theta = defaultView.theta;
desiredView.phi = defaultView.phi;
desiredView.radius = defaultView.radius;
}
document
.getElementById("reset-view")
.addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) {
return;
}
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredView.theta -= deltaX * 0.006;
desiredView.phi = THREE.MathUtils.clamp(
desiredView.phi - deltaY * 0.006,
0.38,
Math.PI - 0.38
);
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredView.radius = THREE.MathUtils.clamp(
desiredView.radius * Math.exp(event.deltaY * 0.001),
8,
26
);
},
{ passive: false }
);
function updateCamera() {
currentView.theta = THREE.MathUtils.lerp(
currentView.theta,
desiredView.theta,
0.12
);
currentView.phi = THREE.MathUtils.lerp(
currentView.phi,
desiredView.phi,
0.12
);
currentView.radius = THREE.MathUtils.lerp(
currentView.radius,
desiredView.radius,
0.12
);
camera.position.setFromSphericalCoords(
currentView.radius,
currentView.phi,
currentView.theta
);
camera.position.add(cameraTarget);
camera.lookAt(cameraTarget);
}
// ------------------------------------------------------------
// DOM label projection
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight,
false
);
});
function animate() {
updateCamera();
positionLabel(labelT1, labelPositionA);
positionLabel(labelT2, labelPositionB);
positionLabel(labelResult, labelPositionResult);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```