A 2D camera representation attached to h2d.Scene
.
Enables ability to move, scale and rotate the scene viewport.
Scene supports usage of multiple Camera instances.
To configure which layers each Camera renders - Camera.layerVisible
method should be overridden.
By default, camera does not clip out the contents that are outside camera bounding box, which can be enabled through Camera.clipViewport
.
Due to Heaps event handling structure, only one Camera instance can handle the mouse/touch input, and can be set through h2d.Scene.interactiveCamera
variable.
Note that during even handing, interactive camera does not check if the Camera itself is visible nor the layers filters as well as clipViewport
is not applied.
Constructor
Variables
anchorX:Float
Horizontal anchor position inside viewport boundaries used for positioning and resize compensation. ( default : 0 )
Value is a percentile (0..1) from left viewport edge to right viewport edge with 0.5 being center.
anchorY:Float
Vertical anchor position inside viewport boundaries used for positioning and resize compensation. ( default : 0 )
Value is a percentile (0..1) from top viewport edge to bottom viewport edge with 0.5 being center.
clipViewport:Bool
Enables viewport clipping. Allow to restrict rendering area of the camera to the viewport boundaries.
Does not affect the user input when Camera is set as interactive camera.
followRotation:Bool = false
Enables h2d.Object.rotation
sync between Camera.follow
object and Camera.
scaleX:Float
Horizontal scale factor of the camera. Scaling applied, using anchored position as pivot.
viewportHeight:Float
Camera viewport height in scene coordinates. ( default: scene.height )
Automatically scales on scene resize.
viewportWidth:Float
Camera viewport width in scene coordinates. ( default : scene.width )
Automatically scales on scene resize.
viewportX:Float
Horizontal viewport offset of the camera relative to internal scene viewport (see h2d.Scene.scaleMode
) in scene coordinates. ( default : 0 )
Automatically scales on scene resize.
viewportY:Float
Vertical viewport offset of the camera relative to internal scene viewport (see h2d.Scene.scaleMode
) in scene coordinates. ( default : 0 )
Automatically scales on scene resize.
visible:Bool
Camera visibility.
Does not affect the user input when Camera is set as interactive camera.
Methods
cameraToScene(pt:Point):Void
Convert local camera position into absolute scene position.
Does not represent screen position, see Camera.cameraToScreen
to convert position with accounting of scaleMode
.
Requires Camera being attached to a Scene.
cameraToScreen(pt:Point):Void
Convert local camera position to absolute screen position.
Requires Camera being attached to a Scene.
dynamiclayerVisible(layer:Int):Bool
Override this method to set visibility only to specific layers. Renders all layers by default.
Does not affect the user input when Camera is set as interactive camera.
Usage example:
final LAYER_SHARED = 0;
final LAYER_PLAYER_1 = 2;
final LAYER_PLAYER_2 = 3;
final LAYER_UI = 4;
// Set first camera to only render shared layer and one that only visible to player 1.
s2d.camera.layerVisible = (layer) -> layer == LAYER_SHARED || layer == LAYER_PLAYER_1;
var player2 = new h2d.Camera(s2d);
// Set second camera to only render shared layer and one that only visible to player 2.
player2.layerVisible = (layer) -> layer == LAYER_SHARED || layer == LAYER_PLAYER_2;
var ui = new h2d.Camera(s2d);
// Set last camera to only render UI layer.
ui.layerVisible = (layer) -> layer == LAYER_UI;
Parameters:
layer | The rendered layer index in |
---|
Returns:
true
if layer can be rendered, false
otherwise.
inlinerotate(angle:Float):Void
Rotates the camera relative to current rotation by given angle
in radians.
sceneToCamera(pt:Point):Void
Convert an absolute scene position into a local camera position.
Does not represent screen position, see Camera.screenToCamera
to convert position with accounting of scaleMode
.
Requires Camera being attached to a Scene.
screenToCamera(pt:Point):Void
Convert screen position into a local camera position.
Requires Camera being attached to a Scene.
inlinesetRawViewport(x:Float = 0, y:Float = 0, w:Float = 1, h:Float = 1):Void
Sets camera viewport dimensions in raw format of 0..1 percentiles.
inlinesetViewport(x:Float = 0, y:Float = 0, w:Float = 0, h:Float = 0):Void
Sets camera viewport dimensions. If w
or h
arguments are 0 - scene size is used (width or height respectively).
Requires Camera being attached to a Scene.