Tips for determining the height and width of a rotated element

When using e.getBoundingClientRect().left, the position of an element from the left side of the screen can be obtained.

However, when trying to determine the position of end points on a rotated element, adding width or height may not suffice. So, how can the position of end points be determined in such cases?

Is obtaining these values even possible?

https://i.sstatic.net/rvHGR.png

let getBoxPos = document.querySelectorAll(".demo")
let box1 = document.querySelector("#box1")
let box2 = document.querySelector("#box2")
let box3 = document.querySelector("#box3")
let s = "";


function func() {
  getBoxPos.forEach(e => {
    let figXValue = e.getBoundingClientRect().left;
    s = Number(s) + Number(1);
    console.log("Start distance for box" + s + ":" + figXValue)
  })
  console.log("End distance for box1:" + (box1.getBoundingClientRect().left + 24));
  console.log("End distance for box3:" + (box3.getBoundingClientRect().left + 104));
  console.log("The correct calculation method for box2 is uncertain: Is it box2.getBoundingClientRect().left + 100, box2.getBoundingClientRect().left + 20, or perhaps neither?");
}
func();
.demo {
  height: 100px;
  width: 20px;
  margin-left: 40px;
  border: 2px solid black;
}

.demo:nth-child(2) {
  border: 2px solid red;
  transform: rotate(-45deg);
}

.demo:nth-child(3) {
  border: 2px solid green;
  transform: rotate(-90deg);
}
<div class="demo" id="box1"></div>
<div class="demo" id="box2"></div>
<div class="demo" id="box3"></div>

Answer №1

Information at hand: We know the height, width of the element, and the angle θ due to animation or transformation.

What we need to determine: The new width and height of the element post-rotation.

If we have an angle and a side (height, width in this case) of a right-angled triangle, then we can calculate the other sides as well.

To solve a triangle with just one side, you also require one of the non-right angled angles. Otherwise, it is not possible:

  1. If you are aware of the hypotenuse, multiply it by sin(θ) to find the length of the side opposite the angle.
  2. Alternatively, multiply the hypotenuse by cos(θ) to obtain the side adjacent to the angle.
  3. If you have the non-hypotenuse side adjacent to the angle, divide it by cos(θ) to determine the length of the hypotenuse.
  4. Alternatively, multiply this length by tan(θ) to ascertain the length of the side opposite the angle.
  5. If you know an angle and the side opposite to it, dividing the side length by sin(θ) will give you the hypotenuse.
  6. Alternatively, dividing the length by tan(θ) will provide you with the length of the side adjacent to the angle.

Referenced from OMNI Calculator

https://i.sstatic.net/C6NVo.png

Though only a single angle is provided, others can be derived using angle formulas and relationships.

Now, to address the question regarding obtaining the position of the blue point, we must add this to the left value

Height*Sinθ + Width*Cosθ

The final code would look like this:

box2.getBoundingClientRect().left + Height*Sinθ + Width*Cosθ

Remember to include any borders in the height and width if desired from the border's edge.

As highlighted by A Haworth, rotation can occur from any point, so I checked compatibility for different positions and found it to be compatible with rotation from any point thus far. https://i.sstatic.net/ehO4h.png https://i.sstatic.net/zaMDp.png >>The element is rotated at an angle from anywhere.

This answer may provide further assistance

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

Error: The method _firestore.default.initializeApp is not defined as a function

I am encountering an issue in my app where "_firestore.default.initializeApp" is not recognized as a function while evaluating src/screens/Login.js, src/appNavigator.js, and App.js. I have already ensured that I have added the firebaseconfig and connected ...

Organize the structure of a JSON object in AngularJS to generate a hierarchical unordered list

Greetings! I am working with a RESTful API that returns a JSON object structured like this: { data: [ { id: 4, name: "Naranja", cant_portion: 2, portion_name: "Piezas", foodgroup_name: "Frutas" } ...

Vue.js does not apply the "selected" attribute to set the HTML <option> tag to default

Trying to designate a default <option> tag from the available options. Utilizing v-model on the <select> tag, the vue.js documentation indicates the following approach: v-model will disregard the ... selected attributes present on form ele ...

Set the modal-dialog to have a fixed width, but ensure that it becomes responsive and adjusts size appropriately when the screen size becomes smaller

Utilizing the default Bootstrap3 CSS files for my project. I've come across an issue where the modal-dialog width is set to 600px by default, while I require it to be 1000px for my screen size. Regardless of the screen size exceeding 1000px, I need t ...

Generate dynamic maps that are exportable to HTML format

I've utilized Leaflet to produce engaging interactive maps in R. However, I'm encountering an issue when trying to export the maps - the background map appears grey after exporting. library(leaflet) library(htmlwidgets) m <- leaflet(data.fra ...

Update the sum upon deleting a feature in an HTML and JavaScript form

Once a row or field is added, this function will display a delete link. When clicked, it will remove the row or field: var ct = 1; function addNewRow(){ ct++; var divElement = document.createElement('div'); divElement.id = ct; // Code to cr ...

Sequelize: Establishing association upon saving without the need to retrieve another object

Suppose I have two tables, parent and child, where the parent has multiple children. They are properly mapped in Sequelize. Now, I need to add a new child to an existing parent. However, I do not have access to the parent instance, only its id. I am aw ...

Creating a Consistent Look for Italic Font Formatting in Tailwind

In an effort to establish consistent typography on a website being developed by my team, we have devised custom utility classes in Tailwind. One of these classes, small-italicized, also requires the text to be italicized. However, it seems that the fontSiz ...

Ways to determine if a device possesses a specific capability

I am currently in the process of developing a web application and am exploring how to determine the capabilities of an Android device. Is there a central registry or database where I can look up the various capabilities and features of an Android device? ...

Learn how to properly implement cookies in a fetch request within Nextjs

Here is a code snippet to consider: Index.getInitialProps = async function({req}) { const res = await fetch("http://localhost/api/tiles"); const json = await res.json(); } If the /api/tiles endpoint requires access to the uid cookie from the user, t ...

How can I refresh the content on my Vue.js website automatically when a webhook is triggered

I currently have a Vue 3 website that displays a list fetched from a REST-API using the fetch method. Occasionally, this list is updated in the original database, and I want my Vue component to reflect those changes. The exciting part is that a webhook is ...

Adding data to each span and div using JavaScript is a simple task that can be achieved easily

What is the best way to add information to each span and div element using JavaScript? $(document).on("click",".selection-state",function(){ stateid = $(this).attr("rel"); $("#my_tooltip").html(data); } e ...

Enhancing the appearance of dropdown menus for WooCommerce Variable products with custom CSS styling

I currently have a Wordpress website with WooCommerce and several variable products. Each product's variation has a dropdown menu that displays different options when clicked. My main concern is how to customize the appearance of these dropdown menus ...

Is it possible to detect the source of a digest cycle in AngularJS?

I've found myself knee-deep in a legacy AngularJS project lately. The codebase is quite intricate and expansive, making it difficult to showcase here. However, I've come across an issue where functions triggered during digest changes are firing h ...

Strategies for Refreshing a Component After Modifying Data in the Store

Having trouble updating my table component when the data in my store changes. I have a simple table using v-for as shown below: <tr v-for="d in getDatas" v-bind:key="d.id"> and buttons to navigate between pages: <button class= ...

The curly braces in AngularJS are failing to display the values on the HTML page

After trying various articles and solutions to different questions, I am still unable to resolve my issue. I am working on a blank ionic project and it is running smoothly in my browser using ionic serve without any errors. However, instead of displaying ...

Tips for designing a personalized payment page in PayPal for flexible one-time and subscription-based payments

How can I display a PayPal checkout page with custom fields such as tax and total amount when a user makes a payment for a custom amount? Can multiple fields like sales tax and total amount be added? In addition to that, our web application already has Pa ...

Using JS or jQuery to redirect to a URL after receiving a JSON object from an event listener

My main aim is to create a process where I can redirect to a different URL based on an interaction in a cross-domain iframe. The desired process looks like this: A user visits example.com/apps/ and completes a form inside a cross-domain iframe. After su ...

The issue of passing state in React Router v4 Redirect unresolved

I have a specific private route, /something, that I only want to be accessible when logged in. I've used Redirect with the state parameter set, however, when I try to access it at the destination, location.state is showing as undefined. Here is how I ...

Avoid changing the orientation

Despite having a similar title, my question is not a duplicate of Prevent orientation change in iOS Safari. I have modified the code provided below. I have written the following code to automatically rotate the content when a user rotates their iPad/iPhon ...