简体   繁体   English

Snap.svg-拖动后旋转不起作用

[英]Snap.svg - Rotate after drag not working

Hopefully this is a basic question and can be answered easily! 希望这是一个基本问题,可以轻松回答! Basically, I want to drag a shape then double click on it and rotate by 90deg. 基本上,我想拖动一个形状,然后双击它并旋转90度。 But once you move the shape from the start point the rotations go haywire. 但是,一旦您从起点移动形状,旋转就会变成麻烦。 I have tried to recalculate the centre point for rotation like so... 我试图像这样重新计算旋转的中心点...

var bbox = tri.getBBox();
tri.animate({
    transform: "R"+angle+"," + bbox.cx  + ","  + bbox.cy
}, 500)

See fiddle... http://jsfiddle.net/henryJack/hsfor158/7/ . 参见小提琴... http://jsfiddle.net/henryJack/hsfor158/7/

Any help much appreciated! 任何帮助,不胜感激!

I think there's potentially two issues. 我认为可能存在两个问题。

Firstly, transforming will overwrite any existing transformations, so you want to include the existing transform and then include the new one on top of that. 首先,转换将覆盖任何现有的转换,因此您要包括现有的转换,然后在其之上包括新的转换。

element.transform()

Will give the existing transform in place. 将现有的转换放置到位。 So you can combine the old with the new. 因此,您可以将新旧结合起来。

tri.animate({
   transform: tri.transform() + "R"+angle+"," + bbox.cx  + ","  + bbox.cy
}, 500)

However, the 2nd issue I think is that getBBox() doesn't take into account transforms by default, and it will accumulate transforms, so you may want to store the transform somewhere on a double click and use that instead of transform(). 但是,我认为的第二个问题是,默认情况下,getBBox()不考虑转换,它会累积转换,因此您可能需要双击存储转换,然后使用它而不是transform()。

There is a hidden parameter to Snaps getBBox(), if you give it getBBox(1), I think it will take into account it's local transform (I may be wrong on this, so if you need to know definitely, I would ask a new question on this specific topic, as it may take a bit of digging through source code...the source code calls the parameter isWithoutTransform, but I think it depends how you interpret this, have a play). Snaps getBBox()有一个隐藏参数,如果您给它getBBox(1),我认为它将考虑到它的局部转换(对此我可能是错的,因此,如果您一定要知道,我会问一个这个特定主题有一个新问题,因为可能需要对源代码进行一些挖掘...源代码将参数称为isWithoutTransform,但我认为这取决于您如何解释这一点(发挥作用)。

So you can combine the two... 因此,您可以将两者结合起来...

var bbox = tri.getBBox(1);
angle += 90;
tri.animate({
    transform: tri.transform() + "r"+angle+"," + bbox.cx  + ","  + bbox.cy
}, 500);

jsfiddle jsfiddle

Possible better solution , there may be a better way though: If you need to take into account further transformations applied to outer elements (for example if its in a group which is transformed), it gets more complicated, and you will need to either add a function like getTransformedBBox OR maybe better, come up with an alternate solution, for example to drag an outer group and rotate the inner element . 可能会有更好的解决方案 ,但是可能会有更好的方法:如果您需要考虑应用于外部元素的进一步转换(例如,如果在一组已转换的元素中),它将变得更加复杂,您将需要添加像getTransformedBBox之类的函数或可能更好,提供了一个替代解决方案, 例如拖动外部组并旋转内部元素 You could do this anyway as an alternate solution (add the element to a group, add the drag the group, but just rotate the element)! 无论如何,您都可以这样做作为替代解决方案(将元素添加到组中,添加拖动组,但只需旋转元素)!

2nd more reliable example by using an extra group 通过使用额外的组,第二个更可靠的示例

var triGroup = s.group( tri )
var angle = 0;
var rotate = function() {
    var bbox = tri.getBBox();
    angle += 90;
    bbox = tri.getBBox();
    tri.animate({
        transform: "r"+angle+"," + bbox.cx  + ","  + bbox.cy
    }, 500);
}

triGroup.dblclick(rotate);
triGroup.drag();

jsfiddle 2 jsfiddle 2

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

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