---
title: Show floor plans in the 2D viewer
summary: Create a Viewer2d, list the storeys and show one, colour, select and label objects, load it with a versions token, and destroy and unload it again.
order: 10
languages: [javascript]
terms: [viewer, building-storey, object-state-manager, model-manager, qid, versions-token]
see-also: [how-to/viewer3d/show-a-floor-plan, how-to/viewer3d/building-storeys, how-to/viewer3d/select-and-colour-objects, how-to/viewer3d/hide-and-isolate]
---

# Show floor plans in the 2D viewer

```warning
The 2D viewer is being phased out. A new application shows plans in the 3D viewer in orthographic mode: [[page:how-to/viewer3d/show-a-floor-plan]].
```

## Live demo

```demo
name: viewer2d
title: Floor plans in the 2D viewer
models: ac11
height: 520
notes: |
  **Storey** shows another floor. **Colour walls**, **Select doors** and **Space labels** go through the ObjectStateManager, as they would for the 3D viewer. **Destroy** removes the viewer and **Create** makes a new one, which draws the model that is still loaded.
```

## Before you start

Install `@bimworks/viewer2d` with `@bimworks/viewercommon` and `@bimworks/jssdk` ([[page:how-to/install/javascript]]). The credential needs `fs:read`, `geometry:read` and `query:run` on the models: loading a model and listing its storeys both run queries.

## 1. Create the viewer and show a storey

```example
name: viewer2d-floor-plan
code: javascript/viewer2d/floor-plan.js
notes: A page with a `<div id="plan">` of a fixed height. `init()` is asynchronous; load models only after it has resolved.
highlight: 10-13,22-24,41
```

- `new Viewer2d(objectStateManager, settings)` creates the viewer; its `container` goes into the page **before** `await viewer.init()`, because `init()` measures it. `start()` starts drawing; `stop()` pauses it while the viewer is hidden.
- `client.bim.listBuildingStoreys(versionUuids)` answers the storeys merged over the models, each with a `name`, an `elevation`, a `floor` number (0 is the ground floor) and the [[term:qid|Qids]] of its storey objects in `buildingStoreys` ([[page:how-to/viewer3d/building-storeys]]).
- `setBuildingStoreysVisibleExclusive(storeyQids)` shows that storey; `viewFit` fits the plan to its objects. The 2D viewer draws one storey at a time.
- Spaces (`IFCSPACE`) are hidden by default, as in the 3D viewer. `addToVisibleSet("GET qid WHERE type = IFCSPACE")` after loading shows them; they stay visible when you show another storey, and a viewer created later draws them too.

The first model is fitted when it loads (`camera.fitOnLoad`, on by default). `viewFit` also takes `rotation`, which turns the plan while fitting: `"KEEP"` (the default), `"ORIGINAL"`, `"WIDEST_HORIZONTAL"`, `"MODEL_AXIS"`, or `"OBJECT_AXIS"` with the object in `rotationObject`.

## 2. Colour, select and hide

The 2D viewer follows the [[term:object-state-manager|ObjectStateManager]], so the calls are the ones the 3D guides describe, with a BQL query or a set of Qids:

```javascript {#state}
await objectStateManager.changeColor("GET qid WHERE type IN (IFCWALL, IFCWALLSTANDARDCASE)", "red");
await objectStateManager.setToSelectionSet("GET qid WHERE type = IFCDOOR");
await objectStateManager.removeFromVisibleSet("GET qid WHERE type = IFCFURNISHINGELEMENT");
await objectStateManager.clearSelection();
```

A 3D viewer connected to the same ObjectStateManager shows the same state ([[page:how-to/viewer3d/select-and-colour-objects]], [[page:how-to/viewer3d/hide-and-isolate]]). The x-ray set is the one state the 2D viewer ignores.

## 3. Label objects

```example
name: viewer2d-labels
code: javascript/viewer2d/labels.js
notes: The label of a room that is too small for its text is not drawn; zoom in and it appears.
```

`viewer.setLabels` takes one `{qid, labels}` per object, `labels` being the lines of text. Labels are drawn on visible objects only, which is why the first example shows the spaces. Each call replaces the whole set, as `objectStateManager.setLabels` does: an object left out, and every object after `resetLabels()`, shows its own text again (a space its number, name and area). A label shows when its text fits inside the object, judged as the 3D viewer's plan labels judge it ([[page:how-to/viewer3d/show-a-floor-plan#room-labels]]).

## 4. Create, destroy and unload

```example
name: viewer2d-create-and-destroy
code: javascript/viewer2d/create-and-destroy.js
notes: The model is loaded once; every new viewer draws what the ModelManager holds.
```

`destroy()` releases the viewer's WebGL resources; remove its container yourself. Unload with `modelManager.removeModels(...)`, which unloads from every connected viewer.

## 5. Load with a versions token

A page that shows your users their models signs in with a [[term:versions-token|versions token]] your server creates, and asks for a new one when it expires. Nothing about this is specific to the 2D viewer: `factory.createClientWithAuth(auth)`, then `client.registerExpiredTokenHandler(...)`, as [[page:how-to/auth/create-a-versions-token]] and [[page:how-to/auth/refresh-an-expired-token]] describe, with the scopes listed above.

## 6. Settings

| Setting | Default | Meaning |
|---|---|---|
| `textPlacement.orientation` | `"AUTO"` | Kept for compatibility, without effect: labels are horizontal on screen however the plan is turned. The 3D viewer's plans can turn a label along a narrow room ([[page:reference/viewer3d#planlabels|planLabels]] `orientation: "AUTO"`). |
| `camera.fitOnLoad` | `true` | Fit the plan when the first model has loaded. |
| `canvas.autoSize` | `true` | Follow the size of the container; give the container a size, also in a page that scrolls. |
| `rendering.antialias` | `true` | WebGL antialiasing. |
| `realtimeSettings` | all `true` | `drawTriangles`, `drawWireframes`, `drawLines` (the IFC lines, such as axes) and `drawAdditionalLines`. |

At run time, `toggleFaceRendering`, `toggleWireframeRendering`, `toggleIfcLineRendering` and `toggleDoorOpeningRendering` switch what is drawn; `toggleUserPanning`, `toggleUserZoom`, `toggleUserRotate` and `toggleUserSelection` switch the mouse and touch interaction off, for example for a small plan in a page that scrolls. `screenshot(options)` returns the plan as a PNG `Blob`.

## Check

The ground floor appears with its spaces and fits the container; another storey replaces it; walls turn red and doors are selected in the 2D viewer and in any 3D viewer on the same ObjectStateManager.
