-
Notifications
You must be signed in to change notification settings - Fork 783
/
FrameRenderer.js
341 lines (277 loc) · 10.5 KB
/
FrameRenderer.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
(function () {
var ns = $.namespace('pskl.rendering.frame');
/**
* FrameRenderer will display a given frame inside a canvas element.
* @param {HtmlElement} container HtmlElement to use as parentNode of the Frame
* @param {Object} renderingOptions
* @param {Array} classList array of strings to use for css classList
*/
ns.FrameRenderer = function (container, renderingOptions, classList) {
this.defaultRenderingOptions = {
'supportGridRendering' : false,
'zoom' : 1
};
renderingOptions = $.extend(true, {}, this.defaultRenderingOptions, renderingOptions);
if (container === undefined) {
throw 'Bad FrameRenderer initialization. <container> undefined.';
}
if (isNaN(renderingOptions.zoom)) {
throw 'Bad FrameRenderer initialization. <zoom> not well defined.';
}
this.container = container;
this.zoom = renderingOptions.zoom;
this.offset = {
x : 0,
y : 0
};
this.margin = {
x : 0,
y : 0
};
this.supportGridRendering = renderingOptions.supportGridRendering;
this.classList = classList || [];
this.classList.push('canvas');
/**
* Off dom canvas, will be used to draw the frame at 1:1 ratio
* @type {HTMLElement}
*/
this.canvas = null;
/**
* Displayed canvas, scaled-up from the offdom canvas
* @type {HTMLElement}
*/
this.displayCanvas = null;
this.setDisplaySize(renderingOptions.width, renderingOptions.height);
this.setGridWidth(this.getUserGridWidth_());
$.subscribe(Events.USER_SETTINGS_CHANGED, this.onUserSettingsChange_.bind(this));
};
pskl.utils.inherit(pskl.rendering.frame.FrameRenderer, pskl.rendering.AbstractRenderer);
ns.FrameRenderer.prototype.render = function (frame) {
if (frame) {
this.clear();
this.renderFrame_(frame);
}
};
ns.FrameRenderer.prototype.clear = function () {
pskl.utils.CanvasUtils.clear(this.canvas);
pskl.utils.CanvasUtils.clear(this.displayCanvas);
};
ns.FrameRenderer.prototype.setZoom = function (zoom) {
// Minimum zoom is one to ensure one sprite pixel occupies at least one pixel on screen.
var minimumZoom = 1;
// Maximum zoom is relative to the display dimensions to ensure at least 10 pixels can
// be drawn on screen.
var maximumZoom = Math.min(this.displayWidth, this.displayHeight) / 10;
zoom = pskl.utils.Math.minmax(zoom, minimumZoom, maximumZoom);
if (zoom == this.zoom) {
return;
}
// back up center coordinates
var centerX = this.offset.x + (this.displayWidth / (2 * this.zoom));
var centerY = this.offset.y + (this.displayHeight / (2 * this.zoom));
this.zoom = zoom;
// recenter
this.setOffset(
centerX - (this.displayWidth / (2 * this.zoom)),
centerY - (this.displayHeight / (2 * this.zoom))
);
};
ns.FrameRenderer.prototype.getZoom = function () {
return this.zoom;
};
ns.FrameRenderer.prototype.setDisplaySize = function (width, height) {
this.displayWidth = width;
this.displayHeight = height;
if (this.displayCanvas) {
$(this.displayCanvas).remove();
this.displayCanvas = null;
}
this.createDisplayCanvas_();
};
ns.FrameRenderer.prototype.getDisplaySize = function () {
return {
height : this.displayHeight,
width : this.displayWidth
};
};
ns.FrameRenderer.prototype.getOffset = function () {
return {
x : this.offset.x,
y : this.offset.y
};
};
ns.FrameRenderer.prototype.setOffset = function (x, y) {
var width = pskl.app.piskelController.getWidth();
var height = pskl.app.piskelController.getHeight();
var maxX = width - (this.displayWidth / this.zoom);
x = pskl.utils.Math.minmax(x, 0, maxX);
var maxY = height - (this.displayHeight / this.zoom);
y = pskl.utils.Math.minmax(y, 0, maxY);
this.offset.x = x;
this.offset.y = y;
};
ns.FrameRenderer.prototype.getGridColor = function () {
return pskl.UserSettings.get(pskl.UserSettings.GRID_COLOR);
};
ns.FrameRenderer.prototype.setGridWidth = function (value) {
this.gridWidth_ = value;
};
ns.FrameRenderer.prototype.getGridWidth = function () {
if (!this.supportGridRendering) {
return 0;
}
return this.gridWidth_;
};
/**
* Compute a grid width value best suited to the current display context,
* particularly for the current zoom level
*/
ns.FrameRenderer.prototype.computeGridWidthForDisplay_ = function () {
var gridWidth = this.getGridWidth();
while (this.zoom < 6 * gridWidth) {
gridWidth--;
}
return gridWidth;
};
ns.FrameRenderer.prototype.updateMargins_ = function (frame) {
var deltaX = this.displayWidth - (this.zoom * frame.getWidth());
this.margin.x = Math.max(0, deltaX) / 2;
var deltaY = this.displayHeight - (this.zoom * frame.getHeight());
this.margin.y = Math.max(0, deltaY) / 2;
};
ns.FrameRenderer.prototype.createDisplayCanvas_ = function () {
var height = this.displayHeight;
var width = this.displayWidth;
this.displayCanvas = pskl.utils.CanvasUtils.createCanvas(width, height, this.classList);
pskl.utils.CanvasUtils.disableImageSmoothing(this.displayCanvas);
this.container.append(this.displayCanvas);
};
ns.FrameRenderer.prototype.onUserSettingsChange_ = function (evt, settingName, settingValue) {
var settings = pskl.UserSettings;
if (settingName == settings.GRID_WIDTH || settingName == settings.GRID_ENABLED) {
this.setGridWidth(this.getUserGridWidth_());
}
};
ns.FrameRenderer.prototype.getUserGridWidth_ = function () {
var gridEnabled = pskl.UserSettings.get(pskl.UserSettings.GRID_ENABLED);
var width = pskl.UserSettings.get(pskl.UserSettings.GRID_WIDTH);
return gridEnabled ? width : 0;
};
/**
* Transform a screen pixel-based coordinate (relative to the top-left corner of the rendered
* frame) into a sprite coordinate in column and row.
* @public
*/
ns.FrameRenderer.prototype.getCoordinates = function(x, y) {
var containerOffset = this.container.offset();
x = x - containerOffset.left;
y = y - containerOffset.top;
// apply margins
x = x - this.margin.x;
y = y - this.margin.y;
var cellSize = this.zoom;
// apply frame offset
x = x + this.offset.x * cellSize;
y = y + this.offset.y * cellSize;
return {
x : Math.floor(x / cellSize),
y : Math.floor(y / cellSize)
};
};
ns.FrameRenderer.prototype.reverseCoordinates = function(x, y) {
var cellSize = this.zoom;
x = x * cellSize;
y = y * cellSize;
x = x - this.offset.x * cellSize;
y = y - this.offset.y * cellSize;
x = x + this.margin.x;
y = y + this.margin.y;
var containerOffset = this.container.offset();
x = x + containerOffset.left;
y = y + containerOffset.top;
return {
x : x + (cellSize / 2),
y : y + (cellSize / 2)
};
};
/**
* @private
*/
ns.FrameRenderer.prototype.renderFrame_ = function (frame) {
if (!this.canvas || frame.getWidth() != this.canvas.width || frame.getHeight() != this.canvas.height) {
this.canvas = pskl.utils.CanvasUtils.createCanvas(frame.getWidth(), frame.getHeight());
}
var w = this.canvas.width;
var h = this.canvas.height;
var z = this.zoom;
// Draw in canvas
pskl.utils.FrameUtils.drawToCanvas(frame, this.canvas);
this.updateMargins_(frame);
var displayContext = this.displayCanvas.getContext('2d');
displayContext.save();
var translateX = this.margin.x - this.offset.x * z;
var translateY = this.margin.y - this.offset.y * z;
var isZoomedOut = translateX > 0 || translateY > 0;
// Draw the background / zoomed-out color only if needed. Otherwise the clearRect
// happening after that will clear "out of bounds" and seems to be doing nothing
// on some chromebooks (cf https://github.com/piskelapp/piskel/issues/651)
if (isZoomedOut) {
// Draw background
displayContext.fillStyle = Constants.ZOOMED_OUT_BACKGROUND_COLOR;
// The -1 on the width and height here is a workaround for a Chrome-only bug
// that was potentially fixed, but is very hardware dependant. Seems to be
// triggered when doing clear rect or fill rect using the full width & height
// of a canvas. (https://bugs.chromium.org/p/chromium/issues/detail?id=469906)
displayContext.fillRect(0, 0, this.displayCanvas.width - 1, this.displayCanvas.height - 1);
}
displayContext.translate(translateX, translateY);
// Scale up to draw the canvas content
displayContext.scale(z, z);
if (pskl.UserSettings.get('SEAMLESS_MODE')) {
displayContext.clearRect(-1 * w, -1 * h, 3 * w, 3 * h);
} else {
displayContext.clearRect(0, 0, w, h);
}
if (pskl.UserSettings.get('SEAMLESS_MODE')) {
this.drawTiledFrames_(displayContext, this.canvas, w, h, 1);
}
displayContext.drawImage(this.canvas, 0, 0);
// Draw grid.
var gridWidth = this.computeGridWidthForDisplay_();
if (gridWidth > 0) {
var gridColor = this.getGridColor();
// Scale out before drawing the grid.
displayContext.scale(1 / z, 1 / z);
var drawOrClear;
if (gridColor === Constants.TRANSPARENT_COLOR) {
drawOrClear = displayContext.clearRect.bind(displayContext);
} else {
displayContext.fillStyle = gridColor;
drawOrClear = displayContext.fillRect.bind(displayContext);
}
// Draw or clear vertical lines.
for (var i = 1 ; i < frame.getWidth() ; i++) {
drawOrClear((i * z) - (gridWidth / 2), 0, gridWidth, h * z);
}
// Draw or clear horizontal lines.
for (var j = 1 ; j < frame.getHeight() ; j++) {
drawOrClear(0, (j * z) - (gridWidth / 2), w * z, gridWidth);
}
}
displayContext.restore();
};
/**
* Draw repeatedly the provided image around the main drawing area. Used for seamless
* drawing mode, to easily create seamless textures. A colored overlay is applied to
* differentiate those additional frames from the main frame.
*/
ns.FrameRenderer.prototype.drawTiledFrames_ = function (context, image, w, h, z) {
var opacity = pskl.UserSettings.get('SEAMLESS_OPACITY');
opacity = pskl.utils.Math.minmax(opacity, 0, 1);
context.fillStyle = 'rgba(255, 255, 255, ' + opacity + ')';
[[0, -1], [0, 1], [-1, -1], [-1, 0], [-1, 1], [1, -1], [1, 0], [1, 1]].forEach(function (d) {
context.drawImage(image, d[0] * w * z, d[1] * h * z);
context.fillRect(d[0] * w * z, d[1] * h * z, w * z, h * z);
});
};
})();