使用JavaScript调整多个对象的大小,考虑到旋转。

huangapple go评论70阅读模式
英文:

Resize multiple objects with JS considering rotate

问题

我正在开发一个带有对象和用户交互的可视化编辑器,例如移动、调整大小、旋转等等...

我已经实现了调整大小和旋转功能。现在,我已经实现了多选功能,当用户选择多个对象并调整大小时,保持原始比例。

这个功能运行得很好,但对于旋转过的对象却不行。我已经创建了一个简化的 codepen 示例。基本上问题是 - 如何调整 resize() 函数,以确保它对旋转的对象运行良好。要重现问题,只需点击“旋转”,然后一次或多次点击“增加宽度和高度”。

function resize(incrementX, incrementY, offsetX, offsetY) {
    ...
}
英文:

I'm working on visual editor with objects and user interactions around like move, resize, rotate, etc...

I have resize and rotate functionality in place. Now I have implemented multi-select functionality when user select multiple objects and resize objects keeping the original proportion.

That functionality works very well, however not for rotated objects. I've created a simplified codepen example. Basically the question is - how to adjust resize() function to make sure it works well for rotated objects. To reproduce an issue just click on "Rotate" and then "Increase width & height" once or multiple times.

function resize(incrementX, incrementY, offsetX, offsetY) {
    ...
}

答案1

得分: 1

I'm not sure if this is a valid solution for your problem, but you can undo the rotation before resizing, and reset the rotation afterwards. Like this.

function resize(incrementX, incrementY, offsetX, offsetY) {
  var old_r = objmultiple.r;
  rotate(-objmultiple.r);
  var ratioX = (objmultiple.w + incrementX) / objmultiple.w;
  var ratioY = (objmultiple.h + incrementY) / objmultiple.h;
  
  objmultiple.x += offsetX;
  objmultiple.y += offsetY;
  objmultiple.w = objmultiple.w + incrementX;
  objmultiple.h = objmultiple.h + incrementY;
  
  [obj1, obj2].forEach(function(obj) {
    obj.x = (obj.x - objmultiple.x + offsetX) * ratioX + objmultiple.x;
    obj.y = (obj.y - objmultiple.y + offsetY) * ratioY + objmultiple.y;
    obj.w *= ratioX;
    obj.h *= ratioY;
  });
  rotate(old_r);
}

Codepen here

英文:

I'm not sure if this is a valid solution for your problem, but you can undo the rotation before resizing, and reset the rotation afterwards. Like this.

function resize(incrementX, incrementY, offsetX, offsetY) {
  var old_r = objmultiple.r
  rotate(-objmultiple.r)
  var ratioX = (objmultiple.w + incrementX) / objmultiple.w;
  var ratioY = (objmultiple.h + incrementY) / objmultiple.h;
  
  objmultiple.x += offsetX;
  objmultiple.y += offsetY;
  objmultiple.w = objmultiple.w + incrementX;
  objmultiple.h = objmultiple.h + incrementY;
  
  [obj1, obj2].forEach(function(obj) {
    obj.x = (obj.x - objmultiple.x + offsetX) * ratioX + objmultiple.x;
    obj.y = (obj.y - objmultiple.y + offsetY) * ratioY + objmultiple.y;
    obj.w *= ratioX;
    obj.h *= ratioY;
  });
  rotate(old_r)
}

Codepen here

huangapple
  • 本文由 发表于 2020年1月7日 00:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定