Skip to content

Commit bffdb8f

Browse files
committed
Moved the Canvas tests into the Phaser.Device class.
1 parent ae9460c commit bffdb8f

6 files changed

Lines changed: 82 additions & 283 deletions

File tree

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -129,62 +129,3 @@ PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas)
129129
context.putImageData(pixelData, 0, 0);
130130
};
131131

132-
/**
133-
* Checks if the browser correctly supports putImageData alpha channels.
134-
*
135-
* @method checkInverseAlpha
136-
* @static
137-
*/
138-
PIXI.CanvasTinter.checkInverseAlpha = function()
139-
{
140-
var canvas = new PIXI.CanvasBuffer(2, 1);
141-
142-
canvas.context.fillStyle = "rgba(10, 20, 30, 0.5)";
143-
144-
// Draw a single pixel
145-
canvas.context.fillRect(0, 0, 1, 1);
146-
147-
// Get the color values
148-
var s1 = canvas.context.getImageData(0, 0, 1, 1);
149-
150-
if (s1 === null)
151-
{
152-
return false;
153-
}
154-
155-
// Plot them to x2
156-
canvas.context.putImageData(s1, 1, 0);
157-
158-
// Get those values
159-
var s2 = canvas.context.getImageData(1, 0, 1, 1);
160-
161-
// Compare and return
162-
return (s2.data[0] === s1.data[0] && s2.data[1] === s1.data[1] && s2.data[2] === s1.data[2] && s2.data[3] === s1.data[3]);
163-
};
164-
165-
/**
166-
* If the browser isn't capable of handling tinting with alpha this will be false.
167-
* This property is only applicable if using tintWithPerPixel.
168-
*
169-
* @property canHandleAlpha
170-
* @type Boolean
171-
* @static
172-
*/
173-
PIXI.CanvasTinter.canHandleAlpha = PIXI.CanvasTinter.checkInverseAlpha();
174-
175-
/**
176-
* Whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
177-
*
178-
* @property canUseMultiply
179-
* @type Boolean
180-
* @static
181-
*/
182-
PIXI.CanvasTinter.canUseMultiply = PIXI.canUseNewCanvasBlendModes();
183-
184-
/**
185-
* The tinting method that will be used.
186-
*
187-
* @method tintMethod
188-
* @static
189-
*/
190-
PIXI.CanvasTinter.tintMethod = PIXI.CanvasTinter.canUseMultiply ? PIXI.CanvasTinter.tintWithMultiply : PIXI.CanvasTinter.tintWithPerPixel;

src/pixi/utils/CanvasPool.js

Lines changed: 0 additions & 184 deletions
This file was deleted.

src/pixi/utils/Utils.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,6 @@ PIXI.rgb2hex = function(rgb) {
2222
return ((rgb[0]*255 << 16) + (rgb[1]*255 << 8) + rgb[2]*255);
2323
};
2424

25-
/**
26-
* Checks whether the Canvas BlendModes are supported by the current browser for drawImage
27-
*
28-
* @method canUseNewCanvasBlendModes
29-
* @return {Boolean} whether they are supported
30-
*/
31-
PIXI.canUseNewCanvasBlendModes = function()
32-
{
33-
if (document === undefined) return false;
34-
35-
var pngHead = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAABAQMAAADD8p2OAAAAA1BMVEX/';
36-
var pngEnd = 'AAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==';
37-
38-
var magenta = new Image();
39-
magenta.src = pngHead + 'AP804Oa6' + pngEnd;
40-
41-
var yellow = new Image();
42-
yellow.src = pngHead + '/wCKxvRF' + pngEnd;
43-
44-
var canvas = Phaser.CanvasPool.create(this, 6, 1);
45-
var context = canvas.getContext('2d');
46-
context.globalCompositeOperation = 'multiply';
47-
context.drawImage(magenta, 0, 0);
48-
context.drawImage(yellow, 2, 0);
49-
50-
if (!context.getImageData(2,0,1,1))
51-
{
52-
return false;
53-
}
54-
55-
var data = context.getImageData(2,0,1,1).data;
56-
57-
Phaser.CanvasPool.remove(this);
58-
59-
return (data[0] === 255 && data[1] === 0 && data[2] === 0);
60-
61-
};
6225

6326
/**
6427
* Given a number, this function returns the closest number that is a power of two

src/utils/Device.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ Phaser.Device = function () {
161161
*/
162162
this.canvasBitBltShift = null;
163163

164+
/**
165+
* If the browser isn't capable of handling tinting with alpha this will be false.
166+
* @property {boolean} canHandleAlpha
167+
* @default
168+
*/
169+
this.canHandleAlpha = false;
170+
171+
/**
172+
* Whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
173+
* @property {boolean} canUseMultiply
174+
* @default
175+
*/
176+
this.canUseMultiply = false;
177+
164178
/**
165179
* @property {boolean} webGL - Is webGL available?
166180
* @default
@@ -722,6 +736,71 @@ Phaser.Device._initialize = function () {
722736

723737
}
724738

739+
/**
740+
* Checks if the browser correctly supports putImageData alpha channels.
741+
* If the browser isn't capable of handling tinting with alpha, `Device.canHandleAlpha` will be false.
742+
* Also checks whether the Canvas BlendModes are supported by the current browser for drawImage.
743+
*/
744+
function _checkCanvasFeatures () {
745+
746+
var canvas = Phaser.CanvasPool.create(this, 6, 1);
747+
var context = canvas.getContext('2d');
748+
749+
context.fillStyle = 'rgba(10, 20, 30, 0.5)';
750+
751+
// Draw a single pixel
752+
context.fillRect(0, 0, 1, 1);
753+
754+
// Get the color values
755+
var s1 = context.getImageData(0, 0, 1, 1);
756+
757+
if (s1)
758+
{
759+
// Plot them to x2
760+
context.putImageData(s1, 1, 0);
761+
762+
// Get those values
763+
var s2 = context.getImageData(1, 0, 1, 1);
764+
765+
// Compare and set
766+
device.canHandleAlpha = (
767+
s2.data[0] === s1.data[0] &&
768+
s2.data[1] === s1.data[1] &&
769+
s2.data[2] === s1.data[2] &&
770+
s2.data[3] === s1.data[3]
771+
);
772+
}
773+
774+
// Checks whether the Canvas BlendModes are supported by the current browser for drawImage.
775+
776+
var pngHead = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAABAQMAAADD8p2OAAAAA1BMVEX/';
777+
var pngEnd = 'AAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==';
778+
779+
var magenta = new Image();
780+
magenta.src = pngHead + 'AP804Oa6' + pngEnd;
781+
782+
var yellow = new Image();
783+
yellow.src = pngHead + '/wCKxvRF' + pngEnd;
784+
785+
context.clearRect(0, 0, 6, 1);
786+
787+
context.globalCompositeOperation = 'multiply';
788+
context.drawImage(magenta, 0, 0);
789+
context.drawImage(yellow, 2, 0);
790+
791+
if (context.getImageData(2, 0, 1, 1))
792+
{
793+
var data = context.getImageData(2, 0, 1, 1).data;
794+
795+
device.canUseMultiply = (data[0] === 255 && data[1] === 0 && data[2] === 0);
796+
}
797+
798+
Phaser.CanvasPool.removeByCanvas(canvas);
799+
800+
PIXI.CanvasTinter.tintMethod = (device.canUseMultiply) ? PIXI.CanvasTinter.tintWithMultiply : PIXI.CanvasTinter.tintWithPerPixel;
801+
802+
}
803+
725804
/**
726805
* Check HTML5 features of the host environment.
727806
*/
@@ -1242,6 +1321,7 @@ Phaser.Device._initialize = function () {
12421321
_checkCSS3D();
12431322
_checkDevice();
12441323
_checkFeatures();
1324+
_checkCanvasFeatures();
12451325
_checkFullScreenSupport();
12461326
_checkInput();
12471327

0 commit comments

Comments
 (0)