Having difficulties with incrementally rotating a CSS cube to every direction

Can anyone help me with rotating a cube one side at a time using CSS? I can make the cube rotate to the top / bottom and each side correctly, but when I try to rotate to the top or bottom first and then to a side, the cube spins without rotating left or right.

You can check out my codepen example here:

https://codepen.io/exosyphon/pen/yLGOWyL

The expected behavior is to rotate to the bottom (6), then rotate to the right and see another side (4).

However, the current behavior is that it rotates to the bottom (6) but when trying to rotate to the right, only 6 is visible.

I have a feeling this issue might not be solvable with just CSS.

var cube = document.getElementById('cube');

var min = 1;
var max = 24;
var diceX = 0;
var diceY = 0;

document.getElementById('top').onclick = function() {
diceX += 90;
cube.style.transform = 'rotateX('+diceX+'deg) rotateY('+diceY+'deg)';
}

document.getElementById('side').onclick = function() {
diceY += 90;
cube.style.transform = 'rotateX('+diceX+'deg) rotateY('+diceY+'deg)';
}

Click the dice to roll

Answer №1

It's important to note that the issue at hand is related to JavaScript rather than CSS, making it challenging to resolve using CSS alone.

When rotating the cube, its axis and faces change, leading to erratic behavior where rotation may be correct on certain axes but not on others.

For further insights, you can explore similar questions that have been asked previously:

  • How to properly rotate a cube on click?.
  • 3D CSS Cube - Strange Rotation Inconsistency (Solved: Gimbal Lock)

These linked questions shed light on the challenges faced when dealing with rotations across multiple axes.

To delve deeper into this complex topic is recommended for a better understanding of the issue.

Building upon the reference provided above, I have developed a functional solution for your code.

Here are some key changes to be aware of:

  • The addition of the 'gimbal' class to the div with the cube id
  • Including the 'gimbal' class in the CSS with required styles
  • Setting cube position using translate instead of top to align with the transform-origin property for 'gimbal'

(JavaScript and CSS code snippets have been included for reference)

The technique employed involves adding a new parent div above the cube each time a button is clicked. This parent div is then rotated instead of the cube, keeping the orientation of the inner divs intact while applying a single rotation to the new parent element.

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

Can test files be placed under the pages directory in a Next.js project?

Currently, I am in the process of writing integration tests for the API routes within a Next.js application. One question that has arisen is whether it would be problematic to place the index.test.ts file under the /pages directory. My preference leans tow ...

Optimal practices for localization

Looking to translate our site into multiple languages starting with one language but planning for more in the future. Considering the most effective way to accomplish this. The plan is to use subdirectories to organize the html files, with shared files su ...

What is causing the newly generated input tags to disappear from the page?

I'm having an issue where my code successfully creates new input tags based on user input, but they disappear shortly after being generated. What could be causing this to happen? <form> <input type='text' id='input' /> ...

Set up AngularJS routing for accessing the Web API endpoint

I have encountered an issue with my AngularJS application and Asp.net Web Api. Both applications are hosted on port 80 of the IIS server. The problem arises when the web api URL cannot be accessed due to angularjs routing redirecting API calls to the root ...

As one container is being moved, they both shift down simultaneously

I have set up a container named main that contains another container called banner, like this: <div id="main"> <div id="banner"></div> </div> Main has a gradient background and I want the banner to be positioned in the middle of t ...

Accelerate the generation speed by using JsPDF to convert canvas to PDF

I need to optimize the download speed of a specific div on my webpage as a PDF using jspdf and canvas. Currently, it takes 2 or 3 seconds which is too slow for my client's liking. Is there a way to make this process faster? Additionally, the file size ...

Placing an image on top of a background image in CSS while maintaining a fixed aspect ratio

My CSS code is currently resizing the background image to fit the browser size while keeping the center of the image in the center of the window. Now, I need to add another image, like an icon in a circle, at the center following the same aspect ratio rule ...

Combining HTML, PHP, and Ajax within a single document

Hey everyone! I'm diving back into web programming and challenging myself to create a simple mailing form using just HTML, PHP, and Ajax all within a single file. The goal is to have a self-contained HTML document that doesn't refresh when the fo ...

Mozilla browser experiencing issues with mouse move event functionality

Here, I have a background image on the body and with the following script, when the user moves the mouse, the image in the background also moves. body { background-image: url('../images/1.png'); background-size: 98%; background-posi ...

What is the function of typekit.com and how does it utilize @font-face technology

What is the functionality of typekit.com? Several websites similar to typekit enable CSS designers to utilize custom fonts, but how does it work? Is it possible for me to create a site like typekit myself? What technologies are involved in this process? A ...

"Enhancing User Experience with Animated Progress Bars in Three.js

Is there a way to create a progress bar animation in three.js? I've been grappling with this issue for quite some time now. I attempted to replicate the html5 video player progress bar method, but unfortunately, it doesn't seem to be compatible w ...

When hovering or mousing over, the text remains stationary and does not follow the scroll bar movement within the

A method I am using involves displaying additional data on hover for a shortened header, like this: Hover behavior However, the text shifts across the page when the scrollbar is used, as shown here: Scrollbar interaction This table showcases HTML, CSS, ...

Alt and title attributes for images are getting mixed up when there is a space in between

Every time I include a space in the alt tag of my image, it completely distorts the entire <img> line. For example, if my image title is Windows 10, specifically related to today's news. Let's assume my image tag reads Windows 10 officiall ...

Creating dynamic forms and tables on an HTML webpage

I'm struggling to dynamically add table elements and form elements in an HTML page. My goal is to automatically add input boxes with labels when a user enters a number in the input box without having to click any button. I believe using the 'keyu ...

Using `require` in place of `script` tags in a three.js file when working with Node.js environment

Instead of using the html script tag, I have implemented javascript require in my three.js file for node.js. Here is a snippet from an html file: <script src="../build/three.js"></script> <script src="js/controls/DragControls.js"></s ...

Singularize button/solo in the center

How can I make this button align perfectly center on both PC and Mobile as a container? I have attempted the following CSS and HTML code: <section id="jour"> <div class="container"> <button class="btn1">Jeudi 12</button> ...

utilizing a dynamic illustration as the backdrop

Despite my efforts, the canvas on my webpage is taking up the entire screen and preventing me from clicking on the navigation menu. I've tried various solutions like adding div tags and adjusting the z-index without success. Here's a snippet of t ...

"Unleashing the Power of MongoDB's Dynamic $in

Is there a way to dynamically pass a list of strings to a $in clause in MongoDB? I attempted the following code, but it didn't work and I haven't been able to locate more information other than an example with hardcoded values. The restrictedUs ...

Is it possible to incorporate current cljs components into a React project?

https://www.reddit.com/r/Clojure/comments/4el0xi/how_to_use_an_existing_reactjs_component_with/ In the Reddit post linked above, there is a discussion on how to incorporate existing ReactJS components into a CLJS/Reagent project. However, my aim is slight ...

Working with handleChange and onSubmit functions in pure JavaScript without any libraries

Currently developing my initial app, which is a login/register form using JS/Node.js/MySQL. I am facing issues with connecting my form to the database in order to store user data. I haven't utilized "handleChange" or "onSubmit" functions as I am not e ...