-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathplayer.js
207 lines (188 loc) · 6.93 KB
/
player.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
function Player(script, scriptDir)
{
var that = this;
var kMaxCursorWidth = 32;
var kMaxCursorHeight = 32;
var g_scriptDir = null;
var g_script = null;
var g_index = -1;
var g_divElement = null;
var g_mainCanvas = null;
var g_cursorCanvas = null;
var g_blitImage = null;
var g_imageCache = {};
var g_imageCount = 0;
var g_imageLoadCount = 0;
var g_loaded = false;
var g_inCallback = false;
var g_paused = true;
var g_stepTimeoutID = null;
this.onload = function() {};
this.onloop = function() {};
function assert(x) {
// With IE9, window.console is undefined (unless the console is
// opened), so we must avoid calling console.assert.
if (!x) {
// This line must have braces around it so that a breakpoint can be
// placed on it. Affects at least Firefox 19.
throw new Error("Assertion failed");
}
}
function imageLoaded()
{
g_imageLoadCount++;
assert(g_imageLoadCount <= g_imageCount);
if (g_imageLoadCount == g_imageCount) {
// Use an extra delay in the hope that it will work around a
// problem with IE9. With that browser, there is a player bug
// where the first frame is not drawn, and the canvas instead
// starts off transparent.
window.setTimeout(finishSetup, 0);
}
}
function finishSetup()
{
// Avoid flicker by executing the initial 0-delay steps before firing
// the onload handler. The canvas will be painted when the handler
// fires.
assert(!g_loaded);
for (g_index = 0; 1; g_index++) {
assert(g_index < g_script.length);
var delayMS = g_script[g_index][0];
if (delayMS > 0)
break;
executeStep(g_index);
}
g_loaded = true;
g_inCallback = true;
that.onload();
g_inCallback = false;
if (!g_paused)
beginCurrentStep();
}
function executeStep(index)
{
var stepKind = g_script[index][1];
if (stepKind == "blitimg") {
var url = g_scriptDir + "/" + g_script[index][2];
g_blitImage = g_imageCache[url];
} else if (stepKind == "blit") {
var ctx = g_mainCanvas.getContext("2d");
var blits = g_script[index][2]
for (var i = 0; i < blits.length; ++i) {
var blit = blits[i];
var sx = blit[0];
var sy = blit[1];
var w = blit[2];
var h = blit[3];
var dx = blit[4];
var dy = blit[5];
ctx.drawImage(g_blitImage, sx, sy, w, h, dx, dy, w, h);
}
} else if (stepKind == "screen") {
var url = g_scriptDir + "/" + g_script[index][2];
var ctx = g_mainCanvas.getContext("2d");
ctx.clearRect(0, 0, g_mainCanvas.width, g_mainCanvas.height);
ctx.drawImage(g_imageCache[url], 0, 0);
} else if (stepKind == "cpos") {
g_cursorCanvas.style.left = g_script[index][2] + "px";
g_cursorCanvas.style.top = g_script[index][3] + "px";
} else if (stepKind == "cimg") {
// "cimg" is an obsolete script command kept for backwards
// compatibility with existing animations.
var url = g_scriptDir + "/" + g_script[index][2];
var ctx = g_cursorCanvas.getContext("2d");
ctx.clearRect(0, 0, g_cursorCanvas.width, g_cursorCanvas.height);
ctx.drawImage(g_imageCache[url], 0, 0);
} else if (stepKind == "cursor") {
// Clear the canvas first, then move it, then draw the new cursor.
var url = g_scriptDir + "/" + g_script[index][2];
var ctx = g_cursorCanvas.getContext("2d");
ctx.clearRect(0, 0, g_cursorCanvas.width, g_cursorCanvas.height);
g_cursorCanvas.style.left = g_script[index][3] + "px";
g_cursorCanvas.style.top = g_script[index][4] + "px";
ctx.drawImage(g_imageCache[url], 0, 0);
} else if (stepKind == "nop") {
// Do nothing. This step kind only exists for convenience in adding
// delays to a script.
} else {
alert("Invalid step in animation script: " + g_script[index]);
}
}
function beginCurrentStep()
{
assert(g_loaded && !g_paused);
assert(g_stepTimeoutID === null);
var delayMS = g_script[g_index][0];
g_stepTimeoutID = window.setTimeout(function() {
g_stepTimeoutID = null;
executeStep(g_index);
g_index++;
if (g_index == g_script.length) {
g_index = 0;
g_inCallback = true;
that.onloop();
g_inCallback = false;
}
if (!g_paused)
beginCurrentStep();
}, delayMS);
}
this.start = function() {
g_paused = false;
if (g_loaded && !g_inCallback && g_stepTimeoutID === null) {
beginCurrentStep();
}
}
this.pause = function() {
g_paused = true;
if (g_loaded && !g_inCallback && g_stepTimeoutID !== null) {
window.clearTimeout(g_stepTimeoutID);
g_stepTimeoutID = null;
}
}
this.isPaused = function() {
return g_paused;
}
// Note that this function returns false for the duration of the onload
// callback.
this.isLoaded = function() {
return g_loaded;
}
var widthPx = script["width"];
var heightPx = script["height"];
g_divElement = document.createElement("div");
g_divElement.style.position = "relative";
g_divElement.style.width = widthPx + "px";
g_divElement.style.height = heightPx + "px";
g_divElement.style.overflow = "hidden";
this.element = g_divElement;
g_mainCanvas = document.createElement("canvas");
g_mainCanvas.style.position = "absolute";
g_mainCanvas.style.left = "0px";
g_mainCanvas.style.top = "0px";
g_mainCanvas.width = widthPx;
g_mainCanvas.height = heightPx;
g_divElement.appendChild(g_mainCanvas);
g_cursorCanvas = document.createElement("canvas");
g_cursorCanvas.style.position = "absolute";
g_cursorCanvas.width = kMaxCursorWidth;
g_cursorCanvas.height = kMaxCursorHeight;
g_divElement.appendChild(g_cursorCanvas);
g_scriptDir = scriptDir
g_script = script["steps"]
for (var i = 0; i < g_script.length; ++i) {
var stepKind = g_script[i][1];
if (stepKind == "blitimg" ||
stepKind == "screen" ||
stepKind == "cimg" ||
stepKind == "cursor") {
g_imageCount++;
var url = g_scriptDir + "/" + g_script[i][2];
var image = new Image();
g_imageCache[url] = image;
image.onload = imageLoaded;
image.src = url;
}
}
}