Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/2064181 to your computer and use it in GitHub Desktop.
Save jonathantneal/2064181 to your computer and use it in GitHub Desktop.
Canvas Prototype: getImageData / putImageData / editImageData
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