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

new(?scene:Scene)

Create a new Camera instance and attach to the given scene.

Parameters:

scene

Optional owner Scene to which camera auto-attaches to. Note that when Camera is not attached to the Scene, a number of methods would lead to an error if called.

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.

follow:Object

Makes camera to follow the referenced Object position.

@:value(false)followRotation:Bool = false

Enables h2d.Object.rotation sync between Camera.follow object and Camera.

rotation:Float

Rotation of the camera in radians. Camera is rotated around anchored position.

scaleX:Float

Horizontal scale factor of the camera. Scaling applied, using anchored position as pivot.

scaleY:Float

Vertical 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.

x:Float

X position of the camera in world space based on anchorX.

y:Float

Y position of the camera in world space based on anchorY.

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 h2d.Scene.

Returns:

true if layer can be rendered, false otherwise.

inlinemove(dx:Float, dy:Float):Void

Moves the camera position by given dx and dy.

inlineremove():Void

Detaches Camera from the Scene it currently attached to.

inlinerotate(angle:Float):Void

Rotates the camera relative to current rotation by given angle in radians.

inlinescale(x:Float, y:Float):Void

Multiplies the Camera.scaleX by x and Camera.scaleY by y.

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.

inlinesetAnchor(x:Float, y:Float):Void

Sets the Camera.anchorX and Camera.anchorY to given x and y.

inlinesetPosition(x:Float, y:Float):Void

Sets the camera position to given x and y.

@:value({ h : 1, w : 1, y : 0, x : 0 })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.

inlinesetScale(x:Float, y:Float):Void

Sets the Camera.scaleX and Camera.scaleY to given x and y.

@:value({ h : 0, w : 0, y : 0, x : 0 })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.