25 lines
761 B
TypeScript
Raw Normal View History

2021-01-19 22:30:12 +08:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class CanvasAdapt extends cc.Component {
protected onLoad() {
this.adapt();
// 仅web有效
cc.view.setResizeCallback(() => {
this.adapt();
});
}
private adapt() {
let resolutionRatio = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height;
let ratio = cc.winSize.width / cc.winSize.height;
if (ratio > resolutionRatio) {
cc.Canvas.instance.fitHeight = true;
cc.Canvas.instance.fitWidth = false;
} else {
cc.Canvas.instance.fitHeight = false;
cc.Canvas.instance.fitWidth = true;
}
}
}