JavaScript Tween演算法及緩動效果第1/2頁_Flash As

來源:互聯網
上載者:User
我這裡要教大家的是怎麼利用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 > <div > <div id="idMove" ></div> </div> <div > <div id="idChart" > </div> <div id="idLine" ></div> </div> </div> <div> <p> Tween類型: <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> <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類型: <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> /* 演算法來源:http://www.robertpenner.com/easing/ */ 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 + (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>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

效果說明

首先大家到這裡下載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" ></div> <div id="test2" ></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>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

當前1/2頁  12下一頁閱讀全文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.