当前位置: 代码迷 >> JavaScript >> 转载:JavaScript Tween算法及徐动效果
  详细解决方案

转载:JavaScript Tween算法及徐动效果

热度:385   发布时间:2012-11-25 11:44:31.0
转载:JavaScript Tween算法及缓动效果

Flash做动画时会用到Tween类,利用它可以做很多动画效果,例如缓动、弹簧等等。
我这里要教大家的是怎么利用flash的Tween类的算法,来做js的Tween算法,并利用它做一些简单的缓动效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Tween</title>
</head>
<body>
<div style="padding-left:50px;">
  <div style="position:relative; border:1px solid #000000; width:550px; height:50px;">
    <div id="idMove" style="width:50px; height:50px; background:#930; position:absolute;"></div>
  </div>
  <div style="position:relative;width:550px;height:200px; margin-top:50px;">
    <div id="idChart" style="border:1px solid #000;height:200px;"> </div>
    <div id="idLine" style="position:absolute;top:0;left:0;height:200px;width:1px;background:#000;"></div>
  </div>
</div>
<div>
  <p> Tween类型: <br />
    <label>
      <input name="vTween" type="radio" value="Linear" checked="checked" />
      Linear </label>
    <label>
      <input name="vTween" type="radio" value="Quad" />
      Quadratic </label>
    <label>
      <input name="vTween" type="radio" value="Cubic" />
      Cubic </label>
    <label>
      <input name="vTween" type="radio" value="Quart" />
      Quartic </label>
    <label>
      <input name="vTween" type="radio" value="Quint" />
      Quintic </label>
    <label>
      <input name="vTween" type="radio" value="Sine" />
      Sinusoidal </label>
    <br />
    <label>
      <input name="vTween" type="radio" value="Expo" />
      Exponential </label>
    <label>
      <input name="vTween" type="radio" value="Circ" />
      Circular </label>
    <label>
      <input name="vTween" type="radio" value="Elastic" />
      Elastic </label>
    <label>
      <input name="vTween" type="radio" value="Back" />
      Back </label>
    <label>
      <input name="vTween" type="radio" value="Bounce" />
      Bounce </label>
  </p>
  <p> ease类型: <br />
    <label>
      <input name="vEase" type="radio" value="easeIn" checked="checked" />
      easeIn </label>
    <label>
      <input name="vEase" type="radio" value="easeOut" />
      easeOut </label>
    <label>
      <input name="vEase" type="radio" value="easeInOut" />
      easeInOut </label>
  </p>
  <input id="idSpeed" type="button" value="减慢速度" />
  <input id="idRun" type="button" value=" Run " />
</div>
<script>
/*
算法来源:[url]http://www.robertpenner.com/easing/[/url]
*/
var Tween = {
	Linear: function(t,b,c,d){ return c*t/d + b; },
	Quad: {
		easeIn: function(t,b,c,d){
			return c*(t/=d)*t + b;
		},
		easeOut: function(t,b,c,d){
			return -c *(t/=d)*(t-2) + b;
		},
		easeInOut: function(t,b,c,d){
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		}
	},
	Cubic: {
		easeIn: function(t,b,c,d){
			return c*(t/=d)*t*t + b;
		},
		easeOut: function(t,b,c,d){
			return c*((t=t/d-1)*t*t + 1) + b;
		},
		easeInOut: function(t,b,c,d){
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		}
	},
	Quart: {
		easeIn: function(t,b,c,d){
			return c*(t/=d)*t*t*t + b;
		},
		easeOut: function(t,b,c,d){
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeInOut: function(t,b,c,d){
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		}
	},
	Quint: {
		easeIn: function(t,b,c,d){
			return c*(t/=d)*t*t*t*t + b;
		},
		easeOut: function(t,b,c,d){
			return c*((t=t/d-1)*t*t*t*t + 1) + b;
		},
		easeInOut: function(t,b,c,d){
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		}
	},
	Sine: {
		easeIn: function(t,b,c,d){
			return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
		},
		easeOut: function(t,b,c,d){
			return c * Math.sin(t/d * (Math.PI/2)) + b;
		},
		easeInOut: function(t,b,c,d){
			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
		}
	},
	Expo: {
		easeIn: function(t,b,c,d){
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
		},
		easeOut: function(t,b,c,d){
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInOut: function(t,b,c,d){
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	},
	Circ: {
		easeIn: function(t,b,c,d){
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		easeOut: function(t,b,c,d){
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		},
		easeInOut: function(t,b,c,d){
			if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
			return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
		}
	},
	Elastic: {
		easeIn: function(t,b,c,d,a,p){
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		},
		easeOut: function(t,b,c,d,a,p){
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
		},
		easeInOut: function(t,b,c,d,a,p){
			if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
			if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
			return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
		}
	},
	Back: {
		easeIn: function(t,b,c,d,s){
			if (s == undefined) s = 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		easeOut: function(t,b,c,d,s){
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		},
		easeInOut: function(t,b,c,d,s){
			if (s == undefined) s = 1.70158; 
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		}
	},
	Bounce: {
		easeIn: function(t,b,c,d){
			return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
		},
		easeOut: function(t,b,c,d){
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		},
		easeInOut: function(t,b,c,d){
			if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b;
			else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b;
		}
	}
}

//////////////////////////////////////////////////////////

var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Each = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

//////////////////////////////////////////////////////////

var fun, iChart = 550, iDuration = 100;

function Move(){
	var oM = $("idMove").style, oL = $("idLine").style, t=0, c=500, d=iDuration;
	oM.left=oL.left="0px"; clearTimeout(Move._t);
	function _run(){
		if(t<d){
			t++;
			oM.left = Math.ceil(fun(t,0,c,d)) + "px";
			oL.left = Math.ceil(Tween.Linear(t,0,iChart,d)) + "px";
			Move._t = setTimeout(_run, 10);
		}else{
			oM.left = c + "px";
			oL.left = iChart + "px";
		}
	}
	_run();
}

////////////////////////////////////////////////////////

function Chart(){
	var a = [];
	for (var i = 0; i < iChart; i++) {
		a.push('<div style="background-color:#f60;font-size:0;width:3px;height:3px;position:absolute;left:' + (i-1) + 'px;top:' + (Math.ceil(fun(i,200,-200,iChart))) + 'px;"><\/div>');
	}
	$("idChart").innerHTML = a.join("");
}

////////////////////////////////////////////////////////

var arrTween = document.getElementsByName("vTween");
var arrEase = document.getElementsByName("vEase");

Each(arrTween, function(o){ o.onclick = function(){ SetFun(); Chart(); } })
Each(arrEase, function(o){ o.onclick = function(){ SetFun(); Chart(); } })

function SetFun(){
	var sTween, sEase;
	Each(arrTween, function(o){ if(o.checked){ sTween = o.value; } })
	Each(arrEase, function(o){ if(o.checked){ sEase = o.value; } })
	fun = sTween == "Linear" ? Tween.Linear : Tween[sTween][sEase];
}

$("idRun").onclick = function(){ SetFun(); Chart(); Move(); }

$("idSpeed").onclick = function(){
	if(iDuration == 100){
		iDuration = 500; this.value = "加快速度";
	}else{
		iDuration = 100; this.value = "减慢速度";
	}
}
</script>
</body>
</html>

?

效果说明

首先大家到这里下载flash的as脚本(建议看看这里的demo),1.0和2.0都可以(里面的算法都一样)。
例如打开2.0的可以看到几个as文件,每个文件对应一个Tween效果,分别有(参考里面的说明):
Linear:无缓动效果;
Quadratic:二次方的缓动(t^2);
Cubic:三次方的缓动(t^3);
Quartic:四次方的缓动(t^4);
Quintic:五次方的缓动(t^5);
Sinusoidal:正弦曲线的缓动(sin(t));
Exponential:指数曲线的缓动(2^t);
Circular:圆形曲线的缓动(sqrt(1-t^2));
Elastic:指数衰减的正弦曲线缓动;
Back:超过范围的三次方缓动((s+1)*t^3 - s*t^2);
Bounce:指数衰减的反弹缓动。
ps:以上都是自己的烂翻译,希望各位修正。

每个效果都分三个缓动方式(方法),分别是:
easeIn:从0开始加速的缓动;
easeOut:减速到0的缓动;
easeInOut:前半段从0开始加速,后半段减速到0的缓动。
其中Linear是无缓动效果,没有以上效果。

然后看看里面的算法,以Quadratic的easeOut为例:

static function easeOut (t:Number, b:Number, c:Number, d:Number):Number {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
}

?

这是as代码,四个参数分别是:
t: current time(当前时间);
b: beginning value(初始值);
c: change in value(变化量);
d: duration(持续时间)。
ps:Elastic和Back有其他可选参数,里面都有说明。

那如何在js中利用这些算法呢?可以看到,虽然是as代码,但里面的程序是可以直接放在js里使用的。
我们可以定义一个类,把它这部分放在里面:

var Tween = {
    Quad: {
        easeOut: function(t,b,c,d){
            return -c *(t/=d)*(t-2) + b;
        }
    }
}

?

这样,就可以用Tween.Quad.easeOut取得这个算法了,其他算法也一样处理就行了。

接下来就可以利用这个js的Tween来做一些缓动效果了。
先通过上面的坐标实例说一下算法原理:
x轴是时间,y轴是当前值,b是y轴的初始值,x轴的初始值是0,t是当前时间。当t(x轴)逐渐增加到达d时,当前值(y轴)会到达目标值(b+c)。
想详细理解的话可以找资料看看吧(貌似跟数学关系比较大)。

下面就介绍如何使用这个Tween了,首先b、c、d三个参数(即初始值,变化量,持续时间)在缓动开始前,是需要先确定好的。
举一个简单的例子,一个div要向右缓动,left初始值是50,那么b就是50,要向右移动100,那c就是100,如果知道的是目标值,例如要向右移动到150,那就把目标值150减初始值b就是变化量c了。
至于d的设置就比较灵活,只要符合t是从0向d递增(或递减)就可以了。
d跟步长配合使用来设置持续时间,例如d设置为100,如果设置步长是1,那么从0到100就有100步,即分100次来完成这个过程,步数越多那么持续时间就越长。
至于t的变化相当于时间的变化,一般是均匀变化的,每次变化都增加一个步长,当t从0递增(或递减)到d时,缓动就结束了。
要注意的是t是从0开始的,设置步长时必须确定t确实能到达d,如果上面的步长是3,那么t就永远都到不了d了。更好的处理是当t等于或超过d之后,就停止定时器并设置当前值为目标值。

了解了Tween的使用后就可以实现动画效果了。继续上面的例子,已经确定b是50,c是100,d是100,步长是1,使用Tween.Quad.easeOut算法。那么可以用以下程序实现缓动:

var b=50,c=100,d=100,t=0;
function Run(){
    div.style.left = Math.ceil(Tween.Quad.easeOut(t,b,c,d)) + "px";
    if(t<d){ t++; setTimeout(Run, 10); }
}
Run();

?

一个简单的缓动效果就实现了,要实现不同的缓动,只需要使用对应的Tween算法就行了,以前看来遥不可及的效果,现在这么容易就实现了(当然那些算法才是最难的地方)。

这样关于缓动的效果就介绍完了,但其实远远不止这些,我们还可以自己定义一些算法,发挥想象力,做一些好玩的效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>


<input id="idClick" type="button" value="click">
<div id="test1" style="position:absolute;width:5px;height:5px;background:#333; font-size:0;"></div>

<div id="test2" style="position:absolute;width:5px;height:5px;background:#F00; font-size:0;"></div>
<script>
var Round = {
    X: function(t,b,r){ return r*Math.cos(t/20)+b; },
    Y: function(t,b,r){ return r*Math.sin(t/20)+b; }
}

var t=x=y=0;

document.getElementById("idClick").onclick = function(e){
    document.onmousemove = function(e){
        e=e||event;
        x=e.clientX+document.documentElement.scrollLeft;
        y=e.clientY+document.documentElement.scrollTop;
    };
    document.onmousemove(e);
    setInterval(function(){
        var iX = Math.ceil(Round.X(t,x,100)), iY = Math.ceil(Round.Y(t,y,50));
        with(document.getElementById("test1").style){ left = iX + "px"; top = iY + "px"; }
        with(document.getElementById("test2").style){
            left = Math.ceil(Round.X(t*5,iX,25)) + "px"; top = Math.ceil(Round.Y(t*5,iY,25)) + "px";
        }
        t++;
    }, 10);
}
</script>

</body>
</html>

?

多点几次会有不同的效果

?

转载于http://bbs.51js.com/viewthread.php?tid=82368

  相关解决方案