html5 canvas - 动画路径后面的对象

Ben*_*Ben 18 html5 animation raphael html5-canvas

我对画布有点新意,如果这是一个微不足道的问题,请原谅.

我希望能够在路径(定义为bezier路径)后设置对象的动画,但我不知道该怎么做.

我看过拉斐尔,但我无法弄清楚如何随着时间的推移走这条路.

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

有没有人有这方面的实例?

Phr*_*ogz 20

这个相关问题使用我网站上的代码,但不是在回调中更改等等,而是使用新位置的项目(以及可选的旋转)擦除并重新绘制画布..style.left

请注意,这在内部使用SVG可以轻松地沿着贝塞尔曲线插入点,但是您可以使用它为您提供的点(包括在画布上绘图).

如果我的网站关闭,这是该库的当前快照:

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;
};
Run Code Online (Sandbox Code Playgroud)

......以下是你如何使用它:

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);
});?
Run Code Online (Sandbox Code Playgroud)

在行动:http://jsfiddle.net/Z2YSt/


Iva*_*nos 8

所以,这是详细版本:

t是0到1之间代表时间的任何数字; 的p0,p1,p2,p3对象是开始点,所述第一控制点,所述第二控制点的所述结束点分别为:

var at = 1 - t;
var green1x = p0.x * t + p1.x * at;
var green1y = p0.y * t + p1.y * at;
var green2x = p1.x * t + p2.x * at;
var green2y = p1.y * t + p2.y * at;
var green3x = p2.x * t + p3.x * at;
var green3y = p2.y * t + p3.y * at;
var blue1x = green1x * t + green2x * at;
var blue1y = green1y * t + green2y * at;
var blue2x = green2x * t + green3x * at;
var blue2y = green2y * t + green3y * at;
var finalx = blue1x * t + blue2x * at;
var finaly = blue1y * t + blue2y * at;
Run Code Online (Sandbox Code Playgroud)

这是一个使用<canvas>跟随JSfiddle中的路径的

变量的名称来自这个gif,它是贝塞尔曲线的最佳解释:http://en.wikipedia.org/wiki/File:Bezier_3_big.gif

代码的简短版本,在准备复制/粘贴的函数内:

var calcBezierPoint = function (t, p0, p1, p2, p3) {
    var data = [p0, p1, p2, p3];
    var at = 1 - t;
    for (var i = 1; i < data.length; i++) {
        for (var k = 0; k < data.length - i; k++) {
            data[k] = {
                x: data[k].x * at + data[k + 1].x * t,
                y: data[k].y * at + data[k + 1].y * t
            };
        }
    }
    return data[0];
};
Run Code Online (Sandbox Code Playgroud)



相关内容: