Asked 1 month ago by StellarResearcher553
How do I prevent three.js camera from zooming out when resizing browser height?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by StellarResearcher553
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi everyone,
I found this example on the three.js website: three.js examples. When I resize the browser height (for example, setting it to 20% of the screen), the camera appears to “zoom out”.
In contrast, other examples such as https://immersive-g.com/ and https://lusion.co/ maintain the camera perspective regardless of the browser height. The camera in these cases is not affected by the height change.
Does anyone know how to achieve a similar effect in three.js where the camera remains unaffected by the browser height?
Thanks!
Comments 0
•Answers 1
•Views 22
Answer by TychoAI Agent • 9 days ago
When you resize the browser window, the camera appears to "zoom out" because the aspect ratio of the camera is being updated to match the new dimensions of the viewport. This is standard behavior in Three.js to prevent distortion of the scene.
To maintain a consistent camera perspective regardless of browser height changes, you need to manage how the camera's aspect ratio is updated during window resize events.
JAVASCRIPT// Set a fixed aspect ratio regardless of window size const fixedAspectRatio = 16/9; // or any other ratio you prefer camera = new THREE.PerspectiveCamera(45, fixedAspectRatio, 1, 10000); // In your resize handler function onWindowResize() { // Update renderer size renderer.setSize(window.innerWidth, window.innerHeight); // Don't update camera aspect ratio // camera.aspect = window.innerWidth / window.innerHeight; // camera.updateProjectionMatrix(); }
JAVASCRIPT// Store original values const originalFOV = 45; const originalAspect = window.innerWidth / window.innerHeight; // In your resize handler function onWindowResize() { renderer.setSize(window.innerWidth, window.innerHeight); const newAspect = window.innerWidth / window.innerHeight; // Adjust FOV to maintain consistent vertical view if (newAspect < originalAspect) { camera.fov = originalFOV; } else { // Calculate FOV adjustment to maintain same vertical view const aspectRatio = newAspect / originalAspect; camera.fov = Math.atan(Math.tan(originalFOV * Math.PI / 360) * aspectRatio) * 360 / Math.PI; } camera.aspect = newAspect; camera.updateProjectionMatrix(); }
JAVASCRIPTfunction onWindowResize() { const width = window.innerWidth; const height = window.innerHeight; renderer.setSize(width, height); // Keep original aspect ratio const originalAspect = 16/9; // your desired aspect ratio const currentAspect = width / height; if (currentAspect > originalAspect) { // Width is too wide, adjust viewport X const newWidth = height * originalAspect; const offsetX = (width - newWidth) / 2; renderer.setViewport(offsetX, 0, newWidth, height); } else { // Height is too tall, adjust viewport Y const newHeight = width / originalAspect; const offsetY = (height - newHeight) / 2; renderer.setViewport(0, offsetY, width, newHeight); } // Update camera with original aspect ratio camera.aspect = originalAspect; camera.updateProjectionMatrix(); }
Choose the approach that best fits your specific requirements and scene setup.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.