Created
March 17, 2012 19:00
-
-
Save jonathantneal/2064181 to your computer and use it in GitHub Desktop.
Canvas Prototype: getImageData / putImageData / editImageData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTMLCanvasElement.prototype.getImageData = function (x, y, width, height) { | |
return this.getContext('2d').getImageData(parseFloat(x) || 0, parseFloat(y) || 0, width != null ? parseFloat(width) : this.width, height != null ? parseFloat(height) : this.height); | |
}; | |
HTMLCanvasElement.prototype.putImageData = function (imageData, x, y, width, height) { | |
var canvas = this; | |
canvas.width = width != null ? parseFloat(width) : canvas.width; | |
canvas.height = height != null ? parseFloat(height) : canvas.height; | |
canvas.getContext('2d').putImageData(imageData, parseFloat(x) || 0, parseFloat(y) || 0); | |
return canvas; | |
}; | |
HTMLCanvasElement.prototype.editImageData = function (callback, x, y, width, height) { | |
var canvas = this; | |
if (typeof callback == 'function') { | |
var imageData = canvas.getImageData(x, y, width, height); | |
callback.call(canvas, imageData); | |
canvas.putImageData(imageData, x, y, width, height); | |
} | |
return canvas; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment