Scaling Three.js Mesh to fit renderer while maintaining aspect ratio

Currently, I am using the STLLoader plugin from the Github repository of three.js.

This is a snippet of my code:

this.loadSTL = function(scene, stlFile){
      var loader = new THREE.STLLoader();
      loader.addEventListener('load', function(geometry, materials){
        var geometry = geometry.content;
        var material = new THREE.MeshPhongMaterial({ambient: 0xff5533, color: 0xff5533, specular: 0x111111, shininess: 200});
        var mesh = new THREE.Mesh(geometry, material);

        mesh.position.set(0, -0.9, 0);
        mesh.rotation.set(0, 0, 0);
        mesh.scale.set( 0.012, 0.012, 0.012);

        scene.add(mesh);
      });
      loader.load(stlFile);
    }

The issue at hand is that the object loaded from the STL file tends to change in height when the window resolution changes. Is there a specific setting we should be configuring to maintain a consistent width/height for the mesh regardless of the screen or window resolution?

Answer №1

One possible solution is to manage the window resize event like so:

const handleResize = () => {
    camera.aspect = canvas.width / canvas.height;
    camera.updateProjectionMatrix();
    renderer.setSize(canvas.width, canvas.height);
};

Answer №2

By adjusting the camera.aspect to be equal to 1, I was able to resolve the issue at hand. However, for a more accurate solution, it is advisable to set the aspect ratio as follows:

camera.aspect = $container.width/$container.height

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

Setting a background image in ReactJS

I've been struggling with this issue for a while now, and after doing a lot of research online, I came across a solution that is supposed to work in index.css body { background-image: url(../src/components/images/photo.jpeg) no-repeat center cent ...

Prevent any screen interactions by disabling clicks, and then re-enable them either after a set amount of

My app uses PhoneGap and jQuery Mobile. The issue I am facing is that when I navigate from page A to page B with a single click, everything works fine. However, if I accidentally double-click on page A and move to the next screen (page B) before it's ...

Create functionality for editing and reviewing content within an HTML webpage

Looking to add edit and review modes to my webpage. In edit mode, users can drag and drop divs to different locations or resize them. Once changes are made, users may want to review their work. I have included a link for the user to switch to review mode ...

typescript - instantiate an object using values stored in an array

Assume we have a model defined as follows. export interface Basicdata { materialnumber: number; type: string; materialclass: string; } We also have an array containing values that correspond directly to the Basicdata model in order, like this: ...

Converting JSON data into GeoJSON format using JavaScript

Currently, I am in the process of reading a JSON file and formatting it for GeoJSON function getAddress(id,search,vagas, next) { geo.geocode({address:search}, function (results,status) { if (status == google.maps.GeocoderStatus.OK) { ...

The Less compiler (lessc) encounters an issue on a fresh operating system. ([TypeError: undefined is not a function])

After setting up my new development environment on Windows 10, I encountered an issue with less. Following the instructions on lesscss.org, I installed less using: npm install -g less The installation process completed without any errors. However, when ...

Nightwatch encounters difficulty in accessing the iframe element

While utilizing the xquery selector, I attempted to input a value into the iframe's input field, but unfortunately, my efforts were fruitless. `.frame('someid') .setValue('//input[contains(@name,"project name")]', 'Nig ...

Connect radio input's value to object's key within AngularJS

Within my controller, I currently have an object structured as follows: $scope.colors = { green: "Green", yellow: "Yellow", red: "Red" }; I am attempting to dynamically generate radio inputs and then link the input value to the corresponding ...

AngularJS - Increase the input date by 6 hours before converting it to JSON

Is there a way to add 6 hours to an input date in AngularJS? I attempted the following code: var eventStart = new Date ($scope.event.startdateid); var startDate = new Date ( eventStart ); startDate.setHours ( eventStart.getHours() + 6 ); event.startdate ...

Encountering challenges with concealing a div element

As I'm setting up a table, I want to hide it immediately after creating it without affecting the DOM. Then, when the user selects from a dropdown menu, I show the table and everything works fine. However, the issue arises when I visit the page for the ...

Dealing with numerous identical ajax requests in jQuery

With 5 text fields where various amounts are entered, I utilize AJAX calls for calculations based on these values. The issue arises when the same AJAX call is repeated each time a user types in order to determine the total amount. The problem lies in the ...

The JavaScript functions are not loading within the onload event handler

I've got an HTML document that contains some Script sections. I've consolidated all the functions within a single Script Tag. Now, I want to be able to utilize these methods in both the onload and onclick events. Below is the HTML file with all t ...

Struggling to make the transition from JavaScript to TypeScript in Ionic 2

I recently updated the file extensions for my application files, including app/app.js. Error: Cannot find module 'app/app.js' from 'app_folder' The error message doesn't specify which file I need to locate to resolve the issue. ...

Using select tags in conjunction with JavaScript can add dynamic functionality to your website

I've been working on adding dropdown menus to my website and have been using code similar to this: <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</opti ...

Using jQuery to smoothly scroll to a specific class name

I am faced with an HTML code snippet that looks like this: <div data-stored="storenow" data-save="save" class="saveIcon" data-unique="game">Save</div> My intention is to use jQuery to scroll to the game number 456 as shown below. var contain ...

Personalizing the arrow positioning of the Angular8 date picker (both top and bottom arrow)

I am interested in enhancing the design of the Angular 8 date picker by adding top and bottom arrows instead of the default left and right arrows. Can someone guide me on how to customize it? Check out the Angular 8 date picker here ...

Dynamically assigning classes based on the element that is being scrolled beyond

I am working on a navigation bar (nav) that consists of letters and corresponding sections. My goal is to add an active class to the letter when a user scrolls to its associated section. For instance: When the user scrolls to a section with the id of a, t ...

Is it possible to retrieve values from my model when working with Django Templates and incorporating them in the JavaScript header?

With Django, I have managed to successfully retrieve and display data from my model in the content section of the template. However, I am facing issues retrieving data from the model in the header section of the template. Below is the code --> view.py ...

React's .map is not compatible with arrays of objects

I want to retrieve all products from my API. Here is the code snippet for fetching products from the API. The following code snippet is functioning properly: const heh = () => { products.map((p) => console.log(p.id)) } The issue ari ...

Acquiring a website's dynamic value using jquery

This question is pretty self-explanatory... I am trying to extract a value from a website's source code, but the value I need is dynamically generated using jQuery. Let's use example.com as an example: <div id="currentTime"></div> ...