简体   繁体   English

在RaphaelJs中“应用”一个转变

[英]“Apply” a transformation in RaphaelJs

I've written a wrapper class around RaphaelElement. 我在RaphaelElement周围写了一个包装类。 It has a property elem which stores the corresponding Element and the two convenience methods setPos and getPos. 它有一个属性elem,它存储相应的Element和两个方便的方法setPos和getPos。 Moreover there's a position member with the two entries x and y. 此外,还有一个位置成员,其中包含两个条目x和y。

Therefore getPos() merely returns the position. 因此getPos()只返回位置。 setPos accepts a new position as a parameter and has to update the coordinates of elem. setPos接受一个新位置作为参数,并且必须更新elem的坐标。

Unfortunately there is no information what type of RaphaelElement will be stored in elem, it could be a circle as well as a rectangle. 遗憾的是,没有任何信息可以在elem中存储哪种类型的RaphaelElement,它可以是圆形也可以是矩形。 Right now the code inside setPos looks like this: 现在setPos里面的代码如下所示:

//position is the parameter, this.pos is the member
this.pos = position;
this.elem.attr("x",this.pos.x);
this.elem.attr("y",this.pos.y);
this.elem.attr("cx", this.pos.x);
this.elem.attr("cy", this.pos.y);

This feels like a dirty workaround. 这感觉就像一个肮脏的解决方法。 It works with both circles and rectangles, but on a rectangle there's no attribute "cx" or "cy" and on a circle both "x" and "y" don't exist. 它适用于圆形和矩形,但在矩形上没有属性“cx”或“cy”,并且在圆圈上“x”和“y”都不存在。

I browsed the documentation for a better way to modify the position of a RaphaelElement and found the method transform. 我浏览了文档以获得更好的方法来修改RaphaelElement的位置并找到方法转换。 But there's a problem: I haven't found a way to give "absolute" new coordinates to transform. 但是有一个问题:我没有找到一种方法来提供“绝对”的新坐标来进行转换。 It only offers means to translate, rotate or scale. 它只提供翻译,旋转或缩放的方法。 If I have to change the position by applying a translation from my current position to the new position, then I have to append a new translation to the transformation string. 如果我必须通过应用从当前位置到新位置的转换来更改位置,那么我必须在转换字符串中附加新的转换。 I fear that it might grow VERY long. 我担心它会长得很长。 Moreover, I would have to evaluate an increasingly long string to get my current position. 此外,我将不得不评估一个越来越长的字符串来获得我目前的位置。

It certainly is possible to move my Elements by appending new translations to the transformation but I would like to be able to either set the new position directly or to "apply" or "finalise" a transformation, so that it's string doesn't grow infinitely. 当然可以通过在转换中添加新的翻译来移动我的元素,但我希望能够直接设置新位置或者“应用”或“完成”转换,这样它的字符串就不会无限增长。

The consolidate method on a TransformList will convert a list of transforms into a single matrix. TransformList上的合并方法将转换列表转换为单个矩阵。 You could call this after you've added your translation. 您可以在添加翻译后调用此方法。

Alternatively you could call getItem on the transformList which will give you an SVGTransform object and you can call setTranslate on that which would set the new position directly. 或者,您可以在transformList上调用getItem,它将为您提供一个SVGTransform对象,您可以在其上调用setTranslate,直接设置新位置。 You'd need to be careful that there is a transform and create one if there isn't. 你需要注意有一个变换,如果没有变换就创建一个变换。

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

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