Skip to content

Commit c239700

Browse files
committed
Added getIndex and getPixels methods.
1 parent 27c1426 commit c239700

1 file changed

Lines changed: 99 additions & 10 deletions

File tree

src/textures/CanvasTexture.js

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Class = require('../utils/Class');
8+
var Clamp = require('../math/Clamp');
89
var Color = require('../display/color/Color');
910
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
1011
var Texture = require('./Texture');
@@ -279,9 +280,9 @@ var CanvasTexture = new Class({
279280
* @method Phaser.Textures.CanvasTexture#getPixel
280281
* @since 3.13.0
281282
*
282-
* @param {integer} x - The x coordinate of the pixel to be set. Must lay within the dimensions of this CanvasTexture and be an integer.
283-
* @param {integer} y - The y coordinate of the pixel to be set. Must lay within the dimensions of this CanvasTexture and be an integer.
284-
* @param {Phaser.Display.Color} [out] - An object into which 4 properties will be set: r, g, b and a. If not provided a Color object will be created.
283+
* @param {integer} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
284+
* @param {integer} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
285+
* @param {Phaser.Display.Color} [out] - A Color object to store the pixel values in. If not provided a new Color object will be created.
285286
*
286287
* @return {Phaser.Display.Color} An object with the red, green, blue and alpha values set in the r, g, b and a properties.
287288
*/
@@ -292,16 +293,104 @@ var CanvasTexture = new Class({
292293
out = new Color();
293294
}
294295

295-
var index = ~~(x + (y * this.width));
296+
var index = this.getIndex(x, y);
296297

297-
index *= 4;
298+
if (index > -1)
299+
{
300+
var data = this.data;
301+
302+
var r = data[index + 0];
303+
var g = data[index + 1];
304+
var b = data[index + 2];
305+
var a = data[index + 3];
306+
307+
out.setTo(r, g, b, a);
308+
}
309+
310+
return out;
311+
},
312+
313+
/**
314+
* Returns an array containing all of the pixels in the given region.
315+
*
316+
* If the requested region extends outside the bounds of this CanvasTexture,
317+
* the region is truncated to fit.
318+
*
319+
* If you have drawn anything to this CanvasTexture since it was created you must call `CanvasTexture.update` to refresh the array buffer,
320+
* 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.
321+
*
322+
* @method Phaser.Textures.CanvasTexture#getPixels
323+
* @since 3.16.0
324+
*
325+
* @param {integer} x - The x coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
326+
* @param {integer} y - The y coordinate of the top-left of the region. Must lay within the dimensions of this CanvasTexture and be an integer.
327+
* @param {integer} width - The width of the region to get. Must be an integer.
328+
* @param {integer} [height] - The height of the region to get. Must be an integer. If not given will be set to the `width`.
329+
*
330+
* @return {any[]} An array of Pixel objects.
331+
*/
332+
getPixels: function (x, y, width, height)
333+
{
334+
if (height === undefined) { height = width; }
335+
336+
x = Math.abs(Math.round(x));
337+
y = Math.abs(Math.round(y));
338+
339+
var left = Clamp(x, 0, this.width);
340+
var right = Clamp(x + width, 0, this.width);
341+
var top = Clamp(y, 0, this.height);
342+
var bottom = Clamp(y + height, 0, this.height);
298343

299-
var r = this.data[index];
300-
var g = this.data[++index];
301-
var b = this.data[++index];
302-
var a = this.data[++index];
344+
var pixel = new Color();
345+
346+
var out = [];
347+
348+
for (var py = top; py < bottom; py++)
349+
{
350+
var row = [];
303351

304-
return out.setTo(r, g, b, a);
352+
for (var px = left; px < right; px++)
353+
{
354+
var pixel = this.getPixel(px, py, pixel);
355+
356+
row.push({ x: px, y: py, color: pixel.color, alpha: pixel.alpha });
357+
}
358+
359+
out.push(row);
360+
}
361+
362+
return out;
363+
},
364+
365+
/**
366+
* Returns the Image Data index for the given pixel in this CanvasTexture.
367+
*
368+
* The index can be used to read directly from the `this.data` array.
369+
*
370+
* The index points to the red value in the array. The subsequent 3 indexes
371+
* point to green, blue and alpha respectively.
372+
*
373+
* @method Phaser.Textures.CanvasTexture#getIndex
374+
* @since 3.16.0
375+
*
376+
* @param {integer} x - The x coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
377+
* @param {integer} y - The y coordinate of the pixel to get. Must lay within the dimensions of this CanvasTexture and be an integer.
378+
*
379+
* @return {integer}
380+
*/
381+
getIndex: function (x, y)
382+
{
383+
x = Math.abs(Math.round(x));
384+
y = Math.abs(Math.round(y));
385+
386+
if (x < this.width && y < this.height)
387+
{
388+
return (x + y * this.width) * 4;
389+
}
390+
else
391+
{
392+
return -1;
393+
}
305394
},
306395

307396
/**

0 commit comments

Comments
 (0)