简体   繁体   中英

How can I measure the width and height of a rotated element on Javascript?

There is a bug with my design interface where the width and height of my selected div are false when rotated.

Here's a schema of what I have and another one of what I want.

在此处输入图片说明

I want to find the formula using Javascript, how can I possibly write it ?

PS : I already have the angle, the width and the height of the first schema, no need to calculate it again.

Width and height are not changing, when rotate the element .its also the same see the snippet .get the value via dom use with

  1. offsetWidth
  2. offsetHeight

 var rot =document.getElementById("rot"); console.log(rot.offsetWidth,rot.offsetHeight) 
 #rot{ width:100px; height:40px; background-color:pink; transform:rotate(30deg) } 
 <div id="rot"></div> 

If it is equations you need, Then here you go.

w-y*sin(theta) = x*cos(theta)
h-y*cos(theta) = x*sin(theta)

Where,

w = given width
h = given height
x = width you need
y = height you need
theta = angle

A little trigonometry goes a long way.

Solving this simultaneous equation gives me -

2x = (w+h)/(cos(theta)+sin(theta)) + (w-h)/(cos(theta)-sin(theta))
2y = (w+h)/(cos(theta)+sin(theta)) - (w-h)/(cos(theta)-sin(theta))

Feel free to convert to Math functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM