1.1.0 • Published 8 months ago

@basementuniverse/camera v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Game Component: Camera

A camera component for use in 2d browser games.

How to use

Create a camera:

import Camera from '@basementuniverse/camera';

const camera = new Camera();

Update the camera transforms:

class Game {
  // ...

  public draw(context: CanvasRenderingContext2D) {
    context.save();

    // This updates context transforms based on the camera's position and scale
    this.camera.draw(context);

    // Draw everything else...

    context.restore();
  }
}

Move the camera by setting camera.position.

Snap the camera to a new position (without easing) by setting camera.positionImmediate.

Zoom the camera by setting camera.scale.

Snap the camera to a new zoom level (without easing) by setting camera.scaleImmediate.

Fetch the camera's world-space bounds:

const bounds: {
  top: number;
  bottom: number;
  left: number;
  right: number;
} = camera.bounds;

Convert a screen-space position to world-space:

const worldPosition: {
  x: number;
  y: number;
} = camera.positionToWorld(screenPosition);

Options

const options = { ... };
const camera = new Camera(options);
OptionTypeDefaultDescription
allowScalebooleantrueAllow the camera to zoom
minScalenumber0.5Minimum zoom level
maxScalenumber4Maximum zoom level
moveEaseAmountnumber0.1Position easing amount, set to 0 for no easing
scaleEaseAmountnumber0.1Scale easing amount, set to 0 for no easing

Other components

1.0.2

8 months ago

1.1.0

8 months ago

1.0.1

1 year ago

1.0.0

1 year ago