Embedding a Three.JS renderer within a canvas or div element

I've been attempting to embed a 3D model viewer in a responsive layout along with other content like text. Initially, I used window.innerWidth and window.innerHeight for setting the renderer size, but later switched to .clientWidth and .clientHeight to control it with CSS. However, the rendering does not seem to work correctly.

This website was quite helpful:

<!DOCTYPE html>
<html>
<head>
<title>threejs - basic</title>
<style type="text/css">
body{
margin: 0; 
overflow: hidden; 

}

canvas{
display: inline-block;
background: red;
width: 40%;
height: 40%;
}

</style>
</head>
<body>


<canvas id="myCanvas"></canvas>
<canvas>
         <p>hi</p>
</canvas>
<canvas id="myCanvas"></canvas>
<canvas id="myCanvas"></canvas>
<canvas id="myCanvas"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/95/three.js"></script>

<script >
var renderer, 
scene,
camera,
myCanvas = document.getElementById('myCanvas');
...

...
};

</script>

</body>
</html>

Answer №1

After reviewing your code, I have identified two issues:

  1. Avoid using the same ID for multiple elements. Instead, consider using a class and implementing a loop to trigger the rendering function for each canvas element.

  2. The correct usage of 'clientWidth' is for nodes selected with 'getElementById' or similar functions to map HTML nodes. If you are referencing the window object, use 'innerWidth' and 'innerHeight' instead. In this case, to set the canvas size as 40% of the viewport, utilize 'window.innerHeight * 0.40'.

I hope this information helps you optimize your code.

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 scroll() function in jQuery does not bubble up

Recently, I encountered an issue with a Wordpress plugin that displays popups on scroll. The code I currently have is as follows: jQuery(window).scroll(function(){ //display popup }); The problem arises with a particular website that has specific CSS ...

Is there a way to activate the CSS Overview feature if it is not available in the development tools menu?

I received a message stating: "To access xxx, you must have CSS enabled in your browser." Following the online steps provided: Launch DevTools (Command + Option + I on Mac; Control + Shift + I on Windows) Go to DevTool Settings (? or Function ...

Having trouble initiating the server using npm start

Just starting out with nodeJs: I have created a server.js file and installed nodejs. However, when I try to run "npm start", I encounter the following errors. C:\Users\server\server.js:43 if (!(req.headers &amp;& req.headers. ...

Tips on adjusting CSS code to ensure that a single accordion panel is open when the page first loads

I have a code that is functioning perfectly. However, I need to make a modification so that there is a default panel open when the page loads or refreshes. #acc-label { display: block; float: left; height: 330px; width: 50px; margin-bottom: 10px ...

"Utilizing the power of Angular 6's JSON pipe

Looking for a well-structured formatted JSON, but all I get is confusion and this strange image: https://i.sstatic.net/6mvBu.png Does anyone have any insights on what might be causing the issue? HTML <span style="font-weight: 500;">Payload Data: ...

What are the strategies used by AngularJS's official website to create SEO-friendly pages?

If you're interested in seeing how AngularJS.org presents pre-rendered content to search engine bots and scrapers, take a look at http://docs.angularjs.org/?_escaped_fragment_=/tutorial/step_09 I'm intrigued by the implementation strategy used f ...

Is it possible to create a horizontal navigation bar with buttons that scrolls when media queries are activated? I am looking for a solution using HTML,CSS, JavaScript, and Bootstrap

Struggling to create a horizontally scrollable navigation with buttons for media queries in Bootstrap 5.2, using SCSS for styling. This project is starting to feel overwhelming as we try to replicate the EA site. We're aiming to mimic the behavior of ...

Crafting Effective AngularJS Directives

Recently, I've been delving into AngularJS and have grasped the core concepts quite well. As I embark on building my own application, I find myself struggling with laying out the blueprint, particularly in terms of directive design. Are there any not ...

The event listener for the hardware back button in $ionicPlatform is being triggered repeatedly

Encountering a glitch with the back button being triggered multiple times. While I'm in the "messages" $state, everything functions normally when hitting the back button. var messageIsClosed = true; $ionicPlatform.onHardwareBackButton(function(even ...

Creating a custom JavaScript grid from the ground up

I'm intrigued to learn about the process of creating a JavaScript grid from the ground up. This grid should include features similar to jqGrid found at http://www.trirand.com/blog/jqgrid/jqgrid.html. Could someone kindly provide me with some guidance ...

What could be causing the canvasTexture in Three.js to appear unusually small?

Creating a Custom Canvas Texture for THREE.Sprite: const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = 320 canvas.height = 320 const colors = { border: &apos ...

Sleek Dialog Boxes with Bootstrap 4 - Bootbox Modal

Within the structure of my webpage, there resides a table containing various elements. One specific cell within this table holds a link that triggers jQuery scripts upon being clicked. An example action initiated by clicking this link is displaying a Boots ...

WordPress now features the ability to toggle both parent menu items when clicked, providing a more streamlined user experience

I am currently facing an issue with a menu structure, where parent menu items have submenus that need to toggle on click. However, I am encountering a problem where, upon placing the code inside a forEach loop, both submenus toggle instead of only togglin ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...

How do I capture data using an input tag in Node.js and save it in MongoDB?

I need help with inserting data from an input form and submit button into Node.js and MongoDB. Can someone provide me with tips or links on how to do this? :) <form method="post" ...> <input placeholder="Name & Surname"> <button type= ...

How can we eliminate the modal-open class in Angular when transitioning to a different URL?

Currently, I am facing an issue with a bootstrap modal. There is a button inside the modal which upon clicking should navigate the current component to another component named 'questions'. The problem arises when the new component is loaded, as t ...

Difficulty Loading Static JavaScript File in Express.js

Currently in the process of setting up an express server with create-react-app. Encountering this error in the console: Uncaught SyntaxError: Unexpected token < bundle.js:1 Upon clicking the error, it directs me to the homepage htm ...

Enhance your input text form with a stylish button using Bootstrap

This is the code snippet I am currently working on: <h2>Workout Selector</h1> <div class="form-group"> <label for="workout-name">Search by Keywords (Comma Separated):</label> <input ...

Using Angular 1's ng-repeat along with a filter to separate string values within a single JSON array

With the use of ng-repeat, I was able to append the values from a JSON array. However, I encountered an issue with the pictureURLS object containing string values of images. My goal is to separate the string values in pictureURLS and display the images ind ...

CSS: choose all elements starting from the n-th element and apply styles to them

Is it possible to choose specific child elements beyond the first few in a parent element? To clarify, let's say there's a div containing 7 spans. How can I select only the spans starting from the 3rd element onwards - so specifically span 4, 5, ...