Prompt specification
Build or customize the Three.js 3D scene "Mending a Torn Fabric Sample" 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: Mending a Torn Fabric Sample
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mending-a-torn-fabric-sample-x3290
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Animate a simple patch and stitch path over a torn cloth diagram. Keep the repair visibly distinct and label the cloth behavior as schematic.
Working Three.js Starter (mending-a-torn-fabric-sample-x3290):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Patch & Stitch — Schematic Repair Study</title>
<style>
:root {
color-scheme: dark;
--ink: #eef8f7;
--muted: #9db8bd;
--panel: rgba(8, 27, 36, 0.78);
--line: rgba(161, 224, 218, 0.2);
--patch: #39b8a8;
--stitch: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #173847 0%, #0a1d28 45%, #06131b 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.brand {
position: absolute;
top: 30px;
left: 34px;
width: min(390px, calc(100vw - 68px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 11px;
color: #8dcac4;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 27px;
height: 1px;
content: "";
background: #69d6c8;
box-shadow: 0 0 10px rgba(105, 214, 200, 0.55);
}
h1 {
margin: 0;
font-size: clamp(42px, 5.2vw, 72px);
font-weight: 720;
line-height: 0.93;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 340px;
margin: 17px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.schematic-badge {
display: inline-flex;
align-items: center;
gap: 9px;
margin-top: 19px;
padding: 9px 12px;
border: 1px solid rgba(92, 224, 180, 0.3);
border-radius: 999px;
background: rgba(42, 158, 119, 0.11);
color: #b5f4d7;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.schematic-badge i {
width: 7px;
height: 7px;
border-radius: 50%;
background: #70efad;
box-shadow: 0 0 10px rgba(112, 239, 173, 0.85);
}
.disclaimer {
max-width: 340px;
margin: 10px 0 0;
color: #75959d;
font-size: 11px;
line-height: 1.5;
}
.readout {
position: absolute;
top: 30px;
right: 30px;
width: 235px;
padding: 15px 16px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(12px);
}
.readout-label {
display: flex;
justify-content: space-between;
margin-bottom: 13px;
color: #779da4;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.readout-label span:last-child {
color: #74d8c7;
}
.phase-title {
font-size: 14px;
font-weight: 750;
letter-spacing: 0.015em;
}
.phase-detail {
min-height: 34px;
margin-top: 6px;
color: #8faab0;
font-size: 11px;
line-height: 1.45;
}
.progress-track {
height: 3px;
margin-top: 13px;
overflow: hidden;
border-radius: 10px;
background: rgba(142, 202, 199, 0.12);
}
.progress-fill {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left;
background: linear-gradient(90deg, #48c9b9, #ffd166);
box-shadow: 0 0 10px rgba(72, 201, 185, 0.45);
}
.legend {
position: absolute;
bottom: 31px;
left: 31px;
display: flex;
gap: 18px;
color: #8ca7ad;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
}
.swatch.patch {
background: var(--patch);
box-shadow: 0 0 8px rgba(57, 184, 168, 0.45);
}
.swatch.stitch {
background: var(--stitch);
box-shadow: 0 0 8px rgba(255, 209, 102, 0.45);
}
.swatch.tear {
background: #263c45;
border: 1px solid #56717a;
}
.timeline {
position: absolute;
bottom: 28px;
left: 50%;
width: min(430px, 38vw);
transform: translateX(-50%);
}
.timeline-strip {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 9px;
margin-top: 8px;
}
.step {
color: #58747c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: color 180ms ease;
}
.step::before {
display: inline-block;
width: 5px;
height: 5px;
margin-right: 6px;
border-radius: 50%;
content: "";
background: currentColor;
vertical-align: 1px;
}
.step.is-active {
color: #d8f5ef;
}
.step.is-done {
color: #63cdbd;
}
.controls {
position: absolute;
right: 29px;
bottom: 29px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.control-hint {
margin-right: 4px;
color: #65858d;
font-size: 10px;
}
.control-button {
padding: 9px 12px;
border: 1px solid rgba(145, 213, 205, 0.22);
border-radius: 9px;
background: rgba(11, 36, 46, 0.8);
color: #c2ddda;
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.control-button:hover,
.control-button:focus-visible {
border-color: rgba(100, 221, 202, 0.55);
background: rgba(19, 58, 67, 0.95);
outline: none;
transform: translateY(-1px);
}
.control-button.primary {
border-color: rgba(255, 209, 102, 0.35);
color: #ffe2a0;
}
@media (max-width: 760px) {
.brand {
top: 19px;
left: 19px;
}
h1 {
font-size: 43px;
}
.subtitle {
max-width: 280px;
margin-top: 12px;
font-size: 12px;
}
.schematic-badge {
margin-top: 13px;
}
.disclaimer {
max-width: 280px;
font-size: 9px;
}
.readout {
top: auto;
right: 16px;
bottom: 123px;
width: 205px;
padding: 12px 13px;
}
.timeline {
right: 16px;
bottom: 67px;
left: 16px;
width: auto;
transform: none;
}
.legend {
display: none;
}
.controls {
right: 16px;
bottom: 17px;
}
.control-hint {
display: none;
}
}
@media (max-height: 640px) {
.subtitle,
.disclaimer {
display: none;
}
.schematic-badge {
margin-top: 12px;
}
.readout {
padding: 11px 13px;
}
.phase-detail {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Animated schematic cloth repair scene"></main>
<div class="hud">
<header class="brand">
<div class="eyebrow">Repair study / 01</div>
<h1>Patch &<br />stitch.</h1>
<p class="subtitle">
A small repair sequence staged over a torn cloth diagram.
</p>
<div class="schematic-badge">
<i></i>
Cloth behavior — schematic
</div>
<p class="disclaimer">
Shape and motion are illustrative only. No drape, tension, or material
simulation is being calculated.
</p>
</header>
<aside class="readout" aria-live="polite">
<div class="readout-label">
<span>Sequence</span>
<span id="phase-number">01 / 03</span>
</div>
<div id="phase-title" class="phase-title">Aligning patch</div>
<div id="phase-detail" class="phase-detail">
Position the repair over the torn opening.
</div>
<div
class="progress-track"
role="progressbar"
aria-label="Repair sequence progress"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="0"
>
<div id="progress-fill" class="progress-fill"></div>
</div>
</aside>
<div class="legend" aria-label="Scene legend">
<div class="legend-item">
<span class="swatch patch"></span>
Patch
</div>
<div class="legend-item">
<span class="swatch stitch"></span>
Stitch path
</div>
<div class="legend-item">
<span class="swatch tear"></span>
Tear
</div>
</div>
<div class="timeline" aria-hidden="true">
<div class="progress-track">
<div id="timeline-progress" class="progress-fill"></div>
</div>
<div class="timeline-strip">
<div class="step is-active">01 Align</div>
<div class="step">02 Seat</div>
<div class="step">03 Stitch</div>
</div>
</div>
<div class="controls">
<span class="control-hint">Drag to orbit · Scroll to zoom · R to replay</span>
<button id="reset-view" class="control-button" type="button">
Reset view
</button>
<button id="replay" class="control-button primary" type="button">
Replay
</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional schematic showing a patch being placed over torn cloth and stitched"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071a24);
const camera = new THREE.OrthographicCamera(-5, 5, 3, -3, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const orbit = {
yaw: 0.08,
elevation: 0.035,
radius: 9,
};
function updateCamera() {
const cosElevation = Math.cos(orbit.elevation);
camera.position.set(
Math.sin(orbit.yaw) * cosElevation * orbit.radius,
Math.sin(orbit.elevation) * orbit.radius,
Math.cos(orbit.yaw) * cosElevation * orbit.radius
);
camera.lookAt(cameraTarget);
camera.zoom = 9 / orbit.radius;
camera.updateProjectionMatrix();
}
updateCamera();
// Lighting
scene.add(new THREE.AmbientLight(0xffffff, 1.0));
const hemisphere = new THREE.HemisphereLight(0xd8fbff, 0x15242b, 1.55);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd5, 3.0);
keyLight.position.set(-3.5, 5.5, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 4;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x79e6ff, 1.35);
rimLight.position.set(5, -1, 5);
scene.add(rimLight);
// Schematic background grid
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(18, 12),
new THREE.MeshBasicMaterial({ color: 0x0a202b })
);
backdrop.position.z = -0.75;
scene.add(backdrop);
const gridPositions = [];
for (let x = -6; x <= 6.001; x += 0.5) {
gridPositions.push(x, -4, -0.68, x, 4, -0.68);
}
for (let y = -4; y <= 4.001; y += 0.5) {
gridPositions.push(-6, y, -0.68, 6, y, -0.68);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x2a5362,
transparent: true,
opacity: 0.45,
})
);
scene.add(grid);
const diagramRing = new THREE.Mesh(
new THREE.TorusGeometry(4.18, 0.012, 6, 128),
new THREE.MeshBasicMaterial({
color: 0x3d7580,
transparent: true,
opacity: 0.32,
})
);
diagramRing.position.z = -0.6;
scene.add(diagramRing);
// Procedural fabric textures: no external image assets.
function makeFabricTexture(baseColor, lineColor, fleckColor) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 512;
const context = canvas.getContext("2d");
context.fillStyle = baseColor;
context.fillRect(0, 0, 512, 512);
context.lineWidth = 1;
for (let x = 0; x <= 512; x += 16) {
context.strokeStyle = lineColor;
context.beginPath();
context.moveTo(x + 0.5, 0);
context.lineTo(x + 0.5, 512);
context.stroke();
}
for (let y = 0; y <= 512; y += 19) {
context.strokeStyle = lineColor;
context.beginPath();
context.moveTo(0, y + 0.5);
context.lineTo(512, y + 0.5);
context.stroke();
}
context.lineWidth = 1.5;
for (let offset = -512; offset <= 512; offset += 74) {
context.strokeStyle = "rgba(255,255,255,0.055)";
context.beginPath();
context.moveTo(offset, 0);
context.lineTo(offset + 512, 512);
context.stroke();
}
let seed = 7331;
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
for (let i = 0; i < 950; i++) {
context.fillStyle = fleckColor;
context.globalAlpha = 0.15 + random() * 0.25;
const x = random() * 512;
const y = random() * 512;
context.fillRect(x, y, 1 + random() * 1.4, 1 + random() * 1.4);
}
context.globalAlpha = 1;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(0.55, 0.55);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const clothTexture = makeFabricTexture(
"#eee5d1",
"rgba(106,91,67,0.075)",
"rgba(72,62,47,0.35)"
);
const patchTexture = makeFabricTexture(
"#31aa9c",
"rgba(5,74,68,0.13)",
"rgba(222,255,242,0.42)"
);
const fabricRig = new THREE.Group();
scene.add(fabricRig);
// Slightly irregular outer cloth panel.
const outerPoints = [
[-3.08, -1.88],
[-2.35, -2.00],
[-1.55, -1.92],
[-0.72, -2.03],
[0.12, -1.94],
[0.91, -2.02],
[1.72, -1.91],
[2.46, -2.00],
[3.08, -1.86],
[3.04, -1.06],
[3.14, -0.28],
[3.02, 0.48],
[3.10, 1.26],
[2.88, 1.95],
[2.10, 1.91],
[1.26, 2.03],
[0.42, 1.93],
[-0.43, 2.03],
[-1.27, 1.91],
[-2.15, 2.00],
[-2.98, 1.85],
[-3.12, 1.02],
[-3.01, 0.05],
[-3.10, -0.82],
];
const clothShape = new THREE.Shape();
outerPoints.forEach(([x, y], index) => {
if (index === 0) clothShape.moveTo(x, y);
else clothShape.lineTo(x, y);
});
clothShape.closePath();
// Clockwise irregular tear.
const tearPoints = [
[-0.85, 0.56],
[-0.42, 0.78],
[0.08, 0.64],
[0.58, 0.74],
[0.47, 0.27],
[0.74, 0.02],
[0.36, -0.30],
[0.12, -0.66],
[-0.22, -0.52],
[-0.45, -0.78],
[-0.54, -0.26],
[-0.84, -0.03],
];
const tearPath = new THREE.Path();
tearPoints.forEach(([x, y], index) => {
if (index === 0) tearPath.moveTo(x, y);
else tearPath.lineTo(x, y);
});
tearPath.closePath();
clothShape.holes.push(tearPath);
const clothMesh = new THREE.Mesh(
new THREE.ShapeGeometry(clothShape, 32),
new THREE.MeshStandardMaterial({
color: 0xffffff,
map: clothTexture,
roughness: 0.94,
metalness: 0,
side: THREE.DoubleSide,
})
);
clothMesh.receiveShadow = true;
fabricRig.add(clothMesh);
const clothOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
clothShape.getPoints(80).map((point) =>
new THREE.Vector3(point.x, point.y, 0.025)
)
),
new THREE.LineBasicMaterial({
color: 0x70847e,
transparent: true,
opacity: 0.72,
})
);
fabricRig.add(clothOutline);
// Dark shape behind the opening gives the tear visual depth.
const tearShadowShape = new THREE.Shape();
tearPoints.forEach(([x, y], index) => {
const scaledX = x * 1.08;
const scaledY = y * 1.08;
if (index === 0) tearShadowShape.moveTo(scaledX, scaledY);
else tearShadowShape.lineTo(scaledX, scaledY);
});
tearShadowShape.closePath();
const tearShadow = new THREE.Mesh(
new THREE.ShapeGeometry(tearShadowShape, 24),
new THREE.MeshBasicMaterial({
color: 0x172830,
side: THREE.DoubleSide,
})
);
tearShadow.position.z = -0.055;
fabricRig.add(tearShadow);
const tearEdge = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
tearPoints.map(([x, y]) => new THREE.Vector3(x, y, 0.045))
),
new THREE.LineBasicMaterial({
color: 0x35494d,
transparent: true,
opacity: 0.9,
})
);
fabricRig.add(tearEdge);
// Short frayed fibers around the tear.
const frayPositions = [];
const tearCenter = new THREE.Vector2(
tearPoints.reduce((sum, point) => sum + point[0], 0) / tearPoints.length,
tearPoints.reduce((sum, point) => sum + point[1], 0) / tearPoints.length
);
tearPoints.forEach(([x, y], index) => {
if (index % 2 !== 0) return;
const outward = new THREE.Vector2(x, y).sub(tearCenter).normalize();
const length = 0.07 + (index % 4) * 0.025;
frayPositions.push(
x,
y,
0.055,
x + outward.x * length,
y + outward.y * length,
0.055
);
});
const frayGeometry = new THREE.BufferGeometry();
frayGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(frayPositions, 3)
);
fabricRig.add(
new THREE.LineSegments(
frayGeometry,
new THREE.LineBasicMaterial({
color: 0x64746e,
transparent: true,
opacity: 0.58,
})
)
);
// Rounded, slightly asymmetric repair patch.
const patchShape = new THREE.Shape();
patchShape.moveTo(-1.14, -0.72);
patchShape.quadraticCurveTo(-1.24, -0.91, -1.02, -0.98);
patchShape.lineTo(0.42, -0.94);
patchShape.quadraticCurveTo(0.91, -0.96, 1.04, -0.63);
patchShape.lineTo(1.09, 0.54);
patchShape.quadraticCurveTo(1.08, 0.89, 0.70, 0.95);
patchShape.lineTo(-0.70, 0.90);
patchShape.quadraticCurveTo(-1.08, 0.89, -1.12, 0.51);
patchShape.closePath();
const patchGroup = new THREE.Group();
patchGroup.position.set(0.18, 0.11, 1.2);
patchGroup.rotation.z = -0.085;
patchGroup.scale.setScalar(0.82);
fabricRig.add(patchGroup);
const patchEdge = new THREE.Mesh(
new THREE.ShapeGeometry(patchShape, 32),
new THREE.MeshBasicMaterial({
color: 0x115f59,
side: THREE.DoubleSide,
})
);
patchEdge.scale.setScalar(1.025);
patchEdge.position.z = 0.045;
patchGroup.add(patchEdge);
const patchMesh = new THREE.Mesh(
new THREE.ShapeGeometry(patchShape, 32),
new THREE.MeshStandardMaterial({
color: 0xffffff,
map: patchTexture,
roughness: 0.78,
metalness: 0,
emissive: 0x073c37,
emissiveIntensity: 0.12,
side: THREE.DoubleSide,
})
);
patchMesh.position.z = 0.078;
patchMesh.castShadow = true;
patchGroup.add(patchMesh);
const patchOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
patchShape.getPoints(80).map((point) =>
new THREE.Vector3(point.x, point.y, 0.095)
)
),
new THREE.LineBasicMaterial({
color: 0x087067,
transparent: true,
opacity: 0.9,
})
);
patchGroup.add(patchOutline);
// The stitch path follows the patch perimeter.
const perimeterPoints = patchShape
.getPoints(48)
.map((point) => new THREE.Vector3(point.x * 0.96, point.y * 0.96, 0.145))
.filter(
(point, index, points) =>
index === 0 || point.distanceTo(points[index - 1]) > 0.001
);
const stitchCurve = new THREE.CatmullRomCurve3(
perimeterPoints,
true,
"centripetal"
);
const guidePoints = stitchCurve.getPoints(240);
const guideLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(guidePoints),
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.075,
gapSize: 0.065,
transparent: true,
opacity: 0.2,
})
);
guideLine.computeLineDistances();
patchGroup.add(guideLine);
const activePath = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(guidePoints),
new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.95,
})
);
activePath.geometry.setDrawRange(0, 0);
patchGroup.add(activePath);
// Individual cross-stitches appear progressively around the perimeter.
const stitchCount = 34;
const stitchGeometry = new THREE.CylinderGeometry(
0.018,
0.018,
0.19,
8,
1,
false
);
const stitchMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
roughness: 0.48,
metalness: 0.02,
emissive: 0x5a3900,
emissiveIntensity: 0.22,
});
const stitches = [];
const upAxis = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < stitchCount; i++) {
const u = i / stitchCount;
const point = stitchCurve.getPointAt(u);
const tangent = stitchCurve.getTangentAt(u).normalize();
const crossDirection = new THREE.Vector3(
-tangent.y,
tangent.x,
0
).normalize();
const stitch = new THREE.Mesh(stitchGeometry, stitchMaterial);
stitch.position.copy(point);
stitch.quaternion.setFromUnitVectors(upAxis, crossDirection);
stitch.scale.y = 0.001;
stitch.visible = false;
stitch.castShadow = true;
patchGroup.add(stitch);
stitches.push(stitch);
}
// Glowing moving point indicates the direction of the stitch path.
const needleGroup = new THREE.Group();
const needleDot = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 14, 10),
new THREE.MeshBasicMaterial({ color: 0xffe29a })
);
needleGroup.add(needleDot);
needleGroup.visible = false;
patchGroup.add(needleGroup);
// UI state
const phaseNumber = document.getElementById("phase-number");
const phaseTitle = document.getElementById("phase-title");
const phaseDetail = document.getElementById("phase-detail");
const progressFill = document.getElementById("progress-fill");
const timelineProgress = document.getElementById("timeline-progress");
const progressTrack = document.querySelector(".progress-track");
const stepElements = [...document.querySelectorAll(".step")];
const phaseCopy = [
{
number: "01 / 03",
title: "Aligning patch",
detail: "Position the repair over the torn opening.",
},
{
number: "02 / 03",
title: "Seating patch",
detail: "Bring the patch onto the cloth plane.",
},
{
number: "03 / 03",
title: "Forming stitch path",
detail: "Trace and secure the perimeter in sequence.",
},
{
number: "03 / 03",
title: "Repair complete",
detail: "Patch secured by schematic perimeter stitches.",
},
];
let previousPhase = -1;
function updateReadout(phase, progress) {
if (phase !== previousPhase) {
previousPhase = phase;
const copy = phaseCopy[phase];
phaseNumber.textContent = copy.number;
phaseTitle.textContent = copy.title;
phaseDetail.textContent = copy.detail;
stepElements.forEach((step, index) => {
step.classList.toggle("is-active", phase === index);
step.classList.toggle("is-done", phase > index);
});
}
const percentage = Math.round(progress * 100);
progressFill.style.transform = `scaleX(${progress})`;
timelineProgress.style.transform = `scaleX(${progress})`;
progressTrack.setAttribute("aria-valuenow", percentage);
}
// Timeline and animation helpers
let startTime = performance.now();
const clamp01 = (value) => Math.min(1, Math.max(0, value));
const lerp = (a, b, amount) => a + (b - a) * amount;
function smoothstep(edge0, edge1, value) {
const x = clamp01((value - edge0) / (edge1 - edge0));
return x * x * (3 - 2 * x);
}
function replay() {
startTime = performance.now();
previousPhase = -1;
}
document.getElementById("replay").addEventListener("click", replay);
document.getElementById("reset-view").addEventListener("click", () => {
orbit.yaw = 0.08;
orbit.elevation = 0.035;
orbit.radius = 9;
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r" || event.code === "Space") {
event.preventDefault();
replay();
}
});
// Direct camera controls, implemented without Three.js addons.
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
orbit.yaw = THREE.MathUtils.clamp(
orbit.yaw + deltaX * 0.004,
-0.65,
0.65
);
orbit.elevation = THREE.MathUtils.clamp(
orbit.elevation - deltaY * 0.0035,
-0.25,
0.45
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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 + event.deltaY * 0.006,
7,
12.5
);
updateCamera();
},
{ passive: false }
);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(5.8, 7.3 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
function animate(now) {
requestAnimationFrame(animate);
const elapsed = (now - startTime) / 1000;
const align = smoothstep(0.25, 1.35, elapsed);
const seat = smoothstep(1.05, 2.75, elapsed);
const stitchProgress = clamp01((elapsed - 2.65) / 3.2);
// Patch alignment and approach.
const hover = Math.sin(Math.max(0, elapsed - 0.25) * 2.4) *
0.055 *
(1 - seat);
patchGroup.position.set(
lerp(0.18, 0, align),
lerp(0.11, 0, align),
lerp(1.2, 0, seat) + hover
);
patchGroup.scale.setScalar(lerp(0.82, 1, align));
patchGroup.rotation.z = lerp(-0.085, 0.018, align);
// The full dashed path becomes visible as the patch approaches.
guideLine.material.opacity =
0.12 + 0.23 * smoothstep(2.25, 3.0, elapsed);
// Progressive active path.
const activePointCount = Math.max(
2,
Math.floor(stitchProgress * guidePoints.length)
);
activePath.geometry.setDrawRange(0, activePointCount);
activePath.visible = stitchProgress > 0;
// Reveal each stitch as the moving point reaches it.
stitches.forEach((stitch, index) => {
const threshold = (index / (stitchCount - 1)) * 0.9;
const reveal = smoothstep(threshold, threshold + 0.1, stitchProgress);
stitch.visible = reveal > 0.001;
stitch.scale.y = Math.max(0.001, reveal);
stitch.position.z = 0.145 + (1 - reveal) * 0.08;
});
// Moving stitch indicator.
needleGroup.visible =
elapsed > 2.62 && elapsed < 5.92;
if (needleGroup.visible) {
const pathPosition = stitchCurve.getPointAt(
clamp01(stitchProgress * 1.015)
);
needleGroup.position.copy(pathPosition);
needleGroup.position.z = 0.205;
}
// Subtle whole-diagram motion, explicitly kept schematic.
fabricRig.rotation.y = Math.sin(elapsed * 0.32) * 0.012;
fabricRig.position.y = Math.sin(elapsed * 0.45) * 0.015;
let phase = 0;
if (elapsed >= 1.05) phase = 1;
if (elapsed >= 2.65) phase = 2;
if (elapsed >= 5.95) phase = 3;
const overallProgress = clamp01(elapsed / 5.95);
updateReadout(phase, overallProgress);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```