-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathviewer.js
207 lines (169 loc) · 3.63 KB
/
viewer.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
/*
* viewer.js <z64.me>
*
* A reference implementation demonstrating how to use
* EzSpriteSheet's JSON output with JavaScript.
*
*/
/* global variables */
var displayGrid = true;
var view_width = 256;
var view_height = 256;
var animIndex = 0;
var mirrorX = 0;
var mirrorY = 0;
var spriteDB;
/* retrieve animation frame based on milliseconds elapsed */
function framenow(anim, ms)
{
var cur = 0;
var i = 0;
ms %= anim.ms;
while (i < anim.frames-1)
{
var frame = anim.frame[i];
if (ms >= cur && ms < cur+frame.ms)
return frame;
cur += frame.ms;
i++;
}
return anim.frame[i];
}
function button_prev()
{
if (!animIndex)
animIndex = spriteDB.animations;
animIndex--;
}
function button_next()
{
animIndex++;
if (animIndex >= spriteDB.animations)
animIndex = 0;
}
function button_mirrorX()
{
mirrorX = !mirrorX;
}
function button_mirrorY()
{
mirrorY = !mirrorY;
}
function button_grid()
{
displayGrid = !displayGrid;
}
(function ()
{
var spritesheetImage;
var canvas;
function mainLoop ()
{
window.requestAnimationFrame(mainLoop);
/* clear canvas */
var context = canvas.getContext("2d");
context.clearRect(0, 0, view_width, view_height);
/* set up animation frame */
var anim = spriteDB.animation[animIndex];
var ms = Date.now();
var frame = framenow(anim, ms);
var center_x = view_width / 2;
var center_y = view_height / 2;
var x = center_x;
var y = center_y;
var w = frame.w;
var h = frame.h;
var wOff = w;
var hOff = h;
document.getElementById("animName").innerText = anim.name;
/* swap mirror offset axes for rotated images */
if (frame.rot)
{
wOff = h;
hOff = w;
}
/* display the grid: a sprite's pivot point lives at its center */
if (displayGrid)
{
context.globalAlpha = 0.25;
context.fillRect(0, 0, center_x, center_y);
context.fillRect(center_x, center_y, center_x, center_y);
context.globalAlpha = 1.0;
}
context.save();
/* x mirroring */
if (mirrorX)
{
x += frame.ox - wOff;
context.scale(-1, 1);
w *= -1;
x *= -1;
} else
x -= frame.ox;
/* y mirroring */
if (mirrorY)
{
y += frame.oy - hOff;
context.scale(1, -1);
h *= -1;
y *= -1;
} else
y -= frame.oy;
context.translate(x, y);
/* sprite within sprite sheet is rotated */
if (frame.rot)
{
if (mirrorX && mirrorY)
context.translate(-frame.h, 0);
else if (mirrorX)
context.translate(0, frame.w);
else if (mirrorY)
context.translate(0, -hOff);
else
context.translate(frame.h, 0);
context.rotate(1.5708); /* 90 degrees clockwise */
}
/* display */
context.drawImage(
spritesheetImage[frame.sheet],
frame.x, /* cropping rectangle (from sprite sheet) */
frame.y,
frame.w,
frame.h,
0, /* positioning rectangle (onto canvas) */
0,
w,
h
);
context.restore();
}
/* get canvas element */
canvas = document.getElementById("viewer-canvas");
canvas.width = view_width;
canvas.height = view_height;
/* load sprite database from json */
$.ajax({
url: "sprites.json",
dataType: "json",
async: false,
success: function( data )
{
spriteDB = data;
/* output sprite sheets onto web page */
var items = [];
for (var i = 0; i < data.sheets; i++)
items.push( "<img class='sprite-sheet' src='" + data.sheet[i].source + "'>" );
$( "<p/>", {
html: items.join( "<br/>" )
}).appendTo( "body" );
}
});
/* load sprite sheet images */
spritesheetImage = new Array();
for (var i = 0; i < spriteDB.sheets; i++)
{
spritesheetImage[i] = new Image();
spritesheetImage[i].src = spriteDB.sheet[i].source;
}
mainLoop();
} ());