Events are causing the Three.js canvas to resize

Currently, I am developing a mobile-web application using three.js.

I have incorporated the meta viewport tag as well:

<meta name="viewport" content="width=device-width; user-scalable=no; initial-scale:1.0; minimum-scale=1.0; maximum-scale=1.0;" >

The issue I am facing is that the canvas is being resized after raycasting for click events.

The element.style attribute of the canvas has been altered:

<canvas width="1081" height="2029" style="width: 980px; height: 1081px;">

changed to

<canvas width="1081" height="2029" style="width: 412px; height: 773px;">

<!--
and the change in size depends on the screen size of the phone... mine was 412 x 773.
-->

I am unsure about where the canvas's style is being changed and set...

To address this issue, I added the following CSS codes:

  .canvas{
    width: devicve-width !important;
    height: device-height !important;
  }

  canvas{
    width: devicve-width !important;
    height: device-height !important;
  }

However, these changes did not resolve the problem.

The desktop version of the web application works perfectly, but the mobile version encounters this error.

I have not explicitly reset the canvas's size anywhere, so I am uncertain about what steps to take next.

At this point, there are many lines of code (none of which resize the canvas), which leaves me feeling quite puzzled.

Are there any specific methods in three.js to define the canvas's size?

Or perhaps a workaround to prevent this resizing issue?

Your assistance is greatly appreciated.

Answer №1

Make sure to check out the ThreeJS example that comes as default. These are the lines of code that work like magic.

function adjustWindowSize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
}

To see how they're implemented, you can refer to this link.

Answer №2

To adjust the size of the canvas in Three.js, use the following code snippet

renderer.setSize(window.innerWidth, window.innerHeight);

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

"The Material-UI ListItem component acts as a link, updating the URL but failing to render the expected

Seeking help for the first time on this platform because I am completely perplexed. I'm working on coding a navbar for my school project website using Material-UI's List component within the Appbar component. However, I have encountered two issu ...

Send the context parameter value of Unified Service Desk to a JavaScript web resource in CRM

Currently, I am working on CRM 8.2 and Unified Service Desk 4.1. I have a specific requirement where I need to pass parameter values from within Unified Service Desk Data Parameters to a JavaScript Webresource. I have come across some resources that sugge ...

What is holding Firestore back from advancing while Firebase Realtime Database continues to evolve?

I am currently working on a chat application using Firebase within my Vue.js project. In this setup, I need to display the user's status as either active or inactive. To achieve this, I implemented the solution provided by Firebase at https://firebase ...

Refresh ng-repeat array after implementing filter in controller

I am currently facing an issue with updating my table view when changing a variable that filters an array. The filter is applied in the controller based on the values of a specific variable called columnFilter. However, the filter does not reapply to updat ...

Retrieve the specific item from an object array using the designated key

I am working with an array of objects and my goal is to retrieve the object that has the key "Z". While I could iterate through the array, checking each key one by one until finding a match, I believe there might be a more efficient solution than my curre ...

Redux Persist causes the redux state to become undefined

I recently added redux-persist to my project and now I am facing an issue where all of my Redux states are returning undefined. Previously, all the Redux states were functioning properly. However, after incorporating redux-persist, they are no longer work ...

Screen the information passing through the Nivo Bar Chart

With react-table, I have successfully implemented data filtering but I am looking to enhance the dynamism of the filter. The current challenge lies in filtering the data displayed on a graph to make it more meaningful. I want to group products from the sam ...

What is the best way to incorporate JavaScript in a repeater?

I am currently working on an asp.net project that involves using a repeater. <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <asp:label ID="lblexp" runat="server" Text="05/11/1 ...

Using Svelte to effectively connect to a specified object within an array

Check out this code snippet: <script> let data = [ {id: 1, first: "x"}, {id: 2, second: "y"} ]; </script> <input type="text" bind:value={data.first}/> If you modify the value in the input field and ...

Utilizing React and Google Code to Enhance Lead Conversion Pages

I have developed a basic react application featuring a contact form. Upon submission, I aim to display the Google Code for the lead Conversion Page within the application. <!-- Google Code for Purchase Conversion Page --> <script type="text ...

Why does a React error keep popping up when trying to set a background-image in my CSS?

I've been working on my React project and I can't figure out why I keep encountering this error. I double-checked the URL paths and made sure they were named correctly, yet the error persists. Here is a snippet of my CSS: background-image: url ...

JavaScript events are failing to trigger within the content area of an iframe

When attempting to run a given example by double clicking inside the content area of an iframe, the clicks and any JavaScript events are not being fired. Currently, the events can only be triggered by clicking on the border of the iframe, not inside the ...

Tips for using an array as a jQuery selector in JavaScript

Managing an element with an array id id="x[]" can be tricky when it comes to dynamically changing content based on database entries. This specific element is essentially a delete button for removing table rows from the database. <div align="center" id= ...

Turn off the incorrect TypeScript error detection

After setting up 'interact.js' using jspm and npm for TypeScript compatibility, I am encountering errors in my code: import { interact } from 'interact.js/interact' // ==> typescript error: TS2307: Cannot find module 'interact. ...

Styling your content with Bootstrap 4: a sleek col-md-4 design featuring

I have been using this carousel component: https://i.sstatic.net/2haSi.png One issue I have noticed is that there is a border around each block in the carousel. Specifically, on the first slide, the left shadow is not visible, and on the third slide, th ...

Getting started with imports in typescript package

Having trouble with imports in a TypeScript package. I have two classes, A and B, located in the models directory. Currently, I am importing class B into class A like this: import B from "models/B" interface A { b: B } export default A; H ...

Troubleshooting Problems with Cascading Style Sheets

Struggling with a layout issue on my website. I need to display an image with text at the beginning of the site-wrap div under the name background-image, with the text inside that div. Adding line-height to the text div adjusts its height, but I want to m ...

Troubleshooting: Why Your Angular Data Binding is Failing

I am integrating a WCF REST service with an AngularJS application. My goal is to retrieve account information based on the account number provided, however, I am encountering an issue where the text "Account_Type" is displayed three times before showing th ...

The amazingly efficient Chrome quick find feature, accessible by pressing the powerful combination of Ctrl + F, ingeniously

Currently using Google Chrome version 29.0.1547.62 m. I've employed the CSS attribute overflow set to hidden on the parent element, which renders some of my DIV elements hidden from view. Additionally, these concealed DIV elements are adjusted in pos ...

What benefits does Next.js offer compared to using plain React for rendering 3D models with three.js?

In a previous project, I utilized a 3D product configurator application and am now curious about the potential performance improvements (specifically FPS) that "server-side rendering" with Next.JS could provide compared to using plain React. ...