JS Tween 颜色渐变

 更新时间:2008年12月30日 15:16:52   作者:  
从我写的as tween改写的,基本功能跟as里面写的一样,只是没有扩展特定功能的接口(比如alpha2,move2,size2,color2等接口,这些在as tween里面都有实现)。
有31中缓动算法,实现了颜色的自动转换(#f00 #ff0000 rgb(255,0,0)格式到颜色运算格式,最后返回#ff0000格式)、px单位的自动转换。
调用接口:
/**
* 对外接口
* Tween的示例
* @param startProps 开始属性,单个属性或者数组
* @param endProps 结束属性,单个属性或者数组
* @param timeSeconds 运动消耗时间,单位秒
* @param animType 动作类型,字符串型,内部自己转换算子
* @param delay 延迟时间,多长时间后开始运动,单位秒
*/
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需再刷新一下页面才能执行]

选择列表里面的缓动算法,点前面的按钮,就会以想对的缓动算法运动

源代码: 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();
}

相关文章

  • JavaScript 产生不重复的随机数三种实现思路

    JavaScript 产生不重复的随机数三种实现思路

    在 JavaScript 中,一般产生的随机数会重复,但是有时我们需要不重复的随机数,如何实现?本文给于解决方法,需要的朋友可以参考下
    2012-12-12
  • Javascript中引用类型传递的知识点小结

    Javascript中引用类型传递的知识点小结

    这篇文章主要给大家介绍了关于Javascript中引用类型传递的知识点,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-03-03
  • javascript版2048小游戏

    javascript版2048小游戏

    本文给大家分享的是使用javascript制作的2048小游戏的代码,仅仅是想锻炼下自己的编程代码风格,尽量做的规范些,小伙伴们多多给些建议。
    2015-03-03
  • 浏览器解析js生成的html出现样式问题的解决方法

    浏览器解析js生成的html出现样式问题的解决方法

    接触css, javascript有三年多了,今天遇到的问题最令我不可思议,很容易给人一种错觉,那就是js拼成的html结构肯定有问题
    2012-04-04
  • Javascript Worker子线程代码实例

    Javascript Worker子线程代码实例

    这篇文章主要介绍了Javascript Worker子线程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • JS获取随机数和时间转换的简单实例

    JS获取随机数和时间转换的简单实例

    下面小编就为大家带来一篇JS获取随机数和时间转换的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • uniapp在微信小程序中图片宽度显示问题示例代码

    uniapp在微信小程序中图片宽度显示问题示例代码

    在uniapp中,如果你的富文本图片显示宽度不正常,你可以通过设置图片的宽高属性来解决这个问题,这篇文章主要介绍了uniapp在微信小程序中图片宽度显示问题,需要的朋友可以参考下
    2023-02-02
  • 原生js的ajax和解决跨域的jsonp(实例讲解)

    原生js的ajax和解决跨域的jsonp(实例讲解)

    下面小编就为大家带来一篇原生js的ajax和解决跨域的jsonp(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 与ClientWidth有关的一点资料

    与ClientWidth有关的一点资料

    与ClientWidth有关的一点资料...
    2006-08-08
  • 原生js实现碰撞检测

    原生js实现碰撞检测

    这篇文章主要为大家详细介绍了原生js实现碰撞检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03

最新评论