Phaser.BitmapData = function (game, key, width, height){ if (typeof width === 'undefined') { width = 100; } if (typeof height === 'undefined') { height = 100; } this.game = game; this.key = key; this.width = width; this.height = height; this.canvas = Phaser.Canvas.create(width, height, '', true ); this.context = this.canvas.getContext('2d'); this.ctx = this.context; this.imageData = this.context.getImageData(0, 0, width, height); if (this.imageData.data.buffer) { this.pixels = this.imageData.data.buffer; } else { this.pixels = this.imageData.data; } this.baseTexture = new PIXI.BaseTexture(this.canvas); this.texture = new PIXI.Texture(this.baseTexture); this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'bitmapData', game.rnd.uuid()); this.type = Phaser.BITMAPDATA; this._dirty = false ; } ; Phaser.BitmapData.prototype = { add: function (object){ if (Array.isArray(object)) { for (var i = 0; i < _AN_Read_length('length', object); i++ ){ if (object[i].loadTexture) { object[i].loadTexture(this); } } } else { object.loadTexture(this); } } , clear: function (){ this.context.clearRect(0, 0, this.width, this.height); this._dirty = true ; } , resize: function (width, height){ if (width !== this.width || height !== this.height) { console.log('bmd resize', width, height); this.width = width; this.height = height; this.canvas.width = width; this.canvas.height = height; this.textureFrame.width = width; this.textureFrame.height = height; this.imageData = this.context.getImageData(0, 0, width, height); } this._dirty = true ; } , refreshBuffer: function (){ this.imageData = this.context.getImageData(0, 0, this.width, this.height); this.pixels = new Int32Array(this.imageData.data.buffer); } , setPixel32: function (x, y, red, green, blue, alpha){ if (x >= 0 && x <= this.width && y >= 0 && y <= this.height) { this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red; this.context.putImageData(this.imageData, 0, 0); this._dirty = true ; } } , setPixel: function (x, y, red, green, blue){ this.setPixel32(x, y, red, green, blue, 255); } , getPixel: function (x, y){ if (x >= 0 && x <= this.width && y >= 0 && y <= this.height) { return this.data32[y * this.width + x]; } } , getPixel32: function (x, y){ if (x >= 0 && x <= this.width && y >= 0 && y <= this.height) { return this.data32[y * this.width + x]; } } , getPixels: function (rect){ return this.context.getImageData(rect.x, rect.y, rect.width, rect.height); } , copyPixels: function (source, area, destX, destY){ if (typeof source === 'string') { source = this.game.cache.getImage(source); } if (source) { this.context.drawImage(source, area.x, area.y, area.width, area.height, destX, destY, area.width, area.height); } } , draw: function (source, destX, destY){ if (typeof source === 'string') { source = this.game.cache.getImage(source); } if (source) { this.context.drawImage(source, 0, 0, source.width, source.height, destX, destY, source.width, source.height); } } , alphaMask: function (source, mask){ var temp = this.context.globalCompositeOperation; if (typeof mask === 'string') { mask = this.game.cache.getImage(mask); } if (mask) { this.context.drawImage(mask, 0, 0); } this.context.globalCompositeOperation = 'source-atop'; if (typeof source === 'string') { source = this.game.cache.getImage(source); } if (source) { this.context.drawImage(source, 0, 0); } this.context.globalCompositeOperation = temp; } , render: function (){ if (this._dirty) { if (this.game.renderType === Phaser.WEBGL) { PIXI.texturesToUpdate.push(this.baseTexture); } this._dirty = false ; } } } ; Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;