Skip to content

[Question] PixiJS, camera, viewport and scaling #8102

Answered by kaphula
kaphula asked this question in Q&A
Discussion options

You must be logged in to vote

After reading the source code of pixi-viewport I came up with a solution that works well for my purposes. The following function will scale your stage and its contents to the target device window based on the supplied virtual resolution. The virtual resolution determines the scaling factor in pixels and the aspect ratio of the view, which will always remain the same regardless of the target device window dimensions. With center = true the global origin will be always in the middle of the screen.

function fit(center: boolean, stage: PIXI.Container, screenWidth: number, screenHeight: number, virtualWidth: number, virtualHeight: number) {
    stage.scale.x = screenWidth / virtualWidth
    stage

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@codergautam
Comment options

Answer selected by kaphula
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #8101 on January 14, 2022 13:18.