Prompt specification
Build or customize the Three.js 3D scene "Knee flexion with patella tracking" 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: Knee flexion with patella tracking
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/knee-flexion-with-patella-tracking-x1006
Scene Description Prompt:
Create a standalone HTML cutaway using Three.js from cdnjs: approximate knee flexion with the patella tracking the joint.
Working Three.js Starter (knee-flexion-with-patella-tracking-x1006):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Interactive Knee Flexion Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(12, 23, 32, 0.86);
--panel-border: rgba(164, 205, 220, 0.16);
--text: #edf7f8;
--muted: #91a8b2;
--accent: #67d4dc;
--accent-2: #bcecef;
--bone: #e7d7b5;
--muscle: #c85d62;
--tendon: #f1dfb0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, #193341 0%, #0b1720 44%, #050b10 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px);
background-size: 48px 48px;
mask-image: radial-gradient(circle at 50% 45%, black, transparent 80%);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.header {
position: fixed;
top: 28px;
left: 32px;
z-index: 5;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--accent);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 1px;
background: var(--accent);
box-shadow: 0 0 10px rgba(103, 212, 220, 0.7);
}
h1 {
margin: 0;
color: var(--text);
font-size: clamp(27px, 4vw, 48px);
font-weight: 560;
letter-spacing: -0.045em;
line-height: 1.02;
}
.subtitle {
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 10;
top: 28px;
right: 28px;
width: 310px;
padding: 19px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.panel-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.panel-title {
color: var(--text);
font-size: 12px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
}
.angle-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin-bottom: 13px;
}
.angle-value {
color: var(--text);
font-size: 45px;
font-weight: 470;
letter-spacing: -0.055em;
line-height: 0.95;
font-variant-numeric: tabular-nums;
}
.angle-value span {
color: var(--accent);
font-size: 23px;
vertical-align: top;
}
.phase {
padding-bottom: 4px;
color: var(--accent-2);
font-size: 11px;
font-weight: 650;
text-align: right;
}
input[type="range"] {
--p: 0%;
width: 100%;
height: 5px;
margin: 4px 0 10px;
appearance: none;
border-radius: 999px;
background: linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--p),
rgba(255, 255, 255, 0.13) var(--p),
rgba(255, 255, 255, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid #12242e;
border-radius: 50%;
background: var(--accent-2);
box-shadow: 0 0 0 1px rgba(103, 212, 220, 0.8),
0 0 14px rgba(103, 212, 220, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #12242e;
border-radius: 50%;
background: var(--accent-2);
box-shadow: 0 0 0 1px rgba(103, 212, 220, 0.8),
0 0 14px rgba(103, 212, 220, 0.45);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #6f8791;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 16px;
padding: 12px 0;
border-top: 1px solid rgba(255, 255, 255, 0.08);
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.metric-name {
color: var(--muted);
font-size: 11px;
}
.metric-value {
color: var(--text);
font-size: 13px;
font-weight: 650;
font-variant-numeric: tabular-nums;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
button {
min-height: 38px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 10px;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(103, 212, 220, 0.45);
background: rgba(103, 212, 220, 0.08);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(103, 212, 220, 0.3);
color: #d9fbfc;
background: rgba(103, 212, 220, 0.13);
}
.hint {
margin: 13px 0 0;
color: #718892;
font-size: 10px;
line-height: 1.5;
text-align: center;
}
.legend {
position: fixed;
z-index: 5;
bottom: 28px;
left: 32px;
display: flex;
flex-wrap: wrap;
gap: 15px;
max-width: 470px;
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 8px rgba(255, 255, 255, 0.08);
}
.swatch.bone {
background: var(--bone);
}
.swatch.muscle {
background: var(--muscle);
}
.swatch.tendon {
background: var(--tendon);
}
.swatch.track {
background: var(--accent);
box-shadow: 0 0 9px rgba(103, 212, 220, 0.65);
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.label {
position: absolute;
top: 0;
left: 0;
padding: 4px 8px;
border: 1px solid rgba(163, 213, 221, 0.2);
border-radius: 999px;
color: #d9edf0;
background: rgba(7, 16, 24, 0.76);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.035em;
white-space: nowrap;
will-change: transform;
transition: opacity 120ms ease;
}
.label::before {
content: "";
display: inline-block;
width: 4px;
height: 4px;
margin-right: 6px;
border-radius: 50%;
background: var(--accent);
vertical-align: 1px;
}
.labels-hidden .label {
opacity: 0;
}
.footer-note {
position: fixed;
right: 30px;
bottom: 29px;
z-index: 4;
color: #617781;
font-size: 9px;
letter-spacing: 0.05em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 780px) {
.header {
top: 18px;
left: 18px;
max-width: 270px;
}
h1 {
font-size: 29px;
}
.subtitle {
max-width: 235px;
font-size: 11px;
}
.panel {
top: auto;
right: 14px;
bottom: 14px;
left: 14px;
width: auto;
padding: 14px;
}
.panel-top {
margin-bottom: 10px;
}
.angle-value {
font-size: 34px;
}
.metric {
margin-top: 10px;
padding: 9px 0;
}
.buttons {
margin-top: 10px;
}
.legend,
.footer-note {
display: none;
}
.label {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive sagittal cutaway model of a human knee flexing"
></canvas>
<header class="header">
<div class="eyebrow">Kinematic anatomy</div>
<h1>Knee flexion<br>cutaway</h1>
<p class="subtitle">
An approximate sagittal model showing tibial rotation and inferior
patellar tracking through the femoral trochlear groove.
</p>
</header>
<aside class="panel" aria-label="Knee model controls">
<div class="panel-top">
<div class="panel-title">Motion control</div>
<div class="live-dot" aria-hidden="true"></div>
</div>
<div class="angle-row">
<div class="angle-value"><b id="angleValue">0</b><span>°</span></div>
<div class="phase" id="phase">Extension</div>
</div>
<input
id="angle"
type="range"
min="0"
max="140"
step="1"
value="0"
aria-label="Knee flexion angle"
>
<div class="range-labels">
<span>0° Extended</span>
<span>140° Flexed</span>
</div>
<div class="metric">
<div class="metric-name">Approx. patellar glide</div>
<div class="metric-value" id="glide">0.0 cm</div>
</div>
<div class="buttons">
<button class="primary" id="motionButton" type="button">Pause motion</button>
<button id="poseButton" type="button">Extend knee</button>
<button id="viewButton" type="button">Reset view</button>
<button id="labelsButton" type="button" aria-pressed="true">Hide labels</button>
</div>
<p class="hint">Drag to orbit · Scroll or pinch to zoom</p>
</aside>
<div id="labels" aria-hidden="true">
<div class="label" id="label-femur">Femur</div>
<div class="label" id="label-patella">Patella</div>
<div class="label" id="label-tibia">Tibia</div>
<div class="label" id="label-fibula">Fibula</div>
<div class="label" id="label-tendon">Quadriceps tendon</div>
</div>
<div class="legend" aria-hidden="true">
<div class="legend-item"><span class="swatch bone"></span>Cortical bone</div>
<div class="legend-item"><span class="swatch muscle"></span>Muscle</div>
<div class="legend-item"><span class="swatch tendon"></span>Tendon / ligament</div>
<div class="legend-item"><span class="swatch track"></span>Patellar tracking path</div>
</div>
<div class="footer-note">Illustrative model · not for clinical use</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const angleInput = document.querySelector("#angle");
const angleValue = document.querySelector("#angleValue");
const phaseOutput = document.querySelector("#phase");
const glideOutput = document.querySelector("#glide");
const motionButton = document.querySelector("#motionButton");
const poseButton = document.querySelector("#poseButton");
const viewButton = document.querySelector("#viewButton");
const labelsButton = document.querySelector("#labelsButton");
const labelsLayer = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, 0.18, 0);
const orbit = {
theta: 0.27,
phi: 1.49,
radius: 8.4
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
target.y + orbit.radius * Math.cos(orbit.phi),
target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc7e9f2, 0x291b1c, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3dc, 4.0);
keyLight.position.set(4, 7, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x66d9ea, 2.6);
rimLight.position.set(-5, 2, -4);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xffa6a0, 18, 13, 2);
warmFill.position.set(-3, -1, 4);
scene.add(warmFill);
const boneMaterial = new THREE.MeshStandardMaterial({
color: 0xe7d8b8,
roughness: 0.67,
metalness: 0.015
});
const boneEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x7f705c,
transparent: true,
opacity: 0.32
});
const marrowMaterial = new THREE.MeshStandardMaterial({
color: 0xad6f69,
roughness: 0.95,
side: THREE.DoubleSide
});
const cartilageMaterial = new THREE.MeshPhysicalMaterial({
color: 0x78cbd6,
roughness: 0.26,
metalness: 0.02,
transparent: true,
opacity: 0.78,
clearcoat: 0.45,
clearcoatRoughness: 0.28
});
const muscleMaterial = new THREE.MeshStandardMaterial({
color: 0xbd525a,
roughness: 0.83,
metalness: 0
});
const muscleEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x702f39,
transparent: true,
opacity: 0.48
});
const tendonMaterial = new THREE.MeshStandardMaterial({
color: 0xf1dfb0,
roughness: 0.64
});
const ligamentMaterial = new THREE.MeshStandardMaterial({
color: 0xe7c68d,
roughness: 0.7
});
const meniscusMaterial = new THREE.MeshStandardMaterial({
color: 0x34434b,
roughness: 0.76
});
const ghostMaterial = new THREE.MeshPhysicalMaterial({
color: 0xe79091,
roughness: 0.55,
transparent: true,
opacity: 0.095,
depthWrite: false,
side: THREE.DoubleSide
});
const Z_AXIS = new THREE.Vector3(0, 0, 1);
function smoothShape(points, tension = 0.2) {
const curve = new THREE.CatmullRomCurve3(
points.map((p) => new THREE.Vector3(p[0], p[1], 0)),
true,
"catmullrom",
tension
);
const sampled = curve.getPoints(88);
const shape = new THREE.Shape();
shape.moveTo(sampled[0].x, sampled[0].y);
for (let i = 1; i < sampled.length; i++) {
shape.lineTo(sampled[i].x, sampled[i].y);
}
shape.closePath();
return shape;
}
function extrudedMesh(shape, depth, material, edgeMaterial = null) {
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelThickness: 0.025,
bevelSize: 0.025,
bevelOffset: 0,
bevelSegments: 3,
curveSegments: 18
});
geometry.translate(0, 0, -depth / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (edgeMaterial) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 28),
edgeMaterial
);
mesh.add(edges);
}
return mesh;
}
function flatMesh(shape, material) {
return new THREE.Mesh(new THREE.ShapeGeometry(shape), material);
}
// --- Femur -----------------------------------------------------------
const femurGroup = new THREE.Group();
scene.add(femurGroup);
const femurShape = smoothShape([
[-0.43, 3.38],
[0.34, 3.33],
[0.40, 1.28],
[0.30, 0.79],
[0.48, 0.51],
[0.60, 0.24],
[0.54, -0.03],
[0.31, -0.12],
[0.11, -0.05],
[-0.08, 0.22],
[-0.34, 0.40],
[-0.49, 0.78],
[-0.53, 1.42]
]);
const femur = extrudedMesh(
femurShape,
0.56,
boneMaterial,
boneEdgeMaterial
);
femurGroup.add(femur);
const femurMarrowShape = smoothShape([
[-0.12, 3.10],
[0.10, 3.07],
[0.12, 1.36],
[0.20, 0.95],
[0.12, 0.52],
[-0.12, 0.58],
[-0.18, 1.20]
]);
const femurMarrow = flatMesh(femurMarrowShape, marrowMaterial);
femurMarrow.position.z = 0.322;
femurGroup.add(femurMarrow);
const femoralCartilage = new THREE.Mesh(
new THREE.SphereGeometry(1, 40, 28),
cartilageMaterial
);
femoralCartilage.position.set(0.33, 0.12, 0.08);
femoralCartilage.scale.set(0.27, 0.42, 0.43);
femoralCartilage.castShadow = true;
femurGroup.add(femoralCartilage);
// --- Muscles and ghosted soft tissue --------------------------------
const thighGhost = extrudedMesh(
smoothShape([
[-0.72, 3.40],
[0.82, 3.28],
[1.02, 1.30],
[0.83, 0.45],
[0.43, 0.10],
[-0.43, 0.43],
[-0.75, 1.48]
]),
0.10,
ghostMaterial
);
thighGhost.position.z = -0.61;
thighGhost.renderOrder = -2;
femurGroup.add(thighGhost);
const quadriceps = extrudedMesh(
smoothShape([
[0.23, 2.58],
[0.66, 2.52],
[0.89, 1.85],
[0.82, 1.00],
[0.59, 0.70],
[0.32, 0.89],
[0.26, 1.66]
]),
0.34,
muscleMaterial,
muscleEdgeMaterial
);
quadriceps.position.z = -0.39;
femurGroup.add(quadriceps);
const hamstring = extrudedMesh(
smoothShape([
[-0.50, 2.20],
[-0.21, 2.06],
[-0.10, 1.23],
[-0.30, 0.51],
[-0.60, 0.62],
[-0.73, 1.44]
]),
0.30,
muscleMaterial,
muscleEdgeMaterial
);
hamstring.position.z = -0.37;
femurGroup.add(hamstring);
// --- Tibia and fibula ------------------------------------------------
const tibiaGroup = new THREE.Group();
scene.add(tibiaGroup);
const tibiaShape = smoothShape([
[-0.36, -0.10],
[0.32, -0.10],
[0.46, -0.19],
[0.42, -0.39],
[0.22, -0.56],
[0.18, -1.00],
[0.15, -2.55],
[-0.24, -2.58],
[-0.31, -1.15],
[-0.44, -0.50]
]);
const tibia = extrudedMesh(
tibiaShape,
0.56,
boneMaterial,
boneEdgeMaterial
);
tibiaGroup.add(tibia);
const tibiaMarrowShape = smoothShape([
[-0.10, -0.46],
[0.09, -0.46],
[0.08, -2.34],
[-0.09, -2.36],
[-0.12, -1.05]
]);
const tibiaMarrow = flatMesh(tibiaMarrowShape, marrowMaterial);
tibiaMarrow.position.z = 0.322;
tibiaGroup.add(tibiaMarrow);
const tibialCartilage = new THREE.Mesh(
new THREE.SphereGeometry(1, 40, 24),
cartilageMaterial
);
tibialCartilage.position.set(0, -0.075, 0.06);
tibialCartilage.scale.set(0.47, 0.075, 0.41);
tibialCartilage.castShadow = true;
tibiaGroup.add(tibialCartilage);
const meniscusShape = smoothShape([
[-0.42, -0.02],
[0.39, -0.03],
[0.33, -0.15],
[-0.35, -0.14]
]);
for (const z of [-0.25, 0.23]) {
const meniscus = extrudedMesh(
meniscusShape,
0.12,
meniscusMaterial
);
meniscus.position.z = z;
tibiaGroup.add(meniscus);
}
const fibulaShape = smoothShape([
[-0.73, -0.16],
[-0.52, -0.13],
[-0.49, -0.60],
[-0.55, -2.34],
[-0.72, -2.31],
[-0.80, -0.65]
]);
const fibula = extrudedMesh(
fibulaShape,
0.34,
boneMaterial,
boneEdgeMaterial
);
fibula.position.z = -0.04;
tibiaGroup.add(fibula);
const lowerLegGhost = extrudedMesh(
smoothShape([
[-0.63, -0.03],
[0.48, -0.03],
[0.43, -0.78],
[0.27, -2.66],
[-0.44, -2.69],
[-0.70, -1.23]
]),
0.10,
ghostMaterial
);
lowerLegGhost.position.z = -0.55;
lowerLegGhost.renderOrder = -2;
tibiaGroup.add(lowerLegGhost);
const calf = extrudedMesh(
smoothShape([
[-0.41, -0.52],
[0.05, -0.59],
[0.08, -1.45],
[-0.03, -2.34],
[-0.31, -2.39],
[-0.49, -1.25]
]),
0.30,
muscleMaterial,
muscleEdgeMaterial
);
calf.position.z = -0.35;
tibiaGroup.add(calf);
// --- Patella ---------------------------------------------------------
const patellaGroup = new THREE.Group();
scene.add(patellaGroup);
const patellaShape = smoothShape([
[-0.18, -0.22],
[0.16, -0.20],
[0.27, -0.05],
[0.22, 0.20],
[0.04, 0.30],
[-0.16, 0.17]
]);
const patella = extrudedMesh(
patellaShape,
0.28,
boneMaterial,
boneEdgeMaterial
);
patellaGroup.add(patella);
const patellarBack = new THREE.Mesh(
new THREE.SphereGeometry(1, 30, 20),
cartilageMaterial
);
patellarBack.position.set(-0.15, 0.02, 0);
patellarBack.scale.set(0.085, 0.25, 0.19);
patellaGroup.add(patellarBack);
// --- Tendons and ligaments ------------------------------------------
function makeLink(radius, material) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 16),
material
);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
const quadricepsTendon = makeLink(0.065, tendonMaterial);
const patellarTendon = makeLink(0.056, tendonMaterial);
const acl = makeLink(0.052, ligamentMaterial);
const pcl = makeLink(0.048, ligamentMaterial);
const yAxis = new THREE.Vector3(0, 1, 0);
function setLink(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
yAxis,
direction.clone().normalize()
);
mesh.scale.set(1, length, 1);
}
// --- Patellar tracking model ----------------------------------------
const trackKeys = [
{ angle: 0, x: 0.73, y: 0.18, rotation: -0.05 },
{ angle: 45, x: 0.61, y: -0.04, rotation: -0.22 },
{ angle: 90, x: 0.57, y: -0.25, rotation: -0.42 },
{ angle: 140, x: 0.69, y: -0.48, rotation: -0.65 }
];
function smoothStep(x) {
return x * x * (3 - 2 * x);
}
function sampleTrack(angle) {
let index = 0;
while (
index < trackKeys.length - 2 &&
angle > trackKeys[index + 1].angle
) {
index++;
}
const a = trackKeys[index];
const b = trackKeys[index + 1];
const t = smoothStep(
THREE.MathUtils.clamp(
(angle - a.angle) / (b.angle - a.angle),
0,
1
)
);
return {
x: THREE.MathUtils.lerp(a.x, b.x, t),
y: THREE.MathUtils.lerp(a.y, b.y, t),
rotation: THREE.MathUtils.lerp(a.rotation, b.rotation, t)
};
}
const pathPoints = [];
for (let a = 0; a <= 140; a += 4) {
const p = sampleTrack(a);
pathPoints.push(new THREE.Vector3(p.x, p.y, 0.43));
}
const trackingPath = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(pathPoints),
new THREE.LineDashedMaterial({
color: 0x67d4dc,
transparent: true,
opacity: 0.64,
dashSize: 0.075,
gapSize: 0.055,
depthWrite: false
})
);
trackingPath.computeLineDistances();
trackingPath.renderOrder = 3;
scene.add(trackingPath);
// --- Labels -----------------------------------------------------------
const labelElements = {
femur: document.querySelector("#label-femur"),
patella: document.querySelector("#label-patella"),
tibia: document.querySelector("#label-tibia"),
fibula: document.querySelector("#label-fibula"),
tendon: document.querySelector("#label-tendon")
};
const labelOffsets = {
femur: [30, -4],
patella: [38, 4],
tibia: [-34, 4],
fibula: [-30, 4],
tendon: [35, -4]
};
function projectLabel(element, worldPoint, offset) {
const projected = worldPoint.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
if (!visible) {
element.style.opacity = "0";
return;
}
const rect = canvas.getBoundingClientRect();
const x =
rect.left +
(projected.x * 0.5 + 0.5) * rect.width +
offset[0];
const y =
rect.top +
(-projected.y * 0.5 + 0.5) * rect.height +
offset[1];
element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -50%)`;
}
// --- Pose update -----------------------------------------------------
let currentAngle = 0;
let animationActive = true;
let motionClock = 0;
function tibiaPoint(x, y, z = 0) {
return new THREE.Vector3(x, y, z).applyAxisAngle(
Z_AXIS,
tibiaGroup.rotation.z
);
}
function patellaPoint(x, y, z = 0) {
return new THREE.Vector3(x, y, z)
.applyAxisAngle(Z_AXIS, patellaGroup.rotation.z)
.add(patellaGroup.position);
}
function updatePose(angle) {
const radians = THREE.MathUtils.degToRad(angle);
tibiaGroup.rotation.z = -radians;
const track = sampleTrack(angle);
patellaGroup.position.set(track.x, track.y, 0.10);
patellaGroup.rotation.z = track.rotation;
const quadricepsAnchor = new THREE.Vector3(0.57, 0.97, 0.30);
const patellaSuperior = patellaPoint(0.00, 0.25, 0.29);
const patellaInferior = patellaPoint(0.00, -0.22, 0.29);
const tibialTuberosity = tibiaPoint(0.46, -0.58, 0.29);
setLink(quadricepsTendon, quadricepsAnchor, patellaSuperior);
setLink(patellarTendon, patellaInferior, tibialTuberosity);
const aclFemoral = new THREE.Vector3(-0.13, 0.10, 0.13);
const aclTibial = tibiaPoint(0.12, -0.48, 0.13);
const pclFemoral = new THREE.Vector3(0.17, -0.02, -0.11);
const pclTibial = tibiaPoint(-0.12, -0.47, -0.11);
setLink(acl, aclFemoral, aclTibial);
setLink(pcl, pclFemoral, pclTibial);
angleValue.textContent = Math.round(angle);
angleInput.value = Math.round(angle);
angleInput.style.setProperty("--p", `${(angle / 140) * 100}%`);
let phase = "Extension";
if (angle >= 15) phase = "Early flexion";
if (angle >= 45) phase = "Mid-flexion";
if (angle >= 90) phase = "Deep flexion";
if (angle >= 125) phase = "Full flexion";
phaseOutput.textContent = phase;
const glide = 5.4 * (1 - Math.exp(-angle / 60));
glideOutput.textContent = `${glide.toFixed(1)} cm`;
const tendonMidpoint = new THREE.Vector3()
.addVectors(quadricepsAnchor, patellaSuperior)
.multiplyScalar(0.5);
projectLabel(
labelElements.femur,
new THREE.Vector3(0.02, 1.72, 0.37),
labelOffsets.femur
);
projectLabel(
labelElements.patella,
patellaGroup.position.clone().add(
new THREE.Vector3(0.05, 0.12, 0.26)
),
labelOffsets.patella
);
projectLabel(
labelElements.tibia,
tibiaPoint(-0.04, -1.42, 0.37),
labelOffsets.tibia
);
projectLabel(
labelElements.fibula,
tibiaPoint(-0.64, -1.25, 0.30),
labelOffsets.fibula
);
projectLabel(
labelElements.tendon,
tendonMidpoint,
labelOffsets.tendon
);
}
// --- UI --------------------------------------------------------------
angleInput.addEventListener("input", () => {
animationActive = false;
motionButton.textContent = "Resume motion";
currentAngle = Number(angleInput.value);
});
motionButton.addEventListener("click", () => {
animationActive = !animationActive;
motionButton.textContent = animationActive
? "Pause motion"
: "Resume motion";
});
poseButton.addEventListener("click", () => {
animationActive = false;
motionButton.textContent = "Resume motion";
currentAngle = 0;
});
viewButton.addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
labelsButton.addEventListener("click", () => {
const hidden = labelsLayer.classList.toggle("labels-hidden");
labelsButton.textContent = hidden ? "Show labels" : "Hide labels";
labelsButton.setAttribute("aria-pressed", String(!hidden));
});
function resetCamera() {
orbit.theta = 0.27;
orbit.phi = 1.49;
orbit.radius = 8.4;
updateCamera();
}
// --- Custom camera controls -----------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.006,
0.38,
2.72
);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
4.5,
14
);
},
{ passive: false }
);
// Basic two-pointer pinch zoom.
const activePointers = new Map();
let pinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (pinchDistance / distance),
4.5,
14
);
}
pinchDistance = distance;
}
});
function removePointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size < 2) pinchDistance = 0;
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
// --- Resize and render loop -----------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
function render(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (animationActive) {
motionClock += delta;
currentAngle = 70 - 70 * Math.cos(motionClock * 0.55);
}
updateCamera();
updatePose(currentAngle);
renderer.render(scene, camera);
requestAnimationFrame(render);
}
updatePose(0);
requestAnimationFrame(render);
</script>
</body>
</html>
```