Skip to content

Commit abb0209

Browse files
committed
fix: Restore working canvas dimension pattern in HomeView
Revert to the original working approach: create canvas → append to DOM → measure canvas.clientWidth/clientHeight. The getBoundingClientRect() on the original bare canvas element returns 0×0 since it has no explicit width/height HTML attributes. Measuring the appended canvas after CSS is applied gives correct dimensions.
1 parent e9b3f6c commit abb0209

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

src/components/HomeView.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export function HomeView() {
143143
if (!container) return;
144144

145145
let animationId: number;
146-
let retryId: number | undefined;
147146
let cleanup = false;
148147
let renderer: any = null;
149148
let scene: any = null;
@@ -166,17 +165,7 @@ export function HomeView() {
166165
try {
167166
const THREE = await import('three');
168167

169-
const rect = container.getBoundingClientRect();
170-
const width = Math.round(rect.width) || 400;
171-
const height = Math.round(rect.height) || 280;
172-
if (width === 0 || height === 0) {
173-
retryId = requestAnimationFrame(initScene);
174-
return;
175-
}
176-
177168
canvas = document.createElement('canvas');
178-
canvas.width = width;
179-
canvas.height = height;
180169
canvas.style.width = '100%';
181170
canvas.style.height = '100%';
182171
canvas.style.display = 'block';
@@ -185,8 +174,11 @@ export function HomeView() {
185174
canvas.addEventListener('webglcontextlost', handleContextLost);
186175
canvas.addEventListener('webglcontextrestored', handleContextRestored);
187176

177+
const w = canvas.clientWidth;
178+
const h = canvas.clientHeight;
179+
188180
scene = new THREE.Scene();
189-
const camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 100);
181+
const camera = new THREE.PerspectiveCamera(50, w / h, 0.1, 100);
190182
camera.position.set(0, 2.5, 5);
191183
camera.lookAt(0, 0.5, 0);
192184

@@ -198,7 +190,7 @@ export function HomeView() {
198190
}
199191
if (cleanup) return;
200192

201-
renderer.setSize(width, height);
193+
renderer.setSize(w, h);
202194
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
203195
renderer.setClearColor(0x0f0f23, 1);
204196

@@ -443,7 +435,6 @@ export function HomeView() {
443435

444436
return () => {
445437
cleanup = true;
446-
if (retryId) cancelAnimationFrame(retryId);
447438
if (animationId) cancelAnimationFrame(animationId);
448439

449440
if (canvas) {

0 commit comments

Comments
 (0)