Camera
A flexible camera system for 2D games.
Camera provides smooth scrolling, target following, boundary constraints, and various effects for controlling the viewport in your game world.
Features:
- Smooth target following with configurable speed and curves
- Dead zones to reduce camera movement for small target changes
- Content boundary clamping to keep camera within level bounds
- Zoom support
- Friction and braking near boundaries
- Anchor points for different camera behaviors
The camera doesn't render anything itself - instead, you apply its transform to your game visuals to create the scrolling effect.
// Create a camera following the player
var camera = new Camera(screen.width, screen.height);
camera.followTarget = true;
camera.trackSpeedX = 15;
camera.trackSpeedY = 10;
// In update loop
camera.target(player.x, player.y);
camera.update(delta);
// Apply transform to layer
gameLayer.transform = camera.contentTransform;
Instance Members
x: Float
Camera x position in world coordinates. This is the left edge of what the camera sees.
y: Float
Camera y position in world coordinates. This is the top edge of what the camera sees.
followTarget: Bool
When true, the camera smoothly follows the target position. Use target() or targetX/targetY to set what to follow.
clampToContentBounds: Bool
If true, camera will try to stay inside content bounds. When the viewport is larger than content, camera will be centered. Useful for keeping the camera within level boundaries.
brakeNearBoundsX: Float
Percentage of viewport width where camera will "brake" to stay inside content bounds
brakeNearBoundsY: Float
Percentage of viewport height where camera will "brake" to stay inside content bounds
targetX: Float
Target x position
targetY: Float
Target y position
trackSpeedX: Float
Tracking x speed factor
trackSpeedY: Float
Tracking y speed factor
trackCurve: Float
Affects the smoothness of camera tracking. Lower values (0.1-0.5) create more easing/lag. Higher values (0.8-1.0) create more direct following. Must be between 0 (exclusive) and 1 (inclusive).
zoom: Float
Camera zoom level. 1.0 = normal size 2.0 = zoomed in 2x (objects appear larger) 0.5 = zoomed out (objects appear smaller)
deadZoneX: Float
Horizontal dead zone as percentage of viewport width (0-1). Camera won't move until target moves outside this zone. Reduces camera jitter from small movements.
deadZoneY: Float
Vertical dead zone as percentage of viewport height (0-1). Camera won't move until target moves outside this zone. Reduces camera jitter from small movements.
frictionX: Float
Horizontal friction. More the value is below 1.0, higher is the friction.
frictionY: Float
Vertical friction. More the value is below 1.0, higher is the friction.
contentX: Float
Content x (top left corner) position
contentY: Float
Content y (top left corner) position
contentWidth: Float
Content width
contentHeight: Float
Content height
viewportWidth: Float
Viewport width: the visible area width for this camera. Usually set to screen width or render area width.
viewportHeight: Float
Viewport height: the visible area height for this camera. Usually set to screen height or render area height.
movementThreshold: Float
A threshold value to stop the camera if its movement is lower than this value
contentTranslateX: Float
Translation X that should be applied to the content so that the camera is pointing to the correct area. This value is computed by the camera when it is updated.
contentTranslateY: Float
Translation Y that should be applied to the content so that the camera is pointing to the correct area This value is computed by the camera when it is updated.
contentTransform: Transform
The transform to apply to the content in order to reflect camera position.
Set camera x & y position
Name | Type | Description |
---|---|---|
x |
Float | * |
y |
Float |
Set brakeNearBoundsX
& brakeNearBoundsY
Name | Type | Description |
---|---|---|
brakeNearBoundsX |
Float | * |
brakeNearBoundsY |
Float |
Set targetX
& targetY
, which define the position the camera may follow if followTarget
is true
Name | Type | Description |
---|---|---|
targetX |
Float | * |
targetY |
Float |
Name | Type |
---|---|
delta |
Float |
stabilize(?maxUpdates: Int = 128, ?delta: Float = 0.1, ?thresholdX: Float = 0.01, ?thresholdY: Float = 0.01): Void
Name | Type | Default |
---|---|---|
maxUpdates |
Int | 128 |
delta |
Float | 0.1 |
thresholdX |
Float | 0.01 |
thresholdY |
Float | 0.01 |
updateContentTransform(): Void
new(): Void
Private Members
hasPrevTransform: Bool
averageFrameTime: Float
Name | Type |
---|---|
x0 |
Float |
y0 |
Float |
x1 |
Float |
y1 |
Float |
Returns |
---|
Float |
Name | Type |
---|---|
delta |
Float |
Metadata
Name | Parameters |
---|---|
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |