【问题标题】:raphaelJS 2.1 animate along pathraphaelJS 2.1 沿路径动画
【发布时间】:2012-10-29 00:57:33
【问题描述】:

我想沿着弯曲的路径制作一条路径(实际上是一组路径,但我会讲到)。

RaphaelJS 2 删除了 animateAlong 方法,原因我无法辨别。深入研究由 Zevan 抽象的 Raphael 文档的 gears demo,我已经做到了这一点:

//adding a custom attribute to Raphael
(function() {
  Raphael.fn.addGuides = function() {
    this.ca.guide = function(g) {
      return {
        guide: g
      };
    };
    this.ca.along = function(percent) {
      var g = this.attr("guide");
      var len = g.getTotalLength();
      var point = g.getPointAtLength(percent * len);
      var t = {
        transform: "t" + [point.x, point.y]
      };
      return t;
    };
  };
})();

var paper = Raphael("container", 600, 600);

paper.addGuides();

// the paths
var circ1 = paper.circle(50, 150, 40);
var circ2 = paper.circle(150, 150, 40);
var circ3 = paper.circle(250, 150, 40);
var circ4 = paper.circle(350, 150, 40);

var arc1 = paper.path("M179,204c22.667-7,37,5,38,9").attr({'stroke-width': '2', 'stroke': 'red'});

// the animation

// works but not at the right place
circ3.attr({guide : arc1, along : 1})
         .animate({along : 0}, 2000, "linear");

http://jsfiddle.net/hKGLG/4/

我希望第三个圆圈沿红色路径设置动画。 现在正在制作动画,但距离红色路径的距离等于第三个圆的原始坐标。奇怪的是,无论along 对象中的变换translate 是相对的(小写“t”)还是绝对的(大写“T”),都会发生这种情况。即使我在 animate 调用之前用变换平移推动它,它也总是在同一个位置进行动画处理。

非常感谢任何帮助。我刚在矢量陆地下船。指针很有帮助——一个工作小提琴更好。

【问题讨论】:

    标签: javascript animation path raphael vector-graphics


    【解决方案1】:

    你只是一个跳跃,跳过,然后跳离你想要的功能。这里的混淆涉及转换和对象属性之间的交互 ​​- 具体来说,转换不会修改原始对象属性。翻译只是添加而不是替换圆圈的原始坐标。

    解决方案非常简单。在你的方法中:

    this.ca.along = function(percent) {
      var box = this.getBBox( false );  // determine the fundamental location of the object before transformation occurs
      var g = this.attr("guide");
      var len = g.getTotalLength();
      var point = g.getPointAtLength(percent * len);
      var t = {
        transform: "...T" + [point.x - ( box.x + ( box.width / 2 ) ), point.y - ( box.y + ( box.height / 2 ) )]  // subtract the center coordinates of the object from the translation offset at this point in the guide.
      };
      return t;
    

    显然,这里有一些优化空间(即,在 0,0 处创建所有圆,然后将它们转换为所需的显示坐标,避免大量迭代数学可能是有意义的)。但它的功能...see here.

    另一个警告:...T 转换不会影响已应用于给定圆的任何其他变换。此实现保证与其他转换很好地配合。

    【讨论】:

    • 太棒了,凯文。很简单。谢谢!一件事:你能解释一下“...”append/prepend 的转换语法吗?文档几乎没有耳语地略过这个。我真的不知道它是干什么用的。
    • 拉斐尔的参考文献恐怕不需要大修。 append/prepend 非常有用,尤其是当您处理可能在不同位置单独转换的路径时。从本质上讲,它允许您将特定的转换插入到现有的转换链中,而不会清除其他转换。例如,如果您的元素已经具有“R45 0,0 S2,2”的变换,则在“T100,100...”前面添加“T100,100 R45 0,0 S2”的累积变换, 2”,并附加它会导致“R45 0,0 S2,0 T100,100”——不同的结果。
    • 太好了,谢谢。是的,我上周看到德米特里刚刚在推特上吹嘘说他拒绝了两次写一本关于拉斐尔的书的提议。 “不喜欢写”。也许你应该写那本书?我要买一本:)
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2015-07-15
    • 2018-05-23
    • 2014-02-05
    • 1970-01-01
    相关资源
    最近更新 更多