MCPcopy Create free account
hub / github.com/melonjs/melonJS / onresize

Function onresize

packages/melonjs/src/application/resize.ts:40–116  ·  view source on GitHub ↗
(game: Application)

Source from the content-addressed store, hash-verified

38 * @param game - the game application instance triggering the resize
39 */
40export function onresize(game: Application): void {
41 const renderer = game.renderer;
42 const settings = renderer.settings as any;
43 let scaleX = settings.scale,
44 scaleY = settings.scale;
45 let nodeBounds;
46
47 if (settings.autoScale) {
48 // set max the canvas max size if CSS values are defined
49 let canvasMaxWidth = Infinity;
50 let canvasMaxHeight = Infinity;
51
52 if (globalThis.getComputedStyle) {
53 const style = globalThis.getComputedStyle(renderer.getCanvas(), null);
54 canvasMaxWidth = parseInt(style.maxWidth, 10) || Infinity;
55 canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity;
56 }
57
58 if (typeof game.settings.scaleTarget !== "undefined") {
59 // get the bounds of the given scale target
60 nodeBounds = device.getElementBounds(game.settings.scaleTarget);
61 } else {
62 // get the maximum canvas size within the parent div containing the canvas container
63 nodeBounds = device.getParentBounds(game.getParentElement());
64 }
65
66 const _max_width = Math.min(canvasMaxWidth, nodeBounds.width);
67 const _max_height = Math.min(canvasMaxHeight, nodeBounds.height);
68
69 // calculate final canvas width & height
70 const screenRatio = _max_width / _max_height;
71
72 if (
73 (settings.scaleMethod === "fill-min" &&
74 screenRatio > renderer.designRatio) ||
75 (settings.scaleMethod === "fill-max" &&
76 screenRatio < renderer.designRatio) ||
77 settings.scaleMethod === "flex-width"
78 ) {
79 // resize the display canvas to fill the parent container
80 const sWidth = Math.min(canvasMaxWidth, settings.height * screenRatio);
81 scaleX = scaleY = _max_width / sWidth;
82 renderer.resize(Math.floor(sWidth), settings.height);
83 } else if (
84 (settings.scaleMethod === "fill-min" &&
85 screenRatio < renderer.designRatio) ||
86 (settings.scaleMethod === "fill-max" &&
87 screenRatio > renderer.designRatio) ||
88 settings.scaleMethod === "flex-height"
89 ) {
90 // resize the display canvas to fill the parent container
91 const sHeight = Math.min(
92 canvasMaxHeight,
93 settings.width * (_max_height / _max_width),
94 );
95 scaleX = scaleY = _max_height / sHeight;
96 renderer.resize(settings.width, Math.floor(sHeight));
97 } else if (settings.scaleMethod === "flex") {

Callers 2

initMethod · 0.90
resizeMethod · 0.90

Calls 5

getCanvasMethod · 0.80
getParentElementMethod · 0.80
scaleFunction · 0.70
floorMethod · 0.65
resizeMethod · 0.45

Tested by

no test coverage detected