-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteManager-es5.js
executable file
·39 lines (36 loc) · 1.42 KB
/
SpriteManager-es5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function(window, document, undefined) {
function SM(params) {
this.container = params.container;
this.fps = params.fps;
this.frameHeight = params.frameHeight;
this.offset = 0;
this.height = params.height;
this.isPlaying = false;
this.loop = params.loop || false;
this.loopDelay = params.loopDelay || 0;
this.complete = params.complete || false;
this.beforeLoop = typeof params.beforeLoop == "function" ? params.beforeLoop : null;
}
SM.prototype.play = function() {
if (this.isPlaying) return;
this.isPlaying = true;
this.offset = 0;
clearInterval(this.playInterval);
this.playInterval = setInterval(function () {
this.offset -= this.frameHeight;
this.container.style.backgroundPositionY = this.offset + "px";
if (this.offset <= -this.height + this.frameHeight) {
clearInterval(this.playInterval);
this.isPlaying = false;
if (this.complete) this.complete();
if (this.loop) {
setTimeout(function() {
if (this.beforeLoop) this.beforeLoop();
this.play();
}.bind(this), this.loopDelay);
}
}
}.bind(this), 1000 / this.fps);
}
window.SpriteManager = SM;
})(window, document);