forked from Kalmani/csspriteLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (85 loc) · 2.67 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
var Class = require('uclass');
var onRemove = require('udom/element/onRemove');
var csSpriteLite = new Class({
Binds : ['stop'],
Implements : [
require('uclass/options')
],
options : {
url : null,
width : 200,
height : 200,
interval : 100,
anchor : document.body,
loop_from : 0,
ignored_frames : [],
loop_start_coor : {
x : 0, y : 0
},
current_position : {
x : 0, y : 0
}
},
interval : null,
initialize : function(options) {
var self = this;
self.setOptions(options);
},
nextStep : function() {
var self = this;
if (self.options.loop_from == self.frame && !self.loop_flag) {
self.options.loop_start_coor = {x : self.options.current_position.x, y : self.options.current_position.y};
self.loop_flag = true;
}
self.options.current_position.x += self.options.width;
if (self.img_size.width <= self.options.current_position.x)
self.options.current_position = {x : 0, y : self.options.current_position.y + self.options.height};
if (self.img_size.height <= self.options.current_position.y) {
self.frame = self.options.loop_from;
self.options.current_position = {x : self.options.loop_start_coor.x, y : self.options.loop_start_coor.y};
}
if (self.options.ignored_frames.indexOf(self.frame) !== -1)
self.nextStep();
},
setStyles : function(obj, propertyObject) {
for (var property in propertyObject)
obj.style[property] = propertyObject[property];
},
play : function() {
var self = this,
src_img = new Image();
src_img.onload = function() {
self.img_size = {
'width' : this.width,
'height' : this.height
};
var canvas = document.createElement("div");
canvas.id = "css-sprite";
self.setStyles(canvas, {
"backgroundImage" : "url('" + self.options.url + "')",
"backgroundRepeat" : "no-repeat",
"backgroundPosition" : "0px 0px",
"width" : self.options.width || 200,
"height" : self.options.height || 200
});
self.options.anchor.appendChild(canvas);
onRemove(canvas, self.stop);
self.frame = 0,
self.loop_flag = false;
self.interval = setInterval(function() {
self.nextStep();
self.setStyles(canvas, {
"backgroundPosition" : "-" + self.options.current_position.x + "px -" + self.options.current_position.y + "px"
});
self.frame++;
}, self.options.interval);
};
src_img.src = self.options.url;
},
stop : function() {
var self = this;
clearInterval(self.interval);
}
});
module.exports = csSpriteLite;