[英]Calculate correct width and height when resizing a rotated element
I'm creating a selection tool where the user may scale and rotate an HTMLElement using a selection control element. 我正在创建一个选择工具,用户可以在其中使用选择控件元素缩放和旋转HTMLElement。 Rotating the element and scaling it when it's not rotated, works fine.
旋转元素并在不旋转时缩放它,效果很好。 I'm now a bit lost with the math to scale an element which is already rotated.
我现在对使用数学运算来缩放已经旋转的元素有些迷惑。
Here is the simplified code. 这是简化的代码。
// rotating the element
selection.style.transform = "rotate(" + degrees + "deg)";
... ...
// scaling the element
var left = selection.offsetLeft;
var top = selection.offsetTop;
selection.style.width = (e.clientX - left) + "px";
selection.style.height = (e.clientY - top) + "px";
How do I calculate the new width/height when the element is rotated and its new position? 元素旋转时如何计算新的宽度/高度及其新位置?
A full code example would be a bit too long so I've made a fiddle showing the current state and issue. 完整的代码示例可能太长了,因此我摆弄了一下当前状态和问题。
Thanks for any help, link to the necessary math or code example. 感谢您的帮助,链接到必要的数学或代码示例。
Maybe getBoundingClientRect
is what you want. 也许
getBoundingClientRect
是您想要的。
console.log(document.querySelector('.rotate').getBoundingClientRect())
.rotate { width: 80px; height: 50px; background: blue; transform: rotate(45deg); }
<div class="rotate"></div>
You could also make life easy and just use a scale transformation? 您还可以使生活变得轻松,仅使用比例转换?
Finally I've found a working solution. 终于,我找到了一个可行的解决方案。 The key was to calculate the new center point by getting the current mouse position and the opposite corner.
关键是要通过获取当前鼠标位置和对角来计算新的中心点。 Now I rotate both points back to its unrotated state and get the bounds to use.
现在,我将两个点都旋转回其未旋转状态,并获得了要使用的范围。 The relevant code looks like this:
相关代码如下:
// the current rotation angle in degrees
angle = me.angle;
// the current mouse point (dragging a selection control)
var mousePoint = new Point(e.clientX, e.clientY);
// Get opposite point. Rotate it to get the visual position
var topLeft = new Point(left, top).rotate(center, angle);
// calculate the new center
var centerPoint = mousePoint.getMiddle(topLeft);
// rotate the opposite point back around the new center
var topLeftRotated = topLeft.rotate(centerPoint, -angle);
// rotate the mouse point around the new center.
var mousePointRotated = mousePoint.rotate(centerPoint, -angle);
// now we have the top left and lower right points and
// can calculate the dimensions
var x1 = topLeftRotated.x;
var x2 = mousePointRotated.x;
var w = x2-x1;
var y1 = topLeftRotated.y;
var y2 = mousePointRotated.y;
var h = y2-y1;
me.selection.style.width = w + "px";
me.selection.style.height = h + "px";
me.selection.style.left = x1 + "px";
me.selection.style.top = y1 + "px";
See the updated fiddle (Note. This is more a proof of concept and no production ready solution.) 请参阅更新的小提琴 (注意。这更多是概念证明,尚无生产就绪的解决方案。)
I'm open to more elegant solutions. 我愿意接受更优雅的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.