-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
CanvasTexture.js
647 lines (564 loc) · 22.1 KB
/
CanvasTexture.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var Clamp = require('../math/Clamp');
var Color = require('../display/color/Color');
var CONST = require('../const');
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
var Texture = require('./Texture');
/**
* @classdesc
* A Canvas Texture is a special kind of Texture that is backed by an HTML Canvas Element as its source.
*
* You can use the properties of this texture to draw to the canvas element directly, using all of the standard
* canvas operations available in the browser. Any Game Object can be given this texture and will render with it.
*
* Note: When running under WebGL the Canvas Texture needs to re-generate its base WebGLTexture and reupload it to
* the GPU every time you modify it, otherwise the changes you make to this texture will not be visible. To do this
* you should call `CanvasTexture.refresh()` once you are finished with your changes to the canvas. Try and keep
* this to a minimum, especially on large canvas sizes, or you may inadvertently thrash the GPU by constantly uploading
* texture data to it. This restriction does not apply if using the Canvas Renderer.
*
* It starts with only one frame that covers the whole of the canvas. You can add further frames, that specify
* sections of the canvas using the `add` method.
*
* Should you need to resize the canvas use the `setSize` method so that it accurately updates all of the underlying
* texture data as well. Forgetting to do this (i.e. by changing the canvas size directly from your code) could cause
* graphical errors.
*
* @class CanvasTexture
* @extends Phaser.Textures.Texture
* @memberof Phaser.Textures
* @constructor
* @since 3.7.0
*
* @param {Phaser.Textures.TextureManager} manager - A reference to the Texture Manager this Texture belongs to.
* @param {string} key - The unique string-based key of this Texture.
* @param {HTMLCanvasElement} source - The canvas element that is used as the base of this texture.
* @param {number} width - The width of the canvas.
* @param {number} height - The height of the canvas.
*/
var CanvasTexture = new Class({
Extends: Texture,
initialize:
function CanvasTexture (manager, key, source, width, height)
{
Texture.call(this, manager, key, source, width, height);
this.add('__BASE', 0, 0, 0, width, height);
/**
* A reference to the Texture Source of this Canvas.
*
* @name Phaser.Textures.CanvasTexture#_source
* @type {Phaser.Textures.TextureSource}
* @private
* @since 3.7.0
*/
this._source = this.frames['__BASE'].source;
/**
* The source Canvas Element.
*
* @name Phaser.Textures.CanvasTexture#canvas
* @readonly
* @type {HTMLCanvasElement}
* @since 3.7.0
*/
this.canvas = this._source.image;
/**
* The 2D Canvas Rendering Context.
*
* @name Phaser.Textures.CanvasTexture#context
* @readonly
* @type {CanvasRenderingContext2D}
* @since 3.7.0
*/
this.context = this.canvas.getContext('2d', { willReadFrequently: true });
/**
* The width of the Canvas.
* This property is read-only, if you wish to change it use the `setSize` method.
*
* @name Phaser.Textures.CanvasTexture#width
* @readonly
* @type {number}
* @since 3.7.0
*/
this.width = width;
/**
* The height of the Canvas.
* This property is read-only, if you wish to change it use the `setSize` method.
*
* @name Phaser.Textures.CanvasTexture#height
* @readonly
* @type {number}
* @since 3.7.0
*/
this.height = height;
/**
* The context image data.
* Use the `update` method to populate this when the canvas changes.
*
* @name Phaser.Textures.CanvasTexture#imageData
* @type {ImageData}
* @since 3.13.0
*/
this.imageData = this.context.getImageData(0, 0, width, height);
/**
* A Uint8ClampedArray view into the `buffer`.
* Use the `update` method to populate this when the canvas changes.
* Note that this is unavailable in some browsers, such as Epic Browser, due to their security restrictions.
*
* @name Phaser.Textures.CanvasTexture#data
* @type {Uint8ClampedArray}
* @since 3.13.0
*/
this.data = null;
if (this.imageData)
{
this.data = this.imageData.data;
}
/**
* An Uint32Array view into the `buffer`.
*
* @name Phaser.Textures.CanvasTexture#pixels
* @type {Uint32Array}
* @since 3.13.0
*/
this.pixels = null;
/**
* An ArrayBuffer the same size as the context ImageData.
*
* @name Phaser.Textures.CanvasTexture#buffer
* @type {ArrayBuffer}
* @since 3.13.0
*/
this.buffer;
if (this.data)
{
if (this.imageData.data.buffer)
{
this.buffer = this.imageData.data.buffer;
this.pixels = new Uint32Array(this.buffer);
}
else if (window.ArrayBuffer)
{
this.buffer = new ArrayBuffer(this.imageData.data.length);
this.pixels = new Uint32Array(this.buffer);
}
else
{
this.pixels = this.imageData.data;
}
}
},
/**
* This re-creates the `imageData` from the current context.
* It then re-builds the ArrayBuffer, the `data` Uint8ClampedArray reference and the `pixels` Int32Array.
*
* Warning: This is a very expensive operation, so use it sparingly.
*
* @method Phaser.Textures.CanvasTexture#update
* @since 3.13.0
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
update: function ()
{
this.imageData = this.context.getImageData(0, 0, this.width, this.height);
this.data = this.imageData.data;
if (this.imageData.data.buffer)
{
this.buffer = this.imageData.data.buffer;
this.pixels = new Uint32Array(this.buffer);
}
else if (window.ArrayBuffer)
{
this.buffer = new ArrayBuffer(this.imageData.data.length);
this.pixels = new Uint32Array(this.buffer);
}
else
{
this.pixels = this.imageData.data;
}
if (this.manager.game.config.renderType === CONST.WEBGL)
{
this.refresh();
}
return this;
},
/**
* Draws the given Image or Canvas element to this CanvasTexture, then updates the internal
* ImageData buffer and arrays.
*
* @method Phaser.Textures.CanvasTexture#draw
* @since 3.13.0
*
* @param {number} x - The x coordinate to draw the source at.
* @param {number} y - The y coordinate to draw the source at.
* @param {(HTMLImageElement|HTMLCanvasElement)} source - The element to draw to this canvas.
* @param {boolean} [update=true] - Update the internal ImageData buffer and arrays.
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
draw: function (x, y, source, update)
{
if (update === undefined) { update = true; }
this.context.drawImage(source, x, y);
if (update)
{
this.update();
}
return this;
},
/**
* Draws the given texture frame to this CanvasTexture, then updates the internal
* ImageData buffer and arrays.
*
* @method Phaser.Textures.CanvasTexture#drawFrame
* @since 3.16.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(string|number)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
* @param {number} [x=0] - The x coordinate to draw the source at.
* @param {number} [y=0] - The y coordinate to draw the source at.
* @param {boolean} [update=true] - Update the internal ImageData buffer and arrays.
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
drawFrame: function (key, frame, x, y, update)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (update === undefined) { update = true; }
var textureFrame = this.manager.getFrame(key, frame);
if (textureFrame)
{
var cd = textureFrame.canvasData;
var width = textureFrame.cutWidth;
var height = textureFrame.cutHeight;
var res = textureFrame.source.resolution;
this.context.drawImage(
textureFrame.source.image,
cd.x, cd.y,
width,
height,
x, y,
width / res,
height / res
);
if (update)
{
this.update();
}
}
return this;
},
/**
* Sets a pixel in the CanvasTexture to the given color and alpha values.
*
* This is an expensive operation to run in large quantities, so use sparingly.
*
* @method Phaser.Textures.CanvasTexture#setPixel
* @since 3.16.0
*
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} red - The red color value. A number between 0 and 255.
* @param {number} green - The green color value. A number between 0 and 255.
* @param {number} blue - The blue color value. A number between 0 and 255.
* @param {number} [alpha=255] - The alpha value. A number between 0 and 255.
*
* @return {this} This CanvasTexture.
*/
setPixel: function (x, y, red, green, blue, alpha)
{
if (alpha === undefined) { alpha = 255; }
x = Math.abs(Math.floor(x));
y = Math.abs(Math.floor(y));
var index = this.getIndex(x, y);
if (index > -1)
{
var imageData = this.context.getImageData(x, y, 1, 1);
imageData.data[0] = red;
imageData.data[1] = green;
imageData.data[2] = blue;
imageData.data[3] = alpha;
this.context.putImageData(imageData, x, y);
}
return this;
},
/**
* Puts the ImageData into the context of this CanvasTexture at the given coordinates.
*
* @method Phaser.Textures.CanvasTexture#putData
* @since 3.16.0
*
* @param {ImageData} imageData - The ImageData to put at the given location.
* @param {number} x - The x coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [dirtyX=0] - Horizontal position (x coordinate) of the top-left corner from which the image data will be extracted.
* @param {number} [dirtyY=0] - Vertical position (x coordinate) of the top-left corner from which the image data will be extracted.
* @param {number} [dirtyWidth] - Width of the rectangle to be painted. Defaults to the width of the image data.
* @param {number} [dirtyHeight] - Height of the rectangle to be painted. Defaults to the height of the image data.
*
* @return {this} This CanvasTexture.
*/
putData: function (imageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
{
if (dirtyX === undefined) { dirtyX = 0; }
if (dirtyY === undefined) { dirtyY = 0; }
if (dirtyWidth === undefined) { dirtyWidth = imageData.width; }
if (dirtyHeight === undefined) { dirtyHeight = imageData.height; }
this.context.putImageData(imageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
return this;
},
/**
* Gets an ImageData region from this CanvasTexture from the position and size specified.
* You can write this back using `CanvasTexture.putData`, or manipulate it.
*
* @method Phaser.Textures.CanvasTexture#getData
* @since 3.16.0
*
* @param {number} x - The x coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the top-left of the area to get the ImageData from. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} width - The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.
* @param {number} height - The height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up.
*
* @return {ImageData} The ImageData extracted from this CanvasTexture.
*/
getData: function (x, y, width, height)
{
x = Clamp(Math.floor(x), 0, this.width - 1);
y = Clamp(Math.floor(y), 0, this.height - 1);
width = Clamp(width, 1, this.width - x);
height = Clamp(height, 1, this.height - y);
var imageData = this.context.getImageData(x, y, width, height);
return imageData;
},
/**
* Get the color of a specific pixel from this texture and store it in a Color object.
*
* If you have drawn anything to this CanvasTexture since it was created you must call `CanvasTexture.update` to refresh the array buffer,
* otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.
*
* @method Phaser.Textures.CanvasTexture#getPixel
* @since 3.13.0
*
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {Phaser.Display.Color} [out] - A Color object to store the pixel values in. If not provided a new Color object will be created.
*
* @return {Phaser.Display.Color} An object with the red, green, blue and alpha values set in the r, g, b and a properties.
*/
getPixel: function (x, y, out)
{
if (!out)
{
out = new Color();
}
var index = this.getIndex(x, y);
if (index > -1)
{
var data = this.data;
var r = data[index + 0];
var g = data[index + 1];
var b = data[index + 2];
var a = data[index + 3];
out.setTo(r, g, b, a);
}
return out;
},
/**
* Returns an array containing all of the pixels in the given region.
*
* If the requested region extends outside the bounds of this CanvasTexture,
* the region is truncated to fit.
*
* If you have drawn anything to this CanvasTexture since it was created you must call `CanvasTexture.update` to refresh the array buffer,
* otherwise this may return out of date color values, or worse - throw a run-time error as it tries to access an array element that doesn't exist.
*
* @method Phaser.Textures.CanvasTexture#getPixels
* @since 3.16.0
*
* @param {number} [x=0] - The x coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [y=0] - The y coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} [width] - The width of the region to get. Must be an integer. Defaults to the canvas width if not given.
* @param {number} [height] - The height of the region to get. Must be an integer. If not given will be set to the `width`.
*
* @return {Phaser.Types.Textures.PixelConfig[][]} A 2d array of Pixel objects.
*/
getPixels: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = width; }
x = Math.abs(Math.round(x));
y = Math.abs(Math.round(y));
var left = Clamp(x, 0, this.width);
var right = Clamp(x + width, 0, this.width);
var top = Clamp(y, 0, this.height);
var bottom = Clamp(y + height, 0, this.height);
var pixel = new Color();
var out = [];
for (var py = top; py < bottom; py++)
{
var row = [];
for (var px = left; px < right; px++)
{
pixel = this.getPixel(px, py, pixel);
row.push({ x: px, y: py, color: pixel.color, alpha: pixel.alphaGL });
}
out.push(row);
}
return out;
},
/**
* Returns the Image Data index for the given pixel in this CanvasTexture.
*
* The index can be used to read directly from the `this.data` array.
*
* The index points to the red value in the array. The subsequent 3 indexes
* point to green, blue and alpha respectively.
*
* @method Phaser.Textures.CanvasTexture#getIndex
* @since 3.16.0
*
* @param {number} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
* @param {number} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
*
* @return {number}
*/
getIndex: function (x, y)
{
x = Math.abs(Math.round(x));
y = Math.abs(Math.round(y));
if (x < this.width && y < this.height)
{
return (x + y * this.width) * 4;
}
else
{
return -1;
}
},
/**
* This should be called manually if you are running under WebGL.
* It will refresh the WebGLTexture from the Canvas source. Only call this if you know that the
* canvas has changed, as there is a significant GPU texture allocation cost involved in doing so.
*
* @method Phaser.Textures.CanvasTexture#refresh
* @since 3.7.0
*
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
*/
refresh: function ()
{
this._source.update();
return this;
},
/**
* Gets the Canvas Element.
*
* @method Phaser.Textures.CanvasTexture#getCanvas
* @since 3.7.0
*
* @return {HTMLCanvasElement} The Canvas DOM element this texture is using.
*/
getCanvas: function ()
{
return this.canvas;
},
/**
* Gets the 2D Canvas Rendering Context.
*
* @method Phaser.Textures.CanvasTexture#getContext
* @since 3.7.0
*
* @return {CanvasRenderingContext2D} The Canvas Rendering Context this texture is using.
*/
getContext: function ()
{
return this.context;
},
/**
* Clears the given region of this Canvas Texture, resetting it back to transparent.
* If no region is given, the whole Canvas Texture is cleared.
*
* @method Phaser.Textures.CanvasTexture#clear
* @since 3.7.0
*
* @param {number} [x=0] - The x coordinate of the top-left of the region to clear.
* @param {number} [y=0] - The y coordinate of the top-left of the region to clear.
* @param {number} [width] - The width of the region.
* @param {number} [height] - The height of the region.
* @param {boolean} [update=true] - Update the internal ImageData buffer and arrays.
*
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
*/
clear: function (x, y, width, height, update)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
if (update === undefined) { update = true; }
this.context.clearRect(x, y, width, height);
if (update)
{
this.update();
}
return this;
},
/**
* Changes the size of this Canvas Texture.
*
* @method Phaser.Textures.CanvasTexture#setSize
* @since 3.7.0
*
* @param {number} width - The new width of the Canvas.
* @param {number} [height] - The new height of the Canvas. If not given it will use the width as the height.
*
* @return {Phaser.Textures.CanvasTexture} The Canvas Texture.
*/
setSize: function (width, height)
{
if (height === undefined) { height = width; }
if (width !== this.width || height !== this.height)
{
// Update the Canvas
this.canvas.width = width;
this.canvas.height = height;
// Update the Texture Source
this._source.width = width;
this._source.height = height;
this._source.isPowerOf2 = IsSizePowerOfTwo(width, height);
// Update the Frame
this.frames['__BASE'].setSize(width, height, 0, 0);
// Update this
this.width = width;
this.height = height;
this.refresh();
}
return this;
},
/**
* Destroys this Texture and releases references to its sources and frames.
*
* @method Phaser.Textures.CanvasTexture#destroy
* @since 3.16.0
*/
destroy: function ()
{
Texture.prototype.destroy.call(this);
this._source = null;
this.canvas = null;
this.context = null;
this.imageData = null;
this.data = null;
this.pixels = null;
this.buffer = null;
}
});
module.exports = CanvasTexture;