Phaser.BitmapData = function (game, key, width, height){ if (width === undefined || width === 0) { width = 256; } if (height === undefined || height === 0) { height = 256; } this.game = game; this.key = key; this.width = width; this.height = height; this.canvas = PIXI.CanvasPool.create(this, width, height); this.context = this.canvas.getContext('2d', { alpha: true } ); this.ctx = this.context; this.imageData = this.context.getImageData(0, 0, width, height); this.data = null ; if (this.imageData) { this.data = this.imageData.data; } this.pixels = null ; if (this.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; } } } 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'); 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(); this._swapCanvas = PIXI.CanvasPool.create(this, width, height); } ; Phaser.BitmapData.prototype = { move: function (x, y){ if (x !== 0) { this.moveH(x); } if (y !== 0) { this.moveV(y); } return this; } , moveH: function (distance){ var c = this._swapCanvas; var ctx = c.getContext('2d'); var h = this.height; var src = this.canvas; ctx.clearRect(0, 0, this.width, this.height); if (distance < 0) { distance = Math.abs(distance); var w = this.width - distance; ctx.drawImage(src, 0, 0, distance, h, w, 0, distance, h); ctx.drawImage(src, distance, 0, w, h, 0, 0, w, h); } else { var w = this.width - distance; ctx.drawImage(src, w, 0, distance, h, 0, 0, distance, h); ctx.drawImage(src, 0, 0, w, h, distance, 0, w, h); } _AN_Call_clear('clear', this); return this.copy(this._swapCanvas); } , moveV: function (distance){ var c = this._swapCanvas; var ctx = c.getContext('2d'); var w = this.width; var src = this.canvas; ctx.clearRect(0, 0, this.width, this.height); if (distance < 0) { distance = Math.abs(distance); var h = this.height - distance; ctx.drawImage(src, 0, 0, w, distance, 0, h, w, distance); ctx.drawImage(src, 0, distance, w, h, 0, 0, w, h); } else { var h = this.height - distance; ctx.drawImage(src, 0, h, w, distance, 0, 0, w, distance); ctx.drawImage(src, 0, 0, w, h, 0, distance, w, h); } _AN_Call_clear('clear', this); return this.copy(this._swapCanvas); } , 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 (x, y, width, height){ if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = this.width; } if (height === undefined) { height = this.height; } this.context.clearRect(x, y, width, height); this.dirty = true ; return this; } , fill: function (r, g, b, a){ if (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; } , generateTexture: function (key){ var image = new Image(); _AN_Write_src('src', image, false , this.canvas.toDataURL("image/png")); var obj = this.game.cache.addImage(key, '', image); return new PIXI.Texture(obj.base); } , 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._swapCanvas.width = width; this._swapCanvas.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 (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = Math.max(1, this.width); } if (height === undefined) { height = Math.max(1, 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 (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = this.width; } if (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 (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = this.width; } if (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 (h === undefined || h === null ) { h = false ; } if (s === undefined || s === null ) { s = false ; } if (l === undefined || l === null ) { l = false ; } if (!h && !s && !l) { return ; } if (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 (h === undefined || h === null ) { h = false ; } if (s === undefined || s === null ) { s = false ; } if (l === undefined || l === null ) { l = false ; } if (!h && !s && !l) { return ; } if (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 (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); } , getFirstPixel: function (direction){ if (direction === undefined) { direction = 0; } var pixel = Phaser.Color.createColor(); var x = 0; var y = 0; var v = 1; var scan = false ; if (direction === 1) { v = -1; y = this.height; } else if (direction === 3) { v = -1; x = this.width; } do { Phaser.Color.unpackPixel(this.getPixel32(x, y), pixel); if (direction === 0 || direction === 1) { x++ ; if (x === this.width) { x = 0; y += v; if (y >= this.height || y <= 0) { scan = true ; } } } else if (direction === 2 || direction === 3) { y++ ; if (y === this.height) { y = 0; x += v; if (x >= this.width || x <= 0) { scan = true ; } } } } while(pixel.a === 0 && !scan)pixel.x = x; pixel.y = y; return pixel; } , getBounds: function (rect){ if (rect === undefined) { rect = new Phaser.Rectangle(); } rect.x = this.getFirstPixel(2).x; if (rect.x === this.width) { return rect.setTo(0, 0, 0, 0); } rect.y = this.getFirstPixel(0).y; rect.width = (this.getFirstPixel(3).x - rect.x) + 1; rect.height = (this.getFirstPixel(1).y - rect.y) + 1; return rect; } , 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 (source === undefined || source === null ) { source = this; } this._image = source; if (source instanceof Phaser.Sprite || source instanceof Phaser.Image || source instanceof Phaser.Text || source instanceof PIXI.Sprite) { 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 (tx === undefined || tx === null ) { tx = source.x; } if (ty === undefined || ty === null ) { ty = source.y; } 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 (x === undefined || x === null ) { x = 0; } if (y === undefined || y === null ) { y = 0; } if (width) { this._size.x = width; } if (height) { this._size.y = height; } if (tx === undefined || tx === null ) { tx = x; } if (ty === undefined || ty === null ) { ty = y; } if (newWidth === undefined || newWidth === null ) { newWidth = this._size.x; } if (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 (blendMode === undefined) { blendMode = null ; } if (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 ; } var ctx = this.context; this._alpha.prev = ctx.globalAlpha; ctx.save(); ctx.globalAlpha = this._alpha.current; if (blendMode) { this.op = blendMode; } if (roundPx) { tx |= 0; ty |= 0; } ctx.translate(tx, ty); ctx.scale(this._scale.x, this._scale.y); ctx.rotate(this._rotate); ctx.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); ctx.restore(); ctx.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); } , drawGroup: function (group, blendMode, roundPx){ if (group.total > 0) { group.forEachExists(this.copy, this, null , null , null , null , null , null , null , null , null , null , null , null , null , null , blendMode, roundPx); } return this; } , drawFull: function (parent, blendMode, roundPx){ if (parent.worldVisible === false || parent.worldAlpha === 0 || (parent.hasOwnProperty('exists') && parent.exists === false )) { return this; } if (parent.type !== Phaser.GROUP && parent.type !== Phaser.EMITTER && parent.type !== Phaser.BITMAPTEXT) { if (parent.type === Phaser.GRAPHICS) { var bounds = parent.getBounds(); this.ctx.save(); this.ctx.translate(bounds.x, bounds.y); PIXI.CanvasGraphics.renderGraphics(parent, this.ctx); this.ctx.restore(); } else { this.copy(parent, null , null , null , null , parent.worldPosition.x, parent.worldPosition.y, null , null , parent.worldRotation, null , null , parent.worldScale.x, parent.worldScale.y, parent.worldAlpha, blendMode, roundPx); } } if (parent.children) { for (var i = 0; i < _AN_Read_length('length', parent.children); i++ ){ this.drawFull(parent.children[i], blendMode, roundPx); } } return this; } , shadow: function (color, blur, x, y){ var ctx = this.context; if (color === undefined || color === null ) { ctx.shadowColor = 'rgba(0,0,0,0)'; } else { ctx.shadowColor = color; ctx.shadowBlur = blur || 5; ctx.shadowOffsetX = x || 10; ctx.shadowOffsetY = y || 10; } } , alphaMask: function (source, mask, sourceRect, maskRect){ if (maskRect === undefined || maskRect === null ) { this.draw(mask).blendSourceAtop(); } else { this.draw(mask, maskRect.x, maskRect.y, maskRect.width, maskRect.height).blendSourceAtop(); } if (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 (a === undefined) { a = 255; } if (resize === undefined) { resize = false ; } if (r2 === undefined) { r2 = r; } if (g2 === undefined) { g2 = g; } if (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; } , text: function (text, x, y, font, color, shadow){ if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (font === undefined) { font = '14px Courier'; } if (color === undefined) { color = 'rgb(255,255,255)'; } if (shadow === undefined) { shadow = true ; } var ctx = this.context; var prevFont = ctx.font; ctx.font = font; if (shadow) { ctx.fillStyle = 'rgb(0,0,0)'; ctx.fillText(text, x + 1, y + 1); } ctx.fillStyle = color; ctx.fillText(text, x, y); ctx.font = prevFont; } , circle: function (x, y, radius, fillStyle){ var ctx = this.context; if (fillStyle !== undefined) { ctx.fillStyle = fillStyle; } ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2, false ); ctx.closePath(); ctx.fill(); return this; } , line: function (x1, y1, x2, y2, color, width){ if (color === undefined) { color = '#fff'; } if (width === undefined) { width = 1; } var ctx = this.context; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.lineWidth = width; ctx.strokeStyle = color; ctx.stroke(); ctx.closePath(); return this; } , textureLine: function (line, image, repeat){ if (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; } var ctx = this.context; ctx.fillStyle = ctx.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); ctx.save(); ctx.translate(this._pos.x, this._pos.y); ctx.rotate(line.angle); ctx.fillRect(0, 0, width, image.height); ctx.restore(); this.dirty = true ; return this; } , render: function (){ if (!this.disableTextureUpload && this.dirty) { this.baseTexture.dirty(); this.dirty = false ; } return this; } , destroy: function (){ PIXI.CanvasPool.remove(this); } , blendReset: function (){ this.op = 'source-over'; return this; } , blendSourceOver: function (){ this.op = 'source-over'; return this; } , blendSourceIn: function (){ this.op = 'source-in'; return this; } , blendSourceOut: function (){ this.op = 'source-out'; return this; } , blendSourceAtop: function (){ this.op = 'source-atop'; return this; } , blendDestinationOver: function (){ this.op = 'destination-over'; return this; } , blendDestinationIn: function (){ this.op = 'destination-in'; return this; } , blendDestinationOut: function (){ this.op = 'destination-out'; return this; } , blendDestinationAtop: function (){ this.op = 'destination-atop'; return this; } , blendXor: function (){ this.op = 'xor'; return this; } , blendAdd: function (){ this.op = 'lighter'; return this; } , blendMultiply: function (){ this.op = 'multiply'; return this; } , blendScreen: function (){ this.op = 'screen'; return this; } , blendOverlay: function (){ this.op = 'overlay'; return this; } , blendDarken: function (){ this.op = 'darken'; return this; } , blendLighten: function (){ this.op = 'lighten'; return this; } , blendColorDodge: function (){ this.op = 'color-dodge'; return this; } , blendColorBurn: function (){ this.op = 'color-burn'; return this; } , blendHardLight: function (){ this.op = 'hard-light'; return this; } , blendSoftLight: function (){ this.op = 'soft-light'; return this; } , blendDifference: function (){ this.op = 'difference'; return this; } , blendExclusion: function (){ this.op = 'exclusion'; return this; } , blendHue: function (){ this.op = 'hue'; return this; } , blendSaturation: function (){ this.op = 'saturation'; return this; } , blendColor: function (){ this.op = 'color'; return this; } , blendLuminosity: function (){ this.op = '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); } } ); Object.defineProperty(Phaser.BitmapData.prototype, "op", { get: function (){ return this.context.globalCompositeOperation; } , set: function (value){ this.context.globalCompositeOperation = 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;