sweet/assets/scriptes/adjustsize.js
2020-08-07 09:55:56 +08:00

67 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

cc.Class({
extends: cc.Component,
properties:{
fixWidth: 480,
fixHeight: 960,
maxHeight: 1100,
whRatio: 1.48,
},
onLoad(){
this.adjustScreenFixedHeight();
// 横竖屏切换调用.
// var self = this;
// cc.view.setResizeCallback(function(){
// self.adjustScreenFixedHeight();
// });
},
// 桌面浏览器调整固定高度.
adjustScreenFixedHeight:function(){
var cv = this.node.getComponent(cc.Canvas);
var window_height = cc.winSize.height;
var window_width = cc.winSize.width;
if(window_width<1) window_width =1;
var width = this.fixWidth;
var height = this.fixHeight;
// 手机浏览器游戏高度1081
var cmpRatio = this.whRatio; //(1080)/720; qq 浏览器去掉上下部分比例
//window_height/window_width>= cmpRatio&&
if(cc.sys.isMobile&&window_height/window_width >= cmpRatio){
//if(window_height/window_width >= cmpRatio){
width = this.fixWidth;
height = width*window_height/window_width;
if(height>=this.maxHeight) height = this.maxHeight; //iphonex
cv.fitHeight = false;
cv.fitWidth = true;
}else{
cv.fitHeight = true;
cv.fitWidth = false;
}
cv.designResolution = cc.size(width,height);
this.setNodeSize(); //动态修改屏幕
if(cv.fitHeight){
console.log("cv.fitHeight......................................");
}
if(cv.fitWidth){
console.log("cv.fitWidth......................................");
}
if (CC_DEBUG) {
console.log("adjust size width "+width +" height"+height +" window_height/window_width:"+window_height/window_width);
console.log("cc.sys.platform :"+ cc.sys.platform);
console.log("cc.sys.MOBILE_BROWSER :"+cc.sys.MOBILE_BROWSER);
console.log("cc.sys.browserType :"+cc.sys.browserType);
console.log("cc.sys.DESKTOP_BROWSER :"+cc.sys.DESKTOP_BROWSER);
console.log("adjust window size : (" + cc.winSize.width.toFixed(2)+" "+cc.winSize.height.toFixed(2)+")" +" screen size :"+ cv.designResolution);
}
},
setNodeSize:function(){
var design = this.node.getComponent(cc.Canvas).designResolution;
this.node.setContentSize(design);
},
});