首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >html5画布-为跟随路径的对象设置动画

html5画布-为跟随路径的对象设置动画
EN

Stack Overflow用户
提问于 2012-02-14 08:43:03
回答 1查看 19.5K关注 0票数 18

我对canvas有点陌生,所以如果这是一个微不足道的问题,请原谅。

我希望能够按照路径(定义为bezier路径)为对象设置动画,但我不确定如何做。

我看过拉斐尔,但我不知道如何随着时间的推移而走下去。

Cake JS在演示中看起来很有前途,但我真的很难找到文档,或者在这种情况下缺乏文档。

有没有人有这方面的工作例子?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-14 11:45:25

使用this related question中的代码on my website,但不是在回调中更改.style.left等,而是在新位置擦除并重新绘制画布上的项目(以及可选的旋转)。

请注意,这在内部使用SVG沿着贝塞尔曲线轻松插值点,但您可以使用它提供的点做任何您想要的事情(包括在画布上绘制)。

以防我的网站宕机,下面是库的最新快照:

代码语言:javascript
复制
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;
};

…下面是你可以使用它的方法:

代码语言:javascript
复制
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);
});​

实际行动:http://jsfiddle.net/Z2YSt/

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9270214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档