Last active
May 4, 2019 15:30
-
-
Save korinVR/207893362b8833c93208a67f1069c51f to your computer and use it in GitHub Desktop.
Resizing the PixiJS stage according to the page size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as PIXI from 'pixi.js' | |
import backgroundImageURL from '/assets/images/background.jpg' | |
const application = new PIXI.Application() | |
const screenW = 1024 | |
const screenH = 1024 | |
const resize = () => { | |
const clientW = application.view.parentNode.clientWidth | |
const clientH = application.view.parentNode.clientHeight | |
const scale = (clientW < clientH) ? clientW / screenW : clientH / screenH | |
application.renderer.resize(clientW, clientH) | |
application.stage.x = clientW / 2 | |
application.stage.y = clientH / 2 | |
application.stage.scale.x = scale | |
application.stage.scale.y = scale | |
} | |
document.body.appendChild(application.view) | |
// resize() | |
application.loader.add('background', backgroundImageURL).load((loader, resources) => { | |
const background = new PIXI.Sprite(resources.background.texture) | |
background.anchor.x = 0.5 | |
background.anchor.y = 0.5 | |
background.scale.x = screenW / 670 | |
background.scale.y = screenH / 670 | |
application.stage.addChild(background) | |
application.ticker.add(delta => { | |
resize() | |
}) | |
}) | |
// window.addEventListener('resize', resize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment