diff --git a/bin/pixi.dev.js b/bin/pixi.dev.js index 8632b0e..12a3c53 100644 --- a/bin/pixi.dev.js +++ b/bin/pixi.dev.js @@ -6183,8 +6183,7 @@ } //PIXI.CanvasTinter.cachTint = true; -PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; -PIXI.CanvasTinter.convertTintToImage = false; + PIXI.CanvasTinter.getTintedTexture = function(sprite, color, canvas) { @@ -6207,6 +6206,34 @@ // clone texture.. var canvas = PIXI.CanvasTinter.canvas || document.createElement("canvas"); + + //PIXI.CanvasTinter.tintWithPerPixel(texture, stringColor, canvas); + + + PIXI.CanvasTinter.tintMethod(texture, color, canvas); + + if(PIXI.CanvasTinter.convertTintToImage) + { + // is this better? + var tintImage = new Image(); + tintImage.src = canvas.toDataURL(); + + texture.tintCache[stringColor] = tintImage; + } + else + { + + texture.tintCache[stringColor] = canvas; + // if we are not converting the texture to an image then we need to lose the refferance to the canvas + PIXI.CanvasTinter.canvas = null; + + } + + return canvas; +} + +PIXI.CanvasTinter.tintWithMultiply = function(texture, color, canvas) +{ var context = canvas.getContext( '2d' ); var frame = texture.frame; @@ -6214,7 +6241,7 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); @@ -6240,29 +6267,10 @@ 0, 0, frame.width, - frame.height); - - if(PIXI.CanvasTinter.convertTintToImage) - { - // is this better? - var tintImage = new Image(); - tintImage.src = canvas.toDataURL(); - - texture.tintCache[stringColor] = tintImage; - } - else - { - - texture.tintCache[stringColor] = canvas; - // if we are not converting the texture to an image then we need to lose the refferance to the canvas - PIXI.CanvasTinter.canvas = null; - - } - - return canvas; + frame.height); } -PIXI.CanvasTinter._getTintedTextureFast = function(texture, color, canvas) +PIXI.CanvasTinter.tintWithOverlay = function(texture, color, canvas) { var context = canvas.getContext( '2d' ); @@ -6271,39 +6279,64 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; - + + + context.globalCompositeOperation = 'copy'; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); - context.globalCompositeOperation = 'multiply'; - + context.globalCompositeOperation = 'destination-atop'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); - context.globalCompositeOperation = 'destination-in'; + + //context.globalCompositeOperation = 'copy'; +} + + +PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas) +{ + var context = canvas.getContext( '2d' ); + + var frame = texture.frame; + + canvas.width = frame.width; + canvas.height = frame.height; + + context.globalCompositeOperation = 'copy'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); + var rgbValues = PIXI.hex2rgb(color); + var r = rgbValues[0], g = rgbValues[1], b = rgbValues[2]; + var pixelData = context.getImageData(0, 0, frame.width, frame.height) - texture.tintCache[stringColor] = canvas; + var pixels = pixelData.data; - return canvas; + for (var i = 0; i < pixels.length; i += 4) + { + pixels[i+0] *= r; + pixels[i+1] *= g; + pixels[i+2] *= b; + } + + context.putImageData(pixelData, 0, 0); } PIXI.CanvasTinter.roundColor = function(color) @@ -6362,6 +6395,16 @@ return canvas; } + +PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; +PIXI.CanvasTinter.convertTintToImage = false; + +PIXI.CanvasTinter.canUseMultiply = PIXI.canUseNewCanvasBlendModes(); + +PIXI.CanvasTinter.tintMethod = PIXI.CanvasTinter.canUseMultiply ? PIXI.CanvasTinter.tintWithMultiply : PIXI.CanvasTinter.tintWithPerPixel; + + + /** * @author Mat Groves http://matgroves.com/ @Doormat23 */ diff --git a/bin/pixi.dev.js b/bin/pixi.dev.js index 8632b0e..12a3c53 100644 --- a/bin/pixi.dev.js +++ b/bin/pixi.dev.js @@ -6183,8 +6183,7 @@ } //PIXI.CanvasTinter.cachTint = true; -PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; -PIXI.CanvasTinter.convertTintToImage = false; + PIXI.CanvasTinter.getTintedTexture = function(sprite, color, canvas) { @@ -6207,6 +6206,34 @@ // clone texture.. var canvas = PIXI.CanvasTinter.canvas || document.createElement("canvas"); + + //PIXI.CanvasTinter.tintWithPerPixel(texture, stringColor, canvas); + + + PIXI.CanvasTinter.tintMethod(texture, color, canvas); + + if(PIXI.CanvasTinter.convertTintToImage) + { + // is this better? + var tintImage = new Image(); + tintImage.src = canvas.toDataURL(); + + texture.tintCache[stringColor] = tintImage; + } + else + { + + texture.tintCache[stringColor] = canvas; + // if we are not converting the texture to an image then we need to lose the refferance to the canvas + PIXI.CanvasTinter.canvas = null; + + } + + return canvas; +} + +PIXI.CanvasTinter.tintWithMultiply = function(texture, color, canvas) +{ var context = canvas.getContext( '2d' ); var frame = texture.frame; @@ -6214,7 +6241,7 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); @@ -6240,29 +6267,10 @@ 0, 0, frame.width, - frame.height); - - if(PIXI.CanvasTinter.convertTintToImage) - { - // is this better? - var tintImage = new Image(); - tintImage.src = canvas.toDataURL(); - - texture.tintCache[stringColor] = tintImage; - } - else - { - - texture.tintCache[stringColor] = canvas; - // if we are not converting the texture to an image then we need to lose the refferance to the canvas - PIXI.CanvasTinter.canvas = null; - - } - - return canvas; + frame.height); } -PIXI.CanvasTinter._getTintedTextureFast = function(texture, color, canvas) +PIXI.CanvasTinter.tintWithOverlay = function(texture, color, canvas) { var context = canvas.getContext( '2d' ); @@ -6271,39 +6279,64 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; - + + + context.globalCompositeOperation = 'copy'; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); - context.globalCompositeOperation = 'multiply'; - + context.globalCompositeOperation = 'destination-atop'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); - context.globalCompositeOperation = 'destination-in'; + + //context.globalCompositeOperation = 'copy'; +} + + +PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas) +{ + var context = canvas.getContext( '2d' ); + + var frame = texture.frame; + + canvas.width = frame.width; + canvas.height = frame.height; + + context.globalCompositeOperation = 'copy'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); + var rgbValues = PIXI.hex2rgb(color); + var r = rgbValues[0], g = rgbValues[1], b = rgbValues[2]; + var pixelData = context.getImageData(0, 0, frame.width, frame.height) - texture.tintCache[stringColor] = canvas; + var pixels = pixelData.data; - return canvas; + for (var i = 0; i < pixels.length; i += 4) + { + pixels[i+0] *= r; + pixels[i+1] *= g; + pixels[i+2] *= b; + } + + context.putImageData(pixelData, 0, 0); } PIXI.CanvasTinter.roundColor = function(color) @@ -6362,6 +6395,16 @@ return canvas; } + +PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; +PIXI.CanvasTinter.convertTintToImage = false; + +PIXI.CanvasTinter.canUseMultiply = PIXI.canUseNewCanvasBlendModes(); + +PIXI.CanvasTinter.tintMethod = PIXI.CanvasTinter.canUseMultiply ? PIXI.CanvasTinter.tintWithMultiply : PIXI.CanvasTinter.tintWithPerPixel; + + + /** * @author Mat Groves http://matgroves.com/ @Doormat23 */ diff --git a/src/pixi/renderers/canvas/utils/CanvasTinter.js b/src/pixi/renderers/canvas/utils/CanvasTinter.js index d0318fa..83bb6c1 100644 --- a/src/pixi/renderers/canvas/utils/CanvasTinter.js +++ b/src/pixi/renderers/canvas/utils/CanvasTinter.js @@ -10,8 +10,7 @@ } //PIXI.CanvasTinter.cachTint = true; -PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; -PIXI.CanvasTinter.convertTintToImage = false; + PIXI.CanvasTinter.getTintedTexture = function(sprite, color, canvas) { @@ -34,6 +33,34 @@ // clone texture.. var canvas = PIXI.CanvasTinter.canvas || document.createElement("canvas"); + + //PIXI.CanvasTinter.tintWithPerPixel(texture, stringColor, canvas); + + + PIXI.CanvasTinter.tintMethod(texture, color, canvas); + + if(PIXI.CanvasTinter.convertTintToImage) + { + // is this better? + var tintImage = new Image(); + tintImage.src = canvas.toDataURL(); + + texture.tintCache[stringColor] = tintImage; + } + else + { + + texture.tintCache[stringColor] = canvas; + // if we are not converting the texture to an image then we need to lose the refferance to the canvas + PIXI.CanvasTinter.canvas = null; + + } + + return canvas; +} + +PIXI.CanvasTinter.tintWithMultiply = function(texture, color, canvas) +{ var context = canvas.getContext( '2d' ); var frame = texture.frame; @@ -41,7 +68,7 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); @@ -67,29 +94,10 @@ 0, 0, frame.width, - frame.height); - - if(PIXI.CanvasTinter.convertTintToImage) - { - // is this better? - var tintImage = new Image(); - tintImage.src = canvas.toDataURL(); - - texture.tintCache[stringColor] = tintImage; - } - else - { - - texture.tintCache[stringColor] = canvas; - // if we are not converting the texture to an image then we need to lose the refferance to the canvas - PIXI.CanvasTinter.canvas = null; - - } - - return canvas; + frame.height); } -PIXI.CanvasTinter._getTintedTextureFast = function(texture, color, canvas) +PIXI.CanvasTinter.tintWithOverlay = function(texture, color, canvas) { var context = canvas.getContext( '2d' ); @@ -98,39 +106,64 @@ canvas.width = frame.width; canvas.height = frame.height; - context.fillStyle = stringColor; - + + + context.globalCompositeOperation = 'copy'; + context.fillStyle = '#' + ('00000' + ( color | 0).toString(16)).substr(-6); context.fillRect(0, 0, frame.width, frame.height); - context.globalCompositeOperation = 'multiply'; - + context.globalCompositeOperation = 'destination-atop'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); - context.globalCompositeOperation = 'destination-in'; + + //context.globalCompositeOperation = 'copy'; +} + + +PIXI.CanvasTinter.tintWithPerPixel = function(texture, color, canvas) +{ + var context = canvas.getContext( '2d' ); + + var frame = texture.frame; + + canvas.width = frame.width; + canvas.height = frame.height; + + context.globalCompositeOperation = 'copy'; context.drawImage(texture.baseTexture.source, - frame.x, - frame.y, - frame.width, - frame.height, - 0, - 0, - frame.width, - frame.height); + frame.x, + frame.y, + frame.width, + frame.height, + 0, + 0, + frame.width, + frame.height); + var rgbValues = PIXI.hex2rgb(color); + var r = rgbValues[0], g = rgbValues[1], b = rgbValues[2]; + var pixelData = context.getImageData(0, 0, frame.width, frame.height) - texture.tintCache[stringColor] = canvas; + var pixels = pixelData.data; - return canvas; + for (var i = 0; i < pixels.length; i += 4) + { + pixels[i+0] *= r; + pixels[i+1] *= g; + pixels[i+2] *= b; + } + + context.putImageData(pixelData, 0, 0); } PIXI.CanvasTinter.roundColor = function(color) @@ -188,4 +221,13 @@ frame.height); return canvas; -} \ No newline at end of file +} + +PIXI.CanvasTinter.cacheStepsPerColorChannel = 8; +PIXI.CanvasTinter.convertTintToImage = false; + +PIXI.CanvasTinter.canUseMultiply = PIXI.canUseNewCanvasBlendModes(); + +PIXI.CanvasTinter.tintMethod = PIXI.CanvasTinter.canUseMultiply ? PIXI.CanvasTinter.tintWithMultiply : PIXI.CanvasTinter.tintWithPerPixel; + +