Skip to content

Commit 36c0645

Browse files
committed
Added guards around context.getImageData calls in BitmapData, Text and Canvas Tinting classes to avoid crashing restricted browsers like Epic Browser. Please understand that several Phaser features won't work correctly with this browser (thanks @Erik3000 phaserjs#1714)
1 parent ef5c0d8 commit 36c0645

5 files changed

Lines changed: 60 additions & 20 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ or the minified version:
9797

9898
`<script src="//cdn.jsdelivr.net/phaser/2.3.0/phaser.min.js"></script>`
9999

100+
[cdnjs.com](https://cdnjs.com/libraries/phaser) also offers a free CDN service. They have all versions of Phaser and even the custom builds:
101+
102+
`<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.3.0/phaser.js"></script>`
103+
100104
### Phaser Sandbox
101105

102106
If you'd like to try coding in Phaser right now, with nothing more than your web browser then you can head over to the [Phaser Sandbox](http://phaser.io/sandbox). You'll find Quick Start templates and a user-friendly editor filled with handy code-completion features.
@@ -244,13 +248,16 @@ Version 2.3.1 - "Katar" - in dev
244248

245249
### Updates
246250

251+
* TypeScript definitions fixes and updates (thanks @clark-stevenson @isuda)
247252
* Added missing `resumed` method to Phaser.State class template.
248253

249254
### Bug Fixes
250255

251256
* The LoadTexture component has had a redundant `dirty` call removed from it.
252257
* TileSprites were missing a `physicsType` property, causing them to not collide with anything (thanks @numbofathma #1702)
253258
* Sprite was missing the Health and InCamera components.
259+
* A Tween could be incorrectly set to never end if it was given a duration of zero (thanks @hardalias #1710)
260+
* Added guards around `context.getImageData` calls in BitmapData, Text and Canvas Tinting classes to avoid crashing restricted browsers like Epic Browser. Please understand that several Phaser features won't work correctly with this browser (thanks @Erik3000 #1714)
254261

255262
For changes in previous releases please see the extensive [Version History](https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md).
256263

src/gameobjects/BitmapData.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ Phaser.BitmapData = function (game, key, width, height) {
6464
this.imageData = this.context.getImageData(0, 0, width, height);
6565

6666
/**
67-
* @property {Uint8ClampedArray} data - A Uint8ClampedArray view into BitmapData.buffer.
67+
* A Uint8ClampedArray view into BitmapData.buffer.
68+
* Note that this is unavailable in some browsers (such as Epic Browser due to its security restrictions)
69+
* @property {Uint8ClampedArray} data
6870
*/
69-
this.data = this.imageData.data;
71+
this.data = null;
72+
73+
if (this.imageData)
74+
{
75+
this.data = this.imageData.data;
76+
}
7077

7178
/**
7279
* @property {Uint32Array} pixels - An Uint32Array view into BitmapData.buffer.
@@ -76,21 +83,24 @@ Phaser.BitmapData = function (game, key, width, height) {
7683
/**
7784
* @property {ArrayBuffer} buffer - An ArrayBuffer the same size as the context ImageData.
7885
*/
79-
if (this.imageData.data.buffer)
86+
if (this.data)
8087
{
81-
this.buffer = this.imageData.data.buffer;
82-
this.pixels = new Uint32Array(this.buffer);
83-
}
84-
else
85-
{
86-
if (window['ArrayBuffer'])
88+
if (this.imageData.data.buffer)
8789
{
88-
this.buffer = new ArrayBuffer(this.imageData.data.length);
90+
this.buffer = this.imageData.data.buffer;
8991
this.pixels = new Uint32Array(this.buffer);
9092
}
9193
else
9294
{
93-
this.pixels = this.imageData.data;
95+
if (window['ArrayBuffer'])
96+
{
97+
this.buffer = new ArrayBuffer(this.imageData.data.length);
98+
this.pixels = new Uint32Array(this.buffer);
99+
}
100+
else
101+
{
102+
this.pixels = this.imageData.data;
103+
}
94104
}
95105
}
96106

src/pixi/renderers/canvas/utils/CanvasTinter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ PIXI.CanvasTinter.checkInverseAlpha = function()
237237
// Get the color values
238238
var s1 = canvas.context.getImageData(0, 0, 1, 1);
239239

240+
if (s1 === null)
241+
{
242+
return false;
243+
}
244+
240245
// Plot them to x2
241246
canvas.context.putImageData(s1, 1, 0);
242247

src/pixi/text/Text.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ PIXI.Text.prototype.determineFontProperties = function(fontStyle)
353353
{
354354
var properties = PIXI.Text.fontPropertiesCache[fontStyle];
355355

356-
if(!properties)
356+
if (!properties)
357357
{
358358
properties = {};
359359

@@ -380,6 +380,17 @@ PIXI.Text.prototype.determineFontProperties = function(fontStyle)
380380
context.fillStyle = '#000';
381381
context.fillText('|MÉq', 0, baseline);
382382

383+
if (!context.getImageData(0, 0, width, height))
384+
{
385+
properties.ascent = baseline;
386+
properties.descent = baseline + 6;
387+
properties.fontSize = properties.ascent + properties.descent;
388+
389+
PIXI.Text.fontPropertiesCache[fontStyle] = properties;
390+
391+
return properties;
392+
}
393+
383394
var imagedata = context.getImageData(0, 0, width, height).data;
384395
var pixels = imagedata.length;
385396
var line = width * 4;
@@ -390,17 +401,18 @@ PIXI.Text.prototype.determineFontProperties = function(fontStyle)
390401
var stop = false;
391402

392403
// ascent. scan from top to bottom until we find a non red pixel
393-
for(i = 0; i < baseline; i++)
404+
for (i = 0; i < baseline; i++)
394405
{
395-
for(j = 0; j < line; j += 4)
406+
for (j = 0; j < line; j += 4)
396407
{
397-
if(imagedata[idx + j] !== 255)
408+
if (imagedata[idx + j] !== 255)
398409
{
399410
stop = true;
400411
break;
401412
}
402413
}
403-
if(!stop)
414+
415+
if (!stop)
404416
{
405417
idx += line;
406418
}
@@ -416,17 +428,18 @@ PIXI.Text.prototype.determineFontProperties = function(fontStyle)
416428
stop = false;
417429

418430
// descent. scan from bottom to top until we find a non red pixel
419-
for(i = height; i > baseline; i--)
431+
for (i = height; i > baseline; i--)
420432
{
421-
for(j = 0; j < line; j += 4)
433+
for (j = 0; j < line; j += 4)
422434
{
423-
if(imagedata[idx + j] !== 255)
435+
if (imagedata[idx + j] !== 255)
424436
{
425437
stop = true;
426438
break;
427439
}
428440
}
429-
if(!stop)
441+
442+
if (!stop)
430443
{
431444
idx -= line;
432445
}

src/pixi/utils/Utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ PIXI.canUseNewCanvasBlendModes = function()
4949
context.drawImage(magenta, 0, 0);
5050
context.drawImage(yellow, 2, 0);
5151

52+
if (!context.getImageData(2,0,1,1))
53+
{
54+
return false;
55+
}
56+
5257
var data = context.getImageData(2,0,1,1).data;
5358

5459
return (data[0] === 255 && data[1] === 0 && data[2] === 0);

0 commit comments

Comments
 (0)