---
title: Animate state changes
summary: Change visibility, ghosts, the selection, the x-ray set, colours and the camera in one ObjectStateManager call that animates, step after step, running together, queued or replacing what runs, and stopped at its start, where it is or its end.
order: 36
since: 3.0
languages: [javascript]
terms: [object-state-manager, qid, bql]
see-also: [how-to/viewer3d/hide-and-isolate, how-to/viewer3d/select-and-colour-objects, how-to/viewer3d/xray-set, how-to/viewer3d/set-the-camera, reference/bvl]
---

# Animate state changes

`objectStateManager.transition(steps, options)` makes several state changes in one call and animates them: objects fade out and in, colours and transparency blend, the selection, x-ray and ghost overlays fade, and the camera flies. A list of steps plays back to back, so "hide everything but the doors, then fly to one of them" is one call. The existing methods keep working as before and apply at once.

The 3D viewer on WebGPU (Viewer3D-Next) animates. Viewer3D on WebGL and the 2D viewer receive each step through their usual methods when it starts: the state is the same, nothing animates ([[page:how-to/viewer3d/choose-the-viewer]]).

## Live demo

```demo
name: transitions
title: Animated state changes
models: ac11
height: 560
notes: |
  Each button on the left is one `osm.transition` call, and the panel shows its code. **Doors only** fades everything but the doors out; **Then fly** adds a camera step. **Glass** dissolves the walls to 70 % transparency and back, **Recolour** blends the slabs to orange and then green, **Select, x-ray, ghost** fades in three overlays in one step, **Storeys** shows the storeys one by one, and **BVL** runs a script with a `TRANSITION` header. Set the duration and easing before you click; **Slow motion** makes everything ten times slower. Choose **queue** or **replace** and click two scenes in a row to see the schedules, and stop a running scene with **Stop: start**, **current** or **end**. **Reset** puts the model back at once.
```

## Steps

1. **Several changes, then the camera.**

   ```example
   name: transitions-api
   code: javascript/viewer3d/transitions.js
   run: model
   notes: Fades everything but the doors out, flies to the widest door, and then brings everything back with the slabs orange and the doors x-rayed.
   ```

   A step is an object with the keys of the methods it stands for: `visible` (as `setVisibleSet`), `show`, `hide`, `ghosted`, `ghost`, `unghost`, `selected`, `select`, `deselect`, `xrayed`, `xray`, `unxray`, `resetColors`, `colors` (a `ColorChangeMap`, as `changeColorMulti`) and `camera`. Each one takes a [[term:bql|BQL]] condition or a set of [[term:qid|Qids]]. `duration` is in milliseconds (default 500), `easing` is `"linear"`, `"ease-in"`, `"ease-out"` or `"ease-in-out"` (the default). Within a step the changes apply in that order, so `show` and `hide` of the same object end hidden.

   `camera` is `{fit, margin, viewDirection, updatePivot, duration}` to fit objects, as `viewFit` does, or `{eye, target, up, pivot, duration}` to fly to a pose. A step lasts as long as the longer of its own duration and its camera's. A step without changes is a pause, and a step of 0 ms applies at once.

2. **When it starts and how it stops.**

   ```example
   name: transitions-schedule
   code: javascript/viewer3d/transitions-schedule.js
   run: model
   notes: Hides the walls, queues the slabs turning orange behind it, stops the walls halfway back to their start, and then replaces everything with the model as it was.
   ```

   The second argument says when a transition starts. `schedule: "together"`, the default, starts it at once, alongside the ones that run. Where two change the same object or the camera, the later one takes over from what shows. `"queue"` starts it when every transition submitted before it has ended. `"replace"` first stops every transition that runs or waits.

   `onStop` says what a transition leaves when it is stopped before its end: `"end"` (the default) jumps to its end state, `"start"` puts back what it changed, `"current"` keeps what shows. `stopOthers` gives the stop mode for the transitions a `"replace"` stops. `transition.stop(how)` stops one transition, `objectStateManager.stopTransitions(how)` stops all of them.

3. **From BVL.** A script that starts with `TRANSITION <duration> ["<easing>"];` animates its result ([[page:reference/bvl#transition]]), and `VIEWFIT` takes a time, a side and a margin:

   ```example
   name: transitions-bvl
   code: javascript/viewer3d/transitions-bvl.js
   notes: Resolves a script with a TRANSITION header and plays it with BvlHelper; the second call adds a schedule, the third applies the script at once.
   ```

   `BvlHelper.processResults` turns the result into one step and resolves to the `Transition` once its state is in. A `transition` option goes over the script's header key by key, and `transition: false` applies the result at once. Labels, annotations, wireframes and the object flags change at once either way.

## What you get back

`transition()` returns at once with a `Transition`:

- `started` settles when the first step's changes are in the manager's sets. From then on `getVisibleQids()`, `getSelectedQids()` and the others give the new state, and picking follows it: an object that is fading out cannot be clicked any more.
- `finished` settles with `"finished"` when the last step has been drawn, or with `"stopped"`. It rejects when one of its queries fails, and then nothing of it applies.
- `stop(how)` stops it, whether it is running or still waiting.

## How it combines with the other calls

- **The state changes when a step starts.** The manager's sets, its listeners and the picking follow the step at its start, and only the picture takes the duration to catch up. A query run in the meantime sees the new state.
- **The existing methods win.** A call like `addToVisibleSet` or `changeColorMulti` on an object that a transition animates applies at once and takes the object over: the transition stops animating that object, and a stop to `"start"` leaves it alone.
- **A cross-fade.** What fades out, fades in or turns transparent blends from the picture at the start to the picture at the end, as one picture fades into another: a building fading out shows what is behind it more and more, without its inner walls showing through. Glass behind a fading wall comes in with it, glass in front of it keeps its tint.
- **Several viewers.** Every viewer connected to the manager plays the same steps, each with its own camera.
- **Reduced motion.** When the operating system asks for reduced motion, camera flights jump and everything else fades.

## Limits

- On WebGL, and in the 2D viewer, every step applies at once.
- `"current"` is exact for the camera and for colours between two fixed colours or two transparencies. A fade from an object's own colour stops at the colour it is nearer to. An object stopped halfway through fading out becomes visible and transparent: its inner surfaces then show, where the cross-fade showed what lay behind the whole object.
- On a canvas larger than 2560 × 1600 pixels the edges of fading objects are not antialiased while they fade. Lines drawn in front of a fading object fade with it.
- Section caps and shadows follow the state at the step's start, not the fade.
- Section planes, annotations and labels do not animate.

## Check

After `transition([{visible: "type = IFCDOOR"}, {camera: {fit: door}}])` the rest of the model fades out over half a second, the camera then flies to the door, and `finished` resolves to `"finished"`. Calling `stopTransitions("start")` halfway brings everything back as it was.
