Skip to content

Instantly share code, notes, and snippets.

@JetLua
Last active June 9, 2024 14:55
Show Gist options
  • Save JetLua/c7b492792f7b52a949b5b9ba6bfbcb82 to your computer and use it in GitHub Desktop.
Save JetLua/c7b492792f7b52a949b5b9ba6bfbcb82 to your computer and use it in GitHub Desktop.
pixijs 屏幕自适应
const resize = () => {
let
w = window.innerWidth,
h = window.innerHeight,
l, t
// 横屏
if (window.innerWidth / window.innerHeight >= 1) {
view.style.transform = 'rotate(0)'
window.GAME_ROTATE = 0
if (window.innerWidth / window.innerHeight >= GAME_RATIO) {
w = h * GAME_RATIO
} else {
h = w / GAME_RATIO
}
h < window.innerHeight ? t = (window.innerHeight - h) >> 1 : t = 0
w < window.innerWidth ? l = (window.innerWidth - w) >> 1 : l = 0
} else {
view.style.transform = 'rotate(90deg)'
window.GAME_ROTATE = 90
w = window.innerHeight
h = window.innerWidth
if (window.innerHeight / window.innerWidth >= GAME_RATIO) {
w = h * GAME_RATIO
} else {
h = w / GAME_RATIO
}
h < window.innerWidth ? l = h + ((window.innerWidth - h) >> 1) : l = h
w < window.innerHeight ? t = (window.innerHeight - w) >> 1 : t = 0
}
view.style.top = `${t}px`
view.style.left = `${l}px`
view.style.width = `${w}px`
view.style.height = `${h}px`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment