-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriteanimator.js
384 lines (299 loc) · 12.9 KB
/
spriteanimator.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* SpriteAnimator v1.0
* site: Fx-future.ru
* license: MIT License http://www.opensource.org/licenses/mit-license.php
*/
var SpriteAnimator = function () {
var frame = 0; // текущий кадр
var repet = true; // повтор (в проигрывателе и навигации)
var imageArr = []; // массив со всеми изображениями и их параметрами
var GThat = this;
var inputWidth, inputHeight, imageDiv, infoDiv, inputForward, inputBack;
this.statusPlay = false;
this.init = function (arr) {
var that = this;
var h1 = document.createElement('h1');
$(h1).css({
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '18px',
'margin': '3px'
});
h1.appendChild(document.createTextNode('Аниматор спрайтов'));
$("body").append(h1);
// загрузка изображений (текст)
var loadText = document.createTextNode('Загрузка изображений...');
var loadDiv = document.createElement('div');
loadDiv.appendChild(loadText);
$("body").append(loadDiv);
// загрузка изображений (процесс)
for (var key in arr) {
var newim = new Image();
newim.src = arr[key];
imageArr.push({
image: newim,
delay: 100
});
} // end for
$(loadDiv).hide();
// изображение
var imageContent = document.createElement('div');
$(imageContent).css({
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px',
'font-weight': '100',
'top': '20px',
'width': '100%'
});
// $(closeButtonText).click(closeFun); // закрытие галлереи
imageContent.appendChild(document.createElement('br'));
// ширина
imageContent.appendChild(document.createTextNode('Ширина: '));
inputWidth = document.createElement('input');
inputWidth.type = "text";
$(inputWidth).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputWidth.onchange = function (e) {
if (this.value != "") imageArr[frame].image.width = this.value;
else $(imageArr[frame].image).removeAttr("width");
} // end fun
imageContent.appendChild(inputWidth);
// высота
$(imageContent).append(' Высота: ');
inputHeight = document.createElement('input');
inputHeight.type = "text";
inputHeight.value = "500";
$(inputHeight).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputHeight.onchange = function (e) {
if (this.value != "") imageArr[frame].image.height = this.value;
else $(imageArr[frame].image).removeAttr("height");
} // end fun
imageContent.appendChild(inputHeight);
// назад
$(imageContent).append(' Навигация: ');
inputBack = document.createElement('input');
inputBack.type = "button";
inputBack.value = "< назад";
$(inputBack).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputBack.onclick = function (e) {
// включаем кнопку вперёд
inputForward.disabled = false;
if (frame == 0) {
if (repet) {
frame = imageArr.length - 1;
}
} else {
frame--;
} // end fu
// меняем ширину и высоту у нового изображения
if (inputWidth.value != "") imageArr[frame].image.width = inputWidth.value;
else $(imageArr[frame].image).removeAttr("width");
if (inputHeight.value != "") imageArr[frame].image.height = inputHeight.value;
else $(imageArr[frame].image).removeAttr("height");
$(imageDiv).html(imageArr[frame].image);
$(infoDiv).html('Кадр ' + (frame + 1) + ': ' + imageArr[frame].image.src + ' <br>');
if (frame == 0 && !repet) this.disabled = true;
} // end fun
imageContent.appendChild(inputBack);
// вперёд
inputForward = document.createElement('input');
inputForward.type = "button";
inputForward.value = "вперёд >";
$(inputForward).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputForward.onclick = function (e) {
// переключаем вперёд
GThat.forward();
} // end fun
imageContent.appendChild(inputForward);
// проигрыватель
$(imageContent).append(' Проигрыватель: ');
var inputPlayer = document.createElement('input');
inputPlayer.type = "button";
inputPlayer.value = "старт";
$(inputPlayer).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputPlayer.onclick = function (e) {
if (GThat.statusPlay) {
GThat.statusPlay = false;
// меняем надпись кнопки
this.value = "играть";
} else {
GThat.statusPlay = true;
// меняем надпись кнопки
this.value = "пауза";
// старт проигрывателя
spriteAnimatorPlay();
} // end if
} // end fun
imageContent.appendChild(inputPlayer);
// проигрыватель (стоп)
var inputPlayerStop = document.createElement('input');
inputPlayerStop.type = "button";
inputPlayerStop.value = "стоп";
$(inputPlayerStop).css({
'width': '80px',
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px'
});
inputPlayerStop.onclick = function (e) {
GThat.statusPlay = false;
// меняем надпись кнопки
inputPlayer.value = "играть";
frame = -1;
// переключаем вперёд
GThat.forward();
} // end fun
imageContent.appendChild(inputPlayerStop);
// повтор
$(imageContent).append(' Повтор: ');
var inputRepet = document.createElement('input');
inputRepet.checked = "checked";
inputRepet.type = "checkbox";
$(inputRepet).css({
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'color': '#171819'
});
inputRepet.onclick = function (e) {
if (repet) repet = false;
else repet = true;
return true;
} // end fun
imageContent.appendChild(inputRepet);
// разделитель
imageContent.appendChild(document.createElement('hr'));
// путь до изображения
infoDiv = document.createElement('div');
$(infoDiv).html('Кадр ' + (frame + 1) + ': ' + imageArr[frame].image.src + ' <br>');
imageContent.appendChild(infoDiv);
// меняем высоту и добавляем текущие изображение на холст
imageDiv = document.createElement('div');
imageArr[frame].image.height = 500;
imageDiv.appendChild(imageArr[frame].image);
imageContent.appendChild(imageDiv);
// разделитель
imageContent.appendChild(document.createElement('hr'));
var h3 = document.createElement('h3');
$(h3).css({
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '16px',
'margin-top': '15px'
});
h3.appendChild(document.createTextNode('Настройка кадров'));
imageContent.appendChild(h3);
// настройка кадров
for (var key in imageArr) {
var nk = Number(key) + 1;
var h5 = document.createElement('h5');
$(h5).css({
'color': '#171819',
'font-family': 'Arial, Geneva, Helvetica, sans-serif',
'font-size': '14px',
'margin-top': '10px'
});
h5.appendChild(document.createTextNode('Кадр ' + nk));
imageContent.appendChild(h5);
// путь до изображения
imageContent.appendChild(document.createTextNode('Путь: '));
var newin = document.createElement('input');
newin.value = imageArr[key].image.src;
newin.style.width = "700px";
newin.setAttribute("frame", key);
newin.onchange = function (e) {
var tframe = this.getAttribute("frame");
imageArr[tframe].image.src = this.value;
} // end fun
imageContent.appendChild(newin);
imageContent.appendChild(document.createElement('br'));
// задержка
imageContent.appendChild(document.createTextNode('Задержка: '));
var newdelay = document.createElement('input');
newdelay.value = imageArr[key].delay;
newdelay.style.width = "100px";
newdelay.setAttribute("frame", key);
newdelay.onchange = function (e) {
var tframe = this.getAttribute("frame");
imageArr[tframe].delay = this.value;
} // end fun
imageContent.appendChild(newdelay);
imageContent.appendChild(document.createTextNode(' мс'));
imageContent.appendChild(document.createElement('br'));
// прозрачность
imageContent.appendChild(document.createTextNode('Прозрачность: '));
var newopacity = document.createElement('input');
newopacity.value = 1;
newopacity.style.width = "100px";
newopacity.setAttribute("frame", key);
newopacity.onchange = function (e) {
var tframe = this.getAttribute("frame");
imageArr[tframe].image.style.opacity = this.value;
} // end fun
imageContent.appendChild(newopacity);
imageContent.appendChild(document.createTextNode(' от 0 до 1 (например 0.3)'));
imageContent.appendChild(document.createElement('br'));
imageContent.appendChild(document.createElement('br'));
} // end for
$("body").append(imageContent);
return 0;
} // end fun (init)
// функция переключения вперёд
this.forward = function () {
// включаем кнопку назад
inputBack.disabled = false;
if (frame == imageArr.length - 1) {
if (repet) {
frame = 0;
}
} else {
frame++;
} // end fu
// меняем ширину и высоту у нового изображения
if (inputWidth.value != "") imageArr[frame].image.width = inputWidth.value;
else $(imageArr[frame].image).removeAttr("width");
if (inputHeight.value != "") imageArr[frame].image.height = inputHeight.value;
else $(imageArr[frame].image).removeAttr("height");
$(imageDiv).html(imageArr[frame].image);
$(infoDiv).html('Кадр ' + (frame + 1) + ': ' + imageArr[frame].image.src + ' <br>');
if (frame == imageArr.length - 1 && !repet) {
inputForward.disabled = true;
return false;
}
return true;
} // end fun
this.getDelay = function () {
return imageArr[frame].delay;
} // end fun
};
var spriteanimator = new SpriteAnimator();
// проигрыватель на таймере
function spriteAnimatorPlay() {
if (spriteanimator.statusPlay && spriteanimator.forward()) {
var ldaley = spriteanimator.getDelay();
setTimeout('spriteAnimatorPlay()', ldaley);
} // end if
} // end fun