嗨,请帮我找出如何使用Phaser3创建真正响应式的游戏。
响应性很关键,因为游戏(Blockly工作区的表示层)应该能够在会话期间多次扩展到屏幕上的较大部分并缩小回来。
问题是我如何在运行时更改游戏的尺寸?
---编辑---
事实证明有纯CSS解决方案,可以使用CSS缩放属性来调整画布大小。在浏览器中运行良好(对性能没有明显影响),在cordova应用程序(Android)中也能正常运行。
如果CSS缩放可能会破坏某些东西,则Richard Davey的答案如下:
老实说,我从来没有尝试过。试一下看看会发生什么!它可能会破坏输入,或者它可能会继续工作。虽然它可能会破坏比例管理器,但除此之外,没有其他东西会受到太大关注。
// is size of html element size that needed to fit
let props = { w: 1195, h: 612, elementId: 'myGame' };
// need to know game fixed size
let gameW = 1000, gameH = 750;
// detect zoom ratio from width or height
let isScaleWidth = gameW / gameH > props.w / props.h ? true : false;
// find zoom ratio
let zoom = isScaleWidth ? props.w / gameW : props.h / gameH;
// get DOM element, props.elementId is parent prop from Phaser game config
let el = document.getElementById(props.elementId);
// set css zoom of canvas element
el.style.zoom = zoom;