Skip to main content

Working with Viewport

February 20, 2019

Working with Viewport

Ct.js has a Camera object (camera in code and console) that manipulates the viewport. It supports scaling and rotation, and can also follow a copy and create screen shake effects.

Moving the camera around

To move the camera around, you can:

Moving and teleporting

camera.moveTo(x, y) and camera.teleportTo(x, y) both move the camera to a new position. There are differences, though:

Following a copy

A simple line camera.follow = this inside the On Create code of your main character will set up automagical camera movement ✨ You can also set a template to automatically follow in your room's settings.

camera.borderX and camera.borderY define the area at which the camera shifts when the followed copy enters these borders. These values are in UI coordinates.

JavaScript
// Place this code, e.g, to your hero's `OnCreate` code
camera.follow = this;

// Follow the hero so it cannot be closer than 300 px to any side of the screen
camera.borderX = 300;
camera.borderY = 300;

You can also disable following logic for one axis. Setting camera.followX to false will disable horizontal movement, and setting camera.followY will disable vertical movement. This still allows you to move the camera with teleportTo and moveTo methods.

Manual positioning

If you ever find that above methods are not enough for you, use these parameters:

x and y are the current position of the camera without screen shake and effects of shiftX and shiftY.
targetX and targetY will be different if camera.drift is larger than 0, and you should firstly edit these values.

Zooming and rotation

To scale the viewport, use camera.scale.x and camera.scale.y, similarly to scaling copies. This is not a zoom level, but a scaling factor of a capturing rectangle: when using values larger than 1, you will see a larger portion of a room.

To rotate the viewport, use camera.rotation (in degrees). Again, you rotate a capturing rectangle, so the stuff on the screen will rotate clockwise.

A little caveat

You should not change the camera's values in the "Frame End" event, as the camera updates after the "Frame Start" event and before "Frame End" event. If you do, you will notice some inconsistencies when converting UI coordinates to game ones. That's because u.uiToGameCoord and others will use new values though the room is not yet repositioned.

Modifiers and smooth transition

camera.shiftX and camera.shiftY are interpolated in a separate pass than other camera movements but still use camera.drift.

Tips

For smooth scaling and rotation, change values camera.angle, camera.scale.x, camera.scale.y continuously with u.time, or use tween module.

For example, to zoom in, you could use this code:

JavaScript
tween.add({
    obj: camera.scale,
    duration: 500,
    fields: {
        x: 0.5,
        y: 0.5
    }
});

Tips

Or you could manipulate camera angle by user input (in "On Step" event):

JavaScript
camera.angle += actions.CameraRotate.value * u.time * 300;

Screen shake effects

Yes, there is a built-in feature for that 😅 Its design is as follows:

DISCLAIMER

  • Do remember that there are lots of people (e.g. me, the creator of ct.js) that quickly get dizzy because of screen wobble and shaking. There are also people with epilepsy.
  • Do provide controls for screen shake/wobble and don't overuse the effect.
  • Put warnings about screen shake/wobbling at the start of your game and inside your game's description.

There are many parameters described here to control its feel, but default values are good as well. Here are the examples:

JavaScript
// Add an impulse that will accumulate on repetitive calls
camera.shake += 1;
JavaScript
// Make a constant, slow camera wobble
camera.shakeFrequency = 1;
camera.shakeDecay = 0;
camera.shake = 2;

Making an adaptive UI

Contemporary devices all have various resolutions, and thus your app should adapt to them and still give the best quality.

The first step is going select the "Settings" tab and selecting a scaling mode that suits your game project more:

If you are making a pixelart game, make sure you disable image smoothing at the "Project" tab -> "Render Options" on the left side.

In general, you should follow these rules:

Don't forget to test your UI on different screen sizes and devices!