Prompt specification
Build or customize the Three.js 3D scene "Balance Window Timing Challenge" 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: Balance Window Timing Challenge
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/balance-window-timing-challenge-x3656
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts skateboard balance and shifting support. Create a fictional balance game with bounded lean control and a visible stability margin. Score only the declared toy rule and provide an immediate safe visual reset. Voxel style.
Working Three.js Starter (balance-window-timing-challenge-x3656):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Block Balance — Voxel Support Game</title>
<style>
:root {
--ink: #14202a;
--panel: rgba(17, 31, 43, 0.94);
--cream: #fff8e7;
--green: #55db73;
--green-dark: #218847;
--orange: #ff9f43;
--amber: #ffd166;
--blue: #73d8ff;
--red: #ef5b5b;
--shadow: #0b131b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #99d7e5;
color: var(--cream);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
user-select: none;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
position: absolute;
pointer-events: auto;
background: var(--panel);
border: 3px solid var(--ink);
box-shadow: 6px 6px 0 var(--shadow);
padding: 12px 14px;
}
.brand {
top: 18px;
left: 18px;
width: 300px;
}
.brand h1 {
margin: 0;
color: var(--orange);
font-size: 25px;
line-height: 0.95;
letter-spacing: -1px;
text-shadow: 3px 3px 0 var(--ink);
}
.brand p {
margin: 7px 0 0;
color: var(--blue);
font-size: 11px;
letter-spacing: 1px;
}
.score-panel {
top: 18px;
right: 18px;
width: 172px;
text-align: center;
}
.score-label {
color: var(--amber);
font-size: 11px;
letter-spacing: 2px;
}
#score {
color: var(--cream);
font-size: 54px;
line-height: 1;
font-weight: 900;
text-shadow: 4px 4px 0 var(--ink);
}
.score-pop {
animation: scorePop 280ms steps(3, end);
}
@keyframes scorePop {
0% { transform: scale(1); }
50% { transform: scale(1.12); background: #24583a; }
100% { transform: scale(1); }
}
.rule-panel {
top: 111px;
left: 18px;
width: 390px;
border-left: 8px solid var(--green);
}
.rule-title {
color: var(--green);
font-size: 12px;
letter-spacing: 1px;
font-weight: bold;
}
.rule-copy {
margin-top: 7px;
font-size: 13px;
line-height: 1.45;
}
.rule-copy strong {
color: var(--amber);
}
.rule-note {
margin-top: 8px;
padding-top: 8px;
border-top: 2px solid #314657;
color: #afc3cf;
font-size: 10px;
line-height: 1.4;
}
.margin-panel {
left: 50%;
bottom: 20px;
width: 430px;
transform: translateX(-50%);
}
.margin-heading {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
font-size: 11px;
letter-spacing: 1px;
}
#marginState {
padding: 4px 7px;
border: 2px solid var(--ink);
background: var(--red);
color: white;
font-weight: bold;
font-size: 10px;
}
#marginState.safe {
background: var(--green);
color: var(--ink);
}
.lane-gauge {
position: relative;
height: 25px;
margin-top: 9px;
overflow: hidden;
background:
linear-gradient(
to right,
var(--red) 0%,
var(--red) 31%,
var(--amber) 31%,
var(--amber) 36.5%,
var(--green) 36.5%,
var(--green) 63.5%,
var(--amber) 63.5%,
var(--amber) 69%,
var(--red) 69%,
var(--red) 100%
);
border: 3px solid var(--ink);
}
.support-needle {
position: absolute;
top: 2px;
bottom: 2px;
width: 9px;
left: 50%;
transform: translateX(-50%);
background: var(--orange);
border: 2px solid var(--cream);
box-shadow: 2px 2px 0 var(--ink);
transition: left 50ms linear;
}
.command-needle {
position: absolute;
top: -4px;
bottom: -4px;
width: 3px;
left: 50%;
transform: translateX(-50%);
background: white;
box-shadow: 1px 1px 0 var(--ink);
transition: left 50ms linear;
}
.gauge-footer {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 7px;
font-size: 10px;
color: #bdd0dc;
}
.point-progress {
height: 9px;
margin-top: 9px;
overflow: hidden;
border: 2px solid var(--ink);
background: #071019;
}
#pointFill {
width: 0;
height: 100%;
background: var(--green);
}
.controls {
right: 18px;
bottom: 20px;
display: flex;
gap: 9px;
align-items: flex-end;
}
.control-button {
width: 62px;
height: 62px;
border: 3px solid var(--ink);
box-shadow: 4px 4px 0 var(--shadow);
background: #29485d;
color: white;
font: 900 23px ui-monospace, monospace;
cursor: pointer;
touch-action: none;
}
.control-button:active,
.control-button.active {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 var(--shadow);
background: var(--green-dark);
}
.control-button.reset {
width: 76px;
height: 46px;
background: var(--orange);
color: var(--ink);
font-size: 12px;
}
.help-panel {
left: 18px;
bottom: 20px;
width: 255px;
color: #bed0dc;
font-size: 10px;
line-height: 1.55;
}
.help-panel b {
color: var(--cream);
}
#resetPulse {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
border: 0 solid var(--green);
opacity: 0;
}
#resetPulse.flash {
animation: resetFlash 550ms ease-out;
}
@keyframes resetFlash {
0% {
opacity: 0.9;
border-width: 18px;
}
100% {
opacity: 0;
border-width: 0;
}
}
#fallback {
position: fixed;
inset: 0;
z-index: 10;
display: none;
place-items: center;
padding: 30px;
background: var(--ink);
color: white;
text-align: center;
}
@media (max-width: 960px) {
.help-panel {
display: none;
}
.margin-panel {
width: 390px;
}
}
@media (max-width: 650px) {
.brand {
top: 12px;
left: 12px;
width: 180px;
padding: 9px 10px;
}
.brand h1 {
font-size: 18px;
}
.brand p {
font-size: 8px;
}
.score-panel {
top: 12px;
right: 12px;
width: 116px;
padding: 9px;
}
#score {
font-size: 38px;
}
.score-label {
font-size: 8px;
}
.rule-panel {
top: 83px;
left: 12px;
width: calc(100% - 24px);
padding: 9px 11px;
}
.rule-copy {
font-size: 11px;
}
.rule-note {
font-size: 9px;
}
.margin-panel {
bottom: 88px;
width: calc(100% - 24px);
padding: 9px 11px;
}
.controls {
right: 12px;
bottom: 14px;
}
.control-button {
width: 57px;
height: 57px;
}
.control-button.reset {
width: 68px;
height: 43px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel skateboard balance game"></canvas>
<div class="hud">
<section class="panel brand">
<h1>BLOCK BALANCE</h1>
<p>SHIFT THE SUPPORT // STAY IN GREEN</p>
</section>
<section class="panel score-panel" id="scorePanel">
<div class="score-label">RULE POINTS</div>
<div id="score" aria-live="polite">0</div>
</section>
<section class="panel rule-panel">
<div class="rule-title">TOY RULE — THE ONLY SCORING RULE</div>
<div class="rule-copy">
Earn <strong>+1 point</strong> for every complete full second while the
<strong>orange support marker</strong> stays inside the
<strong>green safety lane</strong>.
</div>
<div class="rule-note">
No penalties, bonuses, combos, or hidden points. Reset recenters the
board, support, and camera; it clears the current one-second hold but
keeps the score.
</div>
</section>
<section class="panel help-panel">
<b>A / D</b> or <b>← / →</b>: bounded lean command<br>
<b>R</b>: safe visual reset<br>
<b>Drag scene:</b> orbit camera<br>
<b>Mouse wheel:</b> zoom
</section>
<section class="panel margin-panel">
<div class="margin-heading">
<span>VISIBLE STABILITY MARGIN</span>
<span id="marginState">IN MARGIN</span>
</div>
<div class="lane-gauge" aria-hidden="true">
<div class="command-needle" id="commandNeedle"></div>
<div class="support-needle" id="supportNeedle"></div>
</div>
<div class="gauge-footer">
<span>ORANGE = SUPPORT</span>
<span>WHITE = LEAN COMMAND</span>
</div>
<div class="point-progress" aria-hidden="true">
<div id="pointFill"></div>
</div>
<div class="gauge-footer">
<span id="pointText">NEXT POINT: 1.0s</span>
<span id="leanText">LEAN: 0%</span>
</div>
</section>
<section class="panel controls">
<button class="control-button" id="leftButton" aria-label="Lean left">←</button>
<button class="control-button" id="rightButton" aria-label="Lean right">→</button>
<button class="control-button reset" id="resetButton">RESET</button>
</section>
</div>
<div id="resetPulse"></div>
<div id="fallback">
<div>
<h2>WebGL could not start.</h2>
<p>Please open this file in a browser with WebGL enabled.</p>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scoreElement = document.querySelector("#score");
const scorePanel = document.querySelector("#scorePanel");
const marginState = document.querySelector("#marginState");
const commandNeedle = document.querySelector("#commandNeedle");
const supportNeedle = document.querySelector("#supportNeedle");
const pointFill = document.querySelector("#pointFill");
const pointText = document.querySelector("#pointText");
const leanText = document.querySelector("#leanText");
const resetPulse = document.querySelector("#resetPulse");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
console.error(error);
document.querySelector("#fallback").style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x99d7e5);
scene.fog = new THREE.Fog(0x99d7e5, 12, 26);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemi = new THREE.HemisphereLight(0xe9fbff, 0x75644f, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0c9, 3.0);
sun.position.set(5, 10, 6);
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 = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.00035;
scene.add(sun);
const materialCache = new Map();
function material(color, emissive = 0x000000, emissiveIntensity = 0) {
const key = `${color}-${emissive}-${emissiveIntensity}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02,
flatShading: true,
emissive,
emissiveIntensity
})
);
}
return materialCache.get(key);
}
function box(
parent,
size,
position,
color,
options = {}
) {
const {
cast = true,
receive = true,
emissive = 0x000000,
emissiveIntensity = 0
} = options;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color, emissive, emissiveIntensity)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel environment
// -------------------------------------------------------------------------
box(scene, [30, 0.3, 30], [0, -0.48, 0], 0x8ac6b0, {
cast: false
});
box(scene, [6.35, 0.36, 6.85], [0, -0.18, 0], 0x344b59, {
cast: false
});
// Tiled voxel plaza.
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
const alternate = (x + z) % 2 === 0;
box(
scene,
[0.87, 0.05, 0.87],
[x * 0.88, 0.025, z * 0.88],
alternate ? 0x607783 : 0x536b77,
{ cast: false }
);
}
}
// The visible stability lane:
// green = scoring area, amber = transition, red = outside.
box(
scene,
[0.72, 0.035, 4.85],
[0, 0.065, 0],
0x43ca62,
{
cast: false,
emissive: 0x174d20,
emissiveIntensity: 0.22
}
);
box(
scene,
[0.23, 0.038, 4.85],
[-0.475, 0.067, 0],
0xf5b94b,
{ cast: false }
);
box(
scene,
[0.23, 0.038, 4.85],
[0.475, 0.067, 0],
0xf5b94b,
{ cast: false }
);
box(
scene,
[0.31, 0.039, 4.85],
[-0.745, 0.069, 0],
0xc7514e,
{ cast: false }
);
box(
scene,
[0.31, 0.039, 4.85],
[0.745, 0.069, 0],
0xc7514e,
{ cast: false }
);
// Lane boundary blocks.
for (const z of [-2.36, 2.36]) {
for (const x of [-0.36, 0.36]) {
box(
scene,
[0.13, 0.22, 0.13],
[x, 0.15, z],
0x8ef29d,
{ emissive: 0x225f2b, emissiveIntensity: 0.16 }
);
}
}
// Decorative voxel blocks around the platform.
const decor = [
[-2.65, 0.35, -2.55, 0.7, 0.7, 0.7, 0xe78b57],
[-2.65, 0.95, -2.55, 0.48, 0.5, 0.48, 0xffc767],
[2.62, 0.32, -2.58, 0.64, 0.64, 0.64, 0x70b7d1],
[2.62, 0.84, -2.58, 0.42, 0.42, 0.42, 0x9be4f4],
[-2.65, 0.28, 2.5, 0.56, 0.56, 0.56, 0x8c78cf],
[2.62, 0.42, 2.5, 0.84, 0.84, 0.84, 0xe56b73]
];
for (const [x, y, z, w, h, d, color] of decor) {
box(scene, [w, h, d], [x, y, z], color);
}
// Distant block skyline.
const skyline = [
[-5.4, 0.4, -5.2, 1.1, 1.6, 1.2, 0x73939f],
[-3.9, 0.25, -6.1, 0.9, 1.3, 0.9, 0x829eaa],
[3.8, 0.3, -6.0, 1.0, 1.4, 1.0, 0x6f929f],
[5.5, 0.5, -5.0, 1.3, 1.8, 1.2, 0x819ba5],
[-6.2, 0.25, 2.0, 0.8, 1.2, 0.8, 0x7599a4]
];
for (const [x, y, z, w, h, d, color] of skyline) {
box(scene, [w, h, d], [x, y, z], color);
}
// Simple block clouds.
const clouds = [
[-5.0, 5.8, -7.0],
[2.8, 6.4, -8.0],
[6.2, 5.3, -4.5]
];
for (const [x, y, z] of clouds) {
box(scene, [1.8, 0.55, 0.75], [x, y, z], 0xf5fbf2, { cast: false });
box(scene, [0.75, 0.8, 0.75], [x + 0.35, y + 0.42, z], 0xf5fbf2, {
cast: false
});
box(scene, [0.8, 0.55, 0.75], [x - 0.7, y - 0.1, z], 0xe7f5eb, {
cast: false
});
}
// -------------------------------------------------------------------------
// Skateboard
// -------------------------------------------------------------------------
const board = new THREE.Group();
scene.add(board);
const deckColors = [0x1b9aaa, 0x168c9a, 0x23a0ad];
// Segmented, tapered voxel deck.
for (let i = 0; i < 13; i++) {
const z = -1.62 + i * 0.27;
const taper = Math.min(1, (1.68 - Math.abs(z)) * 2.25);
const width = 0.92 + taper * 0.63;
box(
board,
[width, 0.14, 0.255],
[0, 0, z],
deckColors[i % deckColors.length]
);
box(
board,
[Math.max(0.64, width - 0.08), 0.026, 0.225],
[0, 0.084, z],
0x1b2730
);
}
// Raised voxel nose and tail caps.
box(board, [0.92, 0.18, 0.25], [0, 0.07, -1.73], 0xffb84f);
box(board, [0.92, 0.18, 0.25], [0, 0.07, 1.73], 0xffb84f);
// Trucks, axles, and square wheels.
for (const z of [-1.16, 1.16]) {
box(board, [0.13, 0.34, 0.15], [-0.48, -0.25, z], 0xb8c4c9);
box(board, [0.13, 0.34, 0.15], [0.48, -0.25, z], 0xb8c4c9);
box(board, [1.43, 0.09, 0.14], [0, -0.40, z], 0x66757d);
for (const x of [-0.79, 0.79]) {
box(board, [0.19, 0.32, 0.40], [x, -0.55, z], 0x20262b);
box(board, [0.205, 0.14, 0.15], [x, -0.55, z], 0xd1d8da);
}
}
// -------------------------------------------------------------------------
// Voxel rider
// -------------------------------------------------------------------------
const rider = new THREE.Group();
rider.position.y = 0.11;
board.add(rider);
const skin = 0xf2b58d;
const shirt = 0xff8c42;
const pants = 0x263b68;
box(rider, [0.27, 0.16, 0.47], [-0.18, 0.08, 0.06], 0x15232c);
box(rider, [0.27, 0.16, 0.47], [0.18, 0.08, 0.06], 0x15232c);
box(rider, [0.17, 0.49, 0.21], [-0.18, 0.37, 0.01], pants);
box(rider, [0.17, 0.49, 0.21], [0.18, 0.37, 0.01], pants);
box(rider, [0.64, 0.65, 0.39], [0, 0.91, 0], shirt);
box(rider, [0.67, 0.16, 0.42], [0, 0.65, 0], 0xe36e30);
// Small shirt badge.
box(rider, [0.17, 0.17, 0.035], [0, 0.98, 0.213], 0xfff4cf);
box(rider, [0.49, 0.48, 0.45], [0, 1.53, 0], skin);
box(rider, [0.54, 0.14, 0.49], [0, 1.80, -0.015], 0x243342);
box(rider, [0.52, 0.07, 0.20], [0, 1.76, 0.27], 0x243342);
// Pixel eyes on the forward face.
box(rider, [0.065, 0.065, 0.035], [-0.11, 1.57, 0.239], 0x17232b);
box(rider, [0.065, 0.065, 0.035], [0.11, 1.57, 0.239], 0x17232b);
const leftArm = new THREE.Group();
leftArm.position.set(-0.39, 1.12, 0);
rider.add(leftArm);
box(leftArm, [0.17, 0.52, 0.19], [0, -0.23, 0], shirt);
box(leftArm, [0.18, 0.17, 0.19], [0, -0.55, 0], skin);
const rightArm = new THREE.Group();
rightArm.position.set(0.39, 1.12, 0);
rider.add(rightArm);
box(rightArm, [0.17, 0.52, 0.19], [0, -0.23, 0], shirt);
box(rightArm, [0.18, 0.17, 0.19], [0, -0.55, 0], skin);
// -------------------------------------------------------------------------
// Support marker and vertical balance blocks
// -------------------------------------------------------------------------
const supportMarker = new THREE.Group();
scene.add(supportMarker);
// Foreground marker remains clearly visible under the elevated camera.
box(
supportMarker,
[0.48, 0.10, 0.66],
[0, 0.15, 2.03],
0xff9138,
{
emissive: 0x6f2600,
emissiveIntensity: 0.14
}
);
// Blocky arrow pointing back toward the board.
box(supportMarker, [0.16, 0.07, 0.24], [0, 0.115, 1.53], 0xff9138);
box(supportMarker, [0.11, 0.07, 0.20], [0, 0.115, 1.32], 0xffb35a);
const plumbBlocks = [];
for (let i = 0; i < 4; i++) {
const block = box(
scene,
[0.10, 0.10, 0.10],
[0, 0.27 + i * 0.14, 1.76],
0xffe16b,
{
emissive: 0x6a4a00,
emissiveIntensity: 0.18
}
);
plumbBlocks.push(block);
}
// -------------------------------------------------------------------------
// Game state — points are awarded by exactly one declared rule
// -------------------------------------------------------------------------
const MAX_SUPPORT = 1.08;
const SAFE_HALF_WIDTH = 0.36;
const MAX_LEAN_ANGLE = 0.28;
let score = 0;
let leanCommand = 0;
let actualLean = 0;
let holdSeconds = 0;
let pointerControl = 0;
let resetNoticeUntil = 0;
const keys = new Set();
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function setScore(value) {
scoreElement.textContent = String(value);
}
function announceReset() {
resetPulse.classList.remove("flash");
void resetPulse.offsetWidth;
resetPulse.classList.add("flash");
resetNoticeUntil = performance.now() + 850;
}
function resetToy() {
leanCommand = 0;
actualLean = 0;
holdSeconds = 0;
pointerControl = 0;
board.rotation.z = 0;
board.position.y = 0.81;
rider.position.x = 0;
rider.rotation.z = 0;
supportMarker.position.x = 0;
for (const block of plumbBlocks) {
block.position.x = 0;
}
cameraState.yaw = cameraState.targetYaw = DEFAULT_CAMERA.yaw;
cameraState.pitch = cameraState.targetPitch = DEFAULT_CAMERA.pitch;
cameraState.radius = cameraState.targetRadius = DEFAULT_CAMERA.radius;
announceReset();
updateHUD();
}
// -------------------------------------------------------------------------
// Custom camera controls — no addons
// -------------------------------------------------------------------------
const DEFAULT_CAMERA = {
yaw: 0.62,
pitch: 0.43,
radius: 9.2
};
const cameraState = {
yaw: DEFAULT_CAMERA.yaw,
pitch: DEFAULT_CAMERA.pitch,
radius: DEFAULT_CAMERA.radius,
targetYaw: DEFAULT_CAMERA.yaw,
targetPitch: DEFAULT_CAMERA.pitch,
targetRadius: DEFAULT_CAMERA.radius
};
const cameraTarget = new THREE.Vector3(0, 1.0, 0);
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.targetYaw -= dx * 0.006;
cameraState.targetPitch = clamp(
cameraState.targetPitch + dy * 0.0045,
0.20,
1.15
);
});
function stopDragging() {
dragging = false;
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", stopDragging);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = clamp(
cameraState.targetRadius + event.deltaY * 0.008,
6.0,
13.5
);
},
{ passive: false }
);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 13);
cameraState.yaw +=
(cameraState.targetYaw - cameraState.yaw) * smoothing;
cameraState.pitch +=
(cameraState.targetPitch - cameraState.pitch) * smoothing;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * smoothing;
const horizontalRadius =
Math.cos(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraState.yaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraTarget.z + Math.cos(cameraState.yaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// Keyboard and touch controls
// -------------------------------------------------------------------------
const leftButton = document.querySelector("#leftButton");
const rightButton = document.querySelector("#rightButton");
const resetButton = document.querySelector("#resetButton");
const movementKeys = new Set([
"ArrowLeft",
"ArrowRight",
"KeyA",
"KeyD"
]);
window.addEventListener("keydown", (event) => {
if (movementKeys.has(event.code)) {
event.preventDefault();
keys.add(event.code);
}
if (event.code === "KeyR" && !event.repeat) {
event.preventDefault();
resetToy();
}
});
window.addEventListener("keyup", (event) => {
keys.delete(event.code);
});
window.addEventListener("blur", () => {
keys.clear();
pointerControl = 0;
});
function bindHoldButton(button, direction) {
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
button.setPointerCapture(event.pointerId);
pointerControl = direction;
button.classList.add("active");
});
const release = () => {
if (pointerControl === direction) pointerControl = 0;
button.classList.remove("active");
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("lostpointercapture", release);
}
bindHoldButton(leftButton, -1);
bindHoldButton(rightButton, 1);
resetButton.addEventListener("click", resetToy);
function keyboardAxis() {
const left =
keys.has("ArrowLeft") || keys.has("KeyA") ? 1 : 0;
const right =
keys.has("ArrowRight") || keys.has("KeyD") ? 1 : 0;
return right - left;
}
// -------------------------------------------------------------------------
// HUD
// -------------------------------------------------------------------------
function updateHUD() {
const supportX = actualLean * MAX_SUPPORT;
const isSafe = Math.abs(supportX) <= SAFE_HALF_WIDTH;
const supportPercent =
50 + (supportX / MAX_SUPPORT) * 50;
const commandPercent =
50 + (leanCommand / MAX_SUPPORT) * 50;
supportNeedle.style.left = `${clamp(supportPercent, 2, 98)}%`;
commandNeedle.style.left = `${clamp(commandPercent, 2, 98)}%`;
const leanPercent = Math.round(leanCommand * 100);
leanText.textContent = `LEAN: ${leanPercent > 0 ? "+" : ""}${leanPercent}%`;
if (performance.now() < resetNoticeUntil) {
marginState.textContent = "SAFE RESET";
marginState.classList.add("safe");
} else if (isSafe) {
marginState.textContent = "IN MARGIN";
marginState.classList.add("safe");
} else {
marginState.textContent = "OUTSIDE";
marginState.classList.remove("safe");
}
const progress = clamp(holdSeconds, 0, 1);
pointFill.style.width = `${progress * 100}%`;
if (isSafe) {
const untilNext = Math.max(0, 1 - (holdSeconds % 1));
pointText.textContent = `NEXT POINT: ${untilNext.toFixed(1)}s`;
} else {
pointText.textContent = "RETURN SUPPORT TO GREEN";
}
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
const axis = clamp(keyboardAxis() + pointerControl, -1, 1);
// The lean command is explicitly bounded to [-1, 1].
leanCommand = clamp(
leanCommand + axis * dt * 1.35,
-1,
1
);
// Smooth movement gives the board a toy-like weight.
const response = 1 - Math.exp(-dt * 9.5);
actualLean += (leanCommand - actualLean) * response;
const supportX = actualLean * MAX_SUPPORT;
const isSafe = Math.abs(supportX) <= SAFE_HALF_WIDTH;
// THE ONLY POINT-AWARDING CONDITION:
// one complete continuous second with the marker inside the green lane.
if (isSafe) {
holdSeconds += dt;
if (holdSeconds >= 1) {
holdSeconds -= 1;
score += 1;
setScore(score);
scorePanel.classList.remove("score-pop");
void scorePanel.offsetWidth;
scorePanel.classList.add("score-pop");
}
} else {
holdSeconds = 0;
}
// A small visual wobble never affects the declared scoring rule.
const wobble =
Math.sin(elapsed * 7.4) * 0.012 * Math.abs(actualLean);
const boardAngle = -actualLean * MAX_LEAN_ANGLE + wobble;
board.rotation.z = boardAngle;
// Keep the lower wheel approximately on the floor while the deck rolls.
const wheelX = 0.79;
const wheelY = -0.55;
const lowestWheelLocalY =
-wheelX * Math.abs(Math.sin(boardAngle)) +
wheelY * Math.cos(boardAngle);
board.position.y = 0.26 - lowestWheelLocalY;
// The rider shifts over the deck and counter-balances the board.
rider.position.x = supportX * 0.42;
rider.rotation.z = -boardAngle * 0.52;
leftArm.rotation.z = 0.30 + actualLean * 0.62;
rightArm.rotation.z = -0.30 - actualLean * 0.62;
// Move the orange support marker and the vertical balance blocks.
supportMarker.position.x = supportX;
for (const block of plumbBlocks) {
block.position.x = supportX;
}
updateCamera(dt);
updateHUD();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = width < 650 ? 50 : 43;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
updateCamera(1);
updateHUD();
requestAnimationFrame(animate);
</script>
</body>
</html>
```