pixi-viewport
pixi-viewport copied to clipboard
Position center of viewport
Looking for advice on the appropriate API to use to center the viewport to a specific location, and a specific zoom level.
fitWidth is getting me close to what I need, however the centered position I require is in negative Y.
Attempting to call moveCenter(2.5, -2.5) seems to break the positioning.
Hmmm...that's weird. moveCenter() works fine with negative values. You're doing it the right way: fitWidth() and moveCenter() (in either order). Can you provide more details on the center not working properly?
Many times it's caused by screenWidth/screenHeight not being set correctly on initialize.
screenWidth and screenHeight are initialized to the containing html element's "clientWidth" and "clientHeight".
Could you provide a quick sample of positioning the center of the viewport to a negative Y value?
sorry, this got lost. did you figure it out? viewport.moveCenter(-100, -100) should work without problems.
I have a similar problem, but I don't understand how to solve it:
- I draw a network visualization
- I set the zoom to fit it in the screen with .setZoom(.5)
- I don't understand how to center it
I've checked .scaled() but it seems just to retrieve the scale.
Can you help me?
This is the weblink: https://rodighiero.github.io/COVID-19/
Call viewport.moveCenter(x, y) where (x, y) is the world coordinates that you want at the center of the screen. Two coordinate systems: (1) the screen coordinates: this is the screen with 0, 0 at the top-left of the screen, and (2) the world coordinates: this is relative to the viewport container and includes your "world" elements, ie, all children of your viewport. This is moved by viewport.
So say you have a rectangle at (0, 0, 100, 100) and you want that box in the middle of the screen, you would call viewport.moveCenter(50, 50).
(This is assuming your viewport is full screen. If it's partial, think about "screen" as your canvas.
That's perfect too, thank you!
Thanks for the info here. Another way to center and fit the viewport is: viewport.setTransform(0, 0, 1, 1).fit(true)