Rotating a 3D cube on its axis using Three.js

I am currently experimenting with rotating a cube on a corner, aiming to make it spin like the image shown in the link below.

As a newcomer to Three.js, I apologize if this question seems basic.

Below is the code for my current setup:

var geometry= new THREE.CubeGeometry (200,200,200, 4, 4, 4);
             // material

            var material = new THREE.MeshLambertMaterial({
            map: THREE.ImageUtils.loadTexture('inc/cube.jpg'), overdraw: true
             }); 



            cube = new THREE.Mesh( geometry, material );
            cube.overdraw = true;
            cube.position.y = 80;
            cube.position.x = 5;
            cube.position.z = 6;
            cube.rotation.z = 14.9;
            cube.rotation.y = 0 ;
            cube.rotation.x = 0 ;


            scene.add(cube);

Additionally, I have noticed that when I set the dimensions of the CubeGeometry to (200,200,200) without the 4's, the texture on the cube warps. Does anyone know how to prevent this warping?

For rendering, I am using the following:

renderer = new THREE.CanvasRenderer();
                renderer.setClearColorHex(0xffffff, 0);
                renderer.setSize( window.innerWidth, window.innerHeight ); container.appendChild( renderer.domElement );

While this setup seems to function well on most browsers, I have encountered issues with canvas opacity when using the WebGL renderer.

To view the current state of the project, please visit

Answer №1

To bring a new perspective, consider rotating the cube 45 degrees along a different axis from the previous rotation. I refrain from using THREE as it complicates rather than simplifies the problem - keep in mind the general rule for object rotations: first move the object's center to the origin if necessary, then rotate on each required axis, and finally move the object back to its original position (opposite of the initial translation). I trust this explanation proves beneficial.

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 Bootstrap tabs are not correctly displaying the content

Hey there, I'm currently working on a project that involves using bootstrap tabs (BS4) within a form. One of the requirements is to have forward and back buttons for form navigation, and I'd like to leverage BS4's tab('show') fun ...

swap out a method within a publicly available npm module

Currently, I am using an API wrapper package that utilizes the request module for making API requests. While this setup works well in most cases, I am facing a situation where I need to use this package in a node-webkit environment and replace the request ...

Issue with Bootstrap Table Style When Using window.print();

The color of my Bootstrap table style is not displaying correctly in the print preview using window.print(). Here is a screenshot showing that the table style is not working properly: https://i.stack.imgur.com/eyxjl.jpg Below is the code I am using: < ...

When the "errors" property is not utilized, the error "Cannot read property '_transitionClasses' of undefined" is encountered in vue.runtime.common.js

I encountered a strange issue while running a test on a component that utilizes vee-validate. What's even more bizarre is that the error doesn't occur when I use this.errors in the component (even simply including console.log(this.errors) seems t ...

Developing HTML components for the purpose of testing JavaScript

I am encountering a specific issue in my react component where I am using the following commands: document.getElementById('modalsContainer').appendChild(recognitionProfileZoom); document.getElementById('modalsContainer').appendChild(ca ...

Is it possible to redefine a function that is attached to $ctrl outside of the AngularJS framework?

Within my DOM, there exists an element containing ng-click="$ctrl.goToHome(), which is connected to the logo on my site. This particular element is generated by a third-party AngularJS application. The complete element can be seen below: <img ng-if=":: ...

What is the best way to transform a collection of items into FormData?

In my current project, I have a JavaScript array structured as follows: var items = [{ ID: "1" count:'1', File: (binary file) }, { ID: "2" count:'2', File: (binary file) } ] My goal is to ...

Tips for adding your artistic touch to photos

I'm currently facing a challenge in creating a chart that overlaps an image. The bars and text on the chart are displaying perfectly when I remove the background image. However, I'm struggling to get the bars and text to appear on top of the imag ...

What is the best way to integrate Angular 5 with various sources of JavaScript code?

I am fairly new to angular. I am looking to merge an external angular script file and on-page javascript code with angular 5. I understand that angular does not typically allow for the inclusion of javascript code in html components, but I believe there m ...

Instructions for passing a JavaScript variable to a PHP MySQL database

I am attempting to integrate a JavaScript variable into PHP MySQL, but I'm encountering issues with the insertion. Currently, it is being inserted as <javascript>document.write(window.outerWidth); </javascript> x <javascript>document ...

Performing two ajax calls within a non-existent div that has only just been appended

I've been developing a website similar to 9gag. I attempted to incorporate a voting feature created by someone else, as I'm not well-versed in AJAX requests, but it just doesn't seem to be functioning. My index.php file fetches five posts f ...

What sets apart .addEventListener and .on() when it comes to including an event in Bootstrap 5?

I'm currently attempting to attach a listener to my modal in order to implement a certain behavior each time the modal is opened. As per the guidance from Bootstrap 5 documentation, you can achieve this by using: https://getbootstrap.com/docs/5.2/com ...

Integration of Angular.js functionalities within a Node.js application

After working on my node.js app for a few weeks, I decided to add some additional features like infinite-scroll. To implement this, I needed to use packages in node.js along with angular.js. So, I decided to introduce angular.js support to the app, specifi ...

Place a drop-down menu inside the input box

Struggling with this issue, I've come across various messy solutions, but I'm determined to find a cleaner one. What I'm looking for is an input field that includes a dropdown selection on the left side. This is essentially what I have in m ...

Cannot use Axios instance in Next.js due to error stating 'Localstorage is not defined'

I am currently working on a React app and have created an Axios component that I would like to reuse: import axios from 'axios' import dynamic from 'next/dynamic' const baseUrl = 'http://127.0.0.1:8000/' const axiosInstan ...

External function does not support jQuery types

In my theme.js file, I currently have the following code: jQuery(function ($) { accordion($) }) const accordion = ($) => ... By placing the accordion function directly into the jQuery function, Typescript is able to assist with the installed jquery ...

JS function to reverse a string

Not to worry, I've managed to figure out how to reverse a string (a one-word string at least). > function reverse(s){ > return s.split("").reverse().join(""); } Is there a way to reverse a string in this manner: "Dady come home" -> "yd ...

Flexbox Resizing Notification(?)

Recently, I've been utilizing flexbox in my layout design and it has been a game-changer. However, I am facing an issue that might be linked to my heavy use of AngularJS, particularly the ng-include feature. The specific problem arises when I incorpo ...

Is there a way to selectively update specific keys within existing objects in Mongodb without affecting the rest of the

Here is the scenario I am dealing with: Within my Angular 8 application, I am responsible for creating and managing invoices. The structure of an invoice object looks like this: { "_id": { "$oid": "5ea9ad58f65d8d49841362bd" }, "details": [ { ...

Error arises when uploading csv files to Node.js with Multer due to an unexpected field presence

I'm currently working on implementing a file upload feature with the use of Node.js, Vue, and Multer. Below is the Vue.js front-end code: export default { data(){ return{ selected: "How do you want to input the data?", options: [ ...