Prompt specification
Build or customize the Three.js 3D scene "Painter's Street Perspective Stand" 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: Painter's Street Perspective Stand
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/painter-s-street-perspective-stand-x3318
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts perspective, projection, and visual composition. Build a street corner and a movable easel viewfinder with vanishing-line overlays. Update the overlay from the camera projection rather than drawing unrelated decorative guides. Stylized solid forms.
Working Three.js Starter (painter-s-street-perspective-stand-x3318):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Street Corner — Perspective Study</title>
<style>
:root {
color-scheme: dark;
--paper: #f5f1e8;
--muted: rgba(245, 241, 232, 0.66);
--panel: rgba(15, 22, 28, 0.78);
--gold: #ffd166;
--aqua: #65d6d0;
--coral: #ff7b8b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe6e7;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
color: var(--paper);
user-select: none;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
body.orbiting #scene {
cursor: grabbing;
}
#overlay {
position: fixed;
inset: 0;
z-index: 3;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#hud {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
width: min(330px, calc(100vw - 48px));
padding: 18px 20px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 50px rgba(10, 18, 24, 0.22);
backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 1;
}
#hud p {
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px 12px;
margin-top: 15px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.12);
color: rgba(255, 255, 255, 0.8);
font-size: 10px;
letter-spacing: 0.04em;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 17px;
height: 2px;
border-radius: 2px;
background: currentColor;
}
.x { color: var(--gold); }
.z { color: var(--aqua); }
.y { color: var(--coral); }
.h { color: white; }
#toolbar {
position: fixed;
z-index: 6;
right: 24px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
button {
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 9px;
padding: 10px 13px;
color: var(--paper);
background: rgba(15, 22, 28, 0.8);
box-shadow: 0 8px 24px rgba(10, 18, 24, 0.16);
backdrop-filter: blur(12px);
font: 600 11px/1 inherit;
letter-spacing: 0.035em;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(31, 42, 50, 0.94);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#readout {
position: fixed;
z-index: 5;
left: 24px;
bottom: 24px;
padding: 8px 11px;
border-radius: 7px;
color: rgba(255, 255, 255, 0.78);
background: rgba(15, 22, 28, 0.67);
font: 500 10px/1.25 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.045em;
pointer-events: none;
backdrop-filter: blur(10px);
}
.guide {
fill: none;
stroke-width: 1.3;
stroke-linecap: round;
stroke-linejoin: round;
opacity: 0.82;
vector-effect: non-scaling-stroke;
}
.guide-x {
stroke: var(--gold);
}
.guide-z {
stroke: var(--aqua);
}
.guide-y {
stroke: var(--coral);
}
#horizon {
fill: none;
stroke: rgba(255, 255, 255, 0.88);
stroke-width: 1.4;
stroke-dasharray: 7 6;
opacity: 0.72;
vector-effect: non-scaling-stroke;
}
.vp-dot {
fill: #10171d;
stroke: white;
stroke-width: 1.5;
vector-effect: non-scaling-stroke;
}
.vp-text {
fill: white;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
paint-order: stroke;
stroke: rgba(10, 17, 23, 0.9);
stroke-width: 3px;
stroke-linejoin: round;
}
#thirds {
fill: none;
stroke: rgba(255, 255, 255, 0.24);
stroke-width: 1;
vector-effect: non-scaling-stroke;
}
#frameBorder {
fill: rgba(255, 255, 255, 0.012);
stroke: rgba(255, 255, 255, 0.92);
stroke-width: 1.5;
pointer-events: stroke;
vector-effect: non-scaling-stroke;
}
#frameCorners {
fill: none;
stroke: var(--gold);
stroke-width: 4;
stroke-linecap: square;
pointer-events: stroke;
vector-effect: non-scaling-stroke;
}
.drag-target {
cursor: grab;
}
body.dragging-frame .drag-target {
cursor: grabbing;
}
#frameLabel {
pointer-events: all;
}
#frameLabel rect {
fill: rgba(14, 20, 25, 0.9);
stroke: rgba(255, 255, 255, 0.2);
stroke-width: 1;
}
#frameLabel text {
fill: white;
font: 750 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
}
#frameHandle {
fill: var(--gold);
stroke: rgba(20, 25, 28, 0.35);
stroke-width: 1;
pointer-events: all;
}
@media (max-width: 700px) {
#hud {
top: 14px;
left: 14px;
width: min(285px, calc(100vw - 28px));
padding: 14px 16px;
}
#hud p,
.legend {
font-size: 10px;
}
#toolbar {
right: 14px;
bottom: 14px;
}
#readout {
left: 14px;
bottom: 14px;
max-width: calc(100vw - 210px);
overflow: hidden;
white-space: nowrap;
}
}
</style>
</head>
<body>
<section id="hud">
<div class="eyebrow">Perspective study / 01</div>
<h1>Street Corner</h1>
<p>
Drag the scene to orbit, Shift-drag to pan, and scroll to dolly.
Drag the gold easel border or handle to recompose the viewfinder.
</p>
<div class="legend">
<span class="x"><i class="swatch"></i>X vanishing family</span>
<span class="z"><i class="swatch"></i>Z vanishing family</span>
<span class="y"><i class="swatch"></i>Vertical family</span>
<span class="h"><i class="swatch"></i>Projected horizon</span>
</div>
</section>
<div id="toolbar">
<button id="resetCamera" type="button">Reset camera</button>
<button id="centerFrame" type="button">Center easel</button>
<button id="toggleGuides" type="button">Guides on</button>
</div>
<div id="readout">Initializing projection…</div>
<svg id="overlay" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="frameClip">
<rect id="clipRect" x="0" y="0" width="1" height="1"></rect>
</clipPath>
</defs>
<g id="guideLayer" clip-path="url(#frameClip)">
<path id="horizon"></path>
<g id="guidePaths"></g>
<g id="vpMarkers"></g>
</g>
<g id="frameLayer">
<path id="thirds"></path>
<rect id="frameBorder" class="drag-target"></rect>
<path id="frameCorners" class="drag-target"></path>
<g id="frameLabel" class="drag-target">
<rect width="188" height="34" rx="6"></rect>
<circle cx="15" cy="17" r="4" fill="#ffd166"></circle>
<text x="28" y="20">EASEL VIEWFINDER</text>
</g>
<rect
id="frameHandle"
class="drag-target"
width="58"
height="11"
rx="5.5"
></rect>
</g>
</svg>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const overlay = document.querySelector("#overlay");
const guideLayer = document.querySelector("#guideLayer");
const guidePathsGroup = document.querySelector("#guidePaths");
const vpMarkersGroup = document.querySelector("#vpMarkers");
const horizonPath = document.querySelector("#horizon");
const frameBorder = document.querySelector("#frameBorder");
const frameCorners = document.querySelector("#frameCorners");
const frameLabel = document.querySelector("#frameLabel");
const frameHandle = document.querySelector("#frameHandle");
const thirdsPath = document.querySelector("#thirds");
const clipRect = document.querySelector("#clipRect");
const readout = document.querySelector("#readout");
const svgNS = "http://www.w3.org/2000/svg";
const clamp = THREE.MathUtils.clamp;
let width = innerWidth;
let height = innerHeight;
let guidesVisible = true;
let frame = { x: 0, y: 0, w: 0, h: 0 };
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const canvas = document.createElement("canvas");
canvas.id = "scene";
document.body.prepend(canvas);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(width, height, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dbe6e7");
scene.fog = new THREE.Fog("#dbe6e7", 34, 78);
const camera = new THREE.PerspectiveCamera(
43,
width / height,
0.1,
120
);
const cameraState = {
theta: 0.72,
phi: 1.08,
radius: 29,
target: new THREE.Vector3(0, 1.8, 0)
};
const initialCameraState = {
theta: 0.72,
phi: 1.08,
radius: 29,
target: new THREE.Vector3(0, 1.8, 0)
};
const hemisphere = new THREE.HemisphereLight("#f8fbf5", "#53606a", 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff0d2", 3.3);
sun.position.set(-13, 23, 15);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9d9ff", 0.8);
fill.position.set(16, 9, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Stylized solid street corner
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true
})
);
}
return materialCache.get(color);
}
function box(size, position, color, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(...size),
material(color)
);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, position, color, sides = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, sides),
material(color)
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Road plane and four raised sidewalk blocks.
box([60, 0.18, 60], [0, -0.13, 0], "#3d4952", false);
const sidewalk = "#d6d1c3";
const curb = "#eee9dc";
const blockSize = 25.5;
const blockOffset = 17.25;
for (const x of [-blockOffset, blockOffset]) {
for (const z of [-blockOffset, blockOffset]) {
box([blockSize, 0.22, blockSize], [x, 0.05, z], sidewalk, false);
}
}
// Thin curb caps along the road-facing sidewalk edges.
box([25.5, 0.045, 0.18], [-17.25, 0.18, -4.59], curb, false);
box([25.5, 0.045, 0.18], [17.25, 0.18, -4.59], curb, false);
box([25.5, 0.045, 0.18], [-17.25, 0.18, 4.59], curb, false);
box([25.5, 0.045, 0.18], [17.25, 0.18, 4.59], curb, false);
box([0.18, 0.045, 25.5], [-4.59, 0.18, -17.25], curb, false);
box([0.18, 0.045, 25.5], [4.59, 0.18, -17.25], curb, false);
box([0.18, 0.045, 25.5], [-4.59, 0.18, 17.25], curb, false);
box([0.18, 0.045, 25.5], [4.59, 0.18, 17.25], curb, false);
// Crosswalks.
const roadPaint = "#e8e5da";
const lanePaint = "#d8bd61";
for (let i = -3; i <= 3; i++) {
box([0.52, 0.028, 2.15], [i * 0.92, -0.012, 6.65], roadPaint, false);
box([2.15, 0.028, 0.52], [6.65, -0.012, i * 0.92], roadPaint, false);
}
// Broken lane center markings.
for (let x = -25; x <= 25; x += 4) {
if (Math.abs(x) > 7) {
box([1.8, 0.025, 0.13], [x, -0.005, -3.55], lanePaint, false);
box([1.8, 0.025, 0.13], [x, -0.005, 3.55], lanePaint, false);
}
}
for (let z = -25; z <= 25; z += 4) {
if (Math.abs(z) > 7) {
box([0.13, 0.025, 1.8], [-3.55, -0.005, z], lanePaint, false);
box([0.13, 0.025, 1.8], [3.55, -0.005, z], lanePaint, false);
}
}
// Corner buildings.
box([9, 8.2, 9], [-11, 4.26, -10.5], "#d47d4d");
box([6.4, 4.2, 6.4], [-12, 8.56, -11.5], "#e09a62");
box([6.65, 0.2, 6.65], [-12, 10.76, -11.5], "#7a4d3b");
box([10, 11, 10], [11, 5.66, -11], "#4f8588");
box([10.25, 0.22, 10.25], [11, 11.27, -11], "#31575d");
box([3.1, 1.5, 3.1], [12.5, 12.13, -12], "#668f91");
// Distant blocks.
box([10, 9, 11], [-23, 4.66, -23], "#8999a1");
box([12, 15, 10], [23, 7.66, -22], "#9b8e83");
box([8, 6, 9], [-23, 3.16, 20], "#778b8d");
box([9, 8, 10], [23, 4.16, 21], "#a09383");
// Window panels: simple inset solids rather than textures.
const windowColor = "#244653";
const windowHighlight = "#8bb8bd";
for (const y of [2.05, 4.35, 6.65]) {
for (const z of [-13.4, -10.5, -7.6]) {
box([0.12, 1.3, 1.35], [-6.43, y, z], windowColor, false);
}
}
for (const y of [2.05, 4.35, 6.65]) {
for (const x of [-14, -11.4, -8.8]) {
box([1.35, 1.3, 0.12], [x, y, -5.93], windowColor, false);
}
}
for (const y of [2.2, 4.6, 7, 9.4]) {
for (const z of [-14, -11.5, -9]) {
box([0.12, 1.35, 1.45], [6.07, y, z], windowColor, false);
}
}
for (const y of [2.2, 4.6, 7, 9.4]) {
for (const x of [7.6, 10.4, 13.2]) {
box([1.45, 1.35, 0.12], [x, y, -5.93], windowColor, false);
}
}
// Upper-story and doorway accents.
box([0.12, 1.25, 1.2], [-8.73, 8.55, -11.4], windowHighlight, false);
box([0.12, 1.25, 1.2], [-8.73, 8.55, -8.9], windowHighlight, false);
box([1.55, 2.35, 0.14], [-9.6, 1.34, -5.9], "#334b52", false);
box([1.55, 2.35, 0.14], [8.6, 1.34, -5.9], "#31575d", false);
// Awnings.
box([0.9, 0.18, 3.1], [-6.08, 2.95, -10.5], "#f0c45a");
box([3.1, 0.18, 0.9], [9.1, 3.0, -5.62], "#e6a84f");
// Streetlamp.
function addStreetlamp(x, z, flip = 1) {
cylinder(0.075, 0.11, 5.1, [x, 2.72, z], "#29343a", 10);
box([1.45, 0.11, 0.11], [x + flip * 0.66, 5.16, z], "#29343a");
box([0.55, 0.18, 0.34], [x + flip * 1.27, 5.08, z], "#f0dfae");
}
addStreetlamp(5.8, 5.8, -1);
addStreetlamp(-5.8, -5.8, 1);
// Traffic signal.
cylinder(0.075, 0.095, 4.8, [-5.7, 2.56, -5.7], "#303b40", 10);
box([0.48, 1.35, 0.48], [-5.7, 4.35, -5.7], "#202a2f");
box([0.52, 0.12, 0.52], [-5.7, 5.08, -5.7], "#202a2f");
const signalRed = new THREE.Mesh(
new THREE.SphereGeometry(0.125, 12, 8),
new THREE.MeshStandardMaterial({
color: "#ff625e",
emissive: "#7a1818",
emissiveIntensity: 0.45,
roughness: 0.55
})
);
signalRed.position.set(-5.43, 4.77, -5.7);
scene.add(signalRed);
const signalAmber = new THREE.Mesh(
new THREE.SphereGeometry(0.125, 12, 8),
material("#d59a37")
);
signalAmber.position.set(-5.43, 4.34, -5.7);
scene.add(signalAmber);
const signalGreen = new THREE.Mesh(
new THREE.SphereGeometry(0.125, 12, 8),
material("#4c8b65")
);
signalGreen.position.set(-5.43, 3.91, -5.7);
scene.add(signalGreen);
// Hydrant.
cylinder(0.24, 0.28, 0.85, [7.0, 0.62, 5.9], "#c94f45", 10);
cylinder(0.31, 0.31, 0.13, [7.0, 0.22, 5.9], "#a73d36", 10);
cylinder(0.19, 0.24, 0.22, [7.0, 1.12, 5.9], "#d25b4e", 10);
box([0.75, 0.18, 0.18], [7.0, 0.76, 5.9], "#c94f45");
// Low-poly trees.
function addTree(x, z, scale = 1) {
cylinder(0.16 * scale, 0.23 * scale, 2.5 * scale,
[x, 1.4 * scale, z], "#705039", 8);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.65 * scale, 1),
material("#587c5d")
);
crown.position.set(x, 3.15 * scale, z);
crown.scale.set(1, 1.12, 0.94);
crown.rotation.set(0.15, 0.45, -0.1);
crown.castShadow = true;
crown.receiveShadow = true;
scene.add(crown);
}
addTree(17.5, 14.5, 1.05);
addTree(-17.5, -15.7, 0.9);
addTree(23, 10, 1.1);
// Bench.
box([2.7, 0.15, 0.62], [12.0, 0.73, 8.6], "#8b6242");
box([2.7, 0.65, 0.12], [12.0, 1.08, 8.9], "#8b6242");
box([0.16, 0.55, 0.55], [10.8, 0.43, 8.6], "#394449");
box([0.16, 0.55, 0.55], [13.2, 0.43, 8.6], "#394449");
// Small pedestrian for scale.
function addPerson(x, z, shirtColor) {
const group = new THREE.Group();
group.position.set(x, 0.17, z);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.27, 0.72, 8),
material(shirtColor)
);
torso.position.y = 1.12;
torso.castShadow = true;
group.add(torso);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 12, 8),
material("#d2a27c")
);
head.position.y = 1.72;
head.castShadow = true;
group.add(head);
const leftLeg = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.085, 0.78, 8),
material("#27333b")
);
leftLeg.position.set(-0.1, 0.43, 0);
leftLeg.castShadow = true;
group.add(leftLeg);
const rightLeg = leftLeg.clone();
rightLeg.position.x = 0.1;
group.add(rightLeg);
scene.add(group);
}
addPerson(7.65, 7.25, "#d85e50");
addPerson(-6.15, -7.2, "#397b8a");
// -------------------------------------------------------------------------
// Camera-projected vanishing-line system
//
// Every guide below is reconstructed from:
// 1. A real world-space scene edge.
// 2. The live camera projection of that edge.
// 3. The live camera projection of the matching ideal point at infinity.
// -------------------------------------------------------------------------
const axisVectors = {
x: new THREE.Vector3(1, 0, 0),
y: new THREE.Vector3(0, 1, 0),
z: new THREE.Vector3(0, 0, 1)
};
const guideSegments = [
// Sidewalk and curb edges.
{ a: [-30, 0.18, 4.5], b: [30, 0.18, 4.5], axis: "x" },
{ a: [-30, 0.18, -4.5], b: [30, 0.18, -4.5], axis: "x" },
{ a: [4.5, 0.18, -30], b: [4.5, 0.18, 30], axis: "z" },
{ a: [-4.5, 0.18, -30], b: [-4.5, 0.18, 30], axis: "z" },
// Orange corner building.
{ a: [-15.5, 8.36, -6], b: [-6.5, 8.36, -6], axis: "x" },
{ a: [-15.5, 8.36, -15], b: [-6.5, 8.36, -15], axis: "x" },
{ a: [-15.5, 8.36, -15], b: [-15.5, 8.36, -6], axis: "z" },
{ a: [-6.5, 0.16, -15], b: [-6.5, 8.36, -15], axis: "y" },
{ a: [-6.5, 0.16, -6], b: [-6.5, 8.36, -6], axis: "y" },
// Upper orange building.
{ a: [-15.2, 10.66, -8.3], b: [-8.8, 10.66, -8.3], axis: "x" },
{ a: [-15.2, 10.66, -14.7], b: [-8.8, 10.66, -14.7], axis: "x" },
{ a: [-15.2, 6.46, -8.3], b: [-15.2, 10.66, -8.3], axis: "y" },
// Teal corner building.
{ a: [6, 11.16, -6], b: [16, 11.16, -6], axis: "x" },
{ a: [6, 11.16, -16], b: [16, 11.16, -16], axis: "x" },
{ a: [6, 11.16, -16], b: [6, 11.16, -6], axis: "z" },
{ a: [6, 0.16, -16], b: [6, 11.16, -16], axis: "y" },
{ a: [6, 0.16, -6], b: [6, 11.16, -6], axis: "y" },
{ a: [16, 0.16, -6], b: [16, 11.16, -6], axis: "y" },
// Distant horizontal edges.
{ a: [-28, 9.16, -17.5], b: [-18, 9.16, -17.5], axis: "x" },
{ a: [-28, 9.16, -28.5], b: [-18, 9.16, -28.5], axis: "x" }
].map((segment) => ({
...segment,
a: new THREE.Vector3(...segment.a),
b: new THREE.Vector3(...segment.b)
}));
const guidePaths = guideSegments.map((segment) => {
const path = document.createElementNS(svgNS, "path");
path.setAttribute("class", `guide guide-${segment.axis}`);
guidePathsGroup.appendChild(path);
return path;
});
const vpMarkers = {};
for (const axis of ["x", "y", "z"]) {
const group = document.createElementNS(svgNS, "g");
const circle = document.createElementNS(svgNS, "circle");
circle.setAttribute("r", "4");
circle.setAttribute("class", "vp-dot");
const text = document.createElementNS(svgNS, "text");
text.setAttribute("class", "vp-text");
text.setAttribute("x", "8");
text.setAttribute("y", "-7");
text.textContent = `${axis.toUpperCase()}∞`;
group.append(circle, text);
vpMarkersGroup.appendChild(group);
vpMarkers[axis] = group;
}
const projectionTemp = new THREE.Vector3();
function projectPoint(point) {
projectionTemp.copy(point).project(camera);
return {
x: (projectionTemp.x * 0.5 + 0.5) * width,
y: (-projectionTemp.y * 0.5 + 0.5) * height
};
}
function projectVanishingPoint(direction) {
// A direction with homogeneous w = 0 represents an ideal point at infinity.
const point = new THREE.Vector4(
direction.x,
direction.y,
direction.z,
0
);
point.applyMatrix4(camera.matrixWorldInverse);
point.applyMatrix4(camera.projectionMatrix);
if (Math.abs(point.w) < 0.000001) {
return null;
}
return {
x: (point.x / point.w * 0.5 + 0.5) * width,
y: (-point.y / point.w * 0.5 + 0.5) * height
};
}
function pointString(point) {
return `${point.x.toFixed(2)} ${point.y.toFixed(2)}`;
}
function updateProjectionGuides() {
camera.updateMatrixWorld();
const vanishingPoints = {
x: projectVanishingPoint(axisVectors.x),
y: projectVanishingPoint(axisVectors.y),
z: projectVanishingPoint(axisVectors.z)
};
guideSegments.forEach((segment, index) => {
const a = projectPoint(segment.a);
const b = projectPoint(segment.b);
const vp = vanishingPoints[segment.axis];
const path = guidePaths[index];
if (!vp) {
path.setAttribute("d", "");
return;
}
// The projected edge and its ideal vanishing point are collinear.
// Extend the projected edge in the direction of the actual VP.
const abx = b.x - a.x;
const aby = b.y - a.y;
const lengthSquared = abx * abx + aby * aby;
const t = lengthSquared > 0
? ((vp.x - a.x) * abx + (vp.y - a.y) * aby) / lengthSquared
: 0;
let d;
if (t > 1) {
d = `M ${pointString(a)} L ${pointString(b)} L ${pointString(vp)}`;
} else if (t < 0) {
d = `M ${pointString(vp)} L ${pointString(a)} L ${pointString(b)}`;
} else {
d = `M ${pointString(a)} L ${pointString(vp)} L ${pointString(b)}`;
}
path.setAttribute("d", d);
});
// The horizon is the line joining the two horizontal ideal points.
if (vanishingPoints.x && vanishingPoints.z) {
horizonPath.setAttribute(
"d",
`M ${pointString(vanishingPoints.x)} L ${pointString(vanishingPoints.z)}`
);
} else {
horizonPath.setAttribute("d", "");
}
for (const axis of ["x", "y", "z"]) {
const vp = vanishingPoints[axis];
const marker = vpMarkers[axis];
const visible =
vp &&
vp.x > -80 &&
vp.x < width + 80 &&
vp.y > -80 &&
vp.y < height + 80;
marker.style.display = visible ? "" : "none";
if (visible) {
marker.setAttribute(
"transform",
`translate(${vp.x.toFixed(2)} ${vp.y.toFixed(2)})`
);
}
}
}
// -------------------------------------------------------------------------
// Easel frame and composition grid
// -------------------------------------------------------------------------
function updateFrameGraphics() {
const { x, y, w, h } = frame;
frameBorder.setAttribute("x", x);
frameBorder.setAttribute("y", y);
frameBorder.setAttribute("width", w);
frameBorder.setAttribute("height", h);
clipRect.setAttribute("x", x + 1);
clipRect.setAttribute("y", y + 1);
clipRect.setAttribute("width", Math.max(0, w - 2));
clipRect.setAttribute("height", Math.max(0, h - 2));
thirdsPath.setAttribute(
"d",
[
`M ${x + w / 3} ${y} V ${y + h}`,
`M ${x + (w * 2) / 3} ${y} V ${y + h}`,
`M ${x} ${y + h / 3} H ${x + w}`,
`M ${x} ${y + (h * 2) / 3} H ${x + w}`
].join(" ")
);
const corner = Math.min(24, w * 0.1, h * 0.14);
frameCorners.setAttribute(
"d",
[
`M ${x + corner} ${y} H ${x} V ${y + corner}`,
`M ${x + w - corner} ${y} H ${x + w} V ${y + corner}`,
`M ${x + corner} ${y + h} H ${x} V ${y + h - corner}`,
`M ${x + w - corner} ${y + h} H ${x + w} V ${y + h - corner}`
].join(" ")
);
frameLabel.setAttribute("transform", `translate(${x} ${y - 40})`);
frameHandle.setAttribute(
"transform",
`translate(${x + w / 2 - 29} ${y + h - 5})`
);
}
function clampFrame() {
frame.x = clamp(frame.x, 8, Math.max(8, width - frame.w - 8));
frame.y = clamp(frame.y, 44, Math.max(44, height - frame.h - 8));
}
function resize() {
const oldWidth = width;
const oldHeight = height;
const oldFrameWidth = frame.w;
const oldFrameHeight = frame.h;
const oldCenterX = frame.x + frame.w / 2;
const oldCenterY = frame.y + frame.h / 2;
width = innerWidth;
height = innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
overlay.setAttribute("viewBox", `0 0 ${width} ${height}`);
frame.w = Math.min(width * 0.64, 680, height * 1.04);
frame.h = frame.w * 0.68;
if (!oldWidth || !oldFrameWidth) {
frame.x = (width - frame.w) / 2;
frame.y = (height - frame.h) / 2 + 16;
} else {
frame.x = oldCenterX - frame.w / 2;
frame.y = oldCenterY - frame.h / 2;
}
clampFrame();
updateFrameGraphics();
}
// Dragging the viewfinder.
let draggingFrame = false;
let frameDragOffset = { x: 0, y: 0 };
document.querySelectorAll(".drag-target").forEach((target) => {
target.addEventListener("pointerdown", (event) => {
event.preventDefault();
event.stopPropagation();
draggingFrame = true;
frameDragOffset = {
x: event.clientX - frame.x,
y: event.clientY - frame.y
};
document.body.classList.add("dragging-frame");
});
});
window.addEventListener("pointermove", (event) => {
if (!draggingFrame) return;
frame.x = event.clientX - frameDragOffset.x;
frame.y = event.clientY - frameDragOffset.y;
clampFrame();
updateFrameGraphics();
});
window.addEventListener("pointerup", () => {
draggingFrame = false;
document.body.classList.remove("dragging-frame");
});
// -------------------------------------------------------------------------
// Custom camera controls — no addon imports
// -------------------------------------------------------------------------
let cameraDrag = null;
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
event.preventDefault();
canvas.setPointerCapture(event.pointerId);
cameraDrag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
mode: event.shiftKey || event.button === 2 ? "pan" : "orbit"
};
document.body.classList.add("orbiting");
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraDrag || event.pointerId !== cameraDrag.pointerId) return;
const dx = event.clientX - cameraDrag.x;
const dy = event.clientY - cameraDrag.y;
cameraDrag.x = event.clientX;
cameraDrag.y = event.clientY;
if (cameraDrag.mode === "orbit") {
cameraState.theta -= dx * 0.005;
cameraState.phi = clamp(cameraState.phi + dy * 0.0045, 0.34, 1.43);
} else {
const scale = cameraState.radius / height * 1.15;
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
cameraState.target
.addScaledVector(right, -dx * scale)
.addScaledVector(up, dy * scale);
cameraState.target.x = clamp(cameraState.target.x, -16, 16);
cameraState.target.y = clamp(cameraState.target.y, 0, 7);
cameraState.target.z = clamp(cameraState.target.z, -16, 16);
}
});
function endCameraDrag() {
cameraDrag = null;
document.body.classList.remove("orbiting");
}
canvas.addEventListener("pointerup", endCameraDrag);
canvas.addEventListener("pointercancel", endCameraDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
9,
48
);
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = initialCameraState.theta;
cameraState.phi = initialCameraState.phi;
cameraState.radius = initialCameraState.radius;
cameraState.target.copy(initialCameraState.target);
updateCamera();
}
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
document.querySelector("#centerFrame").addEventListener("click", () => {
frame.x = (width - frame.w) / 2;
frame.y = (height - frame.h) / 2 + 16;
clampFrame();
updateFrameGraphics();
});
document.querySelector("#toggleGuides").addEventListener("click", (event) => {
guidesVisible = !guidesVisible;
guideLayer.style.display = guidesVisible ? "" : "none";
event.currentTarget.textContent = guidesVisible ? "Guides on" : "Guides off";
});
// Arrow keys move the easel; R resets the camera.
window.addEventListener("keydown", (event) => {
const step = event.shiftKey ? 28 : 10;
let handled = true;
switch (event.key) {
case "ArrowLeft":
frame.x -= step;
break;
case "ArrowRight":
frame.x += step;
break;
case "ArrowUp":
frame.y -= step;
break;
case "ArrowDown":
frame.y += step;
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
clampFrame();
updateFrameGraphics();
}
});
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
resize();
updateCamera();
let frameCounter = 0;
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateProjectionGuides();
renderer.render(scene, camera);
if (frameCounter++ % 8 === 0) {
const theta = Math.round(
THREE.MathUtils.radToDeg(cameraState.theta)
);
const phi = Math.round(
THREE.MathUtils.radToDeg(cameraState.phi)
);
readout.textContent =
`PERSPECTIVE CAMERA / θ ${theta}° φ ${phi}° / ` +
`FRAME ${Math.round(frame.w)}×${Math.round(frame.h)}`;
}
}
animate();
</script>
</body>
</html>
```