How-to guide·3D viewerJavaScript

Animate state changes

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.

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 (Choose the 3D viewer).

Live demo#

Animated state changesLive demo
Loads the demo model in this page with the demo API token of your account: open BIM.works and sign in once in this browser.

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.

    // The viewer of the embed example, with a model loaded
    const everything = objectStateManager.getVisibleQids().clone();
    
    // The widest door: BQL picks it, ORDER BY and LIMIT included
    const door = await objectStateManager.queryToQids("type = IFCDOOR ORDER BY aabb.width DESC LIMIT 1");
    
    // Two steps, back to back: everything but the doors fades out over 500 ms, then the camera
    // flies to that door in 1.5 s. transition() returns at once
    const transition = objectStateManager.transition([
    	{visible: "type = IFCDOOR", duration: 500},
    	{camera: {fit: door, viewDirection: [0.4, -1, 0.35], margin: 60}, duration: 1500}
    ]);
    
    // The manager's sets change when a step starts; the picture catches up over its duration
    await transition.started;
    console.log(objectStateManager.getVisibleQids().size + " objects visible, the doors");
    console.log("The transition " + await transition.finished);     // "finished"
    
    // Colours, x-ray and the selection animate the same way, in one step with any other change
    const slabs = new ColorChangeMap();
    slabs.changeQids(await objectStateManager.queryToQids("type = IFCSLAB"), "#ff8c1a");
    await objectStateManager.transition({
    	visible: everything,
    	colors: slabs,
    	xrayed: "type = IFCDOOR",
    	duration: 800,
    	easing: "ease-out"
    }).finished;
    

    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 BQL condition or a set of 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.

    // The viewer of the embed example, with a model loaded
    const everything = objectStateManager.getVisibleQids().clone();
    
    // Together (the default): starts at once, alongside what is running
    const walls = objectStateManager.transition({hide: "type IN (IFCWALL, IFCWALLSTANDARDCASE)", duration: 1000});
    
    // Queued: starts when every transition submitted before it has ended
    const orange = new ColorChangeMap();
    orange.changeQids(await objectStateManager.queryToQids("type = IFCSLAB"), "#ff8c1a");
    const queued = objectStateManager.transition({colors: orange, duration: 1000}, {schedule: "queue"});
    
    // Stopped halfway: "start" puts back what it changed, "current" keeps what shows, "end" jumps to
    // its end state. Without an argument a transition stops the way its onStop option says
    setTimeout(() => walls.stop("start"), 500);
    console.log("walls: " + await walls.finished);      // "stopped", the walls back
    console.log("queued: " + await queued.finished);    // "finished", started when the walls stopped
    
    // Replace: stops everything running or waiting, here to its end state, then starts
    const reset = objectStateManager.transition(
    	{visible: everything, resetColors: everything, duration: 600},
    	{schedule: "replace", stopOthers: "end"});
    console.log("reset: " + await reset.finished);
    
    // Or stop every transition at once, each to its own onStop unless you say how
    objectStateManager.stopTransitions("end");
    

    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 (reference/bvl#transition), and VIEWFIT takes a time, a side and a margin:

    import {BvlHelper} from "@bimworks/viewercommon";
    
    // A script with a TRANSITION header: its result animates from what the viewer shows. VIEWFIT
    // takes a time, a side and a margin of its own
    const bvl = `TRANSITION 600ms "ease-in-out";
    if (type = IFCDOOR) { SHOW COLOR "orange" SELECT VIEWFIT 1500ms FROM "front" MARGIN 10%; } else { HIDE; }`;
    
    // ResolveBvl answers the resolved actions, the header as `transition`. authToken: from
    // LoginWithApiToken, as for every OpenAPI call
    const response = await fetch(new URL("openapi/ifc/ResolveBvl", "https://bim.works"), {
    	method: "POST",
    	headers: {"Content-Type": "application/json", "Authorization": "Bearer " + authToken},
    	body: JSON.stringify({models: [{type: "VersionModelIdentifier", uuid: "<version uuid of the first demo model>"}], bvl})
    });
    const resolved = await response.json();
    
    // One ObjectStateManager.transition step: the visibility, selection, colours and the camera.
    // It resolves to the Transition once its state is in; finished waits for the animation
    const helper = new BvlHelper(objectStateManager, viewer);
    const transition = await helper.processResults([resolved]);
    await transition.finished;
    
    // The caller's timing goes over the script's, key by key; false applies the script at once
    await helper.processResults([resolved], {transition: {duration: 200, schedule: "replace"}});
    await helper.processResults([resolved], {transition: false});
    

    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.

Since 3.0