| 21 | * Handles scaling the game |
| 22 | */ |
| 23 | export class SafeScaleManager { |
| 24 | /** |
| 25 | * Creates an instance of SafeScaleManager. |
| 26 | * @param {object} param |
| 27 | * @param {Number} param.width width of game |
| 28 | * @param {Number} param.height height of game |
| 29 | * @param {Number} param.safeWidth width of safe area for the game |
| 30 | * @param {Number} param.safeHeight height of safe area for the game |
| 31 | * @param {ScaleCallback} param.callback function called to scale game and canvas |
| 32 | * @memberof SafeScaleManager |
| 33 | */ |
| 34 | constructor({ |
| 35 | width, |
| 36 | height, |
| 37 | safeWidth = Infinity, |
| 38 | safeHeight = Infinity, |
| 39 | callback = () => {} |
| 40 | }) { |
| 41 | this.gameWidth = width; |
| 42 | this.gameHeight = height; |
| 43 | this.safeWidth = safeWidth <= width ? safeWidth : width; |
| 44 | this.safeHeight = safeHeight <= height ? safeHeight : height; |
| 45 | this.callback = callback; |
| 46 | this.scaleRatio = 1; |
| 47 | |
| 48 | // Rectangle containing the total viewable game content. |
| 49 | this.viewArea = { |
| 50 | x: 0, |
| 51 | y: 0, |
| 52 | width: 0, |
| 53 | height: 0, |
| 54 | left: 0, |
| 55 | right: 0, |
| 56 | top: 0, |
| 57 | bottom: 0 |
| 58 | }; |
| 59 | |
| 60 | /** @type {ScaledEntity[]} */ |
| 61 | this.entities = []; |
| 62 | |
| 63 | /** @private */ |
| 64 | this.resizer = new ResizeHelper(this.onResize.bind(this)); |
| 65 | |
| 66 | if (callback instanceof Function) { |
| 67 | this.enable(callback); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * onResize maps and passes the relevant data to the user provided callback function. |
| 73 | * @param {object} param |
| 74 | * @param {number} param.width - Current window width |
| 75 | * @param {number} param.height - Current window height |
| 76 | * @private |
| 77 | */ |
| 78 | onResize({ width, height }) { |
| 79 | // Calculate the scaling ratio. |
| 80 | this.scaleRatio = Math.min(width / this.safeWidth, height / this.safeHeight); |
nothing calls this directly
no outgoing calls
no test coverage detected