Having trouble resizing divisions using three.js?

Three.js is an incredible library that offers thorough documentation and functions perfectly.

I am currently attempting to display a plane with trackball control inside a div that resizes according to the window or browser size. However, I am encountering a specific issue:

Upon loading the browser, the plane is visible but does not respond to the trackball control. It only becomes active and works as expected when I minimize the browser or switch tabs. The changes are visible upon refreshing by minimizing or switching tabs, indicating that the trackball and scene are functioning correctly at load time. This issue may be related to rendering or refreshing on load.

Likewise, when resizing the browser, the div changes size but the scene remains frozen. Once again, resetting by minimizing or switching tabs allows the scene to resize and function properly.

I am struggling to identify the root cause of this issue.

Thanks a lot, Sam

<!DOCTYPE html>
<html
lang="en">

   <head>
      <title>three.js
         canvas
         -
         geometry
         -
         cube
      </title>
      <meta
      charset="utf-8">
         <meta
         name="viewport"
         content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
            <style>
               body {
                  font-family: Monospace;
                  background-color: #f0f0f0;
                  margin: 0px;
                  overflow: hidden;
               }
               #Cont3D {
                  min-width: 200px;
                  min-height: 400px;
                  float: none;
                  margin-left: 20%;
                  margin-right: 20%;
                  border-width: thick;
                  border-style: solid;
                  margin-top: 100px;
               }
            </style>
   </head>

   <body>
      <script
      src="build/three.js"></script>
         <script
         src="js/loaders/STLLoader.js"></script>
            <script
            src="js/renderers/SoftwareRenderer.js"></script>
               <script
               src="js/controls/TrackballControls_Persp.js"></script>
                  <script
                  src="js/modifiers/SubdivisionModifier.js"></script>
                     <script
                     src="js/controls/OrbitControls.js"></script>
                        <script
                        src="js/libs/stats.min.js"></script>
                           <script
                           src="js/Detector.js"></script>
                              <script>
                                 function startmusic() {
                                    var container, stats;
                                    var camera, scene, renderer;
                                    var plane;
                                    var targetRotationX = 0;
                                    var targetRotationOnMouseDownX = 0;
                                    var mouseX = 0;
                                    var mouseXOnMouseDown = 0;
                                    var targetRotationY = 0;
                                    var targetRotationOnMouseDownY = 0;
                                    var mouseY = 0;
                                    var mouseYOnMouseDown = 0;
                                    var Width, Height;
                                    init();
                                    animate();

                                    function init() {

                                       container = document.getElementById("Cont3D");

                                       var info = document.createElement('div');
                                       
                                       // Rest of the script...
                                            
                                      }

                                    function onWindowResize(event) {
                                       Width = container.clientWidth;
                                       Height = container.clientHeight;

                                       document.getElementById("logBox").innerHTML = Width + "," + Height;
                                       camera.aspect = Width / Height;
                                       camera.updateProjectionMatrix();
                                       renderer.setSize(Width, Height);
                                       controls.handleResize();
                                       renderer.render(scene, camera);

                                    }

                                    function animate() {
                                       requestAnimationFrame(animate);
                                       render();
                                       controls.update();
                                       stats.update();
                                    }

                                    function render() {
                                       renderer.render(scene, camera);
                                    }
                                    window.onresize = onWindowResize;
                                 }
                                 window.onload = startmusic;
                              </script>
                              <div
                              id="logBox"
                              style="position: absolute; top: 50px; width: 100%; text-align: center;">Blue</div>
                                 <div
                                 id="Cont3D">

                                    </div>
   </body>

   </html>

Answer №1

function handleWindowResize(event) {
    screenWidth = window.innerWidth;
    screenHeight = window.innerHeight;
    document.getElementById("messageArea").innerHTML = screenWidth + "," + screenHeight;
    renderer.setSize(screenWidth, screenHeight);
    camera.aspectRatio = screenWidth / screenHeight;
    camera.updateProjection();
    controls.handleResizeEvent();
}

Avoid using

renderer.render(scene, camera);

Inside your resize function.

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

"Utilizing multiple class names in Next.js to enhance website styling

Looking for a way to apply multiple classNames in Next.js, especially when dealing with variable classnames? I'm following the component level CSS approach. Take a look at my code and what I aim to achieve: import styles from "./ColorGroup.mod ...

Tips for customizing WTForm fields with unique CSS classes

I've recently put together a basic form snippet: class PostForm(FlaskForm): # for publishing a new blog post title = StringField("Title", validators=[DataRequired()]) submit = SubmitField('Post') The structure of my HT ...

The internal style sheet is being overridden by an earlier declared external CSS in the HTML document

Experimenting with the front end of a website using the latest version of bootstrap, 3.0. I am adjusting things by inspecting elements in Firebug, but something strange keeps catching my attention. Even though my <head> is structured like this: < ...

Tips for showcasing two cards side by side on mobile screens?

This is my glitch. I have a layout where I see 3 cards in a row for desktops, 2 cards per row on tablets, and 1 card for mobile devices. However, I would like the cards to resize and display 2 cards in a row on mobile. How can I achieve this? Here is the f ...

Gather data from various HTML elements with JavaScript when submitting a form

How can I extract values from HTML elements and send them for processing through a form? I'm attempting to compile a menu item list with the individual items' structure in place, but I'm struggling to figure out how to fetch the values upon ...

CSS or Coding? Learn how to display items side by side instead of on top of each other

Hey there! I manage a music website that is currently in beta. I'm running into an issue with the way the music results are appearing in the bottom player - they're all stacked on top of each other instead of being listed side by side. I've ...

Container that displays vertical scroll while permitting floating overflows

Is there a way to set up a container so that when the window size is too small, it displays a scroll bar to view all elements that don't fit in one go? At the same time, can the child containing floating elements be allowed to extend beyond the bounda ...

Having trouble with the clear button for text input in Javascript when using Bootstrap and adding custom CSS. Any suggestions on how to fix

My code was working perfectly until I decided to add some CSS to it. You can view the code snippet by clicking on this link (I couldn't include it here due to issues with the code editor): View Gist code snippet here The code is based on Bootstrap. ...

Utilize getElementsByClassName to dynamically alter the appearance of specific elements upon triggering an event

I'm attempting to utilize onmouseover="document.getElementsByClassName().style.background='color'" in order to switch the color of all divs with a specified classname to a different color when hovering over another element on the page. ...

Navigation bar links refusing to decrease in size

I am encountering an issue with the navigation bar on my website. When I reduce the size of the browser tab, the text remains the same size while the logo shrinks and eventually disappears. How can I ensure that everything scales down proportionally? Pleas ...

Content is the main driver for CSS Flexbox dynamic aspect-ratio code

When creating a grid layout for a webpage, I opted to use Flexbox. My goal was to incorporate some automatic functionality so that additional grid boxes could be included without the need to manually add classes or styles in the HTML code. One particular f ...

Revamp selected bootstrap attribute

Imagine a scenario where there is: @media (min-width: 576px) { #projects-edit-middle-column .equal-columns-wrapper .equal-columns { width: 50%; padding-right: 25px; } } This is being utilized within a container: <div class="equal-columns ...

Is it impossible to style an element within a div tag?

How can I apply styling to a specific div with id="1", which contains class="match"? Here is the code: <div class="col-lg-3"> <div id="1"> <p class="match">Match {this.state.matches[0].id}</p> <div class="tea ...

Android mobile browser lacks visibility of certain elements

please provide a brief description of the image The issue I am facing is consistent across all mobile browsers, with the exception of Firefox. It also manifests in Windows Chrome devtools mode and when the device toolbar is activated. I came across a sim ...

Is there a way to prevent this picture from shifting?

I am currently revamping the content on my work website using Joomla. I have received the old copy and now I need to enhance it. The website in question is 24x7cloud.co.uk. At the bottom of the page, I aim to include an "Accreditation's" section. Howe ...

Switch up the placement of articles, going back and forth between left

On the page, I want to display articles with alternating positions - one on the left, the next on the right. In this example, all the articles are currently being displayed twice, both on the left and the right side. How can I ensure that only one instanc ...

It is not possible to decrease the size of the image that is currently used as the background image

Three images are arranged in this manner: https://i.stack.imgur.com/u4PdK.png The HTML code for this setup is as follows: <div class="lb-controlContainer"> <div class="lb-closeContainer"> <a class="lb-close"&g ...

creating a div that functions similarly to a radio button

Is it possible to create a div that mimics the behavior of a radio button? I'm looking for a solution that doesn't involve jquery, as many people have recommended. After doing some research, I found a few potential options. In the past, I'v ...

Ways to identify when a React child element is overflowing

tl;dr How can I create a component that hides overflow and toggles views with a button? For example, the user can initially see tabs 1, 2, and 3 while tabs 4, 5, and 6 are hidden. Clicking a button will hide tabs 1, 2, and 3, and show tabs 4, 5, and 6 with ...

The final li element in the second unordered list is targeted by the selector ul li:last-child

Check out this JsFiddle Having trouble with the pseudo code :last-child. I want the style to apply to the li containing E, but it's affecting the last li in the .subNav. I've attempted a few solutions below, but none have worked. nav ul:first- ...