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: FloatCamera x position in world coordinates. This is the left edge of what the camera sees.
y: FloatCamera y position in world coordinates. This is the top edge of what the camera sees.
followTarget: BoolWhen true, the camera smoothly follows the target position. Use target() or targetX/targetY to set what to follow.
clampToContentBounds: BoolIf 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: FloatPercentage of viewport width where camera will "brake" to stay inside content bounds
brakeNearBoundsY: FloatPercentage of viewport height where camera will "brake" to stay inside content bounds
targetX: FloatTarget x position
targetY: FloatTarget y position
trackSpeedX: FloatTracking x speed factor
trackSpeedY: FloatTracking y speed factor
trackCurve: FloatAffects 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: FloatCamera zoom level. 1.0 = normal size 2.0 = zoomed in 2x (objects appear larger) 0.5 = zoomed out (objects appear smaller)
deadZoneX: FloatHorizontal 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: FloatVertical 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: FloatHorizontal friction. More the value is below 1.0, higher is the friction.
frictionY: FloatVertical friction. More the value is below 1.0, higher is the friction.
contentX: FloatContent x (top left corner) position
contentY: FloatContent y (top left corner) position
contentWidth: FloatContent width
contentHeight: FloatContent height
viewportWidth: FloatViewport width: the visible area width for this camera. Usually set to screen width or render area width.
viewportHeight: FloatViewport height: the visible area height for this camera. Usually set to screen height or render area height.
movementThreshold: FloatA threshold value to stop the camera if its movement is lower than this value
contentTranslateX: FloatTranslation 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: FloatTranslation 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: TransformThe 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(): Voidnew(): VoidPrivate Members
hasPrevTransform: BoolaverageFrameTime: 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() |