缩放旋转的矩形并找到新的控制点和中心

时间:2017-06-12 12:02:44

标签: javascript svg matrix css-transforms

我要分享的代码笔链接中的代码有点复杂和密集,所以我实际上不是要求代码而是要求建议。

https://codepen.io/anon/pen/LLEedg?editors=0010

它是关于缩放旋转的矩形并找到新的缩放矩形的顶点坐标。

当我旋转矩形时,我将其中心平移到视口的0,0并进行旋转。然后我翻译回原来的地方。有用。

然后我做一个刻度,将transform-origin移动到矩形的右下角。为了避免跳跃,我补偿了比例量以及旋转动作应用于我的控制点的平移。最后,当我将transform origin移动到矩形的旋转左下角时,我看不到跳跃。我使用新的控制点计算一个新的边界框(不是旋转坐标,而是缩放版本。)

之后,我使用相同的方法使用新的transform-origin进行缩放。这次我采用新的缩放但未旋转的新边界框,并使用相同的方法进行旋转,以找到新的缩放和旋转控制点坐标。

我希望它们完全位于图像顶点,但它们有点偏离。

可能是关于从旋转控制点的原点缩放而不是正确补偿。

要重现我分享的代码笔链接中的问题。

首先向左旋转任意数量。

然后通过向左拖动左上角手柄进行缩放。

小黑点应准确落在绿色手柄的中心。

小黑点是我计算的新控制点坐标, 红色条纹矩形是新的边界框,它以0,0旋转并转换回来计算新的控制点。带红色填充的蓝色目标是图像的transform-origin。带有绿色填充的黄色目标是手柄的transform-origin,我不会故意更新而不会混淆。具有较大半径和不透明度的蓝色填充圆是来自小黑点的新计算中心。

1 个答案:

答案 0 :(得分:0)

我解决了下面这个问题,我希望能听到更好的解决方案。

这包括在旋转和缩放操作后找到新的顶点和中心。

在每个缩放事件和旋转事件之后,应评估以下所有代码块。

可能是句柄的拖动事件。

heightOfRectangleWidthOfRectangle应为缩放值。

实施例。如果您的当前比例为1.3,那么heightOfRectangle应该是您的initial rectangle's height*1.3

rotationAngle应该是以弧度为单位应用的所有旋转角度的总和。

alphaMath.atan2(heightOfRectangle,WidthOfRectangle);

rMath.sqrt(Math.pow(WidthOfRectangle,2)+Math.pow(heightOfRectangle,2))/2 换句话说,由两个直角三角形共享的海平面形成了矩形;

假设您克服了转换原点问题并可以扩展 来自不同句柄的矩形阻止变换原点跳跃,活动句柄是您拖动的句柄 相反的一面是变换原点所在的位置。 例如,如果从左上角的手柄拖动,则应找到原点 在右下方的手柄位置。

activeHandle是您拖动的句柄。

tl forpleple,o for origin br for the rightright等等。

tc表示我的代码的顶部中心,我拖动旋转。

它只是意味着来自origin事件的旋转。

old开头的值应该是此轮换之前的最后一个句柄位置。 以new开头的值将是此轮换后的新句柄位置。

rx是旋转x的缩写 ry是rotate y

的缩写

你仍然可以在这里查看笔,解决了问题。

https://codepen.io/anon/pen/LLEedg?editors=0010

您可以在第251行查看updateControlPoints功能。

 var cosRot = Math.cos(rotationAngle);
 var sinRot = Math.sin(rotationAngle);

 if (activeHandle === 'tc') {

     var cosAMinusRot = Math.cos(alpha - rotationAngle);
     var sinAMinusRot = Math.sin(alpha - rotationAngle);
     var cosAPlusRot = Math.cos(alpha + rotationAngle);
     var sinAPlusRot = Math.sin(alpha + rotationAngle);
     var cos90APlusRot = Math.cos(Math.PI / 2 - alpha + rotationAngle);
     var sin90APlusRot = Math.sin(Math.PI / 2 - alpha + rotationAngle);
     var ox = old_ox; //means current ox
     var oy = old_oy; //means current oy
     //tl
     var new_x = -r * cosAPlusRot + ox
     var new_y = -r * sinAPlusRot + oy
     new_tl.rx = new_x;
     new_tl.ry = new_y;
     //tr           

     new_x = +r * sin90APlusRot + ox
     new_y = -r * cos90APlusRot + oy
     new_tr.rx = new_x;
     new_tr.ry = new_y;
     //bl
     new_x = -r * cosAMinusRot + ox
     new_y = +r * sinAMinusRot + oy
     new_bl.rx = new_x;
     new_bl.ry = new_y;
     //br
     new_x = +r * cosAPlusRot + ox
     new_y = +r * sinAPlusRot + oy
     new_br.rx = new_x;
     new_br.ry = new_y;

 }

 else if (activeHandle === 'tl') {

     var cosAPlusRot = Math.cos(alpha + rotationAngle);
     var sinAPlusRot = Math.sin(alpha + rotationAngle);

     new_tl.rx = old_br.rx - 2 * r * cosAPlusRot;
     new_tl.ry = old_br.ry - 2 * r * sinAPlusRot;

     new_bl.rx = old_br.rx - WidthOfRectangle * cosRot;
     new_bl.ry = old_br.ry - WidthOfRectangle * sinRot;

     new_tr.rx = old_br.rx + HeightOfRectangle * sinRot;
     new_tr.ry = old_br.ry - HeightOfRectangle * cosRot;

     new_ox = old_br.rx - r * cosAPlusRot;
     new_oy = old_br.ry - r * sinAPlusRot;


 } else if (activeHandle === 'tr') {

     var cos180APlusRot = Math.cos(Math.PI - alpha + rotationAngle);
     var sin180APlusRot = Math.sin(Math.PI - alpha + rotationAngle);

     new_tl.rx = old_bl.rx + HeightOfRectangle * sinRot;
     new_tl.ry = old_bl.ry - HeightOfRectangle * cosRot;

     new_br.rx = old_bl.rx + WidthOfRectangle * cosRot;
     new_br.ry = old_bl.ry + WidthOfRectangle * sinRot;

     new_tr.rx = old_bl.rx - 2 * r * cos180APlusRot;
     new_tr.ry = old_bl.ry - 2 * r * sin180APlusRot;

     new_ox = old_bl.rx - r * cos180APlusRot;
     new_oy = old_bl.ry - r * sin180APlusRot;




 } else if (activeHandle === 'bl') {

     var cosAPlus90MinusRot = Math.cos(alpha + Math.PI / 2 - rotationAngle);
     var sinAPlus90MinusRot = Math.sin(alpha + Math.PI / 2 - rotationAngle);

     new_bl.rx = old_tr.rx - 2 * r * sinAPlus90MinusRot;
     new_bl.ry = old_tr.ry - 2 * r * cosAPlus90MinusRot;

     new_br.rx = old_tr.rx - HeightOfRectangle * sinRot;
     new_br.ry = old_tr.ry + HeightOfRectangle * cosRot;

     new_tl.rx = old_tr.rx - WidthOfRectangle * cosRot;
     new_tl.ry = old_tr.ry - WidthOfRectangle * sinRot;

     new_ox = old_tr.rx - r * sinAPlus90MinusRot;
     new_oy = old_tr.ry - r * cosAPlus90MinusRot;



 } else if (activeHandle === 'br') {

     var cosAPlusRot = Math.cos(alpha + rotationAngle);
     var sinAPlusRot = Math.sin(alpha + rotationAngle);

     new_bl.rx = old_tl.rx - HeightOfRectangle * sinRot;
     new_bl.ry = old_tl.ry + HeightOfRectangle * cosRot;

     new_br.rx = old_tl.rx + 2 * r * cosAPlusRot;
     new_br.ry = old_tl.ry + 2 * r * sinAPlusRot;

     new_tr.rx = old_tl.rx + WidthOfRectangle * cosRot;
     new_tr.ry = old_tl.ry + WidthOfRectangle * sinRot;

     new_ox = old_tl.rx + r * cosAPlusRot;
     new_oy = old_tl.ry + r * sinAPlusRot;



 }
相关问题