The Viewport manages projection and unprojection of coordinates between world and viewport
coordinates.
It provides both direct project/unproject function members as well as
projection matrices including view and projection matrices, and
can generate their inverses as well to facilitate e.g. lighting calculations
in WebGL shaders.
Remarks:
- The
Viewportclass perhaps best thought of as the counterpart of the typicalCameraclass found in most 3D libraries. - The main addition is that to support pixel project/unproject functions
(in addition to the clipspace projection that Camera classes typically manage),
the
Viewportis also aware of the viewport extents. - Also, it can generate WebGL compatible projection matrices (column-major) - Note that these still need to be converted to typed arrays.
Parameters:
props(Object) - Viewport propertiesprops.width(Number) - Width of "viewport" or window. Default to1.props.height(Number) - Height of "viewport" or window. Default to1.props.viewMatrix(Array[16], optional) - View matrix. Default to identidy matrix.props.projectionMatrix(Array[16], optional) - Projection matrix. Default to identidy matrix.
const viewport = new Viewport({width: 500, height: 500});Parameters:
viewport(Viewport) - The viewport to compare with.
Returns:
trueif the given viewport is identical to the current one.
Projects latitude, longitude (and altitude) to pixel coordinates in window using viewport projection parameters.
Parameters:
coordinates(Array) -[lng, lat, altitude]Passing an altitude is optional.opts(Object)topLeft(Boolean, optional) - Whether projected coords are top left. Default totrue.
Returns:
- A screen coordinates array
[x, y]or[x, y, z]if an altitude was given.
Note: By default, returns top-left coordinates for canvas/SVG type render
Unproject pixel coordinates on screen onto [lng, lat, altitude] on map.
Parameters:
pixels(Array) -[x, y, z]Passing azis optional.opts(Object)topLeft(Boolean, optional) - Whether projected coords are top left. Default totrue.
Returns:
- A map coordinates array
[lng, lat]or[lng, lat, altitude]if azwas given.
Note: By default, takes top-left coordinates from JavaScript mouse events.