logo
当前位置:首 页 > 编程技术 >前端开发 > 查看文章

Javascript 视觉 逐帧动画(修正版)

前端开发 你是第7182个围观者 0条评论 供稿者: 标签:,

Javascript 视觉 逐帧动画(修正版)

使用视觉欺骗的方式,改变图片的时间在时间不能察觉懂啊的时间段内,可以不必使用gif图片,

gif图片的缺点大家都知道了,色彩值只用256位,而且比较大,如果用flash的话会提高浏览器的消耗,如果过多的会致使浏览器卡死,特别是ie;所以这个方式不为一个提高效率改善用户体验的好方式!

Bug修正:

1、修正重复点开始累加执行。

2、增加loop未设置初始化为循环。

3、增加设置循环时间的接口。

4、修正loop为flase无法执行第一轮的bug。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div id="showImg" style="width:200px; height:300px;"></div>
<button id="start">开始</button>
<button id="stop">停止</button>
<input type="text" value="200" id="speed" />
</body>
</html>
<script type="text/javascript">
 
/*{
target:null,
img:url,
frames:number, 帧数
speed:500 hm,
loop:true,
width:null, //舞台尺寸
height:null
}*/
var FBFAnimation = (function(){
function start() {
if (this.timeLine) return;
var speed = (this._json.speed ? this._json.speed : 500), bthis = this;
this.timeLine = setInterval(function() {
animation.apply(bthis);
}, speed);
};
 
function stop() {
if (this.timeLine) clearTimeout(this.timeLine);
this.timeLine = null;
};
 
function setSpeed(val) {
if (!val) return;
this._json.speed = val;
};
 
function getPosition() {
var x = 0, y = 0;
if ((this.startFrames == this.endFrames) && this._json.loop) this.startFrames = 0;
x = -(this.width * this.startFrames);
 
this.startFrames++;
return { x : x, y : y };
};
 
function animation() {
if (!this._json.loop && this.startFrames == this.endFrames - 1) this.stop();
var offset = getPosition.apply(this);
this._json.target.style.backgroundPosition = ""+ offset.x +"px "+ offset.y +"px";
};
 
function init() {
var target = this._json.target;
this.width = 0;
this.height = 0;
this.startFrames = 0;
this.endFrames = this._json.frames;
target.style.backgroundImage = 'url(' + this._json.img + ')';
target.style.backgroundRepeat = "no-repeat";
target.style.backgroundPosition = "0 0";
 
if (!this._json.width) this.width = parseInt(this._json.target.style.width);
if (!this._json.height) this.height = parseInt(this._json.target.style.height);
};
 
return function(args) {
this._json = args || {};
 
if (!this._json.target) throw new Error('target is not exist!');
if (!this._json.img) throw new Error('img not exist!');
if (!this._json.frames) throw new Error('frames number not exist');
if (typeof this._json.loop == "undefined") this._json.loop = true;
 
init.apply(this); //initialize
 
this.start = start; //interface
this.stop = stop;
this.setSpeed = setSpeed;
};
})();
 
var s = new FBFAnimation({
target : document.getElementById('showImg'),
img : 'images/demo.jpg',
frames : 8,
speed : 100
});
 
document.getElementById('start').onclick = function() {
s.start();
};
 
document.getElementById('stop').onclick = function() {
s.stop();
};
 
document.getElementById('speed').onchange = function() {
s.setSpeed(document.getElementById('speed').value);
}
 
</script>

 

下面是用于测试的图片

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
你可能也喜欢Related Posts
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao Studio All Rights Reserved. E-mail:cjx2328#126.com(#号改成@) 沪ICP备14052271号-3