Rotating a loaded file using THREE.js

After experimenting with the ThreeJS library for a few weeks and drawing inspiration from others, I managed to create a canvas that displays an external .stl-file.

However, my main issue is that I am struggling to apply any transformations to it. While I can move the camera around smoothly, basic transformations do not seem to work. Due to my limited knowledge of JavaScript, I may have overlooked some crucial elements.

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - STL</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 {
background-color: #000000;
margin: 0px;
overflow: hidden;
}

</style>
</head>
<body>
<script src="../build/three.min.js"></script>

<script src="js/loaders/STLLoader.js"></script>

<script src="js/Detector.js"></script>
<script src="dat.gui.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var container;
var rotationSpeed;
var camera, cameraTarget, scene, renderer;

init();
animate();

function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
camera.position.set( -3, 1, 3 );

cameraTarget = new THREE.Vector3( 0, 0, 0 );

scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
// Remaining code not displayed for brevity

}

The final lines before the render() function only result in a blank, black page being displayed. Even simple operations like model.position.x += 0.02; seem to disrupt the functioning.

Answer №1

After carefully reviewing the code, I have identified an important issue, however, I am encountering difficulties in transferring the code to JSFIDDLE at this time.

The issue lies in the fact that the model variable is not declared in the global scope. It is only initialized locally within the init() function, which causes problems when trying to manipulate it within the render function where it does not have any scope.

To resolve this issue, you simply need to declare the variable model before calling the init() function, similar to how you declare other variables in the code. You can fix this by adding the following line:

var model;

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

What is causing this console to output twice?

My Objective: I aim to utilize Node.js to launch two child processes sequentially at a specific time, displaying their `stdout` as it streams, occasionally alternating between the two processes. The Desired Output: `Proc 1 log # 1` `Proc 1 log # 2` `Pr ...

Encountered error: "Node.js and socket.io: Address already in use"

Experimenting with chat using Node.js and socket.io Currently, I am running Ubuntu 12.04 as a user and have a folder "pp" on my desktop. In this folder, I have placed a server file named server.js. Below is the client code: $(document).ready(function() ...

I'm having trouble getting my HTML page to display appended text. What could be causing the issue?

let mainDiv = document.getElementById("main"); let myParagraph = document.createElement("p"); let myTextNode = document.createTextNode("hello"); myParagraph.append(myTextNode); mainDiv.append(myParagraph); let max = 25; let on ...

Utilize AngularJS to reattach a DOM node rather than simply updating it

Within my template, I have the following setup: <div> <a ng-href="tel:{{phone}}">{{ phone }}</a> </div> Angular updates the link's DOM node when $scope.phone changes, linking it to the new phone number with no issues. Ho ...

What causes the result of UNDEFINED when accessing an ENVIRONMENT VARIABLE in a REACT application?

Unfortunately, I've hit a roadblock with this seemingly simple question and can't seem to find the answer. I'm having trouble accessing environment variables in a specific file. I am using create-react-app (^16.11.0) The .env file is loca ...

Discovering the destination link of a website while utilizing GM_xmlhttpRequest

Picture yourself browsing a webpage called "www.yourWebsite.com" and utilizing userscripts in Tampermonkey to extract data from another site using the GM_xmlhttpRequest function. As you make requests with the GM_xmlhttpRequest function to visit "exampleWe ...

AngularJS dynamically creates an HTML template that includes an `ng-click` attribute calling a function with an argument

Struggling to create an HTML template using an AngularJS directive, the issue arises when trying to pass an object into a function within one of the generated elements. Here is the directive code in question: app.directive('listObject', function ...

Unable to alter the protocol option of the request object in SailsJS

Currently, I am utilizing a library that relies on the req.secure option to proceed without any errors. However, my application is deployed on Heroku and I have implemented custom middleware to check the "x-forwarded-proto" header and set req.secure as tru ...

Enabling Cross-Origin Resource Sharing (CORS) with Javascript on the client side during

Currently, I am attempting to incorporate another website's login verification system into my own site. Although the code below is successfully retrieving the correct response ID, I am encountering CORS errors preventing the completion of the login pr ...

How can I retrieve the span html element without using a class with ajax?

Trying to work with Ajax syntax for the first time, I have this HTML code. <div class="select2-container select2" id="s2id_ServiceID-2-1" style="width: 100%;"> <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabind ...

Ways to retrieve the pending count of XMLHttpRequests in JavaScript

I am trying to figure out how to capture the count of pending xmlhttprequests in a web page using javascript while working with selenium and python. It seems like there is a useful link that could help me get this information, but I'm having trouble ...

What is the best way to update the src of an input using an onclick event?

When I click the input, my background changes color as expected. However, the src of the input only changes on the second click. How can I make it change the source on the first click? Also, how can I change the src back to the original with a second cli ...

Utilizing AngularJS to create an auto complete feature integrated with a SQL Server database

I have a SQL database table with columns as follows: Row1 Row2 Row3 Id Country 1 1a 1b 34 Europe 2 2a 2b 45 US 3 3a 4d 5g Australia I am currently working on implementing an autocomplete feature ...

Alter the colors of all Divs inside a container with each click

Hey everyone, I'm just starting to learn JavaScript. I'm working on a project where I want to change the color of all the divs in a container every time they are clicked. For example, on the first click, I want all the divs to turn red. Then, on ...

Having trouble displaying dynamically generated texture from a fragment shader in ThreeJS

I've been working on creating a texture using a Three.WebGLRenderTarget and then trying to access it in a fragment shader in the following stage. The concept is to execute the first stage once to generate a complex and costly SDF map, and then access ...

What are the steps for building modules using Vuex and fetching data using mapState()?

I've been experimenting with separating my Vuex code into modules, but I'm having trouble retrieving data using mapState(). What's the most effective approach for creating modules and utilizing mapping? Here's the structure of my stor ...

Tips for integrating React into a jQuery-centric web application?

After diving into React, I'm eager to showcase my skills by implementing it on select pages as a proof-of-concept for my supervisor at work. Can anyone guide me on how to achieve this using traditional script tags instead of imports? So far, I'v ...

Is there a way to utilize ajax to submit a form and upload a file to a spring controller?

I have a form with four fields: file, name, type (as a string), and taskInstanceId. <form> <table id="documentDetailsTable"> <tr> <td>Document Type: </td> <td><select id="documentType" ...

The auto-play feature fails to function on iPhone devices when using Reactjs

I am currently working with Reactjs and Nextjs. I have a video on my website that is functioning properly on Android phones but not on iPhones. How can I resolve this issue? I have attempted the following code: <video loop autoplay='' muted> ...

Guide on incorporating jQuery library files into existing application code with the npm command

Recently, I used a node JS yo ko command to create a single-page application using Knockout-JS. Following that, I proceeded to install jquery packages using the command npm install jquery The installation was successful. However, my current goal is to in ...