---
title: Set the camera from code
summary: Choose the first view of a model, jump to the front, side and top views, zoom, switch to orthographic, save and restore a viewpoint, and fly to a viewpoint a BQL query computes.
order: 22
languages: [javascript]
terms: [viewpoint, viewer, bql]
see-also: [how-to/viewer3d/navigation-modes, how-to/viewer3d/show-a-floor-plan, reference/bql/spatial-and-advanced, how-to/viewer3d/take-a-screenshot]
---

# Set the camera from code

The user moves the camera with the mouse; your page moves it with a few calls on the viewer and its camera.

## Live demo

```demo
name: camera
title: The camera from code
models: ac11
height: 520
notes: |
  Each button is one call. The panel shows `getViewpoint()` after every move. **Save view**, then move around and **Restore view**. **From a door to the stairs** flies to the viewpoint a BQL query computes and x-rays the stairs, which are behind walls from there.
```

## Steps

```example
name: viewer-camera
code: javascript/viewer-camera.js
run: model
notes: Runs on a viewer with a model loaded, `vec3` from `@bimworks/viewercommon`.
```

1. **The first view.** After loading, the viewer fits the model from its default direction. `viewer.viewFitModels(vids, {viewDirection})` fits from the direction you give: the vector points from the model to the camera, so `[-0.5, -0.5, 0.3]` looks from the south-west and a little above, `[0, 0, 1]` from straight above. `objectStateManager.viewFit(qids, options)` does the same for a set of objects, in every connected viewer.
2. **Standard views.** `frontView()`, `sideView()` and `topView()` fit everything loaded from the front (−Y), the side (+X) and above (with +Y up), animated.
3. **Zoom.** `viewer.zoom(delta)` zooms as the mouse wheel does: a negative delta zooms in, a positive one out.
4. **Orthographic.** `viewer.getCamera().fov = 0` switches to an orthographic camera, a positive field of view below 120 back to perspective ([[page:how-to/viewer3d/show-a-floor-plan]] uses it for plans).
5. **Save and restore.** `camera.getViewpoint()` returns a copy of the current [[term:viewpoint]]: `position`, `direction` (from the eye to the point it looks at), `up` and `fov`, in metres. `camera.setViewpoint(viewpoint, ms)` flies back in `ms` milliseconds, or jumps with `0`; a viewpoint without `fov` keeps the current one. The promise resolves when the flight starts, not when it ends.
6. **From a query.** The BQL feature `viewPoint(<condition>)` computes, per object, a viewpoint at the object that looks at the objects matching the condition: `position`, `direction`, `up` and `target` as `{x, y, z}` in metres ([[page:reference/bql/spatial-and-advanced]]). Turn them into vectors for `setViewpoint`. Plain `viewPoint`, without a condition, answers only a position and an up vector.

## Check

**Top** shows the model from above with north up; a saved viewpoint brought back with `setViewpoint` shows the same picture as when it was saved.
