Prompt specification
Build or customize the Three.js 3D scene "Scarf-making living room corner" 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: Scarf-making living room corner
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/scarf-making-living-room-corner-x0599
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: scarf-making living room corner. Compose yarn basket, needles, partly knitted scarf, and chair; a short schematic stitch sequence extends the fabric while the remaining yarn supply visibly decreases. Return one self-contained HTML file.
Working Three.js Starter (scarf-making-living-room-corner-x0599):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sunday Corner — Voxel Knitting Scene</title>
<style>
:root {
--ink: #2e2924;
--muted: #746b60;
--paper: #f5efe4;
--rust: #c96443;
--rust-dark: #93432f;
--sage: #687863;
--line: rgba(61, 51, 42, 0.15);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d0c2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.intro {
top: 26px;
left: 30px;
max-width: 380px;
text-shadow: 0 1px 20px rgba(255,255,255,.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--rust-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: .18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 4px;
background: var(--rust);
box-shadow: 8px 0 0 #e0b18b;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 48px);
font-weight: 500;
line-height: .98;
letter-spacing: -.035em;
}
.intro p {
margin: 12px 0 0;
color: #5f574e;
font-size: 13px;
line-height: 1.5;
}
.supply-card {
top: 26px;
right: 28px;
width: 190px;
padding: 14px 15px;
border: 1px solid rgba(255,255,255,.65);
border-radius: 13px;
background: rgba(246, 240, 229, .83);
box-shadow: 0 12px 38px rgba(71, 58, 44, .12);
backdrop-filter: blur(12px);
}
.supply-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.supply-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .13em;
text-transform: uppercase;
}
#yarn-percent {
color: var(--rust-dark);
font-size: 15px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.meter {
height: 7px;
overflow: hidden;
border-radius: 10px;
background: #dfd4c4;
box-shadow: inset 0 1px 2px rgba(50,40,30,.12);
}
#meter-fill {
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--rust-dark), var(--rust), #e1a06f);
transition: width .15s linear;
}
.supply-note {
margin-top: 7px;
color: #81776b;
font-size: 10px;
}
.view-tools {
right: 28px;
bottom: 26px;
display: flex;
gap: 8px;
pointer-events: auto;
}
.tool-button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(67,55,44,.13);
border-radius: 10px;
background: rgba(247,241,230,.88);
color: #625950;
font: 700 10px/1 inherit;
letter-spacing: .08em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 7px 22px rgba(64,52,41,.09);
}
.tool-button:hover { background: #fffaf0; }
.controls {
left: 50%;
bottom: 24px;
width: min(760px, calc(100vw - 36px));
transform: translateX(-50%);
padding: 15px 17px 14px;
border: 1px solid rgba(255,255,255,.72);
border-radius: 17px;
background: rgba(246, 240, 229, .92);
box-shadow: 0 18px 60px rgba(59, 47, 36, .18);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.controls-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
margin-bottom: 11px;
}
.controls-title {
display: flex;
align-items: baseline;
gap: 10px;
white-space: nowrap;
}
.controls-title strong {
font-size: 11px;
letter-spacing: .14em;
text-transform: uppercase;
}
#status {
overflow: hidden;
color: var(--muted);
font-size: 11px;
text-overflow: ellipsis;
white-space: nowrap;
}
.sequence {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.step {
position: relative;
display: grid;
grid-template-columns: 28px 1fr;
grid-template-rows: auto auto;
column-gap: 9px;
min-width: 0;
padding: 10px 11px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,.38);
transition: border-color .2s, background .2s, transform .2s;
}
.step.active {
border-color: rgba(201,100,67,.65);
background: rgba(255,248,235,.85);
transform: translateY(-2px);
}
.step.done { background: rgba(104,120,99,.12); }
.step-number {
grid-row: 1 / 3;
align-self: center;
color: #a49a8c;
font: 800 11px/1 inherit;
font-variant-numeric: tabular-nums;
}
.step.active .step-number { color: var(--rust); }
.step.done .step-number { color: var(--sage); }
.step strong {
overflow: hidden;
font-size: 11px;
letter-spacing: .07em;
text-overflow: ellipsis;
white-space: nowrap;
}
.step small {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.stitch-icon {
position: absolute;
top: 7px;
right: 8px;
display: flex;
gap: 3px;
opacity: .72;
}
.stitch-icon i {
display: block;
width: 4px;
height: 12px;
border-left: 2px solid var(--rust);
border-right: 2px solid var(--rust);
transform: skew(-11deg);
}
.stitch-icon.purl i {
height: 7px;
border: 0;
border-radius: 50%;
background: var(--rust);
}
.stitch-icon.double i:nth-child(even) {
margin-left: 5px;
}
.stitch-icon.off {
gap: 0;
align-items: center;
}
.stitch-icon.off i {
width: 17px;
height: 3px;
border: 0;
border-radius: 0;
background: var(--rust);
transform: none;
}
.controls-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-top: 11px;
}
.hint {
color: #82786d;
font-size: 10px;
line-height: 1.4;
}
#knit-button {
flex: 0 0 auto;
min-width: 176px;
min-height: 38px;
padding: 0 18px;
border: 0;
border-radius: 9px;
background: var(--rust-dark);
color: #fff8ed;
font: 800 10px/1 inherit;
letter-spacing: .1em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 5px 0 #713426, 0 9px 18px rgba(111,52,37,.2);
transition: transform .12s, box-shadow .12s, opacity .12s;
}
#knit-button:hover {
background: #a34832;
transform: translateY(-1px);
box-shadow: 0 6px 0 #713426, 0 11px 22px rgba(111,52,37,.22);
}
#knit-button:active {
transform: translateY(4px);
box-shadow: 0 1px 0 #713426;
}
#knit-button:disabled {
cursor: wait;
opacity: .7;
}
@media (max-width: 700px) {
.intro { top: 18px; left: 18px; }
.intro p { display: none; }
.supply-card { top: 18px; right: 18px; width: 142px; }
.controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
.controls-head { margin-bottom: 9px; }
#status { display: none; }
.sequence { gap: 5px; }
.step {
display: block;
min-height: 51px;
padding: 9px 7px;
text-align: center;
}
.step-number { display: block; margin-bottom: 4px; }
.stitch-icon { display: none; }
.step strong { display: block; }
.step small { display: block; font-size: 8px; }
.hint { display: none; }
#knit-button { width: 100%; }
.controls-foot { margin-top: 10px; }
.view-tools { display: none; }
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel scene of a scarf-making living room corner"></main>
<header class="hud intro">
<div class="eyebrow">Sunday Corner · No. 07</div>
<h1>A little room<br>for slow making.</h1>
<p>Drag to orbit, scroll to zoom, and follow four small stitches into a longer scarf.</p>
</header>
<aside class="hud supply-card" aria-label="Remaining yarn supply">
<div class="supply-top">
<span class="supply-label">Yarn supply</span>
<span id="yarn-percent">100%</span>
</div>
<div class="meter"><div id="meter-fill"></div></div>
<div class="supply-note">The ball visibly shrinks as rows are worked.</div>
</aside>
<section class="hud controls" aria-label="Knitting stitch sequence">
<div class="controls-head">
<div class="controls-title">
<strong>Stitch sequence</strong>
<span id="status">4 steps · about 8 seconds</span>
</div>
</div>
<div class="sequence">
<div class="step" id="step-0">
<span class="step-number">01</span>
<strong>KNIT</strong>
<small>K1 × 2</small>
<span class="stitch-icon" aria-hidden="true"><i></i><i></i><i></i></span>
</div>
<div class="step" id="step-1">
<span class="step-number">02</span>
<strong>PURL</strong>
<small>P1 × 2</small>
<span class="stitch-icon purl" aria-hidden="true"><i></i><i></i><i></i></span>
</div>
<div class="step" id="step-2">
<span class="step-number">03</span>
<strong>KNIT</strong>
<small>K2 × 3</small>
<span class="stitch-icon double" aria-hidden="true"><i></i><i></i><i></i><i></i></span>
</div>
<div class="step" id="step-3">
<span class="step-number">04</span>
<strong>BIND OFF</strong>
<small>Finish edge</small>
<span class="stitch-icon off" aria-hidden="true"><i></i></span>
</div>
</div>
<div class="controls-foot">
<div class="hint">Drag the room to look around · Double-click to reset the view</div>
<button id="knit-button" type="button">Start sequence</button>
</div>
</section>
<div class="hud view-tools">
<button class="tool-button" id="reset-view" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
const container = document.querySelector('#scene');
const statusEl = document.querySelector('#status');
const knitButton = document.querySelector('#knit-button');
const yarnPercentEl = document.querySelector('#yarn-percent');
const meterFillEl = document.querySelector('#meter-fill');
const stepEls = [...document.querySelectorAll('.step')];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d0c2);
scene.fog = new THREE.Fog(0xd8d0c2, 12, 21);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 50);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
container.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff5df, 0x776b5d, 1.65);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe7c5, 3.2);
sun.position.set(-3.5, 8, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d1da, 0.65);
fill.position.set(6, 4, -4);
scene.add(fill);
const material = (color, roughness = 0.82, extra = {}) =>
new THREE.MeshStandardMaterial({ color, roughness, ...extra });
const mats = {
floor: material(0xc8b69d),
wall: material(0xe7dece),
wallSide: material(0xd8cdbb),
trim: material(0xf4eee3),
rug: material(0x7f8b83),
rugInner: material(0x9ca397),
rugStripe: material(0xbb9a78),
wood: material(0x72503b),
woodLight: material(0xa77b54),
woodDark: material(0x4b382d),
wicker: material(0xb98755),
wickerLight: material(0xd0a16a),
wickerDark: material(0x95673f),
sage: material(0x71806c),
sageLight: material(0x8c9a83),
rust: material(0xc86443),
rustDark: material(0x934431),
cream: material(0xe8cfaa),
yarn: material(0xb84f34),
yarnLight: material(0xd9744d),
yarnDark: material(0x873528),
blue: material(0x9ebbc2, 0.45, { emissive: 0x29434b, emissiveIntensity: 0.18 }),
paper: material(0xf2e8d8),
dark: material(0x3d332b)
};
function box(parent, w, h, d, x, y, z, mat, rot = null) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
if (rot) mesh.rotation.set(rot[0] || 0, rot[1] || 0, rot[2] || 0);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createRoom() {
box(scene, 11, 0.24, 8, 0, -0.13, 0.2, mats.floor);
box(scene, 11, 5.2, 0.18, 0, 2.48, -3.62, mats.wall);
box(scene, 0.18, 5.2, 7.9, -5.48, 2.48, 0.18, mats.wallSide);
box(scene, 10.85, 0.13, 0.09, 0, 0.08, -3.48, mats.trim);
box(scene, 0.09, 0.13, 7.7, -5.34, 0.08, 0.18, mats.trim);
// Soft voxel rug.
box(scene, 6.25, 0.045, 4.35, -0.35, 0.012, 0.42, mats.rug);
box(scene, 5.88, 0.05, 3.98, -0.35, 0.022, 0.42, mats.rugInner);
for (let i = 0; i < 9; i++) {
box(scene, 0.08, 0.012, 3.7, -3.08 + i * 0.68, 0.052, 0.42, mats.rugStripe);
}
// Window on the back wall.
box(scene, 2.12, 1.72, 0.11, -2.88, 3.22, -3.48, mats.woodDark);
box(scene, 1.91, 1.51, 0.08, -2.88, 3.22, -3.405, mats.blue);
box(scene, 0.08, 1.53, 0.1, -2.88, 3.22, -3.34, mats.trim);
box(scene, 1.93, 0.08, 0.1, -2.88, 3.22, -3.34, mats.trim);
box(scene, 2.25, 0.12, 0.25, -2.88, 2.31, -3.38, mats.trim);
// A small framed geometric print.
box(scene, 1.15, 1.3, 0.1, -4.45, 2.1, -3.47, mats.wood);
box(scene, 0.97, 1.12, 0.05, -4.45, 2.1, -3.39, mats.paper);
box(scene, 0.54, 0.54, 0.055, -4.45, 2.24, -3.34, mats.rust);
box(scene, 0.18, 0.82, 0.055, -4.63, 1.92, -3.34, mats.sage);
}
createRoom();
// A wall-mounted schematic matching the four interactive steps.
const chartCanvas = document.createElement('canvas');
chartCanvas.width = 1200;
chartCanvas.height = 320;
const chartContext = chartCanvas.getContext('2d');
const chartTexture = new THREE.CanvasTexture(chartCanvas);
chartTexture.colorSpace = THREE.SRGBColorSpace;
chartTexture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
function drawChart(activeStep) {
const c = chartContext;
c.clearRect(0, 0, 1200, 320);
c.fillStyle = '#f2e8d8';
c.fillRect(0, 0, 1200, 320);
c.strokeStyle = '#44382e';
c.lineWidth = 5;
c.strokeRect(9, 9, 1182, 302);
c.fillStyle = '#3e342c';
c.font = '700 42px sans-serif';
c.fillText('KNIT ALONG', 45, 88);
c.fillStyle = '#9b6048';
c.font = '700 20px sans-serif';
c.fillText('4-STEP SCARF SEQUENCE', 47, 129);
c.strokeStyle = '#bda991';
c.lineWidth = 3;
c.beginPath();
c.moveTo(45, 157);
c.lineTo(1150, 157);
c.stroke();
const names = ['KNIT', 'PURL', 'KNIT 2', 'BIND OFF'];
const codes = ['K1 × 2', 'P1 × 2', 'K2 × 3', 'FINISH'];
const startX = 315;
const cellW = 195;
for (let i = 0; i < 4; i++) {
const x = startX + i * 210;
const done = activeStep > i || activeStep === 3;
const active = activeStep === i && activeStep < 3;
c.fillStyle = active ? '#e8b08f' : done ? '#dce2d5' : '#fffaf0';
c.fillRect(x, 43, cellW, 228);
c.strokeStyle = active ? '#b95437' : '#b7a38b';
c.lineWidth = active ? 6 : 3;
c.strokeRect(x, 43, cellW, 228);
c.fillStyle = '#4b4037';
c.font = '700 25px sans-serif';
c.fillText(names[i], x + 16, 77);
c.fillStyle = '#9b6048';
c.font = '700 17px sans-serif';
c.fillText(codes[i], x + 16, 247);
c.strokeStyle = '#b95539';
c.fillStyle = '#b95539';
c.lineWidth = 5;
const gx = x + 27;
const gy = 103;
if (i === 0 || i === 2) {
for (let j = 0; j < (i === 0 ? 3 : 4); j++) {
const xx = gx + j * 38;
c.beginPath();
c.moveTo(xx, gy + 75);
c.lineTo(xx + 13, gy);
c.lineTo(xx + 26, gy + 75);
c.stroke();
}
} else if (i === 1) {
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 4; col++) {
c.fillRect(gx + col * 38, gy + row * 21, 25, 12);
}
}
} else {
c.beginPath();
c.moveTo(gx, gy + 13);
c.lineTo(gx + 138, gy + 13);
c.stroke();
for (let j = 0; j < 4; j++) {
c.beginPath();
c.arc(gx + 18 + j * 34, gy + 31, 11, 0, Math.PI * 2);
c.stroke();
}
}
}
chartTexture.needsUpdate = true;
}
drawChart(-1);
box(scene, 4.22, 1.35, 0.14, 0.94, 3.28, -3.48, mats.woodDark);
box(scene, 4.08, 1.22, 0.055, 0.94, 3.28, -3.385,
new THREE.MeshStandardMaterial({ map: chartTexture, roughness: 0.9 }));
function createChair() {
const chair = new THREE.Group();
chair.position.x = 1.2;
scene.add(chair);
for (const x of [-0.65, 0.65]) {
for (const z of [-0.7, 0.78]) {
box(chair, 0.18, 1.12, 0.18, x, 0.56, z, mats.woodDark);
}
}
box(chair, 1.78, 0.18, 1.83, 0, 1.08, 0.08, mats.wood);
box(chair, 1.61, 0.23, 1.68, 0, 1.29, 0.08, mats.sage);
box(chair, 1.5, 0.055, 1.57, 0, 1.435, 0.08, mats.sageLight);
for (const x of [-0.68, 0.68]) {
box(chair, 0.16, 1.92, 0.16, x, 1.98, -0.78, mats.wood);
}
box(chair, 1.47, 0.17, 0.17, 0, 1.69, -0.78, mats.woodLight);
box(chair, 1.47, 0.17, 0.17, 0, 2.18, -0.78, mats.woodLight);
box(chair, 1.47, 0.17, 0.17, 0, 2.67, -0.78, mats.woodLight);
const pillow = box(chair, 1.15, 0.72, 0.23, 0, 2.08, -0.58, mats.cream, [-0.1, 0, 0]);
pillow.scale.set(1, 1, 0.9);
}
createChair();
const basketX = -2.35;
const basketZ = 0.05;
function createBasket() {
const basket = new THREE.Group();
basket.position.set(basketX, 0, basketZ);
scene.add(basket);
box(basket, 1.36, 0.14, 1.05, 0, 0.13, 0, mats.wickerDark);
for (let i = 0; i < 7; i++) {
const x = -0.54 + i * 0.18;
const mat = i % 2 ? mats.wicker : mats.wickerLight;
box(basket, 0.095, 0.78, 0.09, x, 0.51, 0.49, mat);
box(basket, 0.095, 0.78, 0.09, x, 0.51, -0.49, mat);
}
for (let i = 0; i < 5; i++) {
const z = -0.4 + i * 0.2;
const mat = i % 2 ? mats.wicker : mats.wickerLight;
box(basket, 0.09, 0.77, 0.095, -0.64, 0.51, z, mat);
box(basket, 0.09, 0.77, 0.095, 0.64, 0.51, z, mat);
}
for (const y of [0.27, 0.55, 0.82]) {
box(basket, 1.31, 0.075, 0.09, 0, y, 0.49, mats.wickerDark);
box(basket, 1.31, 0.075, 0.09, 0, y, -0.49, mats.wickerDark);
box(basket, 0.09, 0.075, 1.01, -0.64, y, 0, mats.wickerDark);
box(basket, 0.09, 0.075, 1.01, 0.64, y, 0, mats.wickerDark);
}
// Voxel arch handle at the back of the basket.
for (let i = 0; i <= 10; i++) {
const t = i / 10;
const x = -0.54 + t * 1.08;
const y = 0.91 + Math.sin(t * Math.PI) * 0.51;
box(basket, 0.12, 0.12, 0.11, x, y, -0.43, mats.wickerDark);
}
}
createBasket();
const yarnBall = new THREE.Group();
yarnBall.position.set(basketX, 0.72, basketZ);
scene.add(yarnBall);
const corePositions = [];
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
if (x * x + y * y + z * z <= 2.25) {
corePositions.push([x * 0.235, y * 0.22, z * 0.235]);
}
}
}
}
const yarnCore = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.25, 0.25, 0.25),
mats.yarn,
corePositions.length
);
const yarnDummy = new THREE.Object3D();
corePositions.forEach((p, i) => {
yarnDummy.position.set(p[0], p[1], p[2]);
yarnDummy.rotation.set(i * 0.17, i * 0.23, i * 0.11);
yarnDummy.scale.setScalar(0.9 + (i % 4) * 0.04);
yarnDummy.updateMatrix();
yarnCore.setMatrixAt(i, yarnDummy.matrix);
yarnCore.setColorAt(i, new THREE.Color(i % 3 === 0 ? 0xd56d47 : i % 3 === 1 ? 0xb94f34 : 0x9b402d));
});
yarnCore.instanceMatrix.needsUpdate = true;
yarnCore.instanceColor.needsUpdate = true;
yarnCore.castShadow = true;
yarnCore.receiveShadow = true;
yarnBall.add(yarnCore);
const windingCount = 48;
const yarnWinding = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.135, 0.065, 0.08),
mats.yarnLight,
windingCount
);
let windingIndex = 0;
for (let ring = 0; ring < 4; ring++) {
const z = (ring - 1.5) * 0.16;
const radius = 0.38 - Math.abs(ring - 1.5) * 0.035;
for (let j = 0; j < 12; j++) {
const a = (j / 12) * Math.PI * 2 + ring * 0.12;
yarnDummy.position.set(Math.cos(a) * radius, Math.sin(a) * radius * 0.72, z);
yarnDummy.rotation.set(0, 0, a);
yarnDummy.scale.setScalar(1);
yarnDummy.updateMatrix();
yarnWinding.setMatrixAt(windingIndex, yarnDummy.matrix);
yarnWinding.setColorAt(
windingIndex,
new THREE.Color(windingIndex % 4 === 0 ? 0xe08056 : windingIndex % 4 === 1 ? 0xc55b3c : 0xa84631)
);
windingIndex++;
}
}
yarnWinding.instanceMatrix.needsUpdate = true;
yarnWinding.instanceColor.needsUpdate = true;
yarnWinding.castShadow = true;
yarnBall.add(yarnWinding);
// Loose yarn coil in the basket; it contracts as the supply is used.
const looseCoil = new THREE.Group();
looseCoil.position.set(basketX, 0.43, basketZ);
scene.add(looseCoil);
const coilMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.14, 0.055, 0.07),
mats.yarn,
20
);
for (let i = 0; i < 20; i++) {
const a = (i / 20) * Math.PI * 2;
yarnDummy.position.set(Math.cos(a) * 0.43, (i % 2) * 0.035, Math.sin(a) * 0.29);
yarnDummy.rotation.set(0, a + Math.PI / 2, 0);
yarnDummy.scale.setScalar(1);
yarnDummy.updateMatrix();
coilMesh.setMatrixAt(i, yarnDummy.matrix);
}
coilMesh.instanceMatrix.needsUpdate = true;
coilMesh.castShadow = true;
looseCoil.add(coilMesh);
// Dynamic scarf fabric.
const BASE_ROWS = 14;
const MAX_ROWS = 22;
const COLS = 11;
const TOTAL_STITCHES = BASE_ROWS * COLS;
function scarfPath(i) {
if (i < 9) return { y: 1.49, z: -0.62 + i * 0.16 };
if (i === 9) return { y: 1.47, z: 0.82 };
if (i === 10) return { y: 1.34, z: 0.92 };
if (i === 11) return { y: 1.18, z: 0.98 };
return { y: 1.04 - (i - 12) * 0.2, z: 1.02 };
}
const scarfMain = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.104, 0.075, 0.145),
material(0xffffff),
MAX_ROWS * COLS
);
const scarfCaps = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.034, 0.028, 0.115),
material(0xffffff),
MAX_ROWS * COLS
);
scarfMain.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scarfCaps.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scarfMain.castShadow = true;
scarfMain.receiveShadow = true;
scarfCaps.castShadow = false;
scarfMain.frustumCulled = false;
scarfCaps.frustumCulled = false;
scene.add(scarfMain, scarfCaps);
let instanceIndex = 0;
for (let row = 0; row < MAX_ROWS; row++) {
const stripe = row === 5 || row === 6 || row === 16 || row === 17 || row === 21;
for (let col = 0; col < COLS; col++) {
const color = stripe ? 0xe6cda7 : (row + col) % 7 === 0 ? 0xb95338 : 0xc96543;
scarfMain.setColorAt(instanceIndex, new THREE.Color(color));
scarfCaps.setColorAt(instanceIndex, new THREE.Color(stripe ? 0xf0d9b5 : 0xd87a55));
instanceIndex++;
}
}
scarfMain.instanceColor.needsUpdate = true;
scarfCaps.instanceColor.needsUpdate = true;
const tasselGroup = new THREE.Group();
scene.add(tasselGroup);
for (let i = 0; i < COLS; i++) {
box(
tasselGroup,
0.035,
0.18,
0.035,
1.2 + (i - (COLS - 1) / 2) * 0.118,
0.13,
1.02,
i % 2 ? mats.cream : mats.rust
);
}
tasselGroup.visible = false;
const stitchDummy = new THREE.Object3D();
let lastScarfRows = -1;
function updateScarf(rowProgress) {
if (Math.abs(rowProgress - lastScarfRows) < 0.001) return;
lastScarfRows = rowProgress;
let index = 0;
for (let row = 0; row < MAX_ROWS; row++) {
const reveal = THREE.MathUtils.clamp(rowProgress - row, 0, 1);
const path = scarfPath(row);
for (let col = 0; col < COLS; col++) {
const x = 1.2 + (col - (COLS - 1) / 2) * 0.123;
stitchDummy.position.set(x, path.y, path.z);
stitchDummy.rotation.set(0, 0, 0);
stitchDummy.scale.set(1, Math.max(0.001, reveal), 1);
stitchDummy.updateMatrix();
scarfMain.setMatrixAt(index, stitchDummy.matrix);
stitchDummy.position.set(x, path.y + 0.048 * reveal, path.z);
stitchDummy.scale.set(1, Math.max(0.001, reveal), 1);
stitchDummy.updateMatrix();
scarfCaps.setMatrixAt(index, stitchDummy.matrix);
index++;
}
}
scarfMain.instanceMatrix.needsUpdate = true;
scarfCaps.instanceMatrix.needsUpdate = true;
const tasselReveal = THREE.MathUtils.clamp((rowProgress - (MAX_ROWS - 1)) * 4, 0, 1);
tasselGroup.visible = tasselReveal > 0;
tasselGroup.scale.y = Math.max(0.001, tasselReveal);
}
updateScarf(BASE_ROWS);
// A pair of chunky wooden knitting needles follows the live edge.
const needleGroup = new THREE.Group();
scene.add(needleGroup);
box(needleGroup, 1.95, 0.055, 0.055, -0.08, 0, 0, mats.woodLight, [0, 0.18, 0]);
box(needleGroup, 1.82, 0.052, 0.052, 0.11, 0.018, 0.08, mats.wood, [0, -0.42, 0]);
box(needleGroup, 0.18, 0.068, 0.068, 0.94, 0, 0, mats.woodLight, [0, 0.18, 0]);
box(needleGroup, 0.17, 0.065, 0.065, -1.09, 0.018, 0.08, mats.wood, [0, -0.42, 0]);
for (let i = 0; i < 6; i++) {
box(needleGroup, 0.075, 0.075, 0.09, -0.48 + i * 0.18, 0.06, 0.02, mats.yarn);
}
function updateNeedles(rowProgress, time) {
const activeIndex = THREE.MathUtils.clamp(Math.floor(rowProgress) - 1, 0, MAX_ROWS - 1);
const path = scarfPath(activeIndex);
needleGroup.position.set(1.2, path.y + 0.14, path.z - 0.03);
const motion = animation ? Math.sin(time * 24) * 0.012 : 0;
needleGroup.position.y += motion;
}
// The working yarn is a voxel chain following a loose curve to the needles.
const strandSegments = 30;
const strandMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.035, 1, 0.035),
mats.yarnLight,
strandSegments
);
strandMesh.frustumCulled = false;
strandMesh.castShadow = true;
scene.add(strandMesh);
const strandDummy = new THREE.Object3D();
const yAxis = new THREE.Vector3(0, 1, 0);
const strandCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(),
new THREE.Vector3(),
new THREE.Vector3(),
new THREE.Vector3(),
new THREE.Vector3()
], false, 'catmullrom', 0.35);
function updateStrand() {
const p0 = new THREE.Vector3(
basketX,
yarnBall.position.y + 0.31 * yarnBall.scale.y,
basketZ
);
const p1 = new THREE.Vector3(basketX + 0.34, 1.34, -0.17);
const p2 = new THREE.Vector3(-1.18, 0.16, 0.64);
const p3 = new THREE.Vector3(-0.12, 1.28, 1.25);
const p4 = needleGroup.position.clone().add(new THREE.Vector3(-0.18, 0.075, 0));
strandCurve.points[0].copy(p0);
strandCurve.points[1].copy(p1);
strandCurve.points[2].copy(p2);
strandCurve.points[3].copy(p3);
strandCurve.points[4].copy(p4);
const a = new THREE.Vector3();
const b = new THREE.Vector3();
const direction = new THREE.Vector3();
for (let i = 0; i < strandSegments; i++) {
const t0 = i / strandSegments;
const t1 = (i + 1) / strandSegments;
strandCurve.getPoint(t0, a);
strandCurve.getPoint(t1, b);
direction.subVectors(b, a);
const length = Math.max(0.001, direction.length());
strandDummy.position.copy(a).add(b).multiplyScalar(0.5);
strandDummy.quaternion.setFromUnitVectors(yAxis, direction.normalize());
strandDummy.scale.set(1, length, 1);
strandDummy.updateMatrix();
strandMesh.setMatrixAt(i, strandDummy.matrix);
}
strandMesh.instanceMatrix.needsUpdate = true;
}
// Custom orbit controls; no examples/addons are imported.
const target = new THREE.Vector3(-0.05, 1.25, 0.18);
let yaw = 0.64;
let pitch = 1.03;
let radius = 10.8;
let desiredYaw = yaw;
let desiredPitch = pitch;
let desiredRadius = radius;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function resetView() {
desiredYaw = 0.64;
desiredPitch = 1.03;
desiredRadius = 10.8;
}
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;
desiredYaw -= (event.clientX - lastPointerX) * 0.005;
desiredPitch = THREE.MathUtils.clamp(
desiredPitch - (event.clientY - lastPointerY) * 0.0045,
0.65,
1.38
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
const 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();
desiredRadius = THREE.MathUtils.clamp(desiredRadius + event.deltaY * 0.008, 7.1, 14.2);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', resetView);
document.querySelector('#reset-view').addEventListener('click', resetView);
function updateCamera() {
yaw += (desiredYaw - yaw) * 0.085;
pitch += (desiredPitch - pitch) * 0.085;
radius += (desiredRadius - radius) * 0.085;
camera.position.set(
target.x + radius * Math.sin(pitch) * Math.sin(yaw),
target.y + radius * Math.cos(pitch),
target.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
const STEP_ADDS = [2, 2, 3, 1];
const STEP_LABELS = ['Knitting K1 rows', 'Working the purl row', 'Adding the K2 rows', 'Binding off the edge'];
let currentStep = -1;
let rowProgress = BASE_ROWS;
let animation = null;
function refreshStepUI() {
stepEls.forEach((el, i) => {
el.classList.toggle('active', i === currentStep && currentStep >= 0 && currentStep < 3);
el.classList.toggle('done', i < currentStep || currentStep === 3);
});
drawChart(currentStep);
}
function startNextStep() {
if (animation) return;
if (currentStep >= 3) {
currentStep = -1;
rowProgress = BASE_ROWS;
updateScarf(rowProgress);
}
currentStep++;
const added = STEP_ADDS.slice(0, currentStep + 1).reduce((a, b) => a + b, 0);
const targetRows = BASE_ROWS + added;
animation = {
from: rowProgress,
to: targetRows,
start: performance.now(),
duration: currentStep === 3 ? 1400 : 1800
};
knitButton.disabled = true;
knitButton.textContent = 'Knitting…';
statusEl.textContent = `${STEP_LABELS[currentStep]} — adding ${STEP_ADDS[currentStep]} live rows`;
refreshStepUI();
}
knitButton.addEventListener('click', startNextStep);
window.addEventListener('keydown', (event) => {
if (event.code === 'Space' && document.activeElement !== knitButton) {
event.preventDefault();
startNextStep();
}
});
function updateYarnSupply() {
const progress = THREE.MathUtils.clamp((rowProgress - BASE_ROWS) / (MAX_ROWS - BASE_ROWS), 0, 1);
const remaining = Math.round(100 - progress * 48);
const scale = remaining / 100;
yarnBall.scale.set(scale, scale * 0.97, scale);
yarnBall.position.y = 0.72 - (1 - scale) * 0.12;
looseCoil.scale.set(0.72 + scale * 0.28, Math.max(0.25, scale), 0.72 + scale * 0.28);
yarnPercentEl.textContent = `${remaining}%`;
meterFillEl.style.width = `${remaining}%`;
}
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener('resize', onResize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
if (animation) {
const t = THREE.MathUtils.clamp((performance.now() - animation.start) / animation.duration, 0, 1);
const eased = t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
rowProgress = THREE.MathUtils.lerp(animation.from, animation.to, eased);
if (t >= 1) {
rowProgress = animation.to;
animation = null;
knitButton.disabled = false;
if (currentStep === 3) {
knitButton.textContent = 'Replay sequence';
statusEl.textContent = 'Scarf complete — the yarn ball and supply are now smaller';
refreshStepUI();
} else {
knitButton.textContent = 'Knit next step';
const remaining = Math.round(100 - ((rowProgress - BASE_ROWS) / (MAX_ROWS - BASE_ROWS)) * 48);
statusEl.textContent = `Step ${currentStep + 1} complete — ${remaining}% yarn remaining`;
}
}
}
updateScarf(rowProgress);
updateYarnSupply();
updateNeedles(rowProgress, time);
updateStrand();
updateCamera();
renderer.render(scene, camera);
}
refreshStepUI();
updateNeedles(rowProgress, 0);
updateStrand();
animate();
</script>
</body>
</html>
```