Exploring the possibilities of device orientation in combination with 3D transforms

Imagine a project that aims to create a cube with sides representing different geolocations. The cube's sides for east, west, up, ground, north, and south are meant to always point in their respective directions. To gather the necessary data, the project relies on the deviceorientation API:

However, many users, including myself, are puzzled as to why this app is not functioning as expected. It should work on Firefox OS or any Android device running Firefox. Not only does the code seem convoluted and poorly styled, but the actual logic behind it is unclear.

Upon researching similar implementations on MDN, I found instructions on how to achieve a comparable effect using device orientation events and 3D transforms: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Using_device_orientation_with_3D_transforms

Both approaches appear flawed, as they assume a simple inversion of device orientation through basic rotations along X, Y, and Z axes.

The correct methodology involves utilizing the alpha, beta, and gamma angles provided by the deviceorientation event, each corresponding to specific rotation axes within the device frame. By sequentially rotating around these axes, one can accurately represent the directional changes.

var b = e.beta,
    g = -e.gamma,
    a = e.alpha,
    axis_y = Array(0,1,0),
    axis_x = rotate(Array(1,0,0), axis_y, g),
    axis_z = rotate(rotate(Array(0,0,1), axis_y, g), axis_x, b);

Subsequently, applying CSS transforms based on these calculations yields the desired results:

document.getElementById("cube").style.transform =
    "rotate3d(" + axis_z[0] + ", " + axis_z[1] + ", " + axis_z[2] + ", " + a + "deg) " +
    "rotate3d(" + axis_x[0] + ", " + axis_x[1] + ", " + axis_x[2] + ", " + b + "deg) " +
    "rotate3d(" + axis_y[0] + ", " + axis_y[1] + ", " + axis_y[2] + ", " + g + "deg)";

The key difference lies in the direction of rotation; while CSS transformations follow a clockwise convention, the alpha-beta-gamma values utilize a counter-clockwise notation. Taking this into consideration, adjustments are made, such as inverting the gamma angle.

If you're interested, you can check out my rendition of this concept here:

To conclude, my question remains: Is this approach the most efficient method for achieving the desired outcome, or is there a way to simplify the process without multiple rotations?

Answer №1

When I dabbled with the Device Orientation API, I found that I was just scratching the surface. I experimented with the beta and gamma values independently, using them to control a ball's velocity in a game demo called Cyber Orb. This demo was created using the Phaser framework and rendered on Canvas. More information can be found in the MDN article and Mozilla Hacks post.

Although CSS 3D transforms are not my strongest suit, I believe in keeping things simple when it comes to coding. If a solution works effectively, then why complicate it? However, I am open to learning more efficient ways of achieving the same results from others who may have different perspectives.

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

An issue in D3.js where the chart bars are not properly synced with the x-axis labels

Currently delving into the world of d3.js for the first time. In our project, we needed to adjust the width of each bar from .attr('width', xScale.rangeBand()) line 46 to .attr('width', '10') line 50 After making this cha ...

Hide the border below the active tab

Could anyone assist me in removing the border below the selected tab? I've attempted multiple approaches without success. I experimented with adding a negative margin to the tab and creating a white border, but as the border I want to conceal is from ...

What is the best way to position a button under an h3 heading using the display flex

Inside my div, I have an h3 and a button. The div uses display:flex; with justify-content:center; and align-items:center;. However, the button ends up sticking to the right side of the h3. I attempted placing the button in its own div, but this caused a la ...

methods for transferring javascript variables to modal

<div> <h5> <a href="<?php echo base_url(); ?>/vendor/home/projects" >Return to Projects</a> </h5> <div> <div class="container"> <div class="row"> ...

Convert a fixed Jquery div to absolute positioning when it reaches the end of the page

I've implemented a Jquery code that adds a fixed class to a div when scrolling down. However, I now want to remove this fixed class when it reaches the bottom of the page and replace it with a new one that includes position:absolute; and margin-bottom ...

Can someone tell me where I can locate the CSS file once I've finished using Scene Builder?

Currently, I am working on my database project using Scene Builder. I have applied some CSS styling and would like to locate the CSS file in order to attach it to my IntelliJ project for further code editing. Can anyone guide me on where to find the CSS ...

I am encountering difficulties displaying the image and CSS files from another folder in my HTML with Node.js

I am facing difficulty in establishing a connection between my front-end and back-end using node.js. As a result, the website only displays the HTML code without linking to the CSS or image files. Here is the folder structure: root -src •pi ...

What could be the reason behind my jQuery UI buttons suddenly appearing oversized and lackluster?

Previously, I utilized the jQuery UI to enhance button appearance. However, with this code snippet: else if (ui.newTab.index() == 3) { $('#MediaContent').load('Content/TwainMedia.html'); $('#MediaContent').css('b ...

Using AngularJS to apply custom css to a tag within a directive for creating a Bootstrap sticky footer

Currently, I am in the process of developing my very first AngularJS application with Bootstrap as the responsive framework. In order to achieve a sticky footer, I usually utilize jQuery to determine the outerHeight of the footer and then apply that value ...

css - targeting the last child element using a type selector

I'm attempting to target the final child element of type <div> within the container "#myDiv" <div id="myDiv"> <div> <img> <div> <div>text</div> ...

When you click on the logo, it will automatically redirect you to a designated Bootstrap Tab and set that tab

After searching through numerous posts, I am still struggling to find a solution to my straightforward issue. My requirement is simple: when a user clicks on the brand (logo), it should not only redirect them to a specific tab but also make it appear as a ...

Working condition may vary depending on the size of the item

I have integrated the Masonry jQuery plugin into my design, but it is not functioning properly. The CSS class mentioned in the documentation only includes: width: 25% For my purposes, I modified it to: width: 24% float: left margin-right: 5px This modi ...

Can anyone shed some light on why the CSS code is not functioning properly within my HTML file?

My CSS doesn't seem to be working in my HTML. I have linked it correctly, but it's not displaying properly - even showing empty in the CSS tab of Chrome Inspector. The links are there, so I'm not sure why it's not functioning correctly. ...

How can I position a vertically centered image to the left of a block paragraph using inline styles?

I am faced with the challenge of aligning a 100x100 vertically-centered image to the left of a text block that extends beyond its height. It is important that the text does not wrap underneath the image but stays in a single block on the right side. I must ...

conceal the mustard-colored dots within the overlay

Whenever I trigger the link, a modal window pops up, but it comes with an unwanted black background color and yellow dots. How can I prevent the yellow dots from showing up on the overlay? http://jsfiddle.net/y88WX/18/embedded/result/ <nav class="da-d ...

Button for toggling the mobile navbar in Bootstrap

Is there a way to adjust the position of the navbar-toggle button http://prntscr.com/5sldgb within the navbar after changing its height? Here is the HTML code: <div class="navbar navbar-default"> <div class="container"> ...

Making modifications to the CSS within an embedded iframe webpage

Assigned with the task of implementing a specific method on a SharePoint 2013 site. Let's dive in. Situation: - Within a page on a SharePoint 2013 site, there is a "content editor" web part that displays a page from the same domain/site collection. T ...

Differentiating onClick events for parent and child elements

I need help with my JSX code: <div id="parent" onClick={clickOnParent} style={{ width: 100, height: 100 }}> <div id="child" onClick={clickOnChild} style={{ width: 20, height: 20 }} /> </div> When I click on the pare ...

The appearance of Recaptcha buttons is unattractive and seemingly impossible to

Let me start by saying that I have already looked into the issue of "Recaptcha is broken" where adjusting the line-height was suggested as a solution. Unfortunately, that did not work for me. After implementing Google's impressive Recaptcha on my web ...

Is there a way to shift this structure to the right without relying on the float property?

Here's the HTML and CSS I have - .font-size-add-class { background-color: #2f2f2f; color: #f9f7f1; } .font-sizes { text-align: right; display: table; margin-top: 15px; cursor: pointer; } .font-sizes > .small { font-size: 12px; } .font ...