Skip to content

Commit a4badb6

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents cc164b5 + caad527 commit a4badb6

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ one set of bindings ever created, which makes things a lot cleaner.
161161
* `CanvasTexture.getIndex` is a new method that will take an x/y coordinate and return the Image Data index offset used to retrieve the pixel values.
162162
* `CanvasTexture.getPixels` is a new method that will take a region as an x/y and width/height and return all of the pixels in that region from the CanvasTexture.
163163
* `CanvasTexture.setPixel` is a new method that sets the given pixel in the CanvasTexture to the color and alpha values provided.
164+
* `CanvasTexture.getData` is a new method that will extract an ImageData block from the CanvasTexture from the region given.
165+
* `CanvasTexture.putData` is a new method that will put an ImageData block at the given coordinates in a CanvasTexture.
164166

165167
### Updates
166168

src/textures/CanvasTexture.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,54 @@ var CanvasTexture = new Class({
312312
return this;
313313
},
314314

315+
/**
316+
* Puts the ImageData into the context of this CanvasTexture at the given coordinates.
317+
*
318+
* @method Phaser.Textures.CanvasTexture#putData
319+
* @since 3.16.0
320+
*
321+
* @param {ImageData} imageData - The ImageData to put at the given location.
322+
* @param {integer} x - The x coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
323+
* @param {integer} y - The y coordinate to put the imageData. Must lay within the dimensions of this CanvasTexture and be an integer.
324+
*
325+
* @return {this} This CanvasTexture.
326+
*/
327+
putData: function (imageData, x, y)
328+
{
329+
x = Math.abs(Math.floor(x));
330+
y = Math.abs(Math.floor(y));
331+
332+
this.context.putImageData(imageData, x, y);
333+
334+
return this;
335+
},
336+
337+
/**
338+
* Gets an ImageData region from this CanvasTexture from the position and size specified.
339+
* You can write this back using `CanvasTexture.putData`, or manipulate it.
340+
*
341+
* @method Phaser.Textures.CanvasTexture#getData
342+
* @since 3.16.0
343+
*
344+
* @param {integer} 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.
345+
* @param {integer} 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.
346+
* @param {integer} width - The width of the region to get. Must be an integer.
347+
* @param {integer} height - The height of the region to get. Must be an integer.
348+
*
349+
* @return {ImageData} The ImageData extracted from this CanvasTexture.
350+
*/
351+
getData: function (x, y, width, height)
352+
{
353+
x = Clamp(Math.floor(x), 0, this.width - 1);
354+
y = Clamp(Math.floor(y), 0, this.height - 1);
355+
width = Clamp(width, 1, this.width - x);
356+
height = Clamp(height, 1, this.height - y);
357+
358+
var imageData = this.context.getImageData(x, y, width, height);
359+
360+
return imageData;
361+
},
362+
315363
/**
316364
* Get the color of a specific pixel from this texture and store it in a Color object.
317365
*

0 commit comments

Comments
 (0)