|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Takes a reference to the Canvas Renderer, a Canvas Rendering Context, a Game Object, a Camera and a parent matrix |
| 9 | + * and then performs the following steps: |
| 10 | + * |
| 11 | + * 1) Checks the alpha of the source combined with the Camera alpha. If 0 or less it aborts. |
| 12 | + * 2) Takes the Camera and Game Object matrix and multiplies them, combined with the parent matrix if given. |
| 13 | + * 3) Sets the blend mode of the context to be that used by the Game Object. |
| 14 | + * 4) Sets the alpha value of the context to be that used by the Game Object combined with the Camera. |
| 15 | + * 5) Saves the context state. |
| 16 | + * 6) Sets the final matrix values into the context via setTransform. |
| 17 | + * |
| 18 | + * This function is only meant to be used internally. Most of the Canvas Renderer classes use it. |
| 19 | + * |
| 20 | + * @function Phaser.Renderer.Canvas.SetTransform |
| 21 | + * @since 3.12.0 |
| 22 | + * |
| 23 | + * @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer. |
| 24 | + * @param {Phaser.GameObjects.GameObject} src - The Game Object being rendered. Can be any type that extends the base class. |
| 25 | + * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object. |
| 26 | + * @param {Phaser.GameObjects.Components.TransformMatrix} [parentMatrix] - A parent transform matrix to apply to the Game Object before rendering. |
| 27 | + * |
| 28 | + * @return {boolean} `true` if the Game Object context was set, otherwise `false`. |
| 29 | + */ |
| 30 | +var SetTransform = function (renderer, ctx, src, camera, parentMatrix) |
| 31 | +{ |
| 32 | + var alpha = camera.alpha * src.alpha; |
| 33 | + |
| 34 | + if (alpha <= 0) |
| 35 | + { |
| 36 | + // Nothing to see, so don't waste time calculating stuff |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + var camMatrix = renderer._tempMatrix1.copyFromArray(camera.matrix.matrix); |
| 41 | + var gameObjectMatrix = renderer._tempMatrix2.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY); |
| 42 | + var calcMatrix = renderer._tempMatrix3; |
| 43 | + |
| 44 | + if (parentMatrix) |
| 45 | + { |
| 46 | + // Multiply the camera by the parent matrix |
| 47 | + camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY); |
| 48 | + |
| 49 | + // Undo the camera scroll |
| 50 | + gameObjectMatrix.e = src.x; |
| 51 | + gameObjectMatrix.f = src.y; |
| 52 | + |
| 53 | + // Multiply by the Sprite matrix, store result in calcMatrix |
| 54 | + camMatrix.multiply(gameObjectMatrix, calcMatrix); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + gameObjectMatrix.e -= camera.scrollX * src.scrollFactorX; |
| 59 | + gameObjectMatrix.f -= camera.scrollY * src.scrollFactorY; |
| 60 | + |
| 61 | + // Multiply by the Sprite matrix, store result in calcMatrix |
| 62 | + camMatrix.multiply(gameObjectMatrix, calcMatrix); |
| 63 | + } |
| 64 | + |
| 65 | + // Blend Mode |
| 66 | + ctx.globalCompositeOperation = renderer.blendModes[src.blendMode]; |
| 67 | + |
| 68 | + // Alpha |
| 69 | + ctx.globalAlpha = alpha; |
| 70 | + |
| 71 | + ctx.save(); |
| 72 | + |
| 73 | + calcMatrix.setToContext(ctx); |
| 74 | + |
| 75 | + return true; |
| 76 | +}; |
| 77 | + |
| 78 | +module.exports = SetTransform; |
0 commit comments