Phaser.BitmapData = function (game, key, width, height){ if (typeof width === 'undefined') { width = 256; } if (typeof height === 'undefined') { height = 256; } 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); this.data = this.imageData.data; this.pixels = null ; if (this.imageData.data.buffer) { this.buffer = this.imageData.data.buffer; this.pixels = new Uint32Array(this.buffer); } else { if (window.ArrayBuffer) { this.buffer = new ArrayBuffer((_AN_Read_length('length', this.imageData.data))); this.pixels = new Uint32Array(this.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.texture.frame = this.textureFrame; this.type = Phaser.BITMAPDATA; this.disableTextureUpload = false ; this.dirty = false ; this.cls = this.clear; this._image = null ; this._pos = new Phaser.Point(); this._size = new Phaser.Point(); this._scale = new Phaser.Point(); this._rotate = 0; this._alpha = { prev: 1, current: 1} ; this._anchor = new Phaser.Point(); this._tempR = 0; this._tempG = 0; this._tempB = 0; this._circle = new Phaser.Circle(); } ; 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); } return this; } , load: function (source){ if (typeof source === 'string') { source = this.game.cache.getImage(source); } if (source) { this.resize(source.width, source.height); this.cls(); } else { return ; } this.draw(source); this.update(); return this; } , clear: function (){ this.context.clearRect(0, 0, this.width, this.height); this.dirty = true ; return this; } , 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 ; return this; } , 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.baseTexture.width = width; this.baseTexture.height = height; this.textureFrame.width = width; this.textureFrame.height = height; this.texture.width = width; this.texture.height = height; this.texture.crop.width = width; this.texture.crop.height = height; this.update(); this.dirty = true ; } return this; } , update: 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); this.data = this.imageData.data; if (this.imageData.data.buffer) { this.buffer = this.imageData.data.buffer; this.pixels = new Uint32Array(this.buffer); } else { if (window.ArrayBuffer) { this.buffer = new ArrayBuffer((_AN_Read_length('length', this.imageData.data))); this.pixels = new Uint32Array(this.buffer); } else { this.pixels = this.imageData.data; } } return this; } , 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, tx, ty); if (result !== false && result !== null && result !== undefined) { 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 ; } return this; } , 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, tx, ty); if (result !== pixel) { this.pixels[ty * this.width + tx] = result; dirty = true ; } } } if (dirty) { this.context.putImageData(this.imageData, 0, 0); this.dirty = true ; } return this; } , 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 ; return this; } , 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 ; return this; } , 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 ; return this; } , 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 ; } } return this; } , setPixel: function (x, y, red, green, blue, immediate){ return 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; 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); } , addToWorld: function (x, y, anchorX, anchorY, scaleX, scaleY){ scaleX = scaleX || 1; scaleY = scaleY || 1; var image = this.game.add.image(x, y, this); image.anchor.set(anchorX, anchorY); image.scale.set(scaleX, scaleY); return image; } , copy: function (source, x, y, width, height, tx, ty, newWidth, newHeight, rotate, anchorX, anchorY, scaleX, scaleY, alpha, blendMode, roundPx){ if (typeof source === 'undefined' || source === null ) { source = this; } this._image = source; if (source instanceof Phaser.Sprite || source instanceof Phaser.Image || source instanceof Phaser.Text) { this._pos.set(source.texture.crop.x, source.texture.crop.y); this._size.set(source.texture.crop.width, source.texture.crop.height); this._scale.set(source.scale.x, source.scale.y); this._anchor.set(source.anchor.x, source.anchor.y); this._rotate = source.rotation; this._alpha.current = source.alpha; this._image = source.texture.baseTexture.source; if (source.texture.trim) { tx += source.texture.trim.x - source.anchor.x * source.texture.trim.width; ty += source.texture.trim.y - source.anchor.y * source.texture.trim.height; } if (source.tint !== 16777215) { if (source.cachedTint !== source.tint) { source.cachedTint = source.tint; source.tintedTexture = PIXI.CanvasTinter.getTintedTexture(source, source.tint); } this._image = source.tintedTexture; } } else { this._pos.set(0); this._scale.set(1); this._anchor.set(0); this._rotate = 0; this._alpha.current = 1; if (source instanceof Phaser.BitmapData) { this._image = source.canvas; } else if (typeof source === 'string') { source = this.game.cache.getImage(source); if (source === null ) { return ; } else { this._image = source; } } this._size.set(this._image.width, this._image.height); } if (typeof x === 'undefined' || x === null ) { x = 0; } if (typeof y === 'undefined' || y === null ) { y = 0; } if (width) { this._size.x = width; } if (height) { this._size.y = height; } if (typeof tx === 'undefined' || tx === null ) { tx = x; } if (typeof ty === 'undefined' || ty === null ) { ty = y; } if (typeof newWidth === 'undefined' || newWidth === null ) { newWidth = this._size.x; } if (typeof newHeight === 'undefined' || newHeight === null ) { newHeight = this._size.y; } if (typeof rotate === 'number') { this._rotate = rotate; } if (typeof anchorX === 'number') { this._anchor.x = anchorX; } if (typeof anchorY === 'number') { this._anchor.y = anchorY; } if (typeof scaleX === 'number') { this._scale.x = scaleX; } if (typeof scaleY === 'number') { this._scale.y = scaleY; } if (typeof alpha === 'number') { this._alpha.current = alpha; } if (typeof blendMode === 'undefined') { blendMode = null ; } if (typeof roundPx === 'undefined') { roundPx = false ; } if (this._alpha.current <= 0 || this._scale.x === 0 || this._scale.y === 0 || this._size.x === 0 || this._size.y === 0) { return ; } this._alpha.prev = this.context.globalAlpha; this.context.save(); this.context.globalAlpha = this._alpha.current; if (blendMode) { this.context.globalCompositeOperation = blendMode; } if (roundPx) { tx |= 0; ty |= 0; } this.context.translate(tx, ty); this.context.scale(this._scale.x, this._scale.y); this.context.rotate(this._rotate); this.context.drawImage(this._image, this._pos.x + x, this._pos.y + y, this._size.x, this._size.y, - newWidth * this._anchor.x, - newHeight * this._anchor.y, newWidth, newHeight); this.context.restore(); this.context.globalAlpha = this._alpha.prev; this.dirty = true ; return this; } , copyRect: function (source, area, x, y, alpha, blendMode, roundPx){ return this.copy(source, area.x, area.y, area.width, area.height, x, y, area.width, area.height, 0, 0, 0, 1, 1, alpha, blendMode, roundPx); } , draw: function (source, x, y, width, height, blendMode, roundPx){ return this.copy(source, null , null , null , null , x, y, width, height, null , null , null , null , null , null , blendMode, roundPx); } , shadow: function (color, blur, x, y){ if (typeof color === 'undefined' || color === null ) { this.context.shadowColor = 'rgba(0,0,0,0)'; } else { this.context.shadowColor = color; this.context.shadowBlur = blur || 5; this.context.shadowOffsetX = x || 10; this.context.shadowOffsetY = y || 10; } } , alphaMask: function (source, mask, sourceRect, maskRect){ if (typeof maskRect === 'undefined' || maskRect === null ) { this.draw(mask).blendSourceAtop(); } else { this.draw(mask, maskRect.x, maskRect.y, maskRect.width, maskRect.height).blendSourceAtop(); } if (typeof sourceRect === 'undefined' || sourceRect === null ) { this.draw(source).blendReset(); } else { this.draw(source, sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height).blendReset(); } return this; } , extract: function (destination, r, g, b, a, resize, r2, g2, b2){ if (typeof a === 'undefined') { a = 255; } if (typeof resize === 'undefined') { resize = false ; } if (typeof r2 === 'undefined') { r2 = r; } if (typeof g2 === 'undefined') { g2 = g; } if (typeof b2 === 'undefined') { b2 = b; } if (resize) { destination.resize(this.width, this.height); } this.processPixelRGB(function (pixel, x, y){ if (pixel.r === r && pixel.g === g && pixel.b === b) { destination.setPixel32(x, y, r2, g2, b2, a, false ); } return false ; } , this); destination.context.putImageData(destination.imageData, 0, 0); destination.dirty = true ; return destination; } , rect: function (x, y, width, height, fillStyle){ if (typeof fillStyle !== 'undefined') { this.context.fillStyle = fillStyle; } this.context.fillRect(x, y, width, height); return this; } , circle: function (x, y, radius, fillStyle){ if (typeof fillStyle !== 'undefined') { this.context.fillStyle = fillStyle; } this.context.beginPath(); this.context.arc(x, y, radius, 0, Math.PI * 2, false ); this.context.closePath(); this.context.fill(); return this; } , textureLine: function (line, image, repeat){ if (typeof repeat === 'undefined') { repeat = 'repeat-x'; } if (typeof image === 'string') { image = this.game.cache.getImage(image); if (!image) { return ; } } var width = _AN_Read_length('length', line); if (repeat === 'no-repeat' && width > image.width) { width = image.width; } this.context.fillStyle = this.context.createPattern(image, repeat); this._circle = new Phaser.Circle(line.start.x, line.start.y, image.height); this._circle.circumferencePoint(line.angle - 1.570796326794897, false , this._pos); this.context.save(); this.context.translate(this._pos.x, this._pos.y); this.context.rotate(line.angle); this.context.fillRect(0, 0, width, image.height); this.context.restore(); this.dirty = true ; return this; } , render: function (){ if (!this.disableTextureUpload && this.game.renderType === Phaser.WEBGL && this.dirty) { PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl); this.dirty = false ; } return this; } , blendReset: function (){ this.context.globalCompositeOperation = 'source-over'; return this; } , blendSourceOver: function (){ this.context.globalCompositeOperation = 'source-over'; return this; } , blendSourceIn: function (){ this.context.globalCompositeOperation = 'source-in'; return this; } , blendSourceOut: function (){ this.context.globalCompositeOperation = 'source-out'; return this; } , blendSourceAtop: function (){ this.context.globalCompositeOperation = 'source-atop'; return this; } , blendDestinationOver: function (){ this.context.globalCompositeOperation = 'destination-over'; return this; } , blendDestinationIn: function (){ this.context.globalCompositeOperation = 'destination-in'; return this; } , blendDestinationOut: function (){ this.context.globalCompositeOperation = 'destination-out'; return this; } , blendDestinationAtop: function (){ this.context.globalCompositeOperation = 'destination-atop'; return this; } , blendXor: function (){ this.context.globalCompositeOperation = 'xor'; return this; } , blendAdd: function (){ this.context.globalCompositeOperation = 'lighter'; return this; } , blendMultiply: function (){ this.context.globalCompositeOperation = 'multiply'; return this; } , blendScreen: function (){ this.context.globalCompositeOperation = 'screen'; return this; } , blendOverlay: function (){ this.context.globalCompositeOperation = 'overlay'; return this; } , blendDarken: function (){ this.context.globalCompositeOperation = 'darken'; return this; } , blendLighten: function (){ this.context.globalCompositeOperation = 'lighten'; return this; } , blendColorDodge: function (){ this.context.globalCompositeOperation = 'color-dodge'; return this; } , blendColorBurn: function (){ this.context.globalCompositeOperation = 'color-burn'; return this; } , blendHardLight: function (){ this.context.globalCompositeOperation = 'hard-light'; return this; } , blendSoftLight: function (){ this.context.globalCompositeOperation = 'soft-light'; return this; } , blendDifference: function (){ this.context.globalCompositeOperation = 'difference'; return this; } , blendExclusion: function (){ this.context.globalCompositeOperation = 'exclusion'; return this; } , blendHue: function (){ this.context.globalCompositeOperation = 'hue'; return this; } , blendSaturation: function (){ this.context.globalCompositeOperation = 'saturation'; return this; } , blendColor: function (){ this.context.globalCompositeOperation = 'color'; return this; } , blendLuminosity: function (){ this.context.globalCompositeOperation = 'luminosity'; return this; } } ; Object.defineProperty(Phaser.BitmapData.prototype, "smoothed", { get: function (){ Phaser.Canvas.getSmoothingEnabled(this.context); } , set: function (value){ Phaser.Canvas.setSmoothingEnabled(this.context, value); } } ); Phaser.BitmapData.getTransform = function (translateX, translateY, scaleX, scaleY, skewX, skewY){ if (typeof translateX !== 'number') { translateX = 0; } if (typeof translateY !== 'number') { translateY = 0; } if (typeof scaleX !== 'number') { scaleX = 1; } if (typeof scaleY !== 'number') { scaleY = 1; } if (typeof skewX !== 'number') { skewX = 0; } if (typeof skewY !== 'number') { skewY = 0; } return { sx: scaleX, sy: scaleY, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY, translateX: translateX, translateY: translateY, tx: translateX, ty: translateY} ; } ; Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;