KlippControls

An orbit camera the user turns, pans and zooms with the mouse or touch, in one piece.

KlippControls is a whole camera in one piece: drag to orbit around a target, right-drag, Shift-drag or drag with two fingers to pan, middle-drag, Ctrl-drag, scroll or pinch to zoom. It's a virtual camera like any other, so it takes part in priority and blending, and takes Extensions and Noise. In React the input is built in. On the core and in three.js, connectControls from @pmndrs/klipp/dom connects it to the canvas in one call.

How it moves

Orbit. horizontal and vertical move the camera around the center of the orbit, in degrees, as in OrbitFollow. The camera always looks at that center.

Zoom. zoom is the natural log of how many times closer than distance the camera is: 0 is distance, ln 2 twice as close, -ln 2 twice as far. minDistance and maxDistance limit it. In an orthographic view the view size scales instead.

Pan. panX and panY take the drag in pixels and move the center of the orbit so the point under the cursor follows it. pan holds how far panning has moved the center from the target, in the frame of orbit.bindingMode. Set it back to [0, 0, 0] to center on the target again. Panning measures the drag against the viewport height from klipp.setSize, in CSS pixels. <Klipp> sets it for you.

Pan lock. panLock stops panning along the axes it holds: { y: true } pans on the ground, { y: true, z: true } only left and right. With frame: 'view', the default, x and z turn with the camera. With frame: 'orbit' they are the axes of orbit.bindingMode: the world's, or the target's with lockToTarget.

Pan bounds. With panBounds, a box [minX, minY, minZ, maxX, maxY, maxZ] or a sphere { center, radius } in world space, the center of the orbit stays inside it, also when the target moves out. Within panSlowingDistance of an edge, panning toward it slows down the closer the center gets. In three.js and React panBounds also takes a Box3 or a Sphere, read every frame.

Target. The camera orbits an Object3D, a ref or a fixed point, and follows it when it moves. Without a target it orbits the world origin, and panning moves it freely. With orbit.damping, the camera glides to a new target instead of jumping.

Starting point. A position in the camera's initialState sets where the camera starts: horizontal and vertical from its direction around the target, and zoom from its distance. With BlendHints.inheritPosition it starts from the shot on screen each time it becomes the active one: at the shot's position, orbiting the point the shot looked at.

Settings

The camera's own settings, name, priority, active, hints, initialState and standbyUpdate, work as on VirtualCamera.

SettingDescriptionDefault
targetWhat to orbit. See Targets. Without one, the world origin.
distanceDistance from the center of the orbit while zoom is at its center.10
minDistanceHow close zooming in brings the camera.0
maxDistanceHow far zooming out takes the camera.Infinity
infinityDollyZooming past minDistance or maxDistance moves the center of the orbit forward or back instead, so the camera keeps going, up to panBounds. reset brings it back. Perspective only.false
panBoundsA box or a sphere in world space the center of the orbit stays in. See How it moves.null
panSlowingDistanceWidth of the zone inside the edge of panBounds where panning toward the edge slows down, in world units. 0 stops it at the edge.0
panLockAxes the center of the orbit doesn't pan along: { x, y, z, frame }. See How it moves.none
orbitSettings of the orbit, the same as on OrbitFollow but radius: damping, bindingMode, orbitStyle, targetOffset and the rest. Later on, change them on controls.orbit.
horizontal, vertical, zoomSettings for each axis, like damping, recentering or autoMove. zoom takes no range: it follows minDistance and maxDistance. Later on, change them on controls.horizontal and the others.see Axes
pandamping and maxSpeed of panX and panY.
debugReact only. Draws the orbit in the scene.false

Axes

AxisRangeCenterWraps
horizontal[-180, 180]0, the camera on the target's +Z sideyes
vertical[-90, 90]0, level with the targetno
zoomfrom maxDistance to minDistance0, at distanceno
panX, panYnone0no

Every axis starts at its center. Their damping smooths the input: 0, the default, follows the hand right away. With recentering, horizontal, vertical and zoom ease back to their centers after a while without input. With autoMove on horizontal, the camera turns around the target. Input and moves from methods restart the wait on every axis.

<KlippControls target={subject} horizontal={{ autoMove: { enabled: true } }} />

Input

SourceDrives
Left button, one fingerhorizontal and vertical, gain 0.3
Right button, two or three fingerspanX and panY
Left button with Shift or Meta (⌘ on a Mac)panX and panY
Middle button, or left button with Ctrl, up and downzoom, gain 0.005, up zooms in
Wheelzoom, gain 0.001
Pinchzoom

<KlippControls> and connectControls(klipp, controls, element, options) take the same input options. connectControls takes input only while the camera is on screen. It returns the controller, an InputControllerDom for pointer lock or raw input, and stop().

OptionDescriptionDefault
mouseButtons, modifiers, touches, wheel, pinchReplace the default mapping of one source or modifier key, or turn it off with null.the table above
blendThresholdTake input only while the camera has at least this share of the shot, see On screen.1
suppressContextMenuDisable the right-click menu on the canvas, so the right button can pan.true
interactiveArea, lockTouchAxis, pointerLockAs on InputController.
enabledReact only. Turns input on or off. Elsewhere, set enabled on the controller.true

controls.inputConfig(overrides) returns the mapping with the axes themselves, for an InputControllerDom of your own.

Methods

MethodDoes
reset(options)Back to the center of each axis and onto the target.
moveTo(point, options)Moves the center of the orbit to point, keeping the direction and the distance.
setLookAt(position, center, options)Puts the camera at position, looking at center, or at the center of the orbit without one.
fit(bounds, options)Moves the center of the orbit to the middle of a box or a sphere, and zooms until it just fills the view, keeping the direction: a box by its corners as the camera sees them, a sphere by its outline. padding leaves room around it, in world units.
fitPoints(points, count, options)Like fit, for the first count points in world space, flat [x, y, z, ...]: the center goes to the middle of the box around them, and the camera as close as it gets with every point in view.
stop()Stops every move where it is: methods, input damping, recentering and autoMove. Following the target goes on.
forward(out, flatten)Writes the direction the camera looks along, from the axes alone. flatten keeps it level.

Every method but stop and forward takes damping. 0, the default, jumps there. Above it, the camera eases there with that response time, and input stops it where it is.

In three.js and React, moveTo and setLookAt also take a Vector3, and fit also takes a Box3, a Sphere, or an Object3D, framed by every vertex of it and its children, and forward also writes into a Vector3.

Events

Besides the transition events of every camera, KlippControls tells when the viewer controls it and when it moves:

EventWhen
controlStartA button or a finger comes down on the view.
controlEndThe viewer lets go.
wakeThe camera starts moving: by input, damping, a method, a target that moves, or a Noise.
restThe camera stops moving.

The wheel moves the camera without a controlStart. The events come once the frame is over, so a listener can start another move. A camera turned off or removed while controlled or moving sends controlEnd and rest.

In React, <KlippControls.Events> takes them as onControlStart, onControlEnd, onWake and onRest, next to the transition callbacks. For example, to lower the resolution while the camera moves:

function RegressWhileMoving() {
  const regress = useThree((state) => state.performance.regress);
  const moving = useRef(false);
  useFrame(() => {
    if (moving.current) regress();
  });
  return <KlippControls.Events onWake={() => (moving.current = true)} onRest={() => (moving.current = false)} />;
}

<KlippControls target={subject}>
  <RegressWhileMoving />
</KlippControls>;

In core and three.js, listen with controls.addEventListener('rest', listener).

Extensions and Noise

Extensions and Noise go on KlippControls as on any camera. The Body and the Aim are built in: setting another one only logs a warning. For a different Body or Aim, build a VirtualCamera with OrbitFollow.

Usage

import { KlippControls, targetPose } from '@pmndrs/klipp';
import { connectControls } from '@pmndrs/klipp/dom';

const model = targetPose.create(); // keep model.position up to date

const controls = new KlippControls('controls', { target: model, priority: 10, distance: 8, minDistance: 2 });
klipp.add(controls);
klipp.setSize(canvas.clientWidth, canvas.clientHeight);
connectControls(klipp, controls, canvas);

Several cameras with their own connectControls never react to the same drag: each takes input only while it is on screen.