javascript - 如何在 Raphaël.js 中用动画绘制简单路径

标签 javascript raphael graphael

我正在尝试使用 Raphaël.js 库在此 Demo 绘制一条从路径开始到结束的带有动画的简单路径。 .

var canvas = Raphael('canvas', 900, 648);
var pathString = "M 200,200 L200,10 L100,10";

$('#start').click(function(e) {
       var line = canvas.path(pathString).attr({ stroke: "#000" });
       line.animate({     },5000);
});

但不确定如何在此处使用 animate() 函数。我怎样才能实现这个目标?

最佳答案

可能有更好的方法来做到这一点,但这是我能找到的最好的方法:getSubpath 允许检索路径的一段。通过实现可以动画的自定义属性,我们可以根据该属性的值控制路径:

var canvas = Raphael('canvas', 900, 648);
var pathString = "M 200,200 L200,10 L100,10";

canvas.customAttributes.subpath = function (from, to) {
  from = Math.floor(from);
  to = Math.floor(to);
  if(to < from)
    to = from;
  return {path: this.parentPath.getSubpath(from, to)};
};

$('#start').click(function(e) {
  canvas.clear();
  // Create an invisible full representation of the path
  var completeLine = canvas.path(pathString).attr({ 'stroke-opacity': 0 });
  var len = completeLine.getTotalLength(pathString);

  // Create a partial representation of the path
  var line = canvas.path(pathString);
  line.parentPath = completeLine;
  line.attr({ stroke: "#000", subpath: [0, 0]});

  // Animate using our custom subpath attribute from 0 to the length of the full path
  line.animate({ subpath: [0, len] }, 5000);
});

https://jsfiddle.net/r40k0kjv/5/

关于javascript - 如何在 Raphaël.js 中用动画绘制简单路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396852/

相关文章:

javascript - append 到 IFRAME 中的链接 URL

onclick - 当页面上有多个对象时,更改单个 RaphaelJS 对象的笔划属性

javascript - 多次调用 Raphael 悬停函数仅有效 'sometimes'

raphael - 带有可点击条形的 g.raphael 条形图

javascript - Accordion 箭头定位在 div 内

javascript - 数据表响应不适用于 ajax 调用

javascript - 最好的 JavaScript 图表库或者我们可以为 Raphael 条形图添加一个比例尺?

javascript - 如何使用javascript for循环和if语句在右表中显示数据?

svg - 用户上传的 .svg 是否存在 XSS 风险?你怎么能 "sterilize"SVG?