Skip to content

Instantly share code, notes, and snippets.

@devwithzachary
Created September 10, 2021 13:45
Laya 1.x screen size fix
window.getAdapterInfo = function (config) {
let scaleX = 1;
let scaleY = 1;
let vw = window.innerWidth;
let vh = window.innerHeight;
let w = config.width;
let h = config.height;
config.scaleMode = config.scaleMode.toLowerCase();
switch (config.scaleMode) {
case "exactfit":
scaleX = vw / w;
scaleY = vh / h;
break;
case "fixedwidth":
scaleX = scaleY = vw / w;
break;
}
return {
scaleX: scaleX,
scaleY: scaleY,
w: w,
h: h,
vw: vw,
vh: vh,
rw: w * scaleX,
rh: h * scaleY
};
};
Laya.init(600, 400, WebGL);// Replace 600 and 400 with your game's width and height.
Laya.stage.scaleMode = "exactfit";// exactfit is used as an example only. Replace it with the actual value in your game.
// Screen adaptation starts.
if(typeof getAdapterInfo !== "undefined"){
var stage = Laya.stage;
var info = getAdapterInfo({width:600, height:400, scaleMode:"exactfit"});// Replace 600 and 400 with your game's width and height.
stage.designWidth = info.w;
stage.designHeight = info.h;
stage.width = info.rw;
stage.height = info.rh;
stage.scale(info.scaleX, info.scaleY);
}
// Screen adaptation ends.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment