简体   繁体   English

在javascript中旋转后调整大小以保持固定角的逻辑

[英]Resize logic to maintain a fixed corner after rotate in javascript

How to calculate or logic behind to get translate x & y while resize(with one fixed corner) after rotate.旋转后如何在调整大小(具有一个固定角)时计算或背后的逻辑以获取平移 x 和 y。

Below image shows corner based resize works perfect with unrotated div.( rotate:0deg )下图显示了基于角的调整大小与未旋转的 div 完美配合。( rotate:0deg

在此处输入图片说明

I handled center based rotate and resize without any problem.我处理了基于中心的旋转和调整大小没有任何问题。

But the problem is, i need a fixed corner when resize a rotated div.但问题是,在调整旋转 div 的大小时,我需要一个固定的角。

When resize a rotated div, using bottom-right handle, top-left needs to remains fixed , likewise opposite corners need to fixed position.调整旋转后的 div 大小时,使用右下手柄,左上角需要保持固定,同样对角需要固定位置。

HTML code: HTML代码:

<div class="box" style="transform: translate(132px, 190px) rotate(45deg); width: 295px; height: 234px;">
<div>

Javascript code: Javascript代码:

function rotate(){
  //Here i update angle
}

function resize(){
 //Here i update translate x & y, width & height

 switch (handle) {
   case 'bottom-right':
     x = previous_x - ? ;   // logic to get translate x when resize a rotated div.
     y = previous_y + ? ;   // logic to get translate y when resize a rotated div.
     break;
   case 'bottom-left':
     x = previous_x - ? ;
     y = previous_y + ? ;
     break;
   case 'top-left':
     x = previous_x - ? ;
     y = previous_y + ? ;
     break;
   case 'top-right':
     x = previous_x - ? ;
     y = previous_y + ? ;
     break;
   default:
 }
}

I checked Canva website, the transform manager div translate x and y gets updated while resize a rotated div to maintain a fixed corner .我检查了Canva网站,变换管理器 div translate x 和 y 在调整旋转 div 的大小以保持固定的角时得到更新

Anybody help me to calculate translate x and y values using rotated angle to maintain a fixed corner.任何人都可以帮助我使用旋转角度来计算平移 x 和 y 值以保持固定角。

Thanks in Advance.提前致谢。

You could just set a transform-origin using CSS so you don't have to calculate the translate to compensate :您可以使用 CSS 设置转换原点,这样您就不必计算翻译来补偿:

.box{transform-origin: 0 100% 0;}

Link to W3C doc链接到W3C 文档

Example on jsfiddle jsfiddle示例

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

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