Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save foo9/8727843 to your computer and use it in GitHub Desktop.

Select an option

Save foo9/8727843 to your computer and use it in GitHub Desktop.
var BitmapData = (function() {
function BitmapData(width, height) {
this.width = width;
this.height = height;
this.canvas = document.createElement('canvas');
this.canvas.setAttribute('width', this.width + 'px');
this.canvas.setAttribute('height', this.height + 'px');
this.context = this.canvas.getContext('2d');
this.imageData = this.context.createImageData(this.canvas.width, this.canvas.height);
this.byteArray = this.imageData.data;
}
BitmapData.prototype.setPixel32 = function(x, y, r, g, b, a) {
var idx;
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
idx = ((x | 0) + (y | 0) * this.width) * 4;
this.byteArray[idx + 0] = r;
this.byteArray[idx + 1] = g;
this.byteArray[idx + 2] = b;
this.byteArray[idx + 3] = a;
}
};
BitmapData.prototype.toImage = function() {
var img;
this.context.putImageData(this.imageData, 0, 0);
img = document.createElement('img');
img.setAttribute('width', this.width + 'px');
img.setAttribute('height', this.height + 'px');
img.setAttribute('src', this.canvas.toDataURL());
return img;
};
return BitmapData;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment