Prompt specification
Build or customize the Three.js 3D scene "Rook Blocked by a Friendly Piece" 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: Rook Blocked by a Friendly Piece
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rook-blocked-by-a-friendly-piece-x3606
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a low-poly chess teaching board with one rook and a friendly blocking piece. Use a small number of pieces and keep the rook's rank and file unobstructed except for that explicit blocker. Highlight the squares the rook can reach according to the current occupancy, stopping its movement ray before the friendly piece. Let the user move the blocker to another square and recalculate the highlights immediately. The rook must not pass through it or mark the occupied square as a legal destination. State that the example focuses on rook movement and occupancy rather than a complete chess engine.
Working Three.js Starter (rook-blocked-by-a-friendly-piece-x3606):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rook Movement & Occupancy</title>
<style>
:root {
color-scheme: dark;
--bg: #0c1422;
--panel: rgba(18, 29, 46, 0.92);
--panel-border: rgba(255, 255, 255, 0.1);
--text: #edf5ff;
--muted: #9eb0c7;
--cyan: #42ddff;
--cyan-soft: rgba(66, 221, 255, 0.16);
--amber: #ffb454;
--amber-soft: rgba(255, 180, 84, 0.18);
--green: #7de2b1;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
background:
radial-gradient(circle at 75% 20%, rgba(48, 104, 142, 0.2), transparent 35%),
radial-gradient(circle at 20% 80%, rgba(50, 105, 88, 0.16), transparent 32%),
var(--bg);
}
.app {
width: 100%;
min-height: 100dvh;
display: flex;
flex-direction: column;
}
.topbar {
min-height: 76px;
padding: 16px 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
border-bottom: 1px solid var(--panel-border);
background: rgba(10, 18, 31, 0.82);
backdrop-filter: blur(14px);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(66, 221, 255, 0.36);
border-radius: 12px;
color: var(--cyan);
background: var(--cyan-soft);
font-size: 24px;
font-weight: 800;
}
.brand h1 {
margin: 0;
font-size: clamp(18px, 2vw, 24px);
letter-spacing: -0.025em;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 13px;
}
.top-actions {
display: flex;
align-items: center;
gap: 12px;
}
.pill {
padding: 8px 11px;
border: 1px solid var(--panel-border);
border-radius: 999px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
button {
border: 1px solid rgba(66, 221, 255, 0.35);
border-radius: 10px;
padding: 10px 14px;
background: var(--cyan-soft);
color: var(--cyan);
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(66, 221, 255, 0.25);
transform: translateY(-1px);
}
button:focus-visible {
outline: 3px solid rgba(66, 221, 255, 0.3);
outline-offset: 2px;
}
main {
flex: 1;
min-height: 0;
display: flex;
}
aside {
width: 340px;
flex: 0 0 340px;
padding: 24px;
border-right: 1px solid var(--panel-border);
background: rgba(12, 21, 35, 0.7);
}
.eyebrow {
margin: 0 0 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
aside h2 {
margin: 0;
font-size: 25px;
line-height: 1.16;
letter-spacing: -0.035em;
}
.intro {
margin: 13px 0 22px;
color: var(--muted);
font-size: 14px;
line-height: 1.6;
}
.status-card,
.legend-card,
.note-card {
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 16px 35px rgba(0, 0, 0, 0.16);
}
.status-card {
padding: 17px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 9px 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
font-size: 13px;
}
.status-row:last-child {
border-bottom: 0;
}
.status-label {
color: var(--muted);
}
.status-value {
font-weight: 750;
text-align: right;
}
.status-value.cyan {
color: var(--cyan);
}
.status-value.amber {
color: var(--amber);
}
.ray-note {
margin: 13px 0 0;
padding: 12px;
border-radius: 10px;
background: var(--amber-soft);
color: #ffd7a3;
font-size: 12px;
line-height: 1.55;
}
.legend-card {
margin-top: 14px;
padding: 15px 17px;
}
.legend-title {
margin-bottom: 12px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
margin: 9px 0;
color: #dce8f7;
font-size: 13px;
}
.swatch {
width: 18px;
height: 18px;
flex: 0 0 18px;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.22);
}
.swatch.legal {
background: rgba(66, 221, 255, 0.55);
box-shadow: 0 0 13px rgba(66, 221, 255, 0.28);
}
.swatch.blocker {
background: var(--amber);
}
.swatch.hover {
background: rgba(255, 255, 255, 0.24);
}
.note-card {
margin-top: 14px;
padding: 15px 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.note-card strong {
color: var(--green);
}
#viewport {
position: relative;
flex: 1;
min-width: 0;
min-height: 540px;
overflow: hidden;
isolation: isolate;
}
#viewport canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.viewport-hint {
position: absolute;
z-index: 3;
left: 50%;
bottom: 18px;
width: max-content;
max-width: calc(100% - 32px);
transform: translateX(-50%);
padding: 10px 15px;
border: 1px solid var(--panel-border);
border-radius: 999px;
background: rgba(12, 21, 35, 0.84);
color: #dce8f7;
font-size: 12px;
text-align: center;
pointer-events: none;
backdrop-filter: blur(10px);
}
.toast {
position: absolute;
z-index: 5;
top: 18px;
left: 50%;
max-width: calc(100% - 32px);
transform: translate(-50%, -12px);
padding: 10px 14px;
border: 1px solid rgba(66, 221, 255, 0.3);
border-radius: 10px;
background: rgba(12, 21, 35, 0.92);
color: var(--cyan);
font-size: 12px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transition: opacity 160ms ease, transform 160ms ease;
}
.toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
footer {
padding: 12px 24px;
border-top: 1px solid var(--panel-border);
color: var(--muted);
font-size: 12px;
text-align: center;
background: rgba(10, 18, 31, 0.7);
}
@media (max-width: 800px) {
.topbar {
padding: 13px 16px;
}
.pill {
display: none;
}
main {
flex-direction: column;
}
#viewport {
order: -1;
width: 100%;
height: 62dvh;
min-height: 440px;
border-bottom: 1px solid var(--panel-border);
}
aside {
width: 100%;
flex-basis: auto;
padding: 20px;
border-right: 0;
}
.viewport-hint {
bottom: 10px;
border-radius: 12px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">♜</div>
<div>
<h1>Rook Movement & Occupancy</h1>
<p>Move the blocker and watch every reachable square update.</p>
</div>
</div>
<div class="top-actions">
<span class="pill">Interactive teaching example</span>
<button id="resetButton" type="button">Reset example</button>
</div>
</header>
<main>
<aside>
<p class="eyebrow">Low-poly chess lesson</p>
<h2>One rook.<br>One friendly blocker.</h2>
<p class="intro">
The cyan squares are the rook’s legal destinations. Its movement ray
stops before a friendly piece, and an occupied square is never marked
as a legal destination.
</p>
<section class="status-card" aria-live="polite">
<div class="status-row">
<span class="status-label">Rook</span>
<span class="status-value" id="rookStatus">d4</span>
</div>
<div class="status-row">
<span class="status-label">Friendly blocker</span>
<span class="status-value amber" id="blockerStatus">g4</span>
</div>
<div class="status-row">
<span class="status-label">Legal destinations</span>
<span class="status-value cyan" id="countStatus">12</span>
</div>
<p class="ray-note" id="rayStatus"></p>
</section>
<section class="legend-card">
<div class="legend-title">Board legend</div>
<div class="legend-item">
<span class="swatch legal"></span>
<span>Legal rook destination</span>
</div>
<div class="legend-item">
<span class="swatch blocker"></span>
<span>Draggable friendly blocker</span>
</div>
<div class="legend-item">
<span class="swatch hover"></span>
<span>Pointer hover indicator</span>
</div>
</section>
<section class="note-card">
<strong>Scope:</strong> This example focuses on rook movement and
occupancy rather than a complete chess engine. It does not model
turns, check, checkmate, captures, or other piece rules.
</section>
</aside>
<section id="viewport" aria-label="Interactive three-dimensional chess board">
<div class="toast" id="toast" role="status"></div>
<div class="viewport-hint">
Drag the orange blocker, or select it and then click a destination square.
</div>
</section>
</main>
<footer>
The rook remains fixed. The friendly blocker can move to any unoccupied square except the rook’s square.
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const resetButton = document.getElementById("resetButton");
const toast = document.getElementById("toast");
const rookStatus = document.getElementById("rookStatus");
const blockerStatus = document.getElementById("blockerStatus");
const countStatus = document.getElementById("countStatus");
const rayStatus = document.getElementById("rayStatus");
const FILE_NAMES = ["a", "b", "c", "d", "e", "f", "g", "h"];
const GROUND_Y = 0.17;
const rook = { file: 3, rank: 3 }; // d4
const blocker = { file: 6, rank: 3 }; // g4
const originalBlocker = { ...blocker };
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.style.cursor = "default";
const camera = new THREE.OrthographicCamera(-8, 8, 6, -6, 0.1, 100);
camera.position.set(9.5, 10.5, 11.5);
camera.lookAt(0, 0, 0);
const ambient = new THREE.HemisphereLight(0xcceaff, 0x354038, 2.15);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff2d9, 3.1);
sun.position.set(5, 11, 7);
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 = 30;
sun.shadow.bias = -0.00045;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x75cfff, 1.15);
rimLight.position.set(-7, 5, -6);
scene.add(rimLight);
const shadowFloor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.ShadowMaterial({
color: 0x02070d,
opacity: 0.3
})
);
shadowFloor.rotation.x = -Math.PI / 2;
shadowFloor.position.y = -0.38;
shadowFloor.receiveShadow = true;
scene.add(shadowFloor);
const board = new THREE.Group();
scene.add(board);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x263343,
roughness: 0.72,
metalness: 0.12,
flatShading: true
});
const boardBase = new THREE.Mesh(
new THREE.BoxGeometry(8.68, 0.36, 8.68),
baseMaterial
);
boardBase.position.y = -0.19;
boardBase.castShadow = true;
boardBase.receiveShadow = true;
board.add(boardBase);
const lowerTrim = new THREE.Mesh(
new THREE.BoxGeometry(8.82, 0.12, 8.82),
new THREE.MeshStandardMaterial({
color: 0x111d2a,
roughness: 0.6,
metalness: 0.2,
flatShading: true
})
);
lowerTrim.position.y = -0.4;
lowerTrim.castShadow = true;
board.add(lowerTrim);
const lightTileMaterial = new THREE.MeshStandardMaterial({
color: 0xd8cba8,
roughness: 0.88,
metalness: 0,
flatShading: true
});
const darkTileMaterial = new THREE.MeshStandardMaterial({
color: 0x638573,
roughness: 0.9,
metalness: 0,
flatShading: true
});
const tileMeshes = [];
function squareToWorld(file, rank) {
return {
x: file - 3.5,
z: rank - 4.5
};
}
function squareName(square) {
return `${FILE_NAMES[square.file]}${square.rank + 1}`;
}
for (let rank = 0; rank < 8; rank++) {
for (let file = 0; file < 8; file++) {
const world = squareToWorld(file, rank);
const tile = new THREE.Mesh(
new THREE.BoxGeometry(0.98, 0.16, 0.98),
(file + rank) % 2 === 0 ? lightTileMaterial : darkTileMaterial
);
tile.position.set(world.x, 0.08, world.z);
tile.receiveShadow = true;
tile.userData.square = { file, rank };
tileMeshes.push(tile);
board.add(tile);
}
}
function createGroundLabel(text, color = "#dce8f7") {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 64;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = color;
context.font = "700 38px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 64, 32);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(0.36, 0.2),
material
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.y = 0.006;
return mesh;
}
for (let file = 0; file < 8; file++) {
const label = createGroundLabel(FILE_NAMES[file]);
label.position.x = file - 3.5;
label.position.z = 4.15;
board.add(label);
}
for (let rank = 0; rank < 8; rank++) {
const label = createGroundLabel(String(rank + 1));
label.position.x = -4.17;
label.position.z = rank - 3.5;
board.add(label);
}
const legalGroup = new THREE.Group();
scene.add(legalGroup);
const legalGeometry = new THREE.BoxGeometry(0.84, 0.018, 0.84);
const legalMaterial = new THREE.MeshBasicMaterial({
color: 0x42ddff,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const hoverMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.91, 0.014, 0.91),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.23,
depthWrite: false
})
);
hoverMesh.position.y = GROUND_Y + 0.035;
hoverMesh.visible = false;
hoverMesh.renderOrder = 3;
scene.add(hoverMesh);
function createRook() {
const group = new THREE.Group();
const stone = new THREE.MeshStandardMaterial({
color: 0xdce7ec,
roughness: 0.48,
metalness: 0.22,
flatShading: true
});
const dark = new THREE.MeshStandardMaterial({
color: 0x30495c,
roughness: 0.48,
metalness: 0.28,
flatShading: true
});
function cylinder(top, bottom, height, segments, material, y) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(top, bottom, height, segments, 1, false),
material
);
mesh.position.y = y;
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
cylinder(0.39, 0.45, 0.14, 12, dark, 0.07);
cylinder(0.31, 0.36, 0.16, 12, stone, 0.22);
cylinder(0.22, 0.29, 0.62, 10, stone, 0.61);
cylinder(0.31, 0.27, 0.12, 12, dark, 0.98);
cylinder(0.34, 0.28, 0.18, 8, stone, 1.13);
const crownPlate = new THREE.Mesh(
new THREE.BoxGeometry(0.64, 0.09, 0.64),
dark
);
crownPlate.position.y = 1.25;
crownPlate.castShadow = true;
group.add(crownPlate);
const merlons = [
[-0.22, -0.22],
[0.22, -0.22],
[-0.22, 0.22],
[0.22, 0.22]
];
for (const [x, z] of merlons) {
const block = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.24, 0.2),
stone
);
block.position.set(x, 1.4, z);
block.castShadow = true;
group.add(block);
}
return group;
}
function createBlockerPawn() {
const group = new THREE.Group();
const orange = new THREE.MeshStandardMaterial({
color: 0xffa348,
roughness: 0.58,
metalness: 0.05,
flatShading: true
});
const deepOrange = new THREE.MeshStandardMaterial({
color: 0xb95725,
roughness: 0.65,
metalness: 0.05,
flatShading: true
});
function cylinder(top, bottom, height, segments, material, y) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(top, bottom, height, segments, 1, false),
material
);
mesh.position.y = y;
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
cylinder(0.34, 0.4, 0.13, 12, deepOrange, 0.065);
cylinder(0.18, 0.27, 0.38, 8, orange, 0.31);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.23, 1),
orange
);
head.position.y = 0.66;
head.castShadow = true;
group.add(head);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.45, 0.025, 6, 32),
new THREE.MeshBasicMaterial({
color: 0xffb454,
transparent: true,
opacity: 0.88,
depthWrite: false
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.035;
group.add(ring);
return group;
}
const rookGroup = createRook();
const rookWorld = squareToWorld(rook.file, rook.rank);
rookGroup.position.set(rookWorld.x, GROUND_Y, rookWorld.z);
scene.add(rookGroup);
const blockerGroup = createBlockerPawn();
const blockerWorld = squareToWorld(blocker.file, blocker.rank);
blockerGroup.position.set(blockerWorld.x, GROUND_Y, blockerWorld.z);
scene.add(blockerGroup);
const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1]
];
function calculateLegalSquares() {
const legal = [];
for (const [dx, dz] of directions) {
let file = rook.file + dx;
let rank = rook.rank + dz;
while (file >= 0 && file < 8 && rank >= 0 && rank < 8) {
// A friendly piece stops the ray. Its occupied square is not legal,
// and no square beyond it is marked.
if (file === blocker.file && rank === blocker.rank) {
break;
}
legal.push({ file, rank });
file += dx;
rank += dz;
}
}
return legal;
}
function recalculateHighlights() {
legalGroup.clear();
for (const square of calculateLegalSquares()) {
const world = squareToWorld(square.file, square.rank);
const highlight = new THREE.Mesh(legalGeometry, legalMaterial);
highlight.position.set(world.x, GROUND_Y + 0.014, world.z);
highlight.renderOrder = 1;
legalGroup.add(highlight);
}
countStatus.textContent = String(legalGroup.children.length);
updateRayStatus();
}
function updateRayStatus() {
const sameFile = blocker.file === rook.file;
const sameRank = blocker.rank === rook.rank;
if (sameFile || sameRank) {
const axis = sameFile ? "file" : "rank";
rayStatus.textContent =
`The blocker is on the rook's ${axis}. The movement ray stops before ` +
`${squareName(blocker)}, and that occupied square is not legal.`;
} else {
rayStatus.textContent =
"The blocker is off the rook's rank and file. All four rook rays " +
"continue to the edge of the board.";
}
}
function updateStatus() {
rookStatus.textContent = squareName(rook);
blockerStatus.textContent = squareName(blocker);
recalculateHighlights();
}
let toastTimer = 0;
function announce(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2200);
}
function moveBlocker(square, options = {}) {
const { announceChange = true, keepSelected = false } = options;
blocker.file = square.file;
blocker.rank = square.rank;
const world = squareToWorld(blocker.file, blocker.rank);
blockerGroup.position.set(world.x, GROUND_Y, world.z);
if (!keepSelected) {
selected = false;
}
updateStatus();
if (announceChange) {
announce(`Blocker moved to ${squareName(blocker)}. Highlights recalculated.`);
}
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let selected = false;
let dragging = false;
let dragOriginal = null;
let hoverSquare = null;
function updatePointer(event) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function hitSquare(event) {
updatePointer(event);
const hit = raycaster.intersectObjects(tileMeshes, false)[0];
return hit ? hit.object.userData.square : null;
}
function hitBlocker(event) {
updatePointer(event);
return raycaster.intersectObject(blockerGroup, true).length > 0;
}
function setHover(square) {
hoverSquare = square;
if (!square) {
hoverMesh.visible = false;
return;
}
const world = squareToWorld(square.file, square.rank);
hoverMesh.position.set(world.x, GROUND_Y + 0.035, world.z);
hoverMesh.visible = true;
}
function isValidDestination(square) {
return square && !(square.file === rook.file && square.rank === rook.rank);
}
renderer.domElement.addEventListener("pointerdown", event => {
event.preventDefault();
if (hitBlocker(event)) {
selected = true;
dragging = true;
dragOriginal = { ...blocker };
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
announce("Blocker selected. Drag it or click a destination square.");
return;
}
const square = hitSquare(event);
if (selected && square) {
if (square.file === rook.file && square.rank === rook.rank) {
announce("The rook's square is occupied.");
return;
}
if (square.file === blocker.file && square.rank === blocker.rank) {
selected = false;
return;
}
moveBlocker(square);
renderer.domElement.style.cursor = "default";
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging) {
const square = hitSquare(event);
if (isValidDestination(square)) {
if (square.file !== blocker.file || square.rank !== blocker.rank) {
// Preview the new occupancy and recalculate immediately.
moveBlocker(square, {
announceChange: false,
keepSelected: true
});
}
}
return;
}
const square = hitSquare(event);
setHover(square);
renderer.domElement.style.cursor =
hitBlocker(event) ? "grab" : square ? "pointer" : "default";
});
renderer.domElement.addEventListener("pointerup", event => {
if (!dragging) return;
dragging = false;
renderer.domElement.style.cursor = "default";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
const square = hitSquare(event);
if (isValidDestination(square)) {
if (square.file !== blocker.file || square.rank !== blocker.rank) {
moveBlocker(square, {
announceChange: false,
keepSelected: true
});
}
selected = false;
announce(`Blocker placed on ${squareName(blocker)}. Highlights recalculated.`);
} else {
moveBlocker(dragOriginal, {
announceChange: false,
keepSelected: true
});
selected = true;
announce("Invalid destination. The blocker returned to its previous square.");
}
});
renderer.domElement.addEventListener("pointercancel", () => {
if (!dragging) return;
dragging = false;
selected = true;
moveBlocker(dragOriginal, {
announceChange: false,
keepSelected: true
});
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) {
setHover(null);
renderer.domElement.style.cursor = "default";
}
});
resetButton.addEventListener("click", () => {
selected = false;
dragging = false;
moveBlocker(originalBlocker, { announceChange: false });
setHover(null);
announce("Example reset: rook on d4, friendly blocker on g4.");
});
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
const aspect = width / height;
const frustumHeight = Math.max(10.8, 13.2 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
updateStatus();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```