简体   繁体   English

调整旋转元素上的手柄大小

[英]Resizing Handles on a Rotated Element

I'm trying to put resizing handles on the four corners of a rectangle, which can be dragged to resize the rectangle. 我正在尝试在矩形的四个角上放置调整大小的手柄,可以拖动它来调整矩形的大小。 What I'm having trouble with is calculating the new width, new height, and new points of the rectangle after I've dragged one of the rectangle's points. 我遇到的麻烦是在拖动其中一个矩形点后计算矩形的新宽度,新高度和新点。

If the rectangle were not rotated, this would obviously be very easy because the width and height would change by the same amout as the mouse's offsetX and offsetY. 如果矩形没有旋转,这显然是非常容易的,因为宽度和高度将改变与鼠标的offsetX和offsetY相同的大小。 However, this rectangle CAN be rotated, so offsetX and offsetY don't match up to the changes in width/height. 但是,此矩形可以旋转,因此offsetX和offsetY与宽度/高度的变化不匹配。

在此输入图像描述

In the image above, I've represented information that I already have in solid black, and information I want to find in light grey. 在上图中,我已经用纯黑色表示了我已经拥有的信息,以及我想要用浅灰色找到的信息。 I've tried to show how the rectangle should change if I dragged the a1 corner up and to the right. 我试图展示如果我将a1角向上拖动到右边,矩形应该如何变化。

Could anyone help me figure out how to calculate the missing information? 谁能帮我弄清楚如何计算缺失的信息?

Thanks for any help! 谢谢你的帮助! It's much appreciated. 非常感谢。

-- -

EDIT: I've got drag start, drag move, and drag end callbacks on each handle. 编辑:我有拖动开始,拖动移动,并拖动每个句柄上的结束回调。 My current code simply gets the new point (in this example, a2). 我当前的代码只是得到了新的点(在这个例子中,a2)。 Unfortunately this simply moves the handle we're currently dragging into its new position, but obviously has no effect on the width/height of the rectangle, and the position of its other points. 不幸的是,这只是移动我们当前拖动到新位置的手柄,但显然对矩形的宽度/高度以及其他点的位置没有影响。 What I'm hoping for help figuring out is how do I calculate the new width and height, and the new position of the other points, based on this drag. 我希望帮助找出的是如何根据这个阻力计算新的宽度和高度,以及其他点的新位置。

Handle Coordinates (before drag) 处理坐标(拖动前)

handles: { 
  a: { x: 11, y: 31 },
  b: { x: 44, y: 12 }, 
  c: { x: 39, y: 2 }, 
  d: { x: 6, y: 21 }
};

Callbacks: 回调:

// Save the original x and original y coordinates
// of the handle, before dragging
onDragStart: function(x, y, handle) {
  handle.data({
   ox: x,
   oy: y
  });
}

// While dragging the handle, update it's coordinates to 
// reflect its new position
onDragMove: function(dx, dy, handle) {
  handle.attr({
   x: handle.data('ox') + dx,
   y: handle.data('oy') + dy
  });
}

// Not currently using the drag end callback
onDragEnd: function(x,y,handle) {}

http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Distance_between_two_points gives you the method for finding the length of a1 to a2 http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Distance_between_two_points为您提供了查找a1到a2长度的方法

var len=Math.sqrt(Math.pow(a2x-a1x,2)+Math.pow(a2y-a1y,2));

Then looking at the triangle formed by the intersection of the grey and black lines (Lets call that point H) and a1 and a2. 然后观察灰线和黑线交叉形成的三角形(让我们称之为H点)和a1和a2。 You can use trigonometry to solve. 您可以使用三角学来解决。 So the angle formed by the line d1 to c1 with the bottom lets call that the angle of rotation(R). 因此,线d1到c1与底部形成的角度称为旋转角度(R)。 That means the R is equal to angle at a2 and the angle at a1 is equal to 90-R. 这意味着R等于a2处的角度,并且a1处的角度等于90-R。 To find the sides then you go 为了找到两侧,你去

//line from a2 to H 
var hDif=Math.sin(R)*len;
//line from a1 to H
var wDif=Math.cos(R)*len;

You can then use these to find the new length and height. 然后,您可以使用它们来查找新的长度和高度。 There will a few more calculations to see if you are adding or subtracting to the old width and height. 还会有一些计算,看看你是在增加还是减去旧的宽度和高度。

图

I have another idea: 我有另一个想法:

Consider that your rectangle is rotated by 30 degress anticlockwise with respect to the origin. 考虑到您的矩形相对于原点逆时针旋转了30度。

When the user clicks on one of the edges or corners, do the following: 当用户单击其中一个边或角时,请执行以下操作:

1) Let StartPt = the point where the mouse starts. 1)让StartPt =鼠标开始的点。
2) Get the point where the mouse moves to. 2)获取鼠标移动到的点。 Let it be EndPt. 让它成为EndPt。
3) Rotate each of the vertex of the Rectangle by -30 so that now it becomes an unrotated rectangle. 3)将矩形的每个顶点旋转-30,使其成为未旋转的矩形。 (Do not draw the unrotated rectangle, it is for calculcation purpose only) (不要绘制未旋转的矩形,仅用于计算目的)
4) Rotate the StartPt and EndPt by -30 degrees. 4)将StartPt和EndPt旋转-30度。 Calculate the change in the width and height of the rectangle when the point. 计算点时矩形的宽度和高度的变化。
5) Add the change to the rotated vertices of the rectangle. 5)将更改添加到矩形的旋转顶点。
6) Rotate the vertices of the Rectangle by +30 degrees. 6)将矩形的顶点旋转+30度。 Now draw the Rectangle. 现在绘制矩形。

https://www.experts-exchange.com/questions/24244758/How-can-let-a-user-drag-to-resize-a-rotated-rectangle.html#answer23964155 https://www.experts-exchange.com/questions/24244758/How-can-let-a-user-drag-to-resize-a-rotated-rectangle.html#answer23964155

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

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