你会如何设置动画以使其跟随曲线?

时间:2010-02-10 20:20:14

标签: javascript jquery animation

如果我有 <div id="curve" style="position:relative; height:100px; width:100px; />

如何让它在曲线上移动?我用谷歌搜索了一切但似乎无法找到另一个可以同时调用两个函数的例子。这是我想要的代码,但不起作用:

$('#curve').click(function () {
    $(this).animate(
        { 
            top: 400,
            left = $(this).left() + $(this).left()*$(this).left()
        },
        'slow',
        function() { $(this).animate( { left: 600 }, 'fast' ); }
    );
});

即使这不是正确的代码,我相信动画只会占用某些东西的“目的地”,所以我认为动态目的地不起作用。我想要做些什么呢?

编辑::我肯定会拿起那个插件,但我也想知道为什么这些代码不能像我期望的那样工作。

这是使用for循环和延迟方法的另一种尝试

$('#curve').click(function () {
    for (var i=0; i<400; i++ )
    {
        $(this).delay(1000);
        $(this).css( { top: i, left: i*1.5 } );
    }
});

除了它立即进入那个位置,没有延迟或任何东西。所以,如果它是从[0,0]开始,一旦我点击它就会传送到[400,600]。为什么延迟不起作用?

5 个答案:

答案 0 :(得分:16)

jQuery.path plugin就是你想要的:

示例:沿弧线动画

var arc_params = {
    center: [285,185],  
    radius: 100,    
    start: 30,
    end: 200,
    dir: -1
};

$("my_elem").animate({path : new $.path.arc(arc_params)});

示例:沿着正弦波动画

var SineWave = function() {
    this.css = function(p) {
        var s = Math.sin(p*20);
        var x = 500 - p * 300;
        var y = s * 50 + 150;
        var o = ((s+2)/4+0.1);
        return {top: y + "px", left: x + "px", opacity: o};
    } 
};

$("my_elem").animate({path : new SineWave});

答案 1 :(得分:15)

我认为这一次,你必须在js中逐个重新计算动画曲线然后通过移动一小部分来做(=你可能找到插件或者你必须自己完成所有的数学运算)< / p>

编辑2 :先前添加的链接已移动=&gt; http://foxparker.wordpress.com/2009/09/22/bezier-curves-and-arcs-in-jquery/。谢谢,扎克。

编辑1 :这引起了我的兴趣,所以我做了一些谷歌研究 - 就像我想的那样:插件可以在这里使用:http://foxparker.wordpress.com/2009/09/22/bezier-curves-and-arcs-in-jquery/

答案 2 :(得分:11)

这是我写的一个简单的小库,允许动画路径的任意三维Bézier曲线,甚至可以为你计算旋转角度。 (该库尚未完善或记录,但它表明即使您的页面中没有SVG元素,站在SVG DOM的肩膀上也是多么容易。)

http://phrogz.net/SVG/animation_on_a_curve.html

Screenshot of website

您可以编辑代码并观察曲线/动画更改,也可以编辑曲线并查看代码更新。

如果我的网站关闭,这里是后人的相关代码:

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 elapsed = ((new Date)-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;
};

答案 3 :(得分:6)

您使用的是jQuery 1.4吗?

$(this).animate({
    left: [500, 'easeInSine'],
    top: 500
});

您需要使用缓动插件才能生效:http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js

E.g。 http://jsbin.com/ofiye3/2

答案 4 :(得分:1)

有一个很小的脚本,只是用于不是直线的动画,称为pathAnimator

它非常小巧,超级高效。你甚至不需要jQuery;)

相关问题