JS Tween 顏色漸變
更新時(shí)間:2008年12月30日 15:16:52 作者:
從我寫的as tween改寫的,基本功能跟as里面寫的一樣,只是沒(méi)有擴(kuò)展特定功能的接口(比如alpha2,move2,size2,color2等接口,這些在as tween里面都有實(shí)現(xiàn))。
有31中緩動(dòng)算法,實(shí)現(xiàn)了顏色的自動(dòng)轉(zhuǎn)換(#f00 #ff0000 rgb(255,0,0)格式到顏色運(yùn)算格式,最后返回#ff0000格式)、px單位的自動(dòng)轉(zhuǎn)換。
調(diào)用接口:
/**
* 對(duì)外接口
* Tween的示例
* @param startProps 開始屬性,單個(gè)屬性或者數(shù)組
* @param endProps 結(jié)束屬性,單個(gè)屬性或者數(shù)組
* @param timeSeconds 運(yùn)動(dòng)消耗時(shí)間,單位秒
* @param animType 動(dòng)作類型,字符串型,內(nèi)部自己轉(zhuǎn)換算子
* @param delay 延遲時(shí)間,多長(zhǎng)時(shí)間后開始運(yùn)動(dòng),單位秒
*/
window.rtween = function(startProps, endProps, timeSeconds, animType, delay)
{
var tw = new Tween();
tw.start(startProps, endProps, timeSeconds, animType, delay);
return tw;
}
示例如下:
http://img.jb51.net/online/Tween.htm
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
選擇列表里面的緩動(dòng)算法,點(diǎn)前面的按鈕,就會(huì)以想對(duì)的緩動(dòng)算法運(yùn)動(dòng)

源代碼: http://img.jb51.net/jslib/jquery/rtween.js
核心代碼:
function Tween()
{
this._frame=20;
//
this._animType = linear;
this._delay = 0;
//
this.run = function(){}
this.complete = function(){}
}
//
Tween.prototype.getValue = function(prop)
{
this._valueType = ”;
if(prop.constructor == Array) return prop;
//
if(typeof(prop) == 'string')
{
if(isColor(prop))
{
this._valueType = ‘color';
return c2a(prop);
}
if(prop.split('px').length>1)
{
this._valueType = ‘px';
return [prop.split('px')[0]];
}
}
return [prop];
}
Tween.prototype.setValue = function(prop)
{
if(this._valueType == ‘color')return a2c(prop);
if(this._valueType == ‘px')return prop[0]+'px';
return prop;
}
Tween.prototype.start = function(startProps, endProps, timeSeconds, animType, delay)
{
if(animType != undefined)this._animType = this.animTypes[animType];
if(delay != undefined)this._delay = delay;
//
this._timeSeconds = timeSeconds;
this._startTimer = new Date().getTime() + this._delay * 1000;
//
this._endProps = this.getValue(endProps);
this._startProps = this.getValue(startProps);
this._currentProps = [];
//
var $this = this;
clearInterval(this._runID);
this._runID = setInterval(
function(){$this._run();}
,this._frame);
}
Tween.prototype.stop = function(state)
{
for(var i in this._startProps)
{
if(Number(state)>0)
this._currentProps[i] = this._endProps[i];
else if(Number(state)<0)
this._currentProps[i] = this._startProps[i];
}
this.callListener();
this.complete();
//
clearInterval(this._runID);
}
Tween.prototype.callListener = function()
{
this.run(this.setValue(this._currentProps));
}
Tween.prototype._run = function()
{
if ( new Date().getTime()- this._startTimer< 0) return;
var isEnd = false;
//
for(var i in this._startProps)
{
this._currentProps[i] = this._animType( new Date().getTime()-this._startTimer,Number(this._startProps[i]),Number(this._endProps[i])-Number(this._startProps[i]),this._timeSeconds * 1000);
//
if(this._startTimer + (this._timeSeconds * 1000) <= new Date().getTime())
{
this._currentProps[i] = this._endProps[i];
isEnd = true;
}
}
//
if(isEnd)this.stop();
else this.callListener();
}
調(diào)用接口:
/**
* 對(duì)外接口
* Tween的示例
* @param startProps 開始屬性,單個(gè)屬性或者數(shù)組
* @param endProps 結(jié)束屬性,單個(gè)屬性或者數(shù)組
* @param timeSeconds 運(yùn)動(dòng)消耗時(shí)間,單位秒
* @param animType 動(dòng)作類型,字符串型,內(nèi)部自己轉(zhuǎn)換算子
* @param delay 延遲時(shí)間,多長(zhǎng)時(shí)間后開始運(yùn)動(dòng),單位秒
*/
window.rtween = function(startProps, endProps, timeSeconds, animType, delay)
{
var tw = new Tween();
tw.start(startProps, endProps, timeSeconds, animType, delay);
return tw;
}
示例如下:
http://img.jb51.net/online/Tween.htm
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
選擇列表里面的緩動(dòng)算法,點(diǎn)前面的按鈕,就會(huì)以想對(duì)的緩動(dòng)算法運(yùn)動(dòng)

源代碼: http://img.jb51.net/jslib/jquery/rtween.js
核心代碼:
function Tween()
{
this._frame=20;
//
this._animType = linear;
this._delay = 0;
//
this.run = function(){}
this.complete = function(){}
}
//
Tween.prototype.getValue = function(prop)
{
this._valueType = ”;
if(prop.constructor == Array) return prop;
//
if(typeof(prop) == 'string')
{
if(isColor(prop))
{
this._valueType = ‘color';
return c2a(prop);
}
if(prop.split('px').length>1)
{
this._valueType = ‘px';
return [prop.split('px')[0]];
}
}
return [prop];
}
Tween.prototype.setValue = function(prop)
{
if(this._valueType == ‘color')return a2c(prop);
if(this._valueType == ‘px')return prop[0]+'px';
return prop;
}
Tween.prototype.start = function(startProps, endProps, timeSeconds, animType, delay)
{
if(animType != undefined)this._animType = this.animTypes[animType];
if(delay != undefined)this._delay = delay;
//
this._timeSeconds = timeSeconds;
this._startTimer = new Date().getTime() + this._delay * 1000;
//
this._endProps = this.getValue(endProps);
this._startProps = this.getValue(startProps);
this._currentProps = [];
//
var $this = this;
clearInterval(this._runID);
this._runID = setInterval(
function(){$this._run();}
,this._frame);
}
Tween.prototype.stop = function(state)
{
for(var i in this._startProps)
{
if(Number(state)>0)
this._currentProps[i] = this._endProps[i];
else if(Number(state)<0)
this._currentProps[i] = this._startProps[i];
}
this.callListener();
this.complete();
//
clearInterval(this._runID);
}
Tween.prototype.callListener = function()
{
this.run(this.setValue(this._currentProps));
}
Tween.prototype._run = function()
{
if ( new Date().getTime()- this._startTimer< 0) return;
var isEnd = false;
//
for(var i in this._startProps)
{
this._currentProps[i] = this._animType( new Date().getTime()-this._startTimer,Number(this._startProps[i]),Number(this._endProps[i])-Number(this._startProps[i]),this._timeSeconds * 1000);
//
if(this._startTimer + (this._timeSeconds * 1000) <= new Date().getTime())
{
this._currentProps[i] = this._endProps[i];
isEnd = true;
}
}
//
if(isEnd)this.stop();
else this.callListener();
}
您可能感興趣的文章:
- JS使用tween.js動(dòng)畫庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能
- tween.js緩動(dòng)補(bǔ)間動(dòng)畫算法示例
- 詳解tween.js 中文使用指南
- 詳解tween.js的使用教程
- 原生JS實(shí)現(xiàn)移動(dòng)端web輪播圖詳解(結(jié)合Tween算法造輪子)
- js學(xué)習(xí)心得_一個(gè)簡(jiǎn)單的動(dòng)畫庫(kù)封裝tween.js
- JavaScript實(shí)現(xiàn)的Tween算法及緩沖特效實(shí)例代碼
- JavaScript Tween算法及緩動(dòng)效果
- JS+HTML5手機(jī)開發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法分析
- javascript中的緩動(dòng)效果實(shí)現(xiàn)程序
- tweenjs緩動(dòng)算法的使用實(shí)例分析
相關(guān)文章
JavaScript 產(chǎn)生不重復(fù)的隨機(jī)數(shù)三種實(shí)現(xiàn)思路
在 JavaScript 中,一般產(chǎn)生的隨機(jī)數(shù)會(huì)重復(fù),但是有時(shí)我們需要不重復(fù)的隨機(jī)數(shù),如何實(shí)現(xiàn)?本文給于解決方法,需要的朋友可以參考下2012-12-12Javascript中引用類型傳遞的知識(shí)點(diǎn)小結(jié)
這篇文章主要給大家介紹了關(guān)于Javascript中引用類型傳遞的知識(shí)點(diǎn),文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03瀏覽器解析js生成的html出現(xiàn)樣式問(wèn)題的解決方法
接觸css, javascript有三年多了,今天遇到的問(wèn)題最令我不可思議,很容易給人一種錯(cuò)覺(jué),那就是js拼成的html結(jié)構(gòu)肯定有問(wèn)題2012-04-04JS獲取隨機(jī)數(shù)和時(shí)間轉(zhuǎn)換的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇JS獲取隨機(jī)數(shù)和時(shí)間轉(zhuǎn)換的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07uniapp在微信小程序中圖片寬度顯示問(wèn)題示例代碼
在uniapp中,如果你的富文本圖片顯示寬度不正常,你可以通過(guò)設(shè)置圖片的寬高屬性來(lái)解決這個(gè)問(wèn)題,這篇文章主要介紹了uniapp在微信小程序中圖片寬度顯示問(wèn)題,需要的朋友可以參考下2023-02-02原生js的ajax和解決跨域的jsonp(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇原生js的ajax和解決跨域的jsonp(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10與ClientWidth有關(guān)的一點(diǎn)資料
與ClientWidth有關(guān)的一點(diǎn)資料...2006-08-08