Prompt specification
Build or customize the Three.js 3D scene "Hinged frame resisting sideways motion" 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: Hinged frame resisting sideways motion
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/hinged-frame-resisting-sideways-motion-x2226
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows diagonal bracing in timber frames. Animate a hinged rectangular frame shearing sideways beside a triangulated version. Keep member lengths fixed and use an explicit idealized joint model for the comparison. Voxel style.
Working Three.js Starter (hinged-frame-resisting-sideways-motion-x2226):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Timber Frame Shear vs. Diagonal Brace</title>
<style>
:root {
--bg: #111923;
--panel: rgba(20, 29, 40, 0.92);
--panel-border: rgba(176, 205, 220, 0.22);
--text: #edf5f4;
--muted: #9cb0b8;
--cyan: #63dce8;
--amber: #ffc55c;
--green: #80d6a2;
--orange: #ee9a55;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
body {
user-select: none;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
z-index: 10;
top: 24px;
left: 28px;
max-width: 620px;
pointer-events: none;
text-shadow: 0 2px 12px rgba(0, 0, 0, 0.55);
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.7vw, 38px);
line-height: 1.05;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
color: #c2d1d5;
font-size: 12px;
line-height: 1.5;
}
.controls {
position: fixed;
z-index: 20;
top: 24px;
right: 28px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: 330px;
}
button {
border: 1px solid var(--panel-border);
border-radius: 0;
padding: 9px 12px;
background: rgba(20, 29, 40, 0.92);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.05em;
cursor: pointer;
}
button:hover,
button:focus-visible {
border-color: var(--cyan);
color: var(--cyan);
outline: none;
}
button.primary {
min-width: 82px;
border-color: rgba(99, 220, 232, 0.55);
color: var(--cyan);
}
.camera-hint {
flex-basis: 100%;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
text-align: right;
}
.world-label {
position: fixed;
z-index: 8;
top: 0;
left: 0;
min-width: 150px;
padding: 8px 10px;
border: 1px solid var(--panel-border);
background: rgba(15, 22, 31, 0.84);
box-shadow: 0 7px 24px rgba(0, 0, 0, 0.22);
pointer-events: none;
transform: translate(-50%, -100%);
will-change: left, top;
}
.world-label .index {
margin-right: 7px;
color: var(--cyan);
font-size: 10px;
font-weight: 900;
}
.world-label.braced .index {
color: var(--amber);
}
.world-label strong {
font-size: 11px;
letter-spacing: 0.08em;
}
.world-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.comparison {
position: fixed;
z-index: 15;
left: 50%;
bottom: 76px;
display: grid;
grid-template-columns: 1fr 1fr 1.45fr;
width: min(1080px, calc(100% - 40px));
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.3);
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
.card {
min-width: 0;
padding: 15px 17px 14px;
border-right: 1px solid var(--panel-border);
}
.card:last-child {
border-right: 0;
}
.tag {
display: block;
margin-bottom: 9px;
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
}
.tag.braced {
color: var(--amber);
}
.value {
display: block;
font-size: 29px;
font-weight: 900;
line-height: 1;
letter-spacing: -0.05em;
}
.value small {
color: var(--muted);
font-size: 12px;
letter-spacing: 0;
}
.metric-label {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.meter {
position: relative;
height: 5px;
margin: 11px 0 9px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
}
.meter i {
display: block;
width: 0;
height: 100%;
background: var(--orange);
}
.meter.locked i {
width: 100%;
background: var(--green);
}
.mini-metrics {
display: flex;
justify-content: space-between;
gap: 10px;
color: var(--muted);
font-size: 9px;
}
.mini-metrics b {
color: var(--text);
font-weight: 800;
}
.model-note {
color: #b9c9ce;
font-size: 10px;
line-height: 1.55;
}
.model-note strong {
display: block;
margin-bottom: 6px;
color: var(--text);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-note .key {
color: var(--cyan);
}
.timeline {
position: fixed;
z-index: 20;
left: 50%;
bottom: 23px;
display: grid;
grid-template-columns: 92px minmax(180px, 560px) 92px;
align-items: center;
gap: 12px;
width: min(780px, calc(100% - 40px));
transform: translateX(-50%);
}
.timeline span {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.timeline span:last-child {
text-align: right;
}
input[type="range"] {
--p: 0%;
width: 100%;
height: 5px;
margin: 0;
appearance: none;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--p),
rgba(255, 255, 255, 0.13) var(--p),
rgba(255, 255, 255, 0.13) 100%
);
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
appearance: none;
border: 2px solid #14202b;
border-radius: 0;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #14202b;
border-radius: 0;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #111923;
color: var(--cyan);
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 780px) {
.header {
top: 14px;
left: 14px;
max-width: calc(100% - 150px);
}
.eyebrow {
font-size: 8px;
}
h1 {
font-size: 20px;
}
.subtitle {
display: none;
}
.controls {
top: 14px;
right: 14px;
max-width: 125px;
}
button {
padding: 7px 8px;
font-size: 9px;
}
.camera-hint {
font-size: 8px;
}
.comparison {
bottom: 68px;
grid-template-columns: 1fr 1fr 1.2fr;
width: calc(100% - 20px);
}
.card {
padding: 10px 8px;
}
.value {
font-size: 21px;
}
.metric-label,
.mini-metrics,
.model-note {
font-size: 7px;
}
.model-note {
line-height: 1.45;
}
.timeline {
bottom: 20px;
grid-template-columns: 55px 1fr 55px;
width: calc(100% - 28px);
gap: 7px;
}
.world-label {
min-width: 116px;
padding: 6px 7px;
}
.world-label strong {
font-size: 8px;
}
.world-label small {
font-size: 7px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="header">
<div class="eyebrow">Structural kinematics / voxel study</div>
<h1>Timber frame:<br>shear vs. diagonal brace</h1>
<p class="subtitle">
Rigid timber bars · ideal frictionless pin joints · invariant member lengths
</p>
</header>
<div class="controls">
<button id="playButton" class="primary" type="button">Pause</button>
<button id="resetButton" type="button">Reset cycle</button>
<button id="cameraButton" type="button">Reset view</button>
<div class="camera-hint">Drag to orbit · wheel to zoom</div>
</div>
<div id="labelLeft" class="world-label">
<span class="index">01</span><strong>UNBRACED</strong>
<small>4-bar pin mechanism</small>
</div>
<div id="labelRight" class="world-label braced">
<span class="index">02</span><strong>DIAGONAL BRACE</strong>
<small>triangulated / locked</small>
</div>
<section class="comparison" aria-label="Frame comparison">
<article class="card">
<span class="tag">01 / UNBRACED</span>
<strong class="value"><span id="offsetValue">0</span> <small>cm</small></strong>
<span class="metric-label">top displacement</span>
<div class="meter"><i id="offsetMeter"></i></div>
<div class="mini-metrics">
<span>side angle <b id="angleValue">0.0°</b></span>
<span>shear DOF <b>1</b></span>
</div>
</article>
<article class="card">
<span class="tag braced">02 / BRACED</span>
<strong class="value">0 <small>cm</small></strong>
<span class="metric-label">top displacement</span>
<div class="meter locked"><i></i></div>
<div class="mini-metrics">
<span>side angle <b>0.0°</b></span>
<span>shear DOF <b>0</b></span>
</div>
</article>
<article class="card model-note">
<strong>Explicit idealized joint model</strong>
Every timber is a rigid, axially inextensible two-force bar. Every
connection is an ideal revolute pin with no moment transfer; the lower
corners are pinned to the datum.
<br><br>
Left: four fixed-length bars form a four-bar linkage with one lateral
mechanism. Right: a fixed-length diagonal joins A–C, forms rigid
triangles, and removes that mechanism.
<br><br>
<span class="key">2.40 m rails · 3.00 m posts · 3.84 m diagonal.</span>
This is a geometric constraint model, not a stress or deformation analysis.
</article>
</section>
<div class="timeline">
<span>Shear cycle</span>
<input
id="timeline"
type="range"
min="0"
max="100"
step="0.1"
value="0"
aria-label="Shear animation phase"
>
<span>+X direction</span>
</div>
<div id="loading">Loading voxel frame model…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 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.PCFShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive comparison of an unbraced and diagonally braced timber frame"
);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111923);
scene.fog = new THREE.Fog(0x111923, 16, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
yaw: 0.08,
pitch: 0.18,
radius: 11.8,
target: new THREE.Vector3(0, 1.65, 0)
};
function updateCamera() {
const horizontalRadius = orbit.radius * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontalRadius,
orbit.target.y + Math.sin(orbit.pitch) * orbit.radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontalRadius
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8ecff, 0x30231e, 1.65));
const keyLight = new THREE.DirectionalLight(0xfff0d5, 3.1);
keyLight.position.set(5.5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x74c7e8, 0.85);
fillLight.position.set(-6, 4, 4);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Shared voxel materials and helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color, roughness = 0.88, metalness = 0) {
const key = `${color}-${roughness}-${metalness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
})
);
}
return materialCache.get(key);
}
function makeBox(width, height, depth, color, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color)
);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
return mesh;
}
// A timber is represented by a row of square-edged blocks. The blocks are
// grouped into one rigid member; their dimensions never change.
function makeTimber(length, colors, thickness = 0.24, depth = 0.40) {
const group = new THREE.Group();
const blockCount = Math.max(5, Math.ceil(length / 0.26));
const blockLength = length / blockCount;
const geometry = new THREE.BoxGeometry(blockLength, thickness, depth);
for (let i = 0; i < blockCount; i++) {
const block = new THREE.Mesh(
geometry,
material(colors[i % colors.length])
);
block.position.x = -length / 2 + blockLength * (i + 0.5);
block.castShadow = true;
block.receiveShadow = true;
group.add(block);
}
group.userData.length = length;
return group;
}
function updateTimber(timber, a, b, z = 0) {
timber.position.set(
(a.x + b.x) * 0.5,
(a.y + b.y) * 0.5,
z
);
timber.rotation.z = Math.atan2(b.y - a.y, b.x - a.x);
}
// -------------------------------------------------------------------------
// Ideal pin-joint graphic
// -------------------------------------------------------------------------
const jointBlockGeometry = new THREE.BoxGeometry(0.30, 0.30, 0.36);
const jointPinGeometry = new THREE.CylinderGeometry(
0.095, 0.095, 0.50, 8
);
const jointCapGeometry = new THREE.CylinderGeometry(
0.052, 0.052, 0.055, 8
);
function makeJoint() {
const group = new THREE.Group();
const block = new THREE.Mesh(
jointBlockGeometry,
material(0x29333c, 0.72, 0.08)
);
block.castShadow = true;
group.add(block);
const pin = new THREE.Mesh(
jointPinGeometry,
material(0xb9c7cc, 0.42, 0.55)
);
pin.rotation.x = Math.PI / 2;
pin.position.z = 0.25;
pin.castShadow = true;
group.add(pin);
const cap = new THREE.Mesh(
jointCapGeometry,
material(0xe8f2f1, 0.36, 0.45)
);
cap.rotation.x = Math.PI / 2;
cap.position.z = 0.515;
cap.castShadow = true;
group.add(cap);
return group;
}
function moveJoint(joint, point) {
joint.position.set(point.x, point.y, 0);
}
// -------------------------------------------------------------------------
// Floor, platforms, and voxel supports
// -------------------------------------------------------------------------
const ground = makeBox(30, 0.30, 14, 0x19232d, false);
ground.position.set(0, -0.53, 0);
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(18, 18, 0x49606a, 0x34444e);
grid.position.y = -0.365;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const LEFT_CENTER = -3.0;
const RIGHT_CENTER = 3.0;
const BASE_Y = 0.26;
const WIDTH = 2.4;
const HEIGHT = 3.0;
const MAX_SHEAR = 0.82;
const DIAGONAL_LENGTH = Math.hypot(WIDTH, HEIGHT);
const leftPlatform = makeBox(4.25, 0.36, 2.45, 0x263640, false);
leftPlatform.position.set(LEFT_CENTER, -0.18, 0);
leftPlatform.receiveShadow = true;
scene.add(leftPlatform);
const rightPlatform = makeBox(4.25, 0.36, 2.45, 0x243b3d, false);
rightPlatform.position.set(RIGHT_CENTER, -0.18, 0);
rightPlatform.receiveShadow = true;
scene.add(rightPlatform);
function makeVoxelFoot(centerX, localX) {
const foot = new THREE.Group();
const lower = makeBox(0.58, 0.10, 0.76, 0x3a4851);
lower.position.y = 0.05;
foot.add(lower);
const upper = makeBox(0.40, 0.15, 0.61, 0x2d3942);
upper.position.y = 0.16;
foot.add(upper);
foot.position.x = centerX + localX;
scene.add(foot);
}
for (const x of [-WIDTH / 2, WIDTH / 2]) {
makeVoxelFoot(LEFT_CENTER, x);
makeVoxelFoot(RIGHT_CENTER, x);
}
// -------------------------------------------------------------------------
// Datum ghost for the unbraced frame
// -------------------------------------------------------------------------
const ghostMaterial = new THREE.MeshBasicMaterial({
color: 0x63dce8,
transparent: true,
opacity: 0.23,
depthWrite: false
});
const ghostGeometry = new THREE.BoxGeometry(0.085, 0.085, 0.07);
function makePixelLine(a, b) {
const group = new THREE.Group();
const distance = a.distanceTo(b);
const count = Math.max(1, Math.ceil(distance / 0.21));
for (let i = 0; i <= count; i++) {
const point = a.clone().lerp(b, i / count);
const pixel = new THREE.Mesh(ghostGeometry, ghostMaterial);
pixel.position.copy(point);
group.add(pixel);
}
scene.add(group);
return group;
}
makePixelLine(
new THREE.Vector3(LEFT_CENTER - WIDTH / 2, BASE_Y + HEIGHT, -0.28),
new THREE.Vector3(LEFT_CENTER + WIDTH / 2, BASE_Y + HEIGHT, -0.28)
);
makePixelLine(
new THREE.Vector3(LEFT_CENTER - WIDTH / 2, BASE_Y, -0.28),
new THREE.Vector3(LEFT_CENTER - WIDTH / 2, BASE_Y + HEIGHT, -0.28)
);
makePixelLine(
new THREE.Vector3(LEFT_CENTER + WIDTH / 2, BASE_Y, -0.28),
new THREE.Vector3(LEFT_CENTER + WIDTH / 2, BASE_Y + HEIGHT, -0.28)
);
// -------------------------------------------------------------------------
// Pixel-style +X direction indicators
// -------------------------------------------------------------------------
function makeDirectionArrow() {
const group = new THREE.Group();
const arrowMaterial = material(0x63dce8, 0.62, 0.05);
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.92, 0.095, 0.10),
arrowMaterial
);
shaft.position.x = -0.30;
group.add(shaft);
const headWidths = [0.42, 0.27, 0.12];
for (let i = 0; i < headWidths.length; i++) {
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.18, headWidths[i], 0.10),
arrowMaterial
);
head.position.x = 0.26 + i * 0.17;
group.add(head);
}
scene.add(group);
return group;
}
const leftArrow = makeDirectionArrow();
leftArrow.position.set(
LEFT_CENTER + 0.18,
BASE_Y + HEIGHT + 0.48,
0.10
);
const rightArrow = makeDirectionArrow();
rightArrow.position.set(
RIGHT_CENTER + 0.18,
BASE_Y + HEIGHT + 0.48,
0.10
);
// -------------------------------------------------------------------------
// Unbraced frame: four rigid members with four ideal pin joints
// -------------------------------------------------------------------------
const cedarPalette = [0xc47b42, 0xe0a15e, 0xb86c38, 0xd18c4d];
const bracedPalette = [0xa96b39, 0xd59a59, 0x91562f, 0xc17e45];
const bracePalette = [0xffc657, 0xffdc84, 0xe8a935, 0xffd06b];
const leftBottom = makeTimber(WIDTH, cedarPalette);
const leftTop = makeTimber(WIDTH, cedarPalette);
const leftPostA = makeTimber(HEIGHT, cedarPalette);
const leftPostB = makeTimber(HEIGHT, cedarPalette);
const leftJointA = makeJoint();
const leftJointB = makeJoint();
const leftJointC = makeJoint();
const leftJointD = makeJoint();
scene.add(
leftBottom,
leftTop,
leftPostA,
leftPostB,
leftJointA,
leftJointB,
leftJointC,
leftJointD
);
const leftPoints = {
A: new THREE.Vector2(),
B: new THREE.Vector2(),
C: new THREE.Vector2(),
D: new THREE.Vector2()
};
// -------------------------------------------------------------------------
// Braced frame: same outer frame plus a fixed-length diagonal A–C
// -------------------------------------------------------------------------
const rightBottom = makeTimber(WIDTH, bracedPalette);
const rightTop = makeTimber(WIDTH, bracedPalette);
const rightPostA = makeTimber(HEIGHT, bracedPalette);
const rightPostB = makeTimber(HEIGHT, bracedPalette);
const rightDiagonal = makeTimber(
DIAGONAL_LENGTH,
bracePalette,
0.21,
0.27
);
const rightJointA = makeJoint();
const rightJointB = makeJoint();
const rightJointC = makeJoint();
const rightJointD = makeJoint();
scene.add(
rightBottom,
rightTop,
rightPostA,
rightPostB,
rightDiagonal,
rightJointA,
rightJointB,
rightJointC,
rightJointD
);
const rightPoints = {
A: new THREE.Vector2(RIGHT_CENTER - WIDTH / 2, BASE_Y),
B: new THREE.Vector2(RIGHT_CENTER + WIDTH / 2, BASE_Y),
C: new THREE.Vector2(RIGHT_CENTER + WIDTH / 2, BASE_Y + HEIGHT),
D: new THREE.Vector2(RIGHT_CENTER - WIDTH / 2, BASE_Y + HEIGHT)
};
updateTimber(rightBottom, rightPoints.A, rightPoints.B);
updateTimber(rightPostB, rightPoints.B, rightPoints.C);
updateTimber(rightTop, rightPoints.D, rightPoints.C);
updateTimber(rightPostA, rightPoints.A, rightPoints.D);
updateTimber(rightDiagonal, rightPoints.A, rightPoints.C, 0.20);
moveJoint(rightJointA, rightPoints.A);
moveJoint(rightJointB, rightPoints.B);
moveJoint(rightJointC, rightPoints.C);
moveJoint(rightJointD, rightPoints.D);
// -------------------------------------------------------------------------
// Exact kinematic update
//
// The lower rail remains fixed. The upper rail translates by s.
// Each post retains length H:
//
// s² + v² = H² => v = sqrt(H² - s²)
//
// Thus all four member lengths remain exactly constant.
// -------------------------------------------------------------------------
const offsetValue = document.getElementById("offsetValue");
const angleValue = document.getElementById("angleValue");
const offsetMeter = document.getElementById("offsetMeter");
function updateUnbracedFrame(shear) {
const verticalComponent = Math.sqrt(
HEIGHT * HEIGHT - shear * shear
);
leftPoints.A.set(
LEFT_CENTER - WIDTH / 2,
BASE_Y
);
leftPoints.B.set(
LEFT_CENTER + WIDTH / 2,
BASE_Y
);
leftPoints.D.set(
LEFT_CENTER - WIDTH / 2 + shear,
BASE_Y + verticalComponent
);
leftPoints.C.set(
LEFT_CENTER + WIDTH / 2 + shear,
BASE_Y + verticalComponent
);
updateTimber(leftBottom, leftPoints.A, leftPoints.B);
updateTimber(leftPostB, leftPoints.B, leftPoints.C);
updateTimber(leftTop, leftPoints.D, leftPoints.C);
updateTimber(leftPostA, leftPoints.A, leftPoints.D);
moveJoint(leftJointA, leftPoints.A);
moveJoint(leftJointB, leftPoints.B);
moveJoint(leftJointC, leftPoints.C);
moveJoint(leftJointD, leftPoints.D);
const angle = THREE.MathUtils.radToDeg(
Math.atan2(shear, verticalComponent)
);
offsetValue.textContent = Math.round(shear * 100);
angleValue.textContent = `${angle.toFixed(1)}°`;
offsetMeter.style.width = `${(shear / MAX_SHEAR) * 100}%`;
}
updateUnbracedFrame(0);
// -------------------------------------------------------------------------
// Animation and interface
// -------------------------------------------------------------------------
const timeline = document.getElementById("timeline");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const cameraButton = document.getElementById("cameraButton");
const TWO_PI = Math.PI * 2;
let phase = 0;
let playing = true;
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : "Play";
}
function resetCycle() {
phase = 0;
timeline.value = "0";
timeline.style.setProperty("--p", "0%");
updateUnbracedFrame(0);
}
playButton.addEventListener("click", () => {
setPlaying(!playing);
});
resetButton.addEventListener("click", resetCycle);
timeline.addEventListener("input", () => {
const normalized = Number(timeline.value) / 100;
phase = normalized * TWO_PI;
setPlaying(false);
const shear = MAX_SHEAR * 0.5 * (1 - Math.cos(phase));
updateUnbracedFrame(shear);
});
// -------------------------------------------------------------------------
// Custom camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.0045,
-0.10,
0.88
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8,
27
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
function resetCamera() {
orbit.yaw = 0.08;
orbit.pitch = 0.18;
orbit.radius = window.innerWidth < 780 ? 16.5 : 11.8;
}
cameraButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
const tag = event.target?.tagName?.toLowerCase();
if (tag === "input" || tag === "button") return;
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
} else if (event.key.toLowerCase() === "r") {
resetCycle();
} else if (event.key.toLowerCase() === "c") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelLeft = document.getElementById("labelLeft");
const labelRight = document.getElementById("labelRight");
const leftLabelAnchor = new THREE.Vector3(
LEFT_CENTER,
BASE_Y + HEIGHT + 0.91,
0
);
const rightLabelAnchor = new THREE.Vector3(
RIGHT_CENTER,
BASE_Y + HEIGHT + 0.91,
0
);
function positionLabel(element, anchor) {
const projected = anchor.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
let compactLayout = window.innerWidth < 780;
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = width < 780 ? 52 : 38;
camera.updateProjectionMatrix();
const nextCompactLayout = width < 780;
if (nextCompactLayout !== compactLayout) {
compactLayout = nextCompactLayout;
orbit.radius = compactLayout ? 16.5 : 11.8;
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
phase = (phase + dt * 0.72) % TWO_PI;
}
const normalizedPhase = phase / TWO_PI;
const shear = MAX_SHEAR * 0.5 * (1 - Math.cos(phase));
updateUnbracedFrame(shear);
timeline.value = String(normalizedPhase * 100);
timeline.style.setProperty("--p", `${normalizedPhase * 100}%`);
updateCamera();
positionLabel(labelLeft, leftLabelAnchor);
positionLabel(labelRight, rightLabelAnchor);
renderer.render(scene, camera);
}
document.getElementById("loading").classList.add("hidden");
animate();
</script>
</body>
</html>
```