沿着svg路径动画div

时间:2012-07-16 02:31:56

标签: jquery animation path svg

有人可以给我一个解决方案 我从phrogz.net获得了这个库, 正如你在这里看到的那样(http://phrogz.net/SVG/animation_on_a_curve.html

function CurveAnimator(from,to,c1,c2){
  this.path = document.createElementNS('http://www.w3.org/2000/svg','path');
  if (!c1) c1 = from;
  if (!c2) c2 = to;
  this.path.setAttribute('d','M'+from.join(',')+'C'+c1.join(',')+' '+c2.join(',')+' '+to.join(','));
  this.updatePath();
  CurveAnimator.lastCreated = this;
}
CurveAnimator.prototype.animate = function(duration,callback,delay){
  var curveAnim = this;
  // TODO: Use requestAnimationFrame if a delay isn't passed
  if (!delay) delay = 1/40;
  clearInterval(curveAnim.animTimer);
  var startTime = new Date;
  curveAnim.animTimer = setInterval(function(){
    var now = new Date;
    var elapsed = (now-startTime)/1000;
    var percent = elapsed/duration;
    if (percent>=1){
      percent = 1;
      clearInterval(curveAnim.animTimer);
    }
    var p1 = curveAnim.pointAt(percent-0.01),
        p2 = curveAnim.pointAt(percent+0.01);
    callback(curveAnim.pointAt(percent),Math.atan2(p2.y-p1.y,p2.x-p1.x)*180/Math.PI);
  },delay*1000);
};
CurveAnimator.prototype.stop = function(){
  clearInterval(this.animTimer);
};
CurveAnimator.prototype.pointAt = function(percent){
  return this.path.getPointAtLength(this.len*percent);
};
CurveAnimator.prototype.updatePath = function(){
  this.len = this.path.getTotalLength();
};
CurveAnimator.prototype.setStart = function(x,y){
  var M = this.path.pathSegList.getItem(0);
  M.x = x; M.y = y;
  this.updatePath();
  return this;
};
CurveAnimator.prototype.setEnd = function(x,y){
  var C = this.path.pathSegList.getItem(1);
  C.x = x; C.y = y;
  this.updatePath();
  return this;
};
CurveAnimator.prototype.setStartDirection = function(x,y){
  var C = this.path.pathSegList.getItem(1);
  C.x1 = x; C.y1 = y;
  this.updatePath();
  return this;
};
CurveAnimator.prototype.setEndDirection = function(x,y){
  var C = this.path.pathSegList.getItem(1);
  C.x2 = x; C.y2 = y;
  this.updatePath();
  return this;
};

以下是我们在jQuery中使用它的方法:

var ctx = document.querySelector('canvas').getContext('2d');
ctx.fillStyle = 'red';

var curve = new CurveAnimator([50, 300], [350, 300], [445, 39], [1, 106]);

curve.animate(5, function(point, angle) {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.fillRect(point.x-10, point.y-10, 20, 20);
});​

它很棒,但事实是我需要更多的曲线点,所以我想给它一个SVG路径。 我的问题是,如何在我的自定义svg路径中使用此库? 我的svg路径是这样的:

<path class="fil0 str0" d="M-13.5003 40.5c0,0 198,-14.0003 332,106 134,120 326,275 387,296 61.0002,20.9999 162.647,49.157 207,110 234,321 367,-579 -89.9119,-451.912 -82.3825,22.9144 -117.088,216.912 -117.088,216.912"/>

我看到有关将this.path.setAttribute更改为init然后给它svg路径但我不知道如何执行此操作以及如何在之后调用该函数的内容。

1 个答案:

答案 0 :(得分:1)

您可以替换构造函数,使其接受您的路径:

function CurveAnimator(path) {
    this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
    this.path.setAttribute('d', path);
    this.updatePath();
    CurveAnimator.lastCreated = this;
}

你会这样称呼:

var ctx = document.querySelector('canvas').getContext('2d');
ctx.fillStyle = 'red';

var path = "M-13.5003 40.5c0,0 198,-14.0003 332,106 134,120 326,275 387,296 61.0002,20.9999 162.647,49.157 207,110 234,321 367,-579 -89.9119,-451.912 -82.3825,22.9144 -117.088,216.912 -117.088,216.912";
var curve = new CurveAnimator(path);

curve.animate(5, function(point, angle) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillRect(point.x-10, point.y-10, 20, 20);
});​