mwg API
    Preparing search index...

    Class Camera

    import { Camera } from '@datamoc/mw_games/two-d/render';
    import type { Container2D } from '@datamoc/mw_games/two-d/render';

    declare const stage: Container2D;

    const camera = new Camera({ zoom: 2 });
    camera.setViewport(960, 540);
    stage.addChild(camera.world);

    camera.snapTo(160, 160); // jump to the start room, no easing
    camera.follow({ x: 176, y: 160 }); // then ease towards wherever the hero is
    camera.update(1 / 60);

    const screenPoint = camera.toScreen(160, 160);
    Index
    stepsPerTurn: number

    how many whole-angle positions a full turn has: 4 for a square grid, 6 for a hex one

    world: Container<ContainerChild> = ...

    put the map and everything in it here

    x: number = 0

    centre of the view, in world units

    y: number = 0
    • get rotation(): number

      the view's rotation, in radians, positive turning the world counter-clockwise

      Returns number

    • get rotationSteps(): number

      the rotation step this view is turned to, always in 0 .. stepsPerTurn - 1

      Returns number

    • get uprightRotation(): number

      The angle that cancels rotation: draw a label, health bar or damage number into world with this and it stays upright however the view is turned. A half turn would otherwise put every label upside down.

      Returns number

    • get view(): { height: number; width: number; x: number; y: number }

      The visible region, in world units, as an axis-aligned box: use it to cull.

      With the view turned, the visible region is the viewport rectangle rotated in world space, so this is the box around its four corners instead of the rectangle itself - over-inclusive rather than clipping tiles that are actually on screen. Unturned, it is the rectangle, at its exact numbers.

      Returns { height: number; width: number; x: number; y: number }

    • Eases the view towards an arbitrary angle, taking whichever way around the turn is shorter - so animating from a hex view's fifth step back to its first sweeps the one 60-degree gap between them rather than the long way around. intensity behaves like follow's: the remaining angle closes by that fraction each second.

      Parameters

      • angle: number
      • intensity: number = 4

      Returns void

    • eases towards something that keeps moving, such as the player

      Parameters

      • target: { x: number; y: number }
      • intensity: number = 8

      Returns void

    • eases towards a fixed point

      Parameters

      • x: number
      • y: number
      • intensity: number = 4

      Returns void

    • turns the view by whole steps; rotate(1) is the next position, rotate(-1) the last

      Parameters

      • delta: number = 1

      Returns void

    • Turns the view to an arbitrary angle immediately, no animation - free rotation, not confined to a grid's whole steps. rotationSteps is left as it was: it names where setRotationStep/rotate last put the view, not this angle, so the two APIs are best used one at a time rather than interleaved.

      Parameters

      • angle: number

      Returns void

    • stops the camera leaving the map; pass null to allow it again

      Parameters

      • bounds: { maxX: number; maxY: number; minX: number; minY: number } | null

      Returns void

    • turns the view to a whole step, wrapping around a full turn

      Parameters

      • step: number

      Returns void

    • The framework calls this on resize; sizes are in screen pixels. screenX/screenY default to 0 (an ordinary camera starting at the corner of the canvas); a Viewport gives its camera a nonzero one so this camera's own rectangle starts partway across the screen instead, for a split-screen layout.

      Parameters

      • width: number
      • height: number
      • screenX: number = 0
      • screenY: number = 0

      Returns void

    • Parameters

      • magnitude: number

        world units

      • duration: number = 0.4

        seconds

      Returns void

    • shake, taking intensity in screen pixels rather than world units.

      shake's own magnitude is world units because the offset it produces feeds clampedCentre alongside every other world-space camera field; a caller thinking in screen pixels (a convention several game engines use for this exact call) otherwise has to divide by zoom at every call site itself. This does that division once, so the shake still reads as intensity pixels on screen at whatever zoom is current when it starts, matching what a caller asked for rather than what world-space asked for.

      Parameters

      • intensity: number
      • duration: number = 0.4

      Returns void

    • jumps to a point with no easing, for a teleport or a scene change

      Parameters

      • x: number
      • y: number

      Returns void

    • world point to screen pixels, through the current rotation

      Parameters

      • x: number
      • y: number

      Returns { x: number; y: number }

    • screen pixels to world point, for turning a click into a tile; the inverse of toScreen

      Parameters

      • x: number
      • y: number

      Returns { x: number; y: number }