Skip to content

Instantly share code, notes, and snippets.

@Farious
Created January 12, 2017 23:53
Show Gist options
  • Save Farious/dbdf4dd326448f5350b9de362360a1af to your computer and use it in GitHub Desktop.
Save Farious/dbdf4dd326448f5350b9de362360a1af to your computer and use it in GitHub Desktop.
var context = canvas.getContext("2d");
context.drawColoredImage = function(img, sx, sy, sw, sh, dx, dy, dw, dh, r, g, b) {
var buffer: Dynamic = Browser.document.createElement('canvas');
buffer.width = dw;
buffer.height = dh;
var bx = buffer.getContext('2d');
// fill offscreen buffer with the tint color
bx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
bx.fillRect(0, 0, dw, dh);
// destination atop makes a result with an alpha channel identical to fg, but with all pixels retaining their original color *as far as I can tell*
bx.globalCompositeOperation = "destination-atop";
bx.drawImage(img, sx, sy, sw, sh, 0., 0., dw, dh);
//then set the global alpha to the amound that you want to tint it, and draw the buffer directly on top of it.
var oAlpha = context.globalAlpha;
context.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh);
context.globalAlpha = 0.5;
context.drawImage(buffer, 0., 0., dw, dh, dx, dy, dw, dh);
context.globalAlpha = oAlpha;
};
@Farious
Copy link
Author

Farious commented Jan 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment