-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanim1.html
More file actions
303 lines (265 loc) · 9.2 KB
/
anim1.html
File metadata and controls
303 lines (265 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3-Point Perspective Cube</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
background: dodgerblue;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
.debug-checkbox {
position: absolute;
top: 1em;
left: 1em;
background: rgba(255, 255, 255, 0.8);
padding: 0.5em;
border-radius: 4px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<canvas id="cubeCanvas"></canvas>
<div class="debug-checkbox">
<label>
<input type="checkbox" id="debugLines"> Show Debug Lines
</label>
</div>
<script>
// Imports, constants then state
const canvas = document.getElementById('cubeCanvas');
if (!canvas) {
console.error('Canvas element not found!');
}
const ctx = canvas.getContext('2d');
if (!ctx) {
console.error('Failed to get 2D context for Canvas!');
}
const debugCheckbox = document.getElementById('debugLines');
let showDebugLines = false;
let horizonY = canvas.height * 0.382; // 38.2% from top (61.8% from bottom)
let vp1 = { x: -canvas.width * 0.5, y: horizonY }; // Left vanishing point (off-screen)
let vp2 = { x: canvas.width * 1.5, y: horizonY }; // Right vanishing point (off-screen)
let vp3 = { x: canvas.width / 2, y: canvas.height * 1.5 }; // Top vanishing point (below canvas)
const perspective = 1000; // Controls depth effect
const cubeSize = 100; // Side length
let cube = { center: { x: 0, y: 0, z: -100 }, vertices: [] }; // Single cube
const cubeFaces = [
{ vertices: [0, 1, 5, 4], color: '#808080' }, // Front
{ vertices: [2, 3, 7, 6], color: '#808080' }, // Back
{ vertices: [0, 3, 7, 4], color: '#808080' }, // Left
{ vertices: [1, 2, 6, 5], color: '#808080' }, // Right
{ vertices: [4, 5, 6, 7], color: '#808080' }, // Top
{ vertices: [0, 1, 2, 3], color: '#808080' } // Bottom
];
const floor = {
points: [
{ x: -canvas.width, y: 0, z: 0 }, // Bottom-left
{ x: canvas.width, y: 0, z: 0 }, // Bottom-right
{ x: canvas.width, y: 0, z: -800 }, // Top-right
{ x: -canvas.width, y: 0, z: -800 } // Top-left
]
};
// Logic
window.addEventListener('resize', resizeCanvas);
debugCheckbox.addEventListener('change', () => {
showDebugLines = debugCheckbox.checked;
render();
});
resizeCanvas();
initializeCube();
render();
// Functions
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initializeCube();
render();
}
function initializeCube() {
const x = 0; // Center in X
const z = Math.random() * -200; // -200 to 0
cube.center = { x, y: 0, z };
cube.vertices = generateCubeVertices(cube.center);
console.log('Cube positioned at:', cube.center);
}
function render() {
try {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update perspective parameters
horizonY = canvas.height * 0.382;
vp1 = { x: -canvas.width * 0.5, y: horizonY };
vp2 = { x: canvas.width * 1.5, y: horizonY };
vp3 = { x: canvas.width / 2, y: canvas.height * 1.5 };
// Draw floor
drawFloor();
// Draw guides
drawGuides();
// Sort faces by average z (far to near)
const sortedFaces = cubeFaces.map(face => {
const avgZ = face.vertices.reduce((sum, idx) => sum + cube.vertices[idx].z, 0) / face.vertices.length;
return { face, avgZ };
}).sort((a, b) => b.avgZ - a.avgZ);
console.log('Sorted faces by avgZ:', sortedFaces.map(f => f.avgZ));
// Render sorted faces
sortedFaces.forEach(({ face }) => {
drawFace(cube, face);
});
// Draw debug lines if enabled
if (showDebugLines) {
drawDebugLines();
}
} catch (error) {
console.error('Error in render:', error);
}
}
function project(x, y, z) {
const d = perspective / (perspective - z);
if (isNaN(d) || d <= 0) {
console.error('Invalid projection:', { x, y, z, d });
return { x: 0, y: 0 };
}
const projX = canvas.width / 2 + x * d + (x < 0 ? (vp1.x - canvas.width / 2) * (1 - d) : (vp2.x - canvas.width / 2) * (1 - d));
const projY = horizonY + y * d + (vp3.y - horizonY) * (1 - d);
if (isNaN(projX) || isNaN(projY)) {
console.error('NaN in projection:', { x, y, z, projX, projY, d });
return { x: 0, y: 0 };
}
return { x: projX, y: projY, scale: d };
}
function drawFace(cube, face) {
try {
ctx.save();
const points2D = face.vertices.map(index => project(
cube.vertices[index].x,
cube.vertices[index].y,
cube.vertices[index].z
));
ctx.beginPath();
ctx.moveTo(points2D[0].x, points2D[0].y);
for (let i = 1; i < points2D.length; i++) {
ctx.lineTo(points2D[i].x, points2D[i].y);
}
ctx.closePath();
ctx.fillStyle = face.color;
ctx.fill();
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.stroke();
ctx.restore();
console.log('Face projected points:', points2D.map(p => ({ x: p.x, y: p.y })));
if (face.vertices.includes(0)) {
console.log('Bottom vertex projected Y:', points2D[0].y);
}
if (face.vertices.includes(4)) {
console.log('Top vertex projected Y:', points2D[0].y);
}
} catch (error) {
console.error('Error drawing face:', error);
}
}
function drawFloor() {
try {
ctx.save();
const points2D = floor.points.map(point => project(point.x, point.y, point.z));
ctx.beginPath();
ctx.moveTo(points2D[0].x, points2D[0].y);
for (let i = 1; i < points2D.length; i++) {
ctx.lineTo(points2D[i].x, points2D[i].y);
}
ctx.closePath();
ctx.fillStyle = '#2A2A2A'; // Charcoal for floor
ctx.fill();
ctx.restore();
console.log('Floor projected points:', points2D.map(p => ({ x: p.x, y: p.y })));
} catch (error) {
console.error('Error drawing floor:', error);
}
}
function drawGuides() {
ctx.save();
ctx.beginPath();
ctx.moveTo(0, horizonY);
ctx.lineTo(canvas.width, horizonY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
ctx.restore();
}
function drawDebugLines() {
ctx.save();
ctx.strokeStyle = 'yellow';
ctx.lineWidth = 1;
ctx.setLineDash([5, 5]); // Dashed lines
cube.vertices.forEach((vertex, index) => {
const point2D = project(vertex.x, vertex.y, vertex.z);
// Lines to VP1 (left-right edges)
if (index === 0 || index === 3 || index === 4 || index === 7) { // Left vertices
ctx.beginPath();
ctx.moveTo(point2D.x, point2D.y);
ctx.lineTo(vp1.x, vp1.y);
ctx.stroke();
console.log(`Debug line to VP1 from vertex ${index}: (${point2D.x}, ${point2D.y}) to (${vp1.x}, ${vp1.y})`);
}
// Lines to VP2 (right-left edges)
if (index === 1 || index === 2 || index === 5 || index === 6) { // Right vertices
ctx.beginPath();
ctx.moveTo(point2D.x, point2D.y);
ctx.lineTo(vp2.x, vp2.y);
ctx.stroke();
console.log(`Debug line to VP2 from vertex ${index}: (${point2D.x}, ${point2D.y}) to (${vp2.x}, ${vp2.y})`);
}
// Lines to VP3 (vertical edges)
ctx.beginPath();
ctx.moveTo(point2D.x, point2D.y);
ctx.lineTo(vp3.x, vp3.y);
ctx.stroke();
console.log(`Debug line to VP3 from vertex ${index}: (${point2D.x}, ${point2D.y}) to (${vp3.x}, ${vp3.y})`);
});
ctx.setLineDash([]); // Reset dash
ctx.restore();
}
// Helper functions
function generateCubeVertices(center) {
const { x, y, z } = center;
const s = cubeSize / 2;
const theta = Math.PI / 6; // 30 degrees
const cosTheta = Math.cos(theta);
const sinTheta = Math.sin(theta);
const baseVertices = [
{ x: -s, y: 0, z: -s }, // 0: Bottom front left
{ x: s, y: 0, z: -s }, // 1: Bottom front right
{ x: s, y: 0, z: s }, // 2: Bottom back right
{ x: -s, y: 0, z: s }, // 3: Bottom back left
{ x: -s, y: cubeSize, z: -s }, // 4: Top front left
{ x: s, y: cubeSize, z: -s }, // 5: Top front right
{ x: s, y: cubeSize, z: s }, // 6: Top back right
{ x: -s, y: cubeSize, z: s } // 7: Top back left
];
const rotatedVertices = baseVertices.map(v => {
const rx = v.x * cosTheta + v.z * sinTheta;
const rz = -v.x * sinTheta + v.z * cosTheta;
return {
x: rx + x,
y: v.y + y,
z: rz + z
};
});
console.log('Rotated vertices:', rotatedVertices);
return rotatedVertices;
}
</script>
</body>
</html>