* onResize maps and passes the relevant data to the user provided callback function. * @param {object} param * @param {number} param.width - Current window width * @param {number} param.height - Current window height * @private
({ width, height })
| 76 | * @private |
| 77 | */ |
| 78 | onResize({ width, height }) { |
| 79 | // Calculate the scaling ratio. |
| 80 | this.scaleRatio = Math.min(width / this.safeWidth, height / this.safeHeight); |
| 81 | |
| 82 | const nWidth = Math.max(0, Math.min(this.gameWidth * this.scaleRatio, width)); |
| 83 | const nHeight = Math.max(0, Math.min(this.gameHeight * this.scaleRatio, height)); |
| 84 | |
| 85 | const scale = { |
| 86 | x: (this.gameWidth / nWidth) * this.scaleRatio, |
| 87 | y: (this.gameHeight / nHeight) * this.scaleRatio |
| 88 | }; |
| 89 | |
| 90 | const scaledWidth = width / this.scaleRatio; |
| 91 | const scaledHeight = height / this.scaleRatio; |
| 92 | |
| 93 | this.viewArea.left = Math.max(-(scaledWidth - this.gameWidth) * 0.5, 0); |
| 94 | this.viewArea.top = Math.max(-(scaledHeight - this.gameHeight) * 0.5, 0); |
| 95 | this.viewArea.right = Math.min(this.viewArea.left + scaledWidth, this.gameWidth); |
| 96 | this.viewArea.bottom = Math.min(this.viewArea.top + scaledHeight, this.gameHeight); |
| 97 | |
| 98 | this.viewArea.x = this.viewArea.left; |
| 99 | this.viewArea.y = this.viewArea.top; |
| 100 | this.viewArea.width = this.viewArea.right - this.viewArea.left; |
| 101 | this.viewArea.height = this.viewArea.bottom - this.viewArea.top; |
| 102 | |
| 103 | /** @type {EntityResizeEvent} */ |
| 104 | this.resizeEventData = Object.freeze({ |
| 105 | offset: { x: this.viewArea.x, y: this.viewArea.y }, |
| 106 | gameSize:{ x: this.gameWidth, y: this.gameHeight }, |
| 107 | viewArea: this.viewArea, |
| 108 | scale |
| 109 | }); |
| 110 | |
| 111 | this.callback({ |
| 112 | width: nWidth, |
| 113 | height: nHeight, |
| 114 | scaleRatio: this.scaleRatio, |
| 115 | viewArea: this.viewArea, |
| 116 | scale |
| 117 | }); |
| 118 | |
| 119 | for (let i = 0, length = this.entities.length; i < length; i++) { |
| 120 | const entity = this.entities[i]; |
| 121 | entity.onResize(this.resizeEventData); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Calculates the offset for anchors. |