-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasresizer.js
356 lines (338 loc) · 13.7 KB
/
canvasresizer.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
'use strict';
/**
* A class to help keeping canvas size suitable for the window or parent
* element size and screen resolution.
* @param {Object} options Object with the following optional keys:
* canvas: HTMLCanvasElement (one is created by default)
* mode: CanvasResizer.Mode (defaults to filling the window)
* width: number Width of the coordinate space.
* height: number Height of the coordinate space.
* parentElement: HTMLElement (defaults to the document body)
* wrapperElement: HTMLElement Optional wrapper element that tightly wraps
* the canvas. Useful for implementing HTML-based UI on top of the canvas.
* The wrapper element should already be the parent of the canvas when it
* is passed in.
*/
var CanvasResizer = function(options) {
var defaults = {
canvas: null,
mode: CanvasResizer.Mode.DYNAMIC,
width: 16,
height: 9,
parentElement: document.body,
wrapperElement: null
};
for(var key in defaults) {
if (!options.hasOwnProperty(key)) {
this[key] = defaults[key];
} else {
this[key] = options[key];
}
}
if (this.canvas !== null) {
if (!options.hasOwnProperty('width')) {
this.width = this.canvas.width;
}
if (!options.hasOwnProperty('height')) {
this.height = this.canvas.height;
}
} else {
this.canvas = document.createElement('canvas');
this.canvas.width = this.width;
this.canvas.height = this.height;
}
this.canvasWidthToHeight = this.width / this.height;
if (this.mode === CanvasResizer.Mode.FIXED_RESOLUTION) {
this.canvas.style.imageRendering = 'pixelated';
this.canvas.width = this.width;
this.canvas.height = this.height;
}
var that = this;
var resize = function() {
that.resize();
}
if (this.parentElement === document.body) {
document.body.style.padding = '0';
document.body.style.margin = '0';
document.body.style.overflow = 'hidden';
} else {
this.parentElement.style.padding = '0';
}
// No need to remove the object from existing parent if it has one
if (this.wrapperElement === null) {
this.parentElement.appendChild(this.canvas);
} else {
this.parentElement.appendChild(this.wrapperElement);
// Assume that wrapper already wraps the canvas - don't re-append the
// canvas to the wrapper since the wrapper might have other children.
}
window.addEventListener('resize', resize, false);
this.resize();
this._scale = 1.0;
this._wrapCtx = null;
};
CanvasResizer.Mode = {
// Fixed amount of pixels, rendered pixelated:
FIXED_RESOLUTION: 0,
// Fixed amount of pixels, rendered interpolated:
FIXED_RESOLUTION_INTERPOLATED: 1,
// Only available for 2D canvas. Set the canvas transform on render to
// emulate a fixed coordinate system:
FIXED_COORDINATE_SYSTEM: 2,
// Fix the aspect ratio, but not the exact width/height of the coordinate
// space:
FIXED_ASPECT_RATIO: 3,
// Make the canvas fill the containing element completely, with the
// coordinate space being set according to the canvas dimensions:
DYNAMIC: 4
};
/**
* Resize callback.
*/
CanvasResizer.prototype.resize = function() {
// Resize only on a render call to avoid flicker from changing canvas
// size.
this.resizeOnNextRender = true;
};
/**
* Do nothing. This function exists just for mainloop.js compatibility.
*/
CanvasResizer.prototype.update = function() {
};
/**
* Call this function in the beginning of rendering a frame to update
* the canvas size. Compatible with mainloop.js.
*/
CanvasResizer.prototype.render = function() {
if (this.resizeOnNextRender) {
var parentProperties = this._getParentProperties();
var parentWidth = parentProperties.width;
var parentHeight = parentProperties.height;
var parentWidthToHeight = parentProperties.widthToHeight;
if (this.mode === CanvasResizer.Mode.FIXED_RESOLUTION ||
this.mode === CanvasResizer.Mode.FIXED_RESOLUTION_INTERPOLATED) {
this._resizeFixedResolution();
} else if (this.mode === CanvasResizer.Mode.FIXED_COORDINATE_SYSTEM ||
this.mode === CanvasResizer.Mode.FIXED_ASPECT_RATIO) {
if (parentWidthToHeight > this.canvasWidthToHeight) {
// Parent is wider, so there will be empty space on the left and right
this.canvas.height = parentHeight;
this.canvas.width = Math.floor(this.canvasWidthToHeight * this.canvas.height);
this.canvas.style.marginTop = '0';
this.canvas.style.marginLeft = Math.round((parentWidth - this.canvas.width) * 0.5) + 'px';
} else {
// Parent is narrower, so there will be empty space on the top and bottom
this.canvas.width = parentWidth;
this.canvas.height = Math.floor(this.canvas.width / this.canvasWidthToHeight);
this.canvas.style.marginTop = Math.round((parentHeight - this.canvas.height) * 0.5) + 'px';
this.canvas.style.marginLeft = '0';
}
this.canvas.style.width = this.canvas.width + 'px';
this.canvas.style.height = this.canvas.height + 'px';
this.canvas.style.marginBottom = '-5px'; // This is to work around a bug in Firefox 38
} else { // CanvasResizer.Mode.DYNAMIC
this.canvas.width = parentWidth;
this.canvas.height = parentHeight;
this.canvas.style.width = parentWidth + 'px';
this.canvas.style.height = parentHeight + 'px';
this.canvas.style.marginTop = '0';
this.canvas.style.marginLeft = '0';
}
if (this.wrapperElement !== null) {
this.wrapperElement.style.width = this.canvas.style.width;
this.wrapperElement.style.height = this.canvas.style.height;
this.wrapperElement.style.marginTop = this.canvas.style.marginTop;
this.wrapperElement.style.marginLeft = this.canvas.style.marginLeft;
this.canvas.style.marginTop = '0';
this.canvas.style.marginLeft = '0';
}
this.resizeOnNextRender = false;
}
if (this.mode == CanvasResizer.Mode.FIXED_COORDINATE_SYSTEM) {
var ctx = this.canvas.getContext('2d');
var scale = this.canvas.width / this.width;
ctx.setTransform(scale, 0, 0, scale, 0, 0);
// Wrap the context so that when ctx.canvas.width/height is queried, they return the coordinate system width/height.
if (this._wrapCtx == null) {
var wrapCtx = {};
for (var prop in ctx) {
if (prop.indexOf('webkit') != 0) {
(function(p) {
if (typeof ctx[prop] == 'function') {
wrapCtx[p] = function() {
ctx[p].apply(ctx, arguments);
};
} else if (prop != 'canvas') {
Object.defineProperty(wrapCtx, p, {
get: function() { return ctx[p]; },
set: function(v) { ctx[p] = v; }
});
}
})(prop);
}
}
wrapCtx.canvas = {};
var that = this;
Object.defineProperty(wrapCtx.canvas, 'width', {
get: function() { return that.width; }
});
Object.defineProperty(wrapCtx.canvas, 'height', {
get: function() { return that.height; }
});
this._wrapCtx = wrapCtx;
}
return this._wrapCtx;
}
};
/**
* Get a canvas coordinate space position from a given event. The coordinate
* space is relative to the width and height properties of the canvas.
* @param {MouseEvent|PointerEvent|TouchEvent} Event to get the position from.
* In case of a touch event, the position is retrieved from the first touch
* point.
* @return {Object} Object with x and y keys for horizontal and vertical
* positions in the canvas coordinate space.
*/
CanvasResizer.prototype.getCanvasPosition = function(event) {
var rect = this.canvas.getBoundingClientRect();
var x, y;
if (event.touches !== undefined && event.touches.length > 0) {
x = event.touches[0].clientX;
y = event.touches[0].clientY;
} else {
x = event.clientX;
y = event.clientY;
}
// +0.5 to position the coordinates to the pixel center.
var xRel = x - rect.left + 0.5;
var yRel = y - rect.top + 0.5;
var coordWidth = this.canvas.width;
var coordHeight = this.canvas.height;
if (this.mode == CanvasResizer.Mode.FIXED_COORDINATE_SYSTEM) {
coordWidth = this.width;
coordHeight = this.height;
}
if (rect.width != coordWidth) {
xRel *= coordWidth / rect.width;
yRel *= coordHeight / rect.height;
}
if (typeof Vec2 !== 'undefined') {
return new Vec2(xRel, yRel);
} else {
return {x: xRel, y: yRel};
}
};
/**
* @return {HTMLCanvasElement} The canvas element this resizer is using.
* If no canvas element was passed in on creation, one has been created.
*/
CanvasResizer.prototype.getCanvas = function() {
return this.canvas;
};
/**
* Set the dimensions of the canvas coordinate space. Note that this has no
* effect when the mode is DYNAMIC. If the mode is FIXED_ASPECT_RATIO, the
* aspect ratio is set based on the width and height.
* @param {number} width New width for the canvas element.
* @param {number} height New height for the canvas element.
*/
CanvasResizer.prototype.changeCanvasDimensions = function(width, height) {
this.width = width;
this.height = height;
this.canvasWidthToHeight = this.width / this.height;
this.resize();
};
/**
* Change the resizing mode.
* @param {CanvasResizer.Mode} mode New mode to use.
*/
CanvasResizer.prototype.changeMode = function(mode) {
this.mode = mode;
this.canvas.style.imageRendering = 'auto';
this.resize();
};
/**
* @return {number} the scale at which the canvas coordinate space is drawn.
*/
CanvasResizer.prototype.getScale = function() {
if (this.mode === CanvasResizer.Mode.FIXED_COORDINATE_SYSTEM) {
return this.canvas.width / this.width;
} else if (this.mode === CanvasResizer.Mode.FIXED_RESOLUTION ||
this.mode === CanvasResizer.Mode.FIXED_RESOLUTION_INTERPOLATED)
{
return this._scale;
} else {
return 1.0;
}
};
/**
* Get properties of the containing element.
* @return {Object} Object containing keys width, height, and widthToHeight.
* @protected
*/
CanvasResizer.prototype._getParentProperties = function() {
var parentProperties = {};
if (this.parentElement === document.body) {
parentProperties.width = window.innerWidth;
parentProperties.height = window.innerHeight;
} else {
parentProperties.width = this.parentElement.clientWidth;
parentProperties.height = this.parentElement.clientHeight;
}
parentProperties.widthToHeight = parentProperties.width / parentProperties.height;
return parentProperties;
};
/**
* Resize the canvas in one of the fixed resolution modes.
* @protected
*/
CanvasResizer.prototype._resizeFixedResolution = function() {
if (this.mode !== CanvasResizer.Mode.FIXED_RESOLUTION &&
this.mode !== CanvasResizer.Mode.FIXED_RESOLUTION_INTERPOLATED) {
return;
}
this.canvas.width = this.width;
this.canvas.height = this.height;
var parentProperties = this._getParentProperties();
var parentWidth = parentProperties.width;
var parentHeight = parentProperties.height;
var parentWidthToHeight = parentProperties.widthToHeight;
var styleWidth = 0;
var styleHeight = 0;
if (this.mode === CanvasResizer.Mode.FIXED_RESOLUTION) {
var maxWidth = parentWidth * window.devicePixelRatio;
var maxHeight = parentHeight * window.devicePixelRatio;
if (this.canvas.width > maxWidth || this.canvas.height > maxHeight) {
if (parentWidthToHeight > this.canvasWidthToHeight) {
styleHeight = parentHeight;
styleWidth = Math.floor(this.canvasWidthToHeight * styleHeight);
} else {
styleWidth = parentWidth;
styleHeight = Math.floor(styleWidth / this.canvasWidthToHeight);
}
this.canvas.style.imageRendering = 'auto';
} else {
var i = 1;
while ((i + 1) * this.width <= maxWidth && (i + 1) * this.height <= maxHeight) {
++i;
}
styleWidth = (this.width * i) / window.devicePixelRatio;
styleHeight = (this.height * i) / window.devicePixelRatio;
this.canvas.style.imageRendering = 'pixelated';
}
} else if (this.mode === CanvasResizer.Mode.FIXED_RESOLUTION_INTERPOLATED) {
if (parentWidthToHeight > this.canvasWidthToHeight) {
styleHeight = parentHeight;
styleWidth = Math.floor(this.canvasWidthToHeight * styleHeight);
} else {
styleWidth = parentWidth;
styleHeight = Math.floor(styleWidth / this.canvasWidthToHeight);
}
}
this.canvas.style.width = styleWidth + 'px';
this.canvas.style.height = styleHeight + 'px';
this._scale = styleHeight / this.canvas.height;
this.canvas.style.marginLeft = Math.round((parentWidth - styleWidth) * 0.5) + 'px';
this.canvas.style.marginTop = Math.round((parentHeight - styleHeight) * 0.5) + 'px';
this.canvas.style.marginBottom = '-5px'; // This is to work around a bug in Firefox 38
};