-
Notifications
You must be signed in to change notification settings - Fork 0
/
SipuSlider.js
214 lines (200 loc) · 6.73 KB
/
SipuSlider.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var SIPUSlider = (function (SIPUSlider, $, undefined) {
"use strict";
//main struct
var DATANODE;
var SLIDERNAME;
var SLIDERS = [];
var Slider = {
Index: -1,
SetAttrVal: "",
SetFadeTime: 0.5,
SetRangeMax: 255,
SetCanvasHeight: 600,
SetCanvasWidth: 800,
SetPlayInterval: 50,
SetImages: "",
CanvasNode: "",
ImageListNode: "",
PlayButtonNode: "",
StopButtonNode: "",
RangeNode: "",
OnPlaying: false,
OnPlayInterval: "",
SetNode: function () {
setNodeSlide(this.Index);
},
ValidCheck: function () {
if (typeof (this.CanvasNode) === "object" &&
typeof (this.ImageListNode) === "object" &&
typeof (this.PlayButtonNode) === "object" &&
typeof (this.StopButtonNode) === "object" &&
typeof (this.RangeNode) === "object") {
return true;
}
},
init: function () {
this.SetNode();
if (this.ValidCheck()) {
this.ImageListNode.style.display = "none";
var callbackfunc = function (index) {
setCanvas(index);
setAnimate(index);
setRangeNode(index);
setPlayButtonNode(index);
setStopButtonNode(index);
//first render images
drawCanvasNode(SLIDERS[index].CanvasNode, 1, SLIDERS[index].SetRangeMax, 1, SLIDERS[index].SetImages, SLIDERS[index].SetCanvasHeight, SLIDERS[index].SetCanvasWidth);
};
this.SetImages = loadImages(this.Index, callbackfunc);
} else {
console.log("Failed Build " + this.SetAttrVal);
}
}
};
function getDataNode() {
var temp = [];
var node = document.getElementsByTagName("*");
for (var i = 0; i < node.length; i++) {
if (node[i].attributes.length > 0) {
var attr = (node[i].attributes);
for (var j = 0; j < attr.length; j++) {
var at = attr[j].name.split("-");
if (at[0] === "data" && at[1] === "sipu") {
temp.push(node[i]);
}
}
}
}
return temp;
}
function getSliderName() {
var temp = [];
var node = DATANODE;
for (var i = 0; i < node.length; i++) {
if (node[i].attributes.length > 0) {
var attr = (node[i].attributes);
for (var j = 0; j < attr.length; j++) {
var at = attr[j].name.split("-");
if (at[0] === "data" && at[1] === "sipu") {
var valueName = attr[j].nodeValue;
if (temp.indexOf(valueName) === -1) {
temp.push(valueName);
}
}
}
}
}
return temp;
}
function initSlider() {
for (var i = 0; i < SLIDERNAME.length; i++) {
SLIDERS.push(Slider);
SLIDERS[i].SetAttrVal = SLIDERNAME[i];
SLIDERS[i].Index = i;
SLIDERS[i].init();
}
}
function setNodeSlide(index) {
if (SLIDERS[index].SetAttrVal.length > 0) {
for (var i = 0; i < DATANODE.length; i++) {
if (DATANODE[i].getAttribute("data-sipu-canvas") === SLIDERS[index].SetAttrVal) {
SLIDERS[index].CanvasNode = DATANODE[i];
}
if (DATANODE[i].getAttribute("data-sipu-images") === SLIDERS[index].SetAttrVal) {
SLIDERS[index].ImageListNode = DATANODE[i];
}
if (DATANODE[i].getAttribute("data-sipu-playbutton") === SLIDERS[index].SetAttrVal) {
SLIDERS[index].PlayButtonNode = DATANODE[i];
}
if (DATANODE[i].getAttribute("data-sipu-stopbutton") === SLIDERS[index].SetAttrVal) {
SLIDERS[index].StopButtonNode = DATANODE[i];
}
if (DATANODE[i].getAttribute("data-sipu-range") === SLIDERS[index].SetAttrVal) {
SLIDERS[index].RangeNode = DATANODE[i];
}
}
}
}
function setCanvas(index) {
//fix bug
SLIDERS[index].CanvasNode.style.width = SLIDERS[index].SetCanvasWidth || 640;
SLIDERS[index].CanvasNode.style.height = SLIDERS[index].SetCanvasHeight || 480;
}
function loadImages(index, func) {
//console.log(index + func);
var img = $(SLIDERS[index].ImageListNode).find("img");
var loadcheck = 0;
var loadimage = new Array(img.length);
$.each(img, function (i) {
var newImg = new Image();
newImg.onload = function () {
loadimage[i] = newImg;
loadcheck++;
if (loadcheck === img.length) {
SLIDERS[index].SetImages = loadimage;
return func(index);
}
};
newImg.src = img[i].src;
});
}
function setRangeNode(index) {
SLIDERS[index].RangeNode.value = 1;
SLIDERS[index].RangeNode.setAttribute("min", 1);
SLIDERS[index].RangeNode.setAttribute("max", SLIDERS[index].SetRangeMax);
$(SLIDERS[index].RangeNode).on("change mousemove input", function () {
drawCanvasNode(SLIDERS[index].CanvasNode, 1, SLIDERS[index].SetRangeMax, this.value, SLIDERS[index].SetImages, SLIDERS[index].SetCanvasHeight, SLIDERS[index].SetCanvasWidth);
});
}
function drawCanvasNode(node, min, max, value, images, h, w) {
var context = node.getContext("2d");
context.canvas.height = h;
context.canvas.width = w;
context.imageSmoothingEnabled = !1;
context.mozImageSmoothingEnabled = !1;
var index = Math.ceil(value / (max / (images.length - 1)));
var alphavalue = Math.ceil(value / (max / (images.length - 1))) - value / (max / (images.length - 1));
context.drawImage(images[index - 1], 0, 0, images[index - 1].width, images[index - 1].height, 0, 0, w, h);
context.globalAlpha = 1 - alphavalue;
context.drawImage(images[index], 0, 0, images[index].width, images[index].height, 0, 0, w, h);
context.globalAlpha = alphavalue;
}
function setPlayButtonNode(index) {
$(SLIDERS[index].PlayButtonNode).click(function () {
if (SLIDERS[index].OnPlaying === false) {
var rangeval = SLIDERS[index].RangeNode.value;
SLIDERS[index].OnPlaying = true;
SLIDERS[index].SetPlayInterval = setInterval(function () {
SLIDERS[index].RangeNode.value = rangeval;
rangeval++;
drawCanvasNode(SLIDERS[index].CanvasNode, 1, SLIDERS[index].SetRangeMax, rangeval, SLIDERS[index].SetImages, SLIDERS[index].SetCanvasHeight, SLIDERS[index].SetCanvasWidth);
if (rangeval === parseInt(SLIDERS[index].RangeNode.getAttribute("max"))) {
rangeval = SLIDERS[index].RangeNode.value;
clearInterval(SLIDERS[index].SetPlayInterval);
SLIDERS[index].OnPlaying = false;
}
}, SLIDERS[index].OnPlayInterval || 20);
}
});
}
function setStopButtonNode(index) {
$(SLIDERS[index].StopButtonNode).click(function () {
SLIDERS[index].OnPlaying = false;
clearInterval(SLIDERS[index].SetPlayInterval);
});
}
function setAnimate(index) {
$(SLIDERS[index].CanvasNode).css("-webkit-transition", "all " + SLIDERS[index].SetFadeTime + "s ease-in-out");
$(SLIDERS[index].CanvasNode).css("-moz-transition", "all " + SLIDERS[index].SetFadeTime + "s ease-in-out");
$(SLIDERS[index].CanvasNode).css("-o-transition", "all " + SLIDERS[index].SetFadeTime + "s ease-in-out");
$(SLIDERS[index].CanvasNode).css("transition", "all " + SLIDERS[index].SetFadeTime + "s ease-in-out");
}
SIPUSlider.run = function () {
//master function
DATANODE = getDataNode();
SLIDERNAME = getSliderName();
initSlider();
};
return SIPUSlider;
})(window.SIPUSlider || {}, jQuery);
SIPUSlider.run();