nuke-dimensions
Version:
屏幕参数
88 lines (81 loc) • 2.31 kB
JavaScript
import { isWeex, isWeb } from 'nuke-env';
const FULL_WIDTH = 750;
const NAVBAR_HEIGHT = 64;
const DEFAULT_SCALE = 2;
function getDimensions(landscape = false) {
let dimensions = {};
if (isWeex) {
let weexEnv = typeof WXEnvironment !== 'undefined' ? WXEnvironment : {};
weexEnv.deviceHeight = parseInt(weexEnv.deviceHeight, 10);
weexEnv.deviceWidth = parseInt(weexEnv.deviceWidth, 10);
let scale = parseFloat(weexEnv.scale, 10) || DEFAULT_SCALE;
if (landscape) {
dimensions = {
window: {
width: (weexEnv.deviceHeight * 750 / weexEnv.deviceWidth).toFixed(),
scale: scale,
fontScale: 1
},
screen: {
width: weexEnv.deviceHeight,
height: weexEnv.deviceWidth
}
};
let navbarHeight = 52;
if (scale > 2) {
navbarHeight = 64;
}
dimensions.window.height = (
FULL_WIDTH -
navbarHeight * scale * FULL_WIDTH / weexEnv.deviceWidth
).toFixed();
} else {
dimensions = {
window: {
width: FULL_WIDTH,
height: (
(weexEnv.deviceHeight - NAVBAR_HEIGHT * scale) *
FULL_WIDTH /
weexEnv.deviceWidth
).toFixed(),
scale: scale,
fontScale: 1
},
screen: {
width: weexEnv.deviceWidth,
height: weexEnv.deviceHeight
}
};
}
} else if (isWeb) {
let documentElement = document.documentElement;
dimensions = {
window: {
width: FULL_WIDTH,
height: (
documentElement.clientHeight *
FULL_WIDTH /
documentElement.clientWidth
).toFixed(),
scale: window.devicePixelRatio || DEFAULT_SCALE,
fontScale: 1
}
};
dimensions.screen = { ...window.screen };
dimensions.screen.width = window.screen.width * window.devicePixelRatio;
dimensions.screen.height = window.screen.height * window.devicePixelRatio;
}
return dimensions;
}
export default class Dimensions {
static get(dim) {
return getDimensions()[dim];
}
static getWindowInfo(landscape) {
return getDimensions(landscape)['window'];
}
static getScreenInfo(landscape) {
return getDimensions(landscape)['screen'];
}
}
;