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.buffer = this.imageData.data.buffer; } else { this.buffer = new ArrayBuffer((_AN_Read_length('length', this.imageData.data))); } this.data = this.imageData.data; this.pixels = new Uint32Array(this.buffer); 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.disableTextureUpload = false ; this.dirty = false ; this.cls = this.clear; this.update = this.refreshBuffer; this._tempR = 0; this._tempG = 0; this._tempB = 0; } ; 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 ; } , fill: function (r, g, b, a){ if (typeof a === 'undefined') { a = 1; } this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')'; this.context.fillRect(0, 0, this.width, this.height); this.dirty = true ; } , resize: function (width, height){ if (width !== this.width || height !== this.height) { this.width = width; this.height = height; this.canvas.width = width; this.canvas.height = height; this.textureFrame.width = width; this.textureFrame.height = height; this.refreshBuffer(); } this.dirty = true ; } , refreshBuffer: function (x, y, width, height){ if (typeof x === 'undefined') { x = 0; } if (typeof y === 'undefined') { y = 0; } if (typeof width === 'undefined') { width = this.width; } if (typeof height === 'undefined') { height = this.height; } this.imageData = this.context.getImageData(x, y, width, height); if (this.imageData.data.buffer) { this.buffer = this.imageData.data.buffer; } else { this.buffer = new ArrayBuffer((_AN_Read_length('length', this.imageData.data))); } this.data = this.imageData.data; this.pixels = new Uint32Array(this.buffer); } , processPixelRGB: function (callback, callbackContext, x, y, width, height){ if (typeof x === 'undefined') { x = 0; } if (typeof y === 'undefined') { y = 0; } if (typeof width === 'undefined') { width = this.width; } if (typeof height === 'undefined') { height = this.height; } var w = x + width; var h = y + height; var pixel = Phaser.Color.createColor(); var result = { r: 0, g: 0, b: 0, a: 0} ; var dirty = false ; for (var ty = y; ty < h; ty++ ){ for (var tx = x; tx < w; tx++ ){ Phaser.Color.unpackPixel(this.getPixel32(tx, ty), pixel); result = callback.call(callbackContext, pixel); if (result !== false && result !== null ) { this.setPixel32(tx, ty, result.r, result.g, result.b, result.a, false ); dirty = true ; } } } if (dirty) { this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } } , processPixel: function (callback, callbackContext, x, y, width, height){ if (typeof x === 'undefined') { x = 0; } if (typeof y === 'undefined') { y = 0; } if (typeof width === 'undefined') { width = this.width; } if (typeof height === 'undefined') { height = this.height; } var w = x + width; var h = y + height; var pixel = 0; var result = 0; var dirty = false ; for (var ty = y; ty < h; ty++ ){ for (var tx = x; tx < w; tx++ ){ pixel = this.getPixel32(tx, ty); result = callback.call(callbackContext, pixel); if (result !== pixel) { this.pixels[ty * this.width + tx] = result; dirty = true ; } } } if (dirty) { this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } } , replaceRGB: function (r1, g1, b1, a1, r2, g2, b2, a2, region){ var sx = 0; var sy = 0; var w = this.width; var h = this.height; var source = Phaser.Color.packPixel(r1, g1, b1, a1); if (region !== undefined && region instanceof Phaser.Rectangle) { sx = region.x; sy = region.y; w = region.width; h = region.height; } for (var y = 0; y < h; y++ ){ for (var x = 0; x < w; x++ ){ if (this.getPixel32(sx + x, sy + y) === source) { this.setPixel32(sx + x, sy + y, r2, g2, b2, a2, false ); } } } this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } , setHSL: function (h, s, l, region){ if (typeof h === 'undefined' || h === null ) { h = false ; } if (typeof s === 'undefined' || s === null ) { s = false ; } if (typeof l === 'undefined' || l === null ) { l = false ; } if (!h && !s && !l) { return ; } if (typeof region === 'undefined') { region = new Phaser.Rectangle(0, 0, this.width, this.height); } var pixel = Phaser.Color.createColor(); for (var y = region.y; y < region.bottom; y++ ){ for (var x = region.x; x < region.right; x++ ){ Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true ); if (h) { pixel.h = h; } if (s) { pixel.s = s; } if (l) { pixel.l = l; } Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel); this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false ); } } this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } , shiftHSL: function (h, s, l, region){ if (typeof h === 'undefined' || h === null ) { h = false ; } if (typeof s === 'undefined' || s === null ) { s = false ; } if (typeof l === 'undefined' || l === null ) { l = false ; } if (!h && !s && !l) { return ; } if (typeof region === 'undefined') { region = new Phaser.Rectangle(0, 0, this.width, this.height); } var pixel = Phaser.Color.createColor(); for (var y = region.y; y < region.bottom; y++ ){ for (var x = region.x; x < region.right; x++ ){ Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel, true ); if (h) { pixel.h = this.game.math.wrap(pixel.h + h, 0, 1); } if (s) { pixel.s = this.game.math.limitValue(pixel.s + s, 0, 1); } if (l) { pixel.l = this.game.math.limitValue(pixel.l + l, 0, 1); } Phaser.Color.HSLtoRGB(pixel.h, pixel.s, pixel.l, pixel); this.setPixel32(x, y, pixel.r, pixel.g, pixel.b, pixel.a, false ); } } this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } , setPixel32: function (x, y, red, green, blue, alpha, immediate){ if (typeof immediate === 'undefined') { immediate = true ; } if (x >= 0 && x <= this.width && y >= 0 && y <= this.height) { if (Phaser.Device.LITTLE_ENDIAN) { this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red; } else { this.pixels[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha; } if (immediate) { this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } } } , setPixel: function (x, y, red, green, blue, immediate){ this.setPixel32(x, y, red, green, blue, 255, immediate); } , getPixel: function (x, y, out){ if (!out) { out = Phaser.Color.createColor(); } var index = ~~(x + (y * this.width)); index *= 4; if (this.data[index]) { out.r = this.data[index]; out.g = this.data[++index]; out.b = this.data[++index]; out.a = this.data[++index]; } return out; } , getPixel32: function (x, y){ if (x >= 0 && x <= this.width && y >= 0 && y <= this.height) { return this.pixels[y * this.width + x]; } } , getPixelRGB: function (x, y, out, hsl, hsv){ return Phaser.Color.unpackPixel(this.getPixel32(x, y), out, hsl, hsv); } , 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); } this.dirty = true ; } , draw: function (source, x, y){ if (typeof x === 'undefined') { x = 0; } if (typeof y === 'undefined') { y = 0; } if (typeof source === 'string') { source = this.game.cache.getImage(source); } if (source) { this.context.drawImage(source, 0, 0, source.width, source.height, x, y, source.width, source.height); } this.dirty = true ; } , drawSprite: function (sprite, x, y){ if (typeof x === 'undefined') { x = 0; } if (typeof y === 'undefined') { y = 0; } var frame = sprite.texture.frame; this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height); this.dirty = true ; } , 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; this.dirty = true ; } , extractMask: function (source, color, alpha){ if (typeof alpha === 'undefined') { alpha = 255; } } , render: function (){ if (!this.disableTextureUpload && this.game.renderType === Phaser.WEBGL && this.dirty) { PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl); this.dirty = false ; } } } ; Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;