|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @author Mat Groves (@Doormat23) |
| 4 | +* @copyright 2016 Photon Storm Ltd. |
| 5 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 6 | +*/ |
| 7 | + |
| 8 | +/** |
| 9 | +* A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view. |
| 10 | +* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y |
| 11 | +* |
| 12 | +* @class Phaser.Camera |
| 13 | +* @constructor |
| 14 | +* @param {Phaser.Game} game - Game reference to the currently running game. |
| 15 | +*/ |
| 16 | +Phaser.Renderer.Canvas = function (game) |
| 17 | +{ |
| 18 | + console.log('CanvasRenderer Alive'); |
| 19 | + |
| 20 | + /** |
| 21 | + * @property {Phaser.Game} game - A reference to the currently running Game. |
| 22 | + */ |
| 23 | + this.game = game; |
| 24 | + |
| 25 | + this.type = Phaser.CANVAS; |
| 26 | + |
| 27 | + // this.resolution = game.resolution; |
| 28 | + |
| 29 | + /** |
| 30 | + * This sets if the CanvasRenderer will clear the canvas or not before the new render pass. |
| 31 | + * If the Stage is NOT transparent Pixi will use a canvas sized fillRect operation every frame to set the canvas background color. |
| 32 | + * If the Stage is transparent Pixi will use clearRect to clear the canvas every frame. |
| 33 | + * Disable this by setting this to false. For example if your game has a canvas filling background image you often don't need this set. |
| 34 | + * |
| 35 | + * @property clearBeforeRender |
| 36 | + * @type Boolean |
| 37 | + * @default |
| 38 | + */ |
| 39 | + this.clearBeforeRender = game.clearBeforeRender; |
| 40 | + |
| 41 | + /** |
| 42 | + * Whether the render view is transparent |
| 43 | + * |
| 44 | + * @property transparent |
| 45 | + * @type Boolean |
| 46 | + */ |
| 47 | + this.transparent = game.transparent; |
| 48 | + |
| 49 | + /** |
| 50 | + * Whether the render view should be resized automatically |
| 51 | + * |
| 52 | + * @property autoResize |
| 53 | + * @type Boolean |
| 54 | + */ |
| 55 | + this.autoResize = false; |
| 56 | + |
| 57 | + /** |
| 58 | + * The width of the canvas view |
| 59 | + * |
| 60 | + * @property width |
| 61 | + * @type Number |
| 62 | + * @default 800 |
| 63 | + */ |
| 64 | + this.width = game.width * game.resolution; |
| 65 | + |
| 66 | + /** |
| 67 | + * The height of the canvas view |
| 68 | + * |
| 69 | + * @property height |
| 70 | + * @type Number |
| 71 | + * @default 600 |
| 72 | + */ |
| 73 | + this.height = game.height * game.resolution; |
| 74 | + |
| 75 | + /** |
| 76 | + * The canvas element that everything is drawn to. |
| 77 | + * |
| 78 | + * @property view |
| 79 | + * @type HTMLCanvasElement |
| 80 | + */ |
| 81 | + this.view = game.canvas; |
| 82 | + |
| 83 | + /** |
| 84 | + * The canvas 2d context that everything is drawn with |
| 85 | + * @property context |
| 86 | + * @type CanvasRenderingContext2D |
| 87 | + */ |
| 88 | + this.context = this.view.getContext('2d', { |
| 89 | + alpha: this.transparent |
| 90 | + }); |
| 91 | + |
| 92 | + this.smoothProperty = Phaser.Canvas.getSmoothingPrefix(this.context); |
| 93 | + |
| 94 | + this.roundPixels = false; |
| 95 | + |
| 96 | + var so = 'source-over'; |
| 97 | + |
| 98 | + this.blendModes = [ so, 'lighter', so, so, so, so, so, so, so, so, so, so, so, so, so, so, so ]; |
| 99 | + |
| 100 | + this.currentBlendMode = 0; |
| 101 | + this.currentScaleMode = 0; |
| 102 | + |
| 103 | + if (this.game.device.canUseMultiply) |
| 104 | + { |
| 105 | + this.mapBlendModes(); |
| 106 | + } |
| 107 | + |
| 108 | + this.resize(this.width, this.height); |
| 109 | + |
| 110 | + // this.renderTypes = []; |
| 111 | + // this.renderTypes[Phaser.GROUP] = Phaser.Renderer.Canvas.GameObjects.Container; |
| 112 | + // this.renderTypes[Phaser.SPRITE] = Phaser.Renderer.Canvas.GameObjects.Sprite; |
| 113 | + |
| 114 | +}; |
| 115 | + |
| 116 | +Phaser.Renderer.Canvas.GameObjects = { |
| 117 | + // Populated by the GameObjects classes |
| 118 | +}; |
| 119 | + |
| 120 | +Phaser.Renderer.Canvas.prototype.constructor = Phaser.Renderer.Canvas; |
| 121 | + |
| 122 | +Phaser.Renderer.Canvas.prototype = { |
| 123 | + |
| 124 | + /** |
| 125 | + * Maps Blend modes to Canvas blend modes. |
| 126 | + * |
| 127 | + * @method mapBlendModes |
| 128 | + * @private |
| 129 | + */ |
| 130 | + mapBlendModes: function () |
| 131 | + { |
| 132 | + var modes = Phaser.blendModes; |
| 133 | + |
| 134 | + this.blendModes[modes.MULTIPLY] = 'multiply'; |
| 135 | + this.blendModes[modes.SCREEN] = 'screen'; |
| 136 | + this.blendModes[modes.OVERLAY] = 'overlay'; |
| 137 | + this.blendModes[modes.DARKEN] = 'darken'; |
| 138 | + this.blendModes[modes.LIGHTEN] = 'lighten'; |
| 139 | + this.blendModes[modes.COLOR_DODGE] = 'color-dodge'; |
| 140 | + this.blendModes[modes.COLOR_BURN] = 'color-burn'; |
| 141 | + this.blendModes[modes.HARD_LIGHT] = 'hard-light'; |
| 142 | + this.blendModes[modes.SOFT_LIGHT] = 'soft-light'; |
| 143 | + this.blendModes[modes.DIFFERENCE] = 'difference'; |
| 144 | + this.blendModes[modes.EXCLUSION] = 'exclusion'; |
| 145 | + this.blendModes[modes.HUE] = 'hue'; |
| 146 | + this.blendModes[modes.SATURATION] = 'saturation'; |
| 147 | + this.blendModes[modes.COLOR] = 'color'; |
| 148 | + this.blendModes[modes.LUMINOSITY] = 'luminosity'; |
| 149 | + |
| 150 | + }, |
| 151 | + |
| 152 | + resize: function (width, height) |
| 153 | + { |
| 154 | + this.width = width * this.game.resolution; |
| 155 | + this.height = height * this.game.resolution; |
| 156 | + |
| 157 | + this.view.width = this.width; |
| 158 | + this.view.height = this.height; |
| 159 | + |
| 160 | + if (this.autoResize) |
| 161 | + { |
| 162 | + this.view.style.width = (this.width / this.game.resolution) + 'px'; |
| 163 | + this.view.style.height = (this.height / this.game.resolution) + 'px'; |
| 164 | + } |
| 165 | + |
| 166 | + if (this.smoothProperty) |
| 167 | + { |
| 168 | + this.context[this.smoothProperty] = (this.scaleMode === Phaser.scaleModes.LINEAR); |
| 169 | + } |
| 170 | + |
| 171 | + }, |
| 172 | + |
| 173 | + /** |
| 174 | + * Renders the Phaser.Stage to the canvas view, then iterates through its children. |
| 175 | + * |
| 176 | + * @method render |
| 177 | + * @param stage {Phaser.Stage} |
| 178 | + */ |
| 179 | + render: function (stage) |
| 180 | + { |
| 181 | + this.context.setTransform(1, 0, 0, 1, 0, 0); |
| 182 | + this.context.globalAlpha = 1; |
| 183 | + this.context.globalCompositeOperation = 'source-over'; |
| 184 | + |
| 185 | + this.currentBlendMode = 0; |
| 186 | + this.currentScaleMode = 0; |
| 187 | + |
| 188 | + // Add Pre-render hook |
| 189 | + |
| 190 | + // this.renderSession.currentBlendMode = 0; |
| 191 | + // this.renderSession.shakeX = this.game.camera._shake.x; |
| 192 | + // this.renderSession.shakeY = this.game.camera._shake.y; |
| 193 | + |
| 194 | + // Is this needed any longer? |
| 195 | + /* |
| 196 | + if (navigator.isCocoonJS && this.view.screencanvas) |
| 197 | + { |
| 198 | + this.context.fillStyle = "black"; |
| 199 | + this.context.clear(); |
| 200 | + } |
| 201 | + */ |
| 202 | + |
| 203 | + if (this.clearBeforeRender) |
| 204 | + { |
| 205 | + if (this.transparent) |
| 206 | + { |
| 207 | + this.context.clearRect(0, 0, this.width, this.height); |
| 208 | + } |
| 209 | + else if (stage._bgColor) |
| 210 | + { |
| 211 | + this.context.fillStyle = stage._bgColor.rgba; |
| 212 | + this.context.fillRect(0, 0, this.width , this.height); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + stage.render(this, stage); |
| 217 | + |
| 218 | + // Add Post-render hook |
| 219 | + |
| 220 | + }, |
| 221 | + |
| 222 | + /** |
| 223 | + * This method adds it to the current stack of masks. |
| 224 | + * |
| 225 | + * @method pushMask |
| 226 | + * @param maskData {Object} the maskData that will be pushed |
| 227 | + * @param renderSession {Object} The renderSession whose context will be used for this mask manager. |
| 228 | + */ |
| 229 | + pushMask: function (maskData) |
| 230 | + { |
| 231 | + this.context.save(); |
| 232 | + |
| 233 | + var cacheAlpha = maskData.alpha; |
| 234 | + var transform = maskData.worldTransform; |
| 235 | + |
| 236 | + var resolution = this.game.resolution; |
| 237 | + |
| 238 | + this.context.setTransform( |
| 239 | + transform.a * resolution, |
| 240 | + transform.b * resolution, |
| 241 | + transform.c * resolution, |
| 242 | + transform.d * resolution, |
| 243 | + transform.tx * resolution, |
| 244 | + transform.ty * resolution |
| 245 | + ); |
| 246 | + |
| 247 | + // PIXI.CanvasGraphics.renderGraphicsMask(maskData, context); |
| 248 | + |
| 249 | + this.context.clip(); |
| 250 | + |
| 251 | + maskData.worldAlpha = cacheAlpha; |
| 252 | + |
| 253 | + }, |
| 254 | + |
| 255 | + popMask: function () |
| 256 | + { |
| 257 | + this.context.restore(); |
| 258 | + }, |
| 259 | + |
| 260 | + /** |
| 261 | + * Removes everything from the renderer and optionally removes the Canvas DOM element. |
| 262 | + * |
| 263 | + * @method destroy |
| 264 | + * @param [removeView=true] {boolean} Removes the Canvas element from the DOM. |
| 265 | + */ |
| 266 | + destroy: function () |
| 267 | + { |
| 268 | + // CanvasPool |
| 269 | + |
| 270 | + this.view = null; |
| 271 | + this.context = null; |
| 272 | + this.maskManager = null; |
| 273 | + |
| 274 | + } |
| 275 | + |
| 276 | +}; |
| 277 | + |
0 commit comments