Rotate a resized rectangle with SVG and Javascript

2019-09-11 14:13发布

问题:

I'm using d3js and SVG to resize and rotate rectangles, I capture the mouse events on handles that are positioned in each corner of the rectangle, there are two handles in each corner, one for resizing (square) and other to rotate (circle).

When I do the following process, the rectangle "jumps" from its location to another one:

  • Rotate (it means it's not more in zero degrees)
  • Resize (any direction)
  • Rotate again (here is when the shape "jumps")

The jump only occurs when the center of the rectangle is updated, using transform rotate from SVG.

One thing that gave me a lead was updating the rotation center while the shape is being resized, when I do this the shape starts to move to the final position where the "jump" would have placed it.

I can post code examples, it will take some time to do, but if helps to see the problem, no problems.

All I need is some direction from someone that already had this problem.

Edit:

I've made a Plunker with somewhat the current structure that I'm using in my project. All events controls are concentrated in the svgApp.directive.js. Things I've noticed while developing this example:

  • The center of the element is not correctly calculated after the angle is not zero, this is my main issue;
  • If you apply the {{vm.model.image.transform.rotate}} to the ng-attr-transform attribute in the center <circle> element, you are going to notice how it always is in the correct center (the right circle - blue - is commented at the bottom of the main SVG);

I know this is not the perfect version of the tool, it still has some bugs, like the inversion of the mouse direction (resize/rotate handles) while rotated, but it's well documented and structured very close to how I'm working on the project.

The Plnkr: http://plnkr.co/edit/fXT0kZcRwJTPb7wQVHpg?p=preview

Any help with this problem will be very appreciated.