Adjusting the aspect ratio of an ortographic camera when resizing

Is there a method to configure an orthographic camera in THREE.JS in a way that:

1)It retains a consistent aspect ratio regardless of window resizing. This aspect ratio should remain constant and not be affected by the window's dimensions or aspect ratio, thus avoiding distortion.

2)Ensures that the entire scene is always visible, regardless of the window size.

Thank you in advance!

Answer №1

To ensure that your entire scene fits within the view of your current camera, adjustments can be made to the variables of an OrthographicCamera including left, right, top, bottom, near, and far. It is important to maintain the relationship left = -right and top = -bottom, while also ensuring the aspect ratio matches that of the window. This essentially involves modifying a single scale value.

Rather than individually processing all points in the scene, a more efficient approach is to approximate the scene with a bounding sphere of radius r centered at point c. By calculating these properties beforehand or storing them with the scene, the focus shifts from fitting the scene to ensuring the entire bounding sphere is visible within the view.

For an orthographic camera, determining the bounds is straightforward. Transforming the sphere's center into camera coordinates is a key step:

var cInCameraSpace = new THREE.Vector3(); //initialize this once

//transform sphere center to camera space
cInCameraSpace.copy(c);
cInCameraSpace.applyMatrix4(camera.matrixWorldInverse);

The bounds can then be calculated as follows:

var halfWidth = Math.abs(cInCameraSpace.x) + r;
var halfHeight = Math.abs(cInCameraSpace.y) + r;
camera.near = -cInCameraSpace.z - r;
camera.far = -cInCameraSpace.z + r;

Next, ensuring the aspect ratio is maintained based on the window's aspect ratio, denoted as windowAspect, is crucial:

if(halfWidth / halfHeight > windowAspect) {
    camera.right = halfWidth;
    camera.top = halfWidth / windowAspect;
} else {
    camera.top = halfHeight;
    camera.right = halfHeight * windowAspect;
}
camera.bottom = -camera.top;
camera.left = -camera.right;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Over time, memory usage increases significantly in JS applications that heavily rely on Ajax

I've noticed significant memory leaks in an app I'm currently developing. Despite its lack of complexity, the app requests approximately 40kb of JSON data from the server every 15 seconds. This data is then used to generate a table on the page. A ...

I am unable to retrieve the local variable beyond the function

I'm having trouble accessing a local variable outside of a function even though I have initialized it before the function is called. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jq ...

Creating a 3D cube with visual graphics as its faces using Three.js

I've been experimenting with different solutions but haven't had any luck finding a solution online. The error I'm encountering when running my program is: XMLHttpRequest cannot load file:///C:/Users/winst/Documents/Programming%20Projects/Mi ...

Capturing page titles accurately for timeonsite tracker in a single-page Angular app is challenging when navigating to other pages

Implemented the timeonsite JS tracker in my Angular web application using HTML tags as shown below, <script type="text/javascript"> var Tos; (function(d, s, id, file) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementByI ...

Including jQuery in an Angular project generated with JHipster

I am facing a challenge with integrating jQuery into my Jhipster Angular project as a newcomer to Jhipster and Angular. My goal is to customize the theme and appearance of the default Jhipster application, so I obtained a theme that uses a combination of ...

What is the best way to store React form data in a queue for future processing?

I am currently developing a ticketing system in React, utilizing Node.js for the backend and Python for the email server. My goal is to process form submissions by extracting all the form data, creating an instance of a plain JavaScript class with only a ...

Triggering a loop error may occur when attempting to update the state based on a

What I want to achieve is updating the state with the value received from a child component. However, whenever I try using the setstate method in my function, it results in a looping error. Here's a snippet of my code: class ParentTab extends Compone ...

Enhancing user experience with dynamically resized Jumbotron images based on screen sizes in HTML

I am experiencing an issue with the jumbotron images on my website where they become increasingly zoomed in as the screen size decreases, and are extremely zoomed in on mobile devices. Is this a normal behavior of jumbotrons? I do understand that the image ...

The command is failing to include functionality with the yarg npm package

I have been attempting to incorporate a command using yargs, however, after executing my code, the command does not seem to be added successfully. Below is the snippet of what I am attempting: const yargs = require('yargs') //create add command ...

Here is a unique rewrite: "Utilize jQuery to extract all data-id and amount from an HTML page, then send it through an

Is there a way to retrieve the data-id and amount values from this HTML page using jQuery? Once I have gathered that information, I would like to store it in an array and then submit it via an AJAX call. It's important to note that this is a Laravel p ...

Choose All Box for Dynamic Tables in AngularJS

Hi everyone, I'm currently working on adding a select-all checkbox to the top of my list of checkboxes using a custom directive. I found some guidance on how to do this in a thread that I came across: https://github.com/lorenzofox3/Smart-Table/issues/ ...

Determine whether an object is present in the THREE.JS scene

How can one determine if there is an object present in the scene? Is there a specific function that works similarly to isEmpty()? ...

Is it advisable to Utilize ES6 Classes in Javascript for Managing React State?

Is it recommended to use ES6 classes directly as React state? I am interested in creating an ES6 class that: Contains member variables that will trigger re-renders on the frontend when changed. Includes methods that sync these member variables with the ...

How do I use onclick to hide a <div> and reveal the content beneath it?

Is there a way for me to hide this <div> when the user clicks outside of it? I want the content behind it to be visible once the <div> is hidden. <html> <style> .overlay { position: fixed; top: 0; left: 0; height: ...

Adjusting the transparency of voxels in Three.js using an array

I am currently working on a project to create a simple voxel viewer using three.js. The visibility of the voxels should be based on whether the corresponding cell in an array is set to 0 or 1. While the voxels display correctly when I manually set the opa ...

By utilizing the power of JSONP, data transfer limitations can be eliminated

Is it possible to remove the limitation on data sent using JSONP? Here's my code. I'm attempting to send 3000 characters (an image converted to base64 data) at a time to a service (serviceCall.ashx). Since my data is quite large, up to 30,000-40, ...

Is there a way to place my searchbox in the top right corner of the page?

I am currently working on creating a search function for my list of products. However, I have encountered an issue where the searchBox is not appearing in the top right corner as intended. Despite trying various solutions, I have been unsuccessful in movin ...

I have the ability to utilize local storage within NextJS with the integration of redux-thunk

Issue with using local Storage in file Store.js An error occurred during page generation. Any console logs will be displayed in the terminal window. In Store.js import { createStore, combineReducers, applyMiddleware } from "redux"; import thunk from " ...

Utilizing AJAX to retrieve data from a PHP function

Seeking to display the output of an AJAX call; This is my PHP code: if(isset($_POST['date']) ) { echo "<input type='radio' id='date'/>"; } And here's my AJAX code: $.ajax( { type: "POST", url: "sched ...

Eradicate HTML by assigning empty values to certain attributes

Allowing the user to copy the HTML code of a div by clicking a button. Some attributes, such as for videos: <video loop muted autoplay> may appear like this after copying: <video loop="" muted="" autoplay=""> The ...