|
| 1 | +import * as device from "./../../system/device.js"; |
| 2 | +import game from "./../../game.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * scale the "displayed" canvas by the given scalar. |
| 6 | + * this will modify the size of canvas element directly. |
| 7 | + * Only use this if you are not using the automatic scaling feature. |
| 8 | + * @param {number} x - x scaling multiplier |
| 9 | + * @param {number} y - y scaling multiplier |
| 10 | + */ |
| 11 | +function scale(renderer, x, y) { |
| 12 | + var canvas = renderer.getCanvas(); |
| 13 | + var context = renderer.getContext(); |
| 14 | + var settings = renderer.settings; |
| 15 | + var pixelRatio = device.devicePixelRatio; |
| 16 | + |
| 17 | + var w = settings.zoomX = canvas.width * x * pixelRatio; |
| 18 | + var h = settings.zoomY = canvas.height * y * pixelRatio; |
| 19 | + |
| 20 | + // update the global scale variable |
| 21 | + renderer.scaleRatio.set(x * pixelRatio, y * pixelRatio); |
| 22 | + |
| 23 | + // adjust CSS style based on device pixel ratio |
| 24 | + canvas.style.width = (w / pixelRatio) + "px"; |
| 25 | + canvas.style.height = (h / pixelRatio) + "px"; |
| 26 | + |
| 27 | + // if anti-alias and blend mode were resetted (e.g. Canvas mode) |
| 28 | + renderer.setAntiAlias(context, settings.antiAlias); |
| 29 | + renderer.setBlendMode(settings.blendMode, context); |
| 30 | + |
| 31 | + // force repaint |
| 32 | + game.repaint(); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * callback for window resize event |
| 37 | + */ |
| 38 | +export function onresize(renderer) { |
| 39 | + var settings = renderer.settings; |
| 40 | + var scaleX = 1, scaleY = 1; |
| 41 | + |
| 42 | + if (settings.autoScale) { |
| 43 | + |
| 44 | + // set max the canvas max size if CSS values are defined |
| 45 | + var canvasMaxWidth = Infinity; |
| 46 | + var canvasMaxHeight = Infinity; |
| 47 | + |
| 48 | + if (globalThis.getComputedStyle) { |
| 49 | + var style = globalThis.getComputedStyle(renderer.getCanvas(), null); |
| 50 | + canvasMaxWidth = parseInt(style.maxWidth, 10) || Infinity; |
| 51 | + canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity; |
| 52 | + } |
| 53 | + |
| 54 | + // get the maximum canvas size within the parent div containing the canvas container |
| 55 | + var nodeBounds = device.getParentBounds(game.getParentElement()); |
| 56 | + |
| 57 | + var _max_width = Math.min(canvasMaxWidth, nodeBounds.width); |
| 58 | + var _max_height = Math.min(canvasMaxHeight, nodeBounds.height); |
| 59 | + |
| 60 | + // calculate final canvas width & height |
| 61 | + var screenRatio = _max_width / _max_height; |
| 62 | + |
| 63 | + if ((settings.scaleMethod === "fill-min" && screenRatio > renderer.designRatio) || |
| 64 | + (settings.scaleMethod === "fill-max" && screenRatio < renderer.designRatio) || |
| 65 | + (settings.scaleMethod === "flex-width") |
| 66 | + ) { |
| 67 | + // resize the display canvas to fill the parent container |
| 68 | + var sWidth = Math.min(canvasMaxWidth, settings.height * screenRatio); |
| 69 | + scaleX = scaleY = _max_width / sWidth; |
| 70 | + renderer.resize(Math.floor(sWidth), settings.height); |
| 71 | + } |
| 72 | + else if ((settings.scaleMethod === "fill-min" && screenRatio < renderer.designRatio) || |
| 73 | + (settings.scaleMethod === "fill-max" && screenRatio > renderer.designRatio) || |
| 74 | + (settings.scaleMethod === "flex-height") |
| 75 | + ) { |
| 76 | + // resize the display canvas to fill the parent container |
| 77 | + var sHeight = Math.min(canvasMaxHeight, settings.width * (_max_height / _max_width)); |
| 78 | + scaleX = scaleY = _max_height / sHeight; |
| 79 | + renderer.resize(settings.width, Math.floor(sHeight)); |
| 80 | + } |
| 81 | + else if (settings.scaleMethod === "flex") { |
| 82 | + // resize the display canvas to fill the parent container |
| 83 | + renderer.resize(Math.floor(_max_width), Math.floor(_max_height)); |
| 84 | + } |
| 85 | + else if (settings.scaleMethod === "stretch") { |
| 86 | + // scale the display canvas to fit with the parent container |
| 87 | + scaleX = _max_width / settings.width; |
| 88 | + scaleY = _max_height / settings.height; |
| 89 | + } |
| 90 | + else { |
| 91 | + // scale the display canvas to fit the parent container |
| 92 | + // make sure we maintain the original aspect ratio |
| 93 | + if (screenRatio < renderer.designRatio) { |
| 94 | + scaleX = scaleY = _max_width / settings.width; |
| 95 | + } |
| 96 | + else { |
| 97 | + scaleX = scaleY = _max_height / settings.height; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // adjust scaling ratio based on the new scaling ratio |
| 102 | + scale(renderer, scaleX, scaleY); |
| 103 | + } else { |
| 104 | + // adjust scaling ratio based on the given settings |
| 105 | + scale(renderer, settings.scale, settings.scale); |
| 106 | + } |
| 107 | +} |
0 commit comments