diff --git a/Phaser/Game.js b/Phaser/Game.js index f9eeac42..3eec63d5 100644 --- a/Phaser/Game.js +++ b/Phaser/Game.js @@ -175,7 +175,7 @@ var Phaser; //this.physics = new Phaser.Physics.Manager(this); this.plugins = new Phaser.PluginManager(this, this); - this.load.onLoadComplete.addOnce(this.loadComplete, this); + this.load.onLoadComplete.add(this.loadComplete, this); this.setRenderer(Phaser.Types.RENDERER_CANVAS); @@ -301,6 +301,7 @@ var Phaser; this._loadComplete = true; } else { // Start the loader going as we have something in the queue + this.load.onLoadComplete.add(this.loadComplete, this); this.load.start(); } } else { diff --git a/Phaser/Game.ts b/Phaser/Game.ts index 7274d7bb..e1795bb3 100644 --- a/Phaser/Game.ts +++ b/Phaser/Game.ts @@ -387,10 +387,6 @@ module Phaser { } - private emptyCallback() { - // Called by onUpdateCallback etc - } - /** * Game loop method will be called when it's running. */ diff --git a/Phaser/Stage.js b/Phaser/Stage.js index 238d5ffb..1438db1a 100644 --- a/Phaser/Stage.js +++ b/Phaser/Stage.js @@ -56,25 +56,15 @@ var Phaser; this.canvas = document.createElement('canvas'); this.canvas.width = width; this.canvas.height = height; + this.context = this.canvas.getContext('2d'); - if ((parent !== '' || parent !== null) && document.getElementById(parent)) { - document.getElementById(parent).appendChild(this.canvas); - document.getElementById(parent).style.overflow = 'hidden'; - } else { - document.body.appendChild(this.canvas); - } + Phaser.CanvasUtils.addToDOM(this.canvas, parent, true); + Phaser.CanvasUtils.setTouchAction(this.canvas); - // Consume default actions on the canvas - this.canvas.style.msTouchAction = 'none'; - this.canvas.style['ms-touch-action'] = 'none'; - this.canvas.style['touch-action'] = 'none'; - this.canvas.style.backgroundColor = 'rgb(0,0,0)'; this.canvas.oncontextmenu = function (event) { event.preventDefault(); }; - this.context = this.canvas.getContext('2d'); - this.css3 = new Phaser.Display.CSS3Filters(this.canvas); this.scaleMode = Phaser.StageScaleMode.NO_SCALE; @@ -125,7 +115,7 @@ var Phaser; if (this.clear || (this.game.paused && this.disablePauseScreen == false)) { if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height); @@ -185,13 +175,6 @@ var Phaser; } }; - Stage.prototype.setImageRenderingCrisp = function () { - this.canvas.style['image-rendering'] = 'crisp-edges'; - this.canvas.style['image-rendering'] = '-moz-crisp-edges'; - this.canvas.style['image-rendering'] = '-webkit-optimize-contrast'; - this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor'; - }; - Stage.prototype.pauseGame = function () { this.game.paused = true; @@ -250,7 +233,7 @@ var Phaser; this.context.fillStyle = this.fillStyle; if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height); diff --git a/Phaser/Stage.ts b/Phaser/Stage.ts index 4dee1b1a..cb8c629c 100644 --- a/Phaser/Stage.ts +++ b/Phaser/Stage.ts @@ -188,7 +188,7 @@ module Phaser { { if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else @@ -357,7 +357,7 @@ module Phaser { if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else diff --git a/Phaser/cameras/Camera.js b/Phaser/cameras/Camera.js index 6122da25..0edca481 100644 --- a/Phaser/cameras/Camera.js +++ b/Phaser/cameras/Camera.js @@ -65,10 +65,13 @@ var Phaser; this.texture = new Phaser.Display.Texture(this); // We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size) - this.texture.canvas = document.createElement('canvas'); - this.texture.canvas.width = width; - this.texture.canvas.height = height; + this._canvas = document.createElement('canvas'); + this._canvas.width = width; + this._canvas.height = height; + this._renderLocal = true; + this.texture.canvas = this._canvas; this.texture.context = this.texture.canvas.getContext('2d'); + this.texture.backgroundColor = this.game.stage.backgroundColor; // Handy proxies this.scale = this.transform.scale; @@ -94,6 +97,24 @@ var Phaser; configurable: true }); + Object.defineProperty(Camera.prototype, "directToStage", { + set: function (value) { + if (value) { + this._renderLocal = false; + this.texture.canvas = this.game.stage.canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor); + } else { + this._renderLocal = true; + this.texture.canvas = this._canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor); + } + + this.texture.context = this.texture.canvas.getContext('2d'); + }, + enumerable: true, + configurable: true + }); + /** * Hides an object from this Camera. Hidden objects are not rendered. * The object must implement a public cameraBlacklist property. diff --git a/Phaser/cameras/Camera.ts b/Phaser/cameras/Camera.ts index 1c1cf325..ca375afa 100644 --- a/Phaser/cameras/Camera.ts +++ b/Phaser/cameras/Camera.ts @@ -45,10 +45,13 @@ module Phaser { this.texture = new Phaser.Display.Texture(this); // We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size) - this.texture.canvas = document.createElement('canvas'); - this.texture.canvas.width = width; - this.texture.canvas.height = height; + this._canvas = document.createElement('canvas'); + this._canvas.width = width; + this._canvas.height = height; + this._renderLocal = true; + this.texture.canvas = this._canvas; this.texture.context = this.texture.canvas.getContext('2d'); + this.texture.backgroundColor = this.game.stage.backgroundColor; // Handy proxies this.scale = this.transform.scale; @@ -58,6 +61,8 @@ module Phaser { } + private _renderLocal: boolean; + private _canvas: HTMLCanvasElement; private _target: Sprite = null; /** @@ -160,6 +165,25 @@ module Phaser { */ public z: number = -1; + public set directToStage(value: boolean) { + + if (value) + { + this._renderLocal = false; + this.texture.canvas = this.game.stage.canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor); + } + else + { + this._renderLocal = true; + this.texture.canvas = this._canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor); + } + + this.texture.context = this.texture.canvas.getContext('2d'); + + } + /** * Hides an object from this Camera. Hidden objects are not rendered. * The object must implement a public cameraBlacklist property. diff --git a/Phaser/cameras/CameraManager.js b/Phaser/cameras/CameraManager.js index 34af934d..18f6c5df 100644 --- a/Phaser/cameras/CameraManager.js +++ b/Phaser/cameras/CameraManager.js @@ -29,6 +29,8 @@ var Phaser; this.defaultCamera = this.addCamera(x, y, width, height); + this.defaultCamera.directToStage = true; + this.current = this.defaultCamera; } /** diff --git a/Phaser/cameras/CameraManager.ts b/Phaser/cameras/CameraManager.ts index e433b799..dccd64af 100644 --- a/Phaser/cameras/CameraManager.ts +++ b/Phaser/cameras/CameraManager.ts @@ -29,6 +29,8 @@ module Phaser { this.defaultCamera = this.addCamera(x, y, width, height); + this.defaultCamera.directToStage = true; + this.current = this.defaultCamera; } diff --git a/Phaser/gameobjects/Sprite.js b/Phaser/gameobjects/Sprite.js index 3d2e38e5..5ee74be9 100644 --- a/Phaser/gameobjects/Sprite.js +++ b/Phaser/gameobjects/Sprite.js @@ -114,7 +114,7 @@ var Phaser; */ Sprite.prototype.bringToTop = function () { if (this.group) { - //this.group.bringToTop(this); + this.group.bringToTop(this); } }; diff --git a/Phaser/loader/Loader.js b/Phaser/loader/Loader.js index 4825ad7c..68db57e6 100644 --- a/Phaser/loader/Loader.js +++ b/Phaser/loader/Loader.js @@ -19,6 +19,10 @@ var Phaser; * @type {string} */ this.crossOrigin = ''; + // If you want to append a URL before the path of any asset you can set this here. + // Useful if you need to allow an asset url to be configured outside of the game code. + // MUST have / on the end of it! + this.baseURL = ''; this.game = game; this._keys = []; @@ -225,7 +229,7 @@ var Phaser; return _this.fileError(file.key); }; file.data.crossOrigin = this.crossOrigin; - file.data.src = file.url; + file.data.src = this.baseURL + file.url; break; case 'audio': @@ -233,7 +237,7 @@ var Phaser; if (file.url !== null) { if (this.game.sound.usingWebAudio) { - this._xhr.open("GET", file.url, true); + this._xhr.open("GET", this.baseURL + file.url, true); this._xhr.responseType = "arraybuffer"; this._xhr.onload = function () { return _this.fileComplete(file.key); @@ -248,7 +252,7 @@ var Phaser; file.data = new Audio(); file.data.name = file.key; file.data.preload = 'auto'; - file.data.src = file.url; + file.data.src = this.baseURL + file.url; this.fileComplete(file.key); } else { file.data = new Audio(); @@ -257,7 +261,7 @@ var Phaser; return _this.fileError(file.key); }; file.data.preload = 'auto'; - file.data.src = file.url; + file.data.src = this.baseURL + file.url; file.data.addEventListener('canplaythrough', Phaser.GAMES[this.game.id].load.fileComplete(file.key), false); file.data.load(); } @@ -267,7 +271,7 @@ var Phaser; break; case 'text': - this._xhr.open("GET", file.url, true); + this._xhr.open("GET", this.baseURL + file.url, true); this._xhr.responseType = "text"; this._xhr.onload = function () { return _this.fileComplete(file.key); @@ -341,7 +345,7 @@ var Phaser; } else { // Load the JSON or XML before carrying on with the next file loadNext = false; - this._xhr.open("GET", file.atlasURL, true); + this._xhr.open("GET", this.baseURL + file.atlasURL, true); this._xhr.responseType = "text"; if (file.format == Loader.TEXTURE_ATLAS_JSON_ARRAY) { diff --git a/Phaser/physics/arcade/Body.js b/Phaser/physics/arcade/Body.js index 83d8dab3..dc4d8c36 100644 --- a/Phaser/physics/arcade/Body.js +++ b/Phaser/physics/arcade/Body.js @@ -186,71 +186,6 @@ var Phaser; enumerable: true, configurable: true }); - - // MOVE THESE TO A UTIL - Body.prototype.render = function (context) { - context.beginPath(); - context.strokeStyle = 'rgb(0,255,0)'; - context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height); - context.stroke(); - context.closePath(); - - // center point - context.fillStyle = 'rgb(0,255,0)'; - context.fillRect(this.position.x, this.position.y, 2, 2); - - if (this.touching & Phaser.Types.LEFT) { - context.beginPath(); - context.strokeStyle = 'rgb(255,0,0)'; - context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight); - context.lineTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight); - context.stroke(); - context.closePath(); - } - if (this.touching & Phaser.Types.RIGHT) { - context.beginPath(); - context.strokeStyle = 'rgb(255,0,0)'; - context.moveTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight); - context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight); - context.stroke(); - context.closePath(); - } - - if (this.touching & Phaser.Types.UP) { - context.beginPath(); - context.strokeStyle = 'rgb(255,0,0)'; - context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight); - context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight); - context.stroke(); - context.closePath(); - } - if (this.touching & Phaser.Types.DOWN) { - context.beginPath(); - context.strokeStyle = 'rgb(255,0,0)'; - context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight); - context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight); - context.stroke(); - context.closePath(); - } - }; - - /** - * Render debug infos. (including name, bounds info, position and some other properties) - * @param x {number} X position of the debug info to be rendered. - * @param y {number} Y position of the debug info to be rendered. - * @param [color] {number} color of the debug info to be rendered. (format is css color string) - */ - Body.prototype.renderDebugInfo = function (x, y, color) { - if (typeof color === "undefined") { color = 'rgb(255,255,255)'; } - this.sprite.texture.context.fillStyle = color; - this.sprite.texture.context.fillText('Sprite: (' + this.sprite.width + ' x ' + this.sprite.height + ')', x, y); - - //this.sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14); - this.sprite.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' rotation: ' + this.sprite.transform.rotation.toFixed(0), x, y + 14); - this.sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28); - this.sprite.texture.context.fillText('acx: ' + this.acceleration.x.toFixed(1) + ' acy: ' + this.acceleration.y.toFixed(1), x, y + 42); - this.sprite.texture.context.fillText('angVx: ' + this.angularVelocity.toFixed(1) + ' angAc: ' + this.angularAcceleration.toFixed(1), x, y + 56); - }; return Body; })(); Physics.Body = Body; diff --git a/Phaser/utils/CanvasUtils.js b/Phaser/utils/CanvasUtils.js index 2f9b5688..3bdf1697 100644 --- a/Phaser/utils/CanvasUtils.js +++ b/Phaser/utils/CanvasUtils.js @@ -1,19 +1,78 @@ -var Shapes; -(function (Shapes) { - - var Point = Shapes.Point = (function () { - function Point(x, y) { - this.x = x; - this.y = y; +/// +/** +* Phaser - CanvasUtils +* +* A collection of methods useful for manipulating canvas objects. +*/ +var Phaser; +(function (Phaser) { + var CanvasUtils = (function () { + function CanvasUtils() { } - Point.prototype.getDist = function () { - return Math.sqrt((this.x * this.x) + (this.y * this.y)); + CanvasUtils.getAspectRatio = function (canvas) { + return canvas.width / canvas.height; }; - Point.origin = new Point(0, 0); - return Point; + + CanvasUtils.setBackgroundColor = function (canvas, color) { + if (typeof color === "undefined") { color = 'rgb(0,0,0)'; } + canvas.style.backgroundColor = color; + return canvas; + }; + + CanvasUtils.setTouchAction = function (canvas, value) { + if (typeof value === "undefined") { value = 'none'; } + canvas.style.msTouchAction = value; + canvas.style['ms-touch-action'] = value; + canvas.style['touch-action'] = value; + + return canvas; + }; + + CanvasUtils.addToDOM = function (canvas, parent, overflowHidden) { + if (typeof parent === "undefined") { parent = ''; } + if (typeof overflowHidden === "undefined") { overflowHidden = true; } + if ((parent !== '' || parent !== null) && document.getElementById(parent)) { + document.getElementById(parent).appendChild(canvas); + + if (overflowHidden) { + document.getElementById(parent).style.overflow = 'hidden'; + } + } else { + document.body.appendChild(canvas); + } + + return canvas; + }; + + CanvasUtils.setTransform = function (context, translateX, translateY, scaleX, scaleY, skewX, skewY) { + context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY); + + return context; + }; + + CanvasUtils.setSmoothingEnabled = function (context, value) { + context['imageSmoothingEnabled'] = value; + context['mozImageSmoothingEnabled'] = value; + context['oImageSmoothingEnabled'] = value; + context['webkitImageSmoothingEnabled'] = value; + context['msImageSmoothingEnabled'] = value; + return context; + }; + + CanvasUtils.setImageRenderingCrisp = function (canvas) { + canvas.style['image-rendering'] = 'crisp-edges'; + canvas.style['image-rendering'] = '-moz-crisp-edges'; + canvas.style['image-rendering'] = '-webkit-optimize-contrast'; + canvas.style.msInterpolationMode = 'nearest-neighbor'; + return canvas; + }; + + CanvasUtils.setImageRenderingBicubic = function (canvas) { + canvas.style['image-rendering'] = 'auto'; + canvas.style.msInterpolationMode = 'bicubic'; + return canvas; + }; + return CanvasUtils; })(); - -})(Shapes || (Shapes = {})); - -var p = new Shapes.Point(3, 4); -var dist = p.getDist(); + Phaser.CanvasUtils = CanvasUtils; +})(Phaser || (Phaser = {})); diff --git a/README.md b/README.md index 97ca1473..71626fae 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,6 @@ Future Plans * Joypad support. * Gestures input class. * Integrate the Advanced Physics system that is 90% ready but needs updating for TypeScript 0.9.1. -* We can streamline the code for a lot of the CanvasRenderer calls re: global ops, alpha, etc. -* Move getOffset inside Phaser.Display.Canvas ToDo before release @@ -169,6 +167,7 @@ V1.0.0 * Added Input.pollRate - this lets you limit how often Pointer events are handled (0 = every frame, 1 = every other frame, etc) * Renamed the 'init' function to 'preload'. It now calls load.start automatically. * Entire framework updated for TypeScript 0.9.1 - what a mammoth amount of work that was! Sorry but not backward compatible. +* Added CanvasUtils class, including ability to set image rendering, add a canvas to the dom and other handy things. V0.9.6 diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index b1fb84bc..13bc37d0 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -126,6 +126,10 @@ world sprite.ts + + + render crisp.ts + diff --git a/Tests/buttons/basic button.js b/Tests/buttons/basic button.js index 9964da5a..19733c7a 100644 --- a/Tests/buttons/basic button.js +++ b/Tests/buttons/basic button.js @@ -31,8 +31,10 @@ function clickedIt() { if (this.image.visible == true) { + game.stage.backgroundColor = ''; this.image.visible = false; } else { + game.stage.backgroundColor = 'rgb(0,0,0)'; this.image.visible = true; } } diff --git a/Tests/display/render crisp.js b/Tests/display/render crisp.js new file mode 100644 index 00000000..c7909c9a --- /dev/null +++ b/Tests/display/render crisp.js @@ -0,0 +1,28 @@ +/// +(function () { + var game = new Phaser.Game(this, 'game', 800, 600, preload, create); + + function preload() { + game.load.image('boss', 'assets/misc/boss1.png'); + game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71); + } + + var boss; + var button; + + function create() { + // For browsers that support it, this keeps our pixel art looking crisp + Phaser.CanvasUtils.setSmoothingEnabled(game.stage.context, false); + + boss = game.add.sprite(game.stage.centerX, game.stage.centerY, 'boss'); + boss.origin.setTo(0.5, 0.5); + + // Zoom in each time we press it + button = game.add.button(32, 32, 'button', clickedIt, this, 2, 1, 0); + } + + function clickedIt() { + boss.scale.x += 0.5; + boss.scale.y += 0.5; + } +})(); diff --git a/Tests/display/render crisp.ts b/Tests/display/render crisp.ts new file mode 100644 index 00000000..abbe1ee6 --- /dev/null +++ b/Tests/display/render crisp.ts @@ -0,0 +1,37 @@ +/// + +(function () { + + var game = new Phaser.Game(this, 'game', 800, 600, preload, create); + + function preload() { + + game.load.image('boss', 'assets/misc/boss1.png'); + game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71); + + } + + var boss: Phaser.Sprite; + var button: Phaser.UI.Button; + + function create() { + + // For browsers that support it, this keeps our pixel art looking crisp + Phaser.CanvasUtils.setSmoothingEnabled(game.stage.context, false); + + boss = game.add.sprite(game.stage.centerX, game.stage.centerY, 'boss'); + boss.origin.setTo(0.5, 0.5); + + // Zoom in each time we press it + button = game.add.button(32, 32, 'button', clickedIt, this, 2, 1, 0); + + } + + function clickedIt() { + + boss.scale.x += 0.5; + boss.scale.y += 0.5; + + } + +})(); diff --git a/Tests/phaser-debug.js b/Tests/phaser-debug.js index 48124ba3..d352586f 100644 --- a/Tests/phaser-debug.js +++ b/Tests/phaser-debug.js @@ -1,5 +1,29 @@ /// /** +* Phaser - http://www.phaser.io +* +* v1.0.0 - August 12th 2013 +* +* A feature-packed 2D canvas game framework born from the firey pits of Flixel and +* constructed via plenty of blood, sweat, tears and coffee by Richard Davey (@photonstorm). +* +* Many thanks to Adam Saltsman (@ADAMATOMIC) for releasing Flixel, from both which Phaser +* and my love of game development originate. +* +* Follow Phaser progress at http://www.photonstorm.com +* +* "If you want your children to be intelligent, read them fairy tales." +* "If you want them to be more intelligent, read them more fairy tales." +* -- Albert Einstein +*/ +var Phaser; +(function (Phaser) { + Phaser.VERSION = 'Phaser version 1.0.0'; + + Phaser.GAMES = []; +})(Phaser || (Phaser = {})); +/// +/** * Types * * This file contains all constants used through-out Phaser. @@ -5320,10 +5344,13 @@ var Phaser; this.texture = new Phaser.Display.Texture(this); // We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size) - this.texture.canvas = document.createElement('canvas'); - this.texture.canvas.width = width; - this.texture.canvas.height = height; + this._canvas = document.createElement('canvas'); + this._canvas.width = width; + this._canvas.height = height; + this._renderLocal = true; + this.texture.canvas = this._canvas; this.texture.context = this.texture.canvas.getContext('2d'); + this.texture.backgroundColor = this.game.stage.backgroundColor; // Handy proxies this.scale = this.transform.scale; @@ -5349,6 +5376,24 @@ var Phaser; configurable: true }); + Object.defineProperty(Camera.prototype, "directToStage", { + set: function (value) { + if (value) { + this._renderLocal = false; + this.texture.canvas = this.game.stage.canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor); + } else { + this._renderLocal = true; + this.texture.canvas = this._canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor); + } + + this.texture.context = this.texture.canvas.getContext('2d'); + }, + enumerable: true, + configurable: true + }); + /** * Hides an object from this Camera. Hidden objects are not rendered. * The object must implement a public cameraBlacklist property. @@ -5697,6 +5742,8 @@ var Phaser; this.defaultCamera = this.addCamera(x, y, width, height); + this.defaultCamera.directToStage = true; + this.current = this.defaultCamera; } /** @@ -19162,25 +19209,15 @@ var Phaser; this.canvas = document.createElement('canvas'); this.canvas.width = width; this.canvas.height = height; + this.context = this.canvas.getContext('2d'); - if ((parent !== '' || parent !== null) && document.getElementById(parent)) { - document.getElementById(parent).appendChild(this.canvas); - document.getElementById(parent).style.overflow = 'hidden'; - } else { - document.body.appendChild(this.canvas); - } + Phaser.CanvasUtils.addToDOM(this.canvas, parent, true); + Phaser.CanvasUtils.setTouchAction(this.canvas); - // Consume default actions on the canvas - this.canvas.style.msTouchAction = 'none'; - this.canvas.style['ms-touch-action'] = 'none'; - this.canvas.style['touch-action'] = 'none'; - this.canvas.style.backgroundColor = 'rgb(0,0,0)'; this.canvas.oncontextmenu = function (event) { event.preventDefault(); }; - this.context = this.canvas.getContext('2d'); - this.css3 = new Phaser.Display.CSS3Filters(this.canvas); this.scaleMode = Phaser.StageScaleMode.NO_SCALE; @@ -19231,7 +19268,7 @@ var Phaser; if (this.clear || (this.game.paused && this.disablePauseScreen == false)) { if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height); @@ -19291,13 +19328,6 @@ var Phaser; } }; - Stage.prototype.setImageRenderingCrisp = function () { - this.canvas.style['image-rendering'] = 'crisp-edges'; - this.canvas.style['image-rendering'] = '-moz-crisp-edges'; - this.canvas.style['image-rendering'] = '-webkit-optimize-contrast'; - this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor'; - }; - Stage.prototype.pauseGame = function () { this.game.paused = true; @@ -19356,7 +19386,7 @@ var Phaser; this.context.fillStyle = this.fillStyle; if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height); @@ -20019,27 +20049,81 @@ var Phaser; })(); Phaser.Game = Game; })(Phaser || (Phaser = {})); -/// +/// /** -* Phaser - http://www.phaser.io +* Phaser - CanvasUtils * -* v1.0.0 - August 12th 2013 -* -* A feature-packed 2D canvas game framework born from the firey pits of Flixel and -* constructed via plenty of blood, sweat, tears and coffee by Richard Davey (@photonstorm). -* -* Many thanks to Adam Saltsman (@ADAMATOMIC) for releasing Flixel, from both which Phaser -* and my love of game development originate. -* -* Follow Phaser progress at http://www.photonstorm.com -* -* "If you want your children to be intelligent, read them fairy tales." -* "If you want them to be more intelligent, read them more fairy tales." -* -- Albert Einstein +* A collection of methods useful for manipulating canvas objects. */ var Phaser; (function (Phaser) { - Phaser.VERSION = 'Phaser version 1.0.0'; + var CanvasUtils = (function () { + function CanvasUtils() { + } + CanvasUtils.getAspectRatio = function (canvas) { + return canvas.width / canvas.height; + }; - Phaser.GAMES = []; + CanvasUtils.setBackgroundColor = function (canvas, color) { + if (typeof color === "undefined") { color = 'rgb(0,0,0)'; } + canvas.style.backgroundColor = color; + return canvas; + }; + + CanvasUtils.setTouchAction = function (canvas, value) { + if (typeof value === "undefined") { value = 'none'; } + canvas.style.msTouchAction = value; + canvas.style['ms-touch-action'] = value; + canvas.style['touch-action'] = value; + + return canvas; + }; + + CanvasUtils.addToDOM = function (canvas, parent, overflowHidden) { + if (typeof parent === "undefined") { parent = ''; } + if (typeof overflowHidden === "undefined") { overflowHidden = true; } + if ((parent !== '' || parent !== null) && document.getElementById(parent)) { + document.getElementById(parent).appendChild(canvas); + + if (overflowHidden) { + document.getElementById(parent).style.overflow = 'hidden'; + } + } else { + document.body.appendChild(canvas); + } + + return canvas; + }; + + CanvasUtils.setTransform = function (context, translateX, translateY, scaleX, scaleY, skewX, skewY) { + context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY); + + return context; + }; + + CanvasUtils.setSmoothingEnabled = function (context, value) { + context['imageSmoothingEnabled'] = value; + context['mozImageSmoothingEnabled'] = value; + context['oImageSmoothingEnabled'] = value; + context['webkitImageSmoothingEnabled'] = value; + context['msImageSmoothingEnabled'] = value; + return context; + }; + + CanvasUtils.setImageRenderingCrisp = function (canvas) { + canvas.style['image-rendering'] = 'crisp-edges'; + canvas.style['image-rendering'] = '-moz-crisp-edges'; + canvas.style['image-rendering'] = '-webkit-optimize-contrast'; + canvas.style.msInterpolationMode = 'nearest-neighbor'; + return canvas; + }; + + CanvasUtils.setImageRenderingBicubic = function (canvas) { + canvas.style['image-rendering'] = 'auto'; + canvas.style.msInterpolationMode = 'bicubic'; + return canvas; + }; + return CanvasUtils; + })(); + Phaser.CanvasUtils = CanvasUtils; })(Phaser || (Phaser = {})); diff --git a/build/phaser-debug.js b/build/phaser-debug.js index 02b3ade9..d352586f 100644 --- a/build/phaser-debug.js +++ b/build/phaser-debug.js @@ -5344,10 +5344,13 @@ var Phaser; this.texture = new Phaser.Display.Texture(this); // We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size) - this.texture.canvas = document.createElement('canvas'); - this.texture.canvas.width = width; - this.texture.canvas.height = height; + this._canvas = document.createElement('canvas'); + this._canvas.width = width; + this._canvas.height = height; + this._renderLocal = true; + this.texture.canvas = this._canvas; this.texture.context = this.texture.canvas.getContext('2d'); + this.texture.backgroundColor = this.game.stage.backgroundColor; // Handy proxies this.scale = this.transform.scale; @@ -5373,6 +5376,24 @@ var Phaser; configurable: true }); + Object.defineProperty(Camera.prototype, "directToStage", { + set: function (value) { + if (value) { + this._renderLocal = false; + this.texture.canvas = this.game.stage.canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor); + } else { + this._renderLocal = true; + this.texture.canvas = this._canvas; + Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor); + } + + this.texture.context = this.texture.canvas.getContext('2d'); + }, + enumerable: true, + configurable: true + }); + /** * Hides an object from this Camera. Hidden objects are not rendered. * The object must implement a public cameraBlacklist property. @@ -5721,6 +5742,8 @@ var Phaser; this.defaultCamera = this.addCamera(x, y, width, height); + this.defaultCamera.directToStage = true; + this.current = this.defaultCamera; } /** @@ -19245,7 +19268,7 @@ var Phaser; if (this.clear || (this.game.paused && this.disablePauseScreen == false)) { if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height); @@ -19363,7 +19386,7 @@ var Phaser; this.context.fillStyle = this.fillStyle; if (this.game.device.patchAndroidClearRectBug) { - this.context.fillStyle = 'rgb(0,0,0)'; + this.context.fillStyle = this._backgroundColor; this.context.fillRect(0, 0, this.width, this.height); } else { this.context.clearRect(0, 0, this.width, this.height);