Skip to content

Commit 22e8340

Browse files
committed
Fixes issue where Pixi CanvasTint didn't allow for browsers that bit-shift the alpha value on putImageData (Android Stock Browser, I'm looking at you!)
1 parent 797c23f commit 22e8340

1 file changed

Lines changed: 52 additions & 4 deletions

File tree

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ PIXI.CanvasTinter.tintWithOverlay = function(texture, color, canvas)
151151
*/
152152
PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas)
153153
{
154-
var context = canvas.getContext( "2d" );
154+
var context = canvas.getContext("2d");
155155

156156
var crop = texture.crop;
157157

@@ -178,9 +178,18 @@ PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas)
178178

179179
for (var i = 0; i < pixels.length; i += 4)
180180
{
181-
pixels[i+0] *= r;
182-
pixels[i+1] *= g;
183-
pixels[i+2] *= b;
181+
pixels[i+0] *= r;
182+
pixels[i+1] *= g;
183+
pixels[i+2] *= b;
184+
185+
if (!PIXI.CanvasTinter.canHandleAlpha)
186+
{
187+
var alpha = pixels[i+3];
188+
189+
pixels[i+0] /= 255 / alpha;
190+
pixels[i+1] /= 255 / alpha;
191+
pixels[i+2] /= 255 / alpha;
192+
}
184193
}
185194

186195
context.putImageData(pixelData, 0, 0);
@@ -206,6 +215,35 @@ PIXI.CanvasTinter.roundColor = function(color)
206215
return PIXI.rgb2hex(rgbValues);
207216
};
208217

218+
/**
219+
* Rounds the specified color according to the PIXI.CanvasTinter.cacheStepsPerColorChannel.
220+
*
221+
* @method roundColor
222+
* @static
223+
* @param color {number} the color to round, should be a hex color
224+
*/
225+
PIXI.CanvasTinter.checkInverseAlpha = function()
226+
{
227+
var canvas = new PIXI.CanvasBuffer(2, 1);
228+
229+
canvas.context.fillStyle = "rgba(10, 20, 30, 0.5)";
230+
231+
// Draw a single pixel
232+
canvas.context.fillRect(0, 0, 1, 1);
233+
234+
// Get the color values
235+
var s1 = canvas.context.getImageData(0, 0, 1, 1);
236+
237+
// Plot them to x2
238+
canvas.context.putImageData(s1, 1, 0);
239+
240+
// Get those values
241+
var s2 = canvas.context.getImageData(1, 0, 1, 1);
242+
243+
// Compare and return
244+
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]);
245+
};
246+
209247
/**
210248
* Number of steps which will be used as a cap when rounding colors.
211249
*
@@ -224,6 +262,16 @@ PIXI.CanvasTinter.cacheStepsPerColorChannel = 8;
224262
*/
225263
PIXI.CanvasTinter.convertTintToImage = false;
226264

265+
/**
266+
* If the browser isn't capable of handling tinting with alpha this will be false.
267+
* This property is only applicable if using tintWithPerPixel.
268+
*
269+
* @property canHandleAlpha
270+
* @type Boolean
271+
* @static
272+
*/
273+
PIXI.CanvasTinter.canHandleAlpha = PIXI.CanvasTinter.checkInverseAlpha();
274+
227275
/**
228276
* Whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
229277
*

0 commit comments

Comments
 (0)