Viewport class abstract

A viewport is a class that potentially translates and resizes the screen. The reason you might want to have a viewport is to make sure you handle any screen size and resolution correctly depending on your needs.

Not only screens can have endless configurations of width and height with different ratios, you can also embed games as widgets within a Flutter app. In fact, the size of the game can even change dynamically (if the layout changes or in desktop, for example).

For some simple games, that is not an issue. The game will just adapt to fit the screen, so if the game world is 1:1 with screen it will just be bigger or smaller. But if you want a consistent experience across platforms and players, you should use a viewport.

When using a viewport, resize should be called by the engine with the raw canvas size (on startup and subsequent resizes) and that will configure effectiveSize and canvasSize.

The Viewport can also apply an offset to render and clip the canvas adding borders (clipping) when necessary. When rendering, call render and put all your rendering inside the lambda so that the correct transformations are applied.

You can think of a Viewport as mechanism to watch a wide-screen movie on a square monitor. You can stretch the movie to fill the square, but the width and height will be stretched by different amounts, causing distortion. You can fill in the smallest dimension and crop the biggest (that causes cropping). Or you can fill in the biggest and add black bars to cover the unused space on the smallest (this is the FixedResolutionViewport).

The other option is to not use a viewport (DefaultViewport) and have your game dynamically render itself to fill in the existing space (basically this means generating either a wide-screen or a square movie on the fly). The disadvantage is that different players on different devices will see different games. For example a hidden door because it's too far away to render in Screen 1 might be visible on Screen 2. Specially if it's an online/competitive game, it can give unfair advantages to users with certain screen resolutions. If you want to "play director" and know exactly what every player is seeing at every time, you should use a Viewport.

Inheritance
Implementers

Constructors

Viewport()

Properties

canvasSize Vector2?
This returns the real widget size (well actually the logical Flutter size of your widget). This is the raw canvas size as it would be without any viewport.
getter/setter pair
effectiveSize Vector2
This returns the effective size, after viewport transformation. This is not the game widget size but for all intents and purposes, inside your game, this size should be used as the real one.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

apply(Canvas c) → void
Applies to the Canvas all necessary transformations to apply this viewport.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
projectVector(Vector2 worldCoordinates) Vector2
Converts a vector in the world space to the screen space.
inherited
render(Canvas c, void renderGame(Canvas)) → void
This transforms the canvas so that the coordinate system is viewport- -aware. All your rendering logic should be put inside the lambda.
resize(Vector2 newCanvasSize) → void
This configures the viewport with a new raw canvas size. It should immediately affect effectiveSize and canvasSize. This must be called by the engine at startup and also whenever the size changes.
scaleVector(Vector2 worldCoordinates) Vector2
Converts a vector representing a delta in the world space to the screen space.
inherited
toString() String
A string representation of this object.
inherited
unprojectVector(Vector2 screenCoordinates) Vector2
Converts a vector in the screen space to the world space.
inherited
unscaleVector(Vector2 screenCoordinates) Vector2
Converts a vector representing a delta in the screen space to the world space.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited