-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.js
160 lines (113 loc) · 3.38 KB
/
Sprite.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
// Here is a simple class that contains loading methods.
// Nothing fancy, but the idea is simply to return a Promise
import { loadImg } from './load';
import {WebSystemObject} from './WebSystemObject';
import {Actor} from './Actor';
export class Sprite extends Actor {
/*
From: Arnaud Tanielian
@Example
import Sprite from './Sprite';
import dataFrame from 'path/to/data-frame.json'
const sprite = new Sprite();
const container = document.getElementById('container');
sprite.init({
src: 'path/to/texture.png',
dataFrames,
container
});
// Later
sprite.play();
*/
/* JSON generated by Texture Packer */
/* https://www.codeandweb.com/texturepacker */
dataFrames = null;
/* SRC of the sprite */
src = null;
/* Container for the Canvas */
container = null;
canvas = null;
frameHeight = 0;
frameHeight = 0;
dataArray = [];
FPS = 60;
isPlaying = false;
currentIndex = 0;
currentDate = new Date();
raf = null;
init(props) {
this.dataFrames = props.dataFrames;
this.src = props.src;
this.container = props.container || document.createElement('div');
this.frameHeight = this.config.frames[0].frame.h;
this.frameWidth = this.config.frames[0].frame.w;
this.loadImage();
}
loadImage() {
loadImg(this.src).then((img) => {
// has disposed before the end of the loading
if (!this.dataFrames) return;
this.createCanvas(img);
this.bindEvents();
})
}
createCanvas(img) {
this.canvas = document.createElement('canvas');
this.canvas.width = this.frameWidth;
this.canvas.height = this.frameHeight;
this.container.appendChild(this.canvas);
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
var canvasPaste = canvas.cloneNode(false);
canvasPaste.width = this.frameWidth;
canvasPaste.height = this.frameHeight;
let canvasPasteTemp, imgData;
for (let i = 0; i < this.dataFrames.frames.length; i++) {
const frame = this.dataFrames.frames[i];
// Store the image data of each tile in the array.
canvasPasteTemp = canvasPaste.cloneNode(false);
imgData = ctx.getImageData(frame.frame.x, frame.frame.y, frame.frame.w, frame.frame.h);
this.dataArray.push(imgData);
}
}
bindEvents() {
this.onUpdate();
}
unbindEvents() {
if (this.raf) window.cancelAnimationFrame(this.raf);
}
onUpdate = () => {
if (this.isPlaying && this.canvas) {
const date = new Date();
const minDelta = (1 / this.FPS) * 1000;
if (date - this.currentDate >= minDelta) {
this.currentDate = date;
const imgData = this.dataArray[this.currentIndex];
this.canvas.getContext('2d').putImageData(imgData, 0, 0);
this.currentIndex++;
if (this.currentIndex >= this.dataArray.length) {
this.isPlaying = false;
this.currentIndex = 0;
}
}
}
this.raf = window.requestAnimationFrame(this.onUpdate);
}
play() {
this.isPlaying = true;
}
dispose() {
this.dataFrames = null;
this.src = null;
this.container = null;
this.isPlaying = false;
this.currentIndex = 0;
this.canvas = null;
this.dataArray.length = 0;
this.dataArray = [];
this.unbindEvents();
}
}