简体   繁体   English

如何在边界矩形框内旋转图像

[英]How to rotate an image inside bounding rectangle box

I have the following img that I'd like to drag (by dragging a circle on top of rectangle) and rotate an image. 我有以下img我想拖动(通过拖动矩形顶部的圆圈)并旋转图像。

有图象的长方形箱子在它里面

When dragging, I just want to rotate an image inside the rectangle and not the rectangle itself. 拖动时,我只想在矩形内旋转图像而不是矩形本身。 I'm not sure I'm doing this correctly, I have this formula when I start dragging: 我不确定我是否正确这样做,当我开始拖动时我有这个公式:

const newAngle = Math.atan(circleDrag.y, circleDrag.x) * (180 / Math.PI);

I'm getting the position of the circle from the top of the rectangle, but it's giving me something between 90 to 110 degrees only. 我从矩形的顶部得到圆圈的位置,但它只给我90110度之间的东西。 Correct way of rotation angle would start from 0 I think, and should rotate between 0-360 degrees. 正确的旋转角度方式将从0开始,我认为应该在0-360度之间旋转。

Could somebody direct me on how to calculate rotation angle? 有人可以指导我如何计算旋转角度?

If I understand your needs right, you have to measure angle of direction from the center point of rectangle to the circle center. 如果我理解您的需求,您必须测量从矩形的中心点到圆心的方向角。

Math.atan(circleDrag.y - rectangleCenter.y, circleDrag.x - rectangleCenter.x)

(At this moment you are calculating direction from coordinate origin to the circle) (此时您正在计算从坐标原点到圆的方向)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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