LittleJS 1.30.1

Camera

FollowCamera

FollowCamera - The 2D camera: follows a target and drives cameraPos, and cameraScale when it has a scale

  • Smoothing is in seconds, so it comes out the same at any frame rate
  • A dead zone lets the target move about without the view moving
  • lookAhead shows more of the way the target is going, sliding across when it turns
  • boundsMin and boundsMax keep the view over the level, centered where the level is smaller than the view; they go by the view's size without cameraAngle
  • Its pos is the middle of the view, before any cameraShake
  • The debug overlay shows its dead zone, offset, look ahead and bounds, to tune them by eye
  • The newest camera made drives the view; a game can still change cameraPos in gameUpdatePost for that frame
  • Destroy it to stop it driving the view, which stays where it was; engineObjectsDestroy removes it as it does any object, so make a new one after, or set persistent to keep it

Extends EngineObject

Constructor

new FollowCamera(target, smoothing)camera.js:84

Make a follow camera, the view goes to its target at once

target EngineObject|Vector2 optional

What to follow, an object or a position

smoothing number optional, default 0.1

Seconds it takes to close most of the gap, 0 snaps

const camera = new FollowCamera(player);
camera.lookAhead = vec2(3, 0);   // see further the way the player runs
camera.boundsMin = vec2();       // keep the view over the level
camera.boundsMax = levelSize;

Members

Methods

Inherited from EngineObject

Members

boundsMax : Vector2|undefinedcamera.js:119

Highest corner of the area the view stays in, Infinity on an axis leaves that side open

boundsMin : Vector2|undefinedcamera.js:115

Lowest corner of the area the view stays in

Size of a box in the middle of the view the target moves in without the view moving

How much more it shows the way the target goes on each axis, 0 for none

lookAheadSmoothing : numbercamera.js:112

Seconds the look takes to slide across when it turns

lookAheadTurn : numbercamera.js:110

How far the target must go back the other way before the look turns, so a jiggle does not flip it

lookFrom : Vector2|undefinedcamera.js:131
lookTarget : EngineObject|Vector2|undefinedcamera.js:133

Where the target sits from the middle of the view, the view moved by it

scale : number|undefinedcamera.js:122

The cameraScale it eases to, undefined leaves cameraScale to the game

scaleSmoothing : numbercamera.js:124

Seconds the zoom takes to close most of the gap to scale, 0 snaps

smoothing : numbercamera.js:101

Seconds it takes to close most of the gap to the target, 0 snaps

What it follows; destroyed or undefined, the view holds

Methods

activate()camera.js:139

Make this the camera that drives the view, it starts from where the view is

A camera draws nothing

renderDebugInfo()camera.js:248

Show the settings on the debug overlay: the dead zone in yellow around the view middle, the offset in cyan and the look ahead in green from the target to the focus, a cross where the view wants its middle, and the bounds in orange

Put it on the target at once, with no easing, for a level start or a teleport; the view goes there too when this camera is the one driving it

takeView()camera.js:142

Start from where the view is, called when it takes over from another camera

updateCamera()camera.js:161

Move toward the target and set the view, called by the camera plugin after the objects update

updatePhysics()camera.js:240

A camera is moved only by its target, physics leaves it alone