Moving the camera
Your page can move the plan’s camera itself: fly through the building as a story scrolls, turn to an office when someone picks it from your list, or follow the scroll exactly. This page describes all of it. Lettera House, the story puts it together on one page.
Getting the plan’s handle
Section titled “Getting the plan’s handle”With the snippet, the element gets an areaform property. Wait for areaform:ready before the first call:
<div id="plan" data-areaform="sp_demo-office" data-view="3d" style="height: 600px"></div><script src="https://cdn.areaform.app/v1/embed.js" async></script><script> const el = document.getElementById('plan'); el.addEventListener('areaform:ready', () => { el.areaform.flyTo('tour-3'); // The garden });</script>With the open packages, the methods are the View’s: createView(…) returns it, and in React <AreaformView ref={…}> and <Areaform3DView ref={…}> give it. Two names differ from the snippet’s: the View has getLevelId() where the snippet has getLevel(), and its events are in camelCase (levelChange, zoneClick, tourStep). The embedding guide shows both.
Getting ids, points and cameras from the Editor
Section titled “Getting ids, points and cameras from the Editor”Every method takes ids, a point or a camera, never a name: names can repeat, change and differ between languages, while ids stay. All of them come out of the Editor, where you build the plan: open the Space at app.areaform.app, or the plan in the playground. Each Copy puts the value on your clipboard, ready to paste into your page.
| What you need | Where in the Editor | What Copy gives |
|---|---|---|
| A viewpoint’s id | Switch to 3D. In the sidebar’s Viewpoints section, Copy beside the viewpoint. | el.areaform.flyTo('tour-3'); // The garden |
| A level’s id | Show the level. In the sidebar’s For your page section, Copy beside Level. | el.areaform.setLevel('level-p1'); // P1 Parking |
| A zone’s id | Select the zone (in 2D or 3D). For your page, Copy beside Zone. | el.areaform.lookAt('b3-z2'); // Room 3.2 |
| A point on the plan | In 2D, For your page, Pick a point to look at…, then click the plan where you want; Copy. Click again to move it; Done when finished. | el.areaform.lookAt([12.5, 4]); (metres, to 10 cm) |
| The camera as you see it now | In 3D, turn and zoom the plan the way you want it. For your page, Copy camera. | { position: […], target: […], levelId: '…' } |
| A viewpoint’s camera, in numbers | Go to beside the viewpoint in Viewpoints, wait for the camera to arrive, then Copy camera. | The same, for that viewpoint |
- For your page is folded at first: click its heading to open it. Each line is ready to paste after the snippet, with the name in a comment; the id is the part in quotes.
- A point can be anywhere on the level, outside the building too, such as a car park beside it. Points are picked in 2D; the camera is copied in 3D.
- A zone’s id was chosen when the plan was made: in the demo plans they read like
b3-z2orentrance-a; in a plan drawn in the Editor they are longer.
The For your page section
Section titled “The For your page section”In the Editor’s sidebar, under Viewpoints, the For your page section holds everything your page needs, ready to copy. Click its heading to open it.
- Level: the line that shows the level you are looking at. Pick another level in Levels and the line follows.
- Zone: select a zone on the plan, in 2D or 3D, and this row gives the line that turns the camera to it. With no zone selected, it says so.
- Point: in 2D, press Pick a point to look at… and click the plan. A box shows the line for that point,
el.areaform.lookAt([x, y]);, with Copy. Click elsewhere to move the point; press Done or Esc to finish. Points snap to the grid like the drawing tools, are given to 10 cm, and can be anywhere on the level, outside the building too. In 3D this row asks you to switch to 2D. - Camera: in 3D, turn and zoom the plan the way you want your page to show it, then Copy camera. In 2D this row asks you to open 3D.
Each line names the snippet’s element el, as in the examples on this page. If yours has another name, change it after pasting. Each viewpoint’s own line is beside it in Viewpoints, under Copy.
You can also read them from your own page once the plan is on it. Open your browser’s console and type:
const el = document.querySelector('[data-areaform]');el.areaform.getLevel(); // the level shownel.areaform.getCamera(); // the camera now: move the plan by hand first, then askel.addEventListener('areaform:zoneclick', (e) => console.log(e.detail)); // then click zonesWhere things are: the coordinates
Section titled “Where things are: the coordinates”Two kinds of coordinates appear, both in metres. You rarely write either by hand: the Editor gives both.
Plan points, such as lookAt([x, y]): x grows to the east (right on the plan) and y to the north (up on the plan), as in the plan format. This is the same point a zone’s corners use.
A camera, as getCamera() gives it and flyTo takes it, is the 3D view’s own: x east, y up from the floor, and z south, so a plan point [x, y] on the floor is [x, 0, -y].
{ position: [58.85, 44.56, -113.02], // where the camera is target: [40.69, 0, -63.12], // the point it looks at, here on the floor levelId: '4bd6d522-01fb-4885-a90a-1fc47b5cf9cf'}levelId is optional when you pass a camera to flyTo: without it, the camera is placed on the level shown. Writing one by hand is rarely worth it; copy one in the Editor, or save one with getCamera().
A viewpoint, saved in the plan, describes a camera the way a person would: the floor point it looks at (target, a plan point), the compass direction it looks towards (bearing, degrees: 0 north, 90 east), how far it leans from straight down (tilt, degrees: 0 looks straight down, up to 87), and its distance from the target in metres. It also has its own flight time (flight, 3 s when absent) and, for tours, how long it stays (pause, 2 s). The viewpoints of a level are in tour order.
Methods
Section titled “Methods”flyTo(to, options?)
Section titled “flyTo(to, options?)”Flies the camera to a viewpoint or to a camera.
| Parameter | Type | Meaning |
|---|---|---|
to |
string or { position, target, levelId? } |
A viewpoint’s id, on any level, or a camera as above. |
options.seconds |
number |
How long the flight takes. 0 jumps there. By default the viewpoint’s own flight, else 3. |
Returns a Promise of { interrupted }. interrupted is false when the camera arrived, and then the areaform:viewpoint event follows. It is true when something took the camera on the way: the visitor, or your page’s next call. An id the plan has no viewpoint for rejects the Promise with an error.
const { interrupted } = await el.areaform.flyTo('tour-2');if (!interrupted) showCaption('Meeting rooms on every corridor');In 3D the camera flies in an arc over the plan, not through walls.
lookAt(to, options?)
Section titled “lookAt(to, options?)”Turns the camera to a zone or to a point, moving it along its line of sight when it should come closer or go back.
| Parameter | Type | Meaning |
|---|---|---|
to |
string or [x, y] |
A zone’s id, on any level (the camera turns to its middle), or a plan point on the level shown. |
options.seconds |
number |
How long the turn takes; 1.5 by default, 0 jumps. |
options.distance |
number |
Metres from the camera to what it looks at, once there. |
Without distance, a zone ends up taking between 40 % and 80 % of the plan’s shorter side. A small one, such as an office seen from across the building, brings the camera closer. One too big to fit sends it back. A zone already in between, and a point, keep the distance the camera has. Returns and rejects as flyTo does, but sends no event.
el.areaform.lookAt('b3-z2'); // Room 3.2, framedel.areaform.lookAt([12, 4], { distance: 15 }); // a point, 15 m awaysetProgress(t)
Section titled “setProgress(t)”Puts the camera t of the way along the viewpoints of the level shown, in tour order: 0 at the first, 1 at the last, 0.5 half way. Values outside 0 to 1 are held at the ends.
It is not a flight. The camera is there at once, with no Promise and no event, so you can call it on every scroll. A visitor’s touch does not stop it: they can look around, and your page’s next call puts the camera back on the path. Each leg between two viewpoints is an equal share of t, eased, so the camera settles at each viewpoint while the scroll passes it. A level without viewpoints does nothing.
It covers one level. To go to another floor, call setLevel in the next part of your page. Called before the plan is ready, it waits and then applies the last value.
addEventListener( 'scroll', () => { const story = document.getElementById('story').getBoundingClientRect(); el.areaform.setProgress(-story.top / (story.height - innerHeight)); }, { passive: true },);setLevel(levelId) and getLevel()
Section titled “setLevel(levelId) and getLevel()”setLevel shows another level, framed whole, as the level switcher does; areaform:levelchange follows. getLevel() returns the id of the level shown.
getCamera()
Section titled “getCamera()”Returns the camera as above, or null before the plan is drawn. In 2D it is a camera straight above the middle of the plan, as high as the zoom shows, so flyTo(getCamera()) comes back to the same view in 2D or 3D.
showPlace(zoneId) and highlight(zoneIds)
Section titled “showPlace(zoneId) and highlight(zoneIds)”showPlace opens one place as data-to does: with data-here on the snippet it draws the way there at once, across levels when it takes the lifts; without it, it shows the place’s level with its card open; with Directions off, the place is highlighted on its level. It resolves true, or false when the plan has no zone with that id. highlight marks zones in the highlight colour, on whichever level each is, for example the shops of one category; highlight([]) clears them.
el.areaform.highlight(['unit-g03', 'unit-g08']); // Food & drinkconst found = await el.areaform.showPlace('unit-g03'); // the way to Saffron Bowlreset()
Section titled “reset()”Puts the plan back as it opened, for the next visitor at a kiosk: its first level and view, the camera where it started, no route or highlight, “You are here” where data-here says, and the camera’s own motion from its start. areaform:reset follows. With data-idle-reset on the snippet, the plan does this by itself after that many seconds without a touch; see A touch screen in your building.
on(event, handler)
Section titled “on(event, handler)”Calls handler with the event’s detail each time it happens; returns a function that stops it. See Events.
const stop = el.areaform.on('zoneclick', ({ id, data }) => openCard(id, data));// later: stop();How the camera behaves
Section titled “How the camera behaves”- Another level: a viewpoint or zone on another level shows that level first, as the level switcher does, then flies or turns there.
- In 2D the plan pans and zooms instead of flying: to the point a viewpoint looks at, showing as much as its camera sees, and for
lookAtby the same 40–80 % rule. The visitor stays in the view they chose; nothing switches to 3D. - The visitor wins. A drag, a zoom, a tap or a key inside the plan stops what your page started, and the Promise says
interrupted. Your page’s next call takes the camera again. - One move at a time. A new call ends the one before it, which resolves
interrupted. - The camera’s own motion stops. A plan set to turn slowly or play its tour stops when your page moves the camera, as it does when a visitor touches it. With
data-motion-resumeon the snippet, it starts again after that many seconds without a touch, so leave that out on a page that tells a story. - Reduced motion: when the visitor asks their system for less motion, flights and turns jump there at once.
- Before the plan is ready, calls wait for it.
el.areaformappears as soon as the plan’s data has arrived, a moment before the plan is drawn; wait forareaform:readyto be sure it is there.
Events
Section titled “Events”Each event is a DOM event on the snippet’s element that bubbles up the page, with what it tells as detail. el.areaform.on() takes the same names without the areaform: prefix.
| Event | When | detail |
|---|---|---|
areaform:ready |
The plan is drawn; el.areaform can be used. |
— |
areaform:levelchange |
Another level is shown, for any reason: your page, the switcher, a route. | { levelId } |
areaform:viewpoint |
A flight from flyTo arrived. |
{ id, levelId } |
areaform:zoneclick |
A visitor clicked a zone, in 2D or 3D. | { id, levelId, data } |
areaform:tourstep |
The tour (data-motion="tour") reached a viewpoint and stays a while. |
{ id, levelId, index } |
areaform:idle |
Nobody touched the page for data-idle-reset seconds. |
{ seconds } |
areaform:reset |
The plan went back to how it opened, by reset() or after idle. |
— |
- In
viewpoint,idisnullwhen the flight went to a camera rather than a viewpoint. - In
zoneclick,datais the zone’s own data from the plan, such as its area or your id for it. - In
tourstep,indexis the viewpoint’s place in its level’s list, from 0. - After
idlethe plan resets itself.event.preventDefault()keeps it as it is, so your page can ask “Still here?” first and callreset()itself.el.areaform.on('idle', …)only listens and cannot keep it.
There is no event for every frame of a flight.
document.addEventListener('areaform:zoneclick', (event) => { console.log('clicked', event.detail.id, 'in', event.target);});Recipes
Section titled “Recipes”One flight per section
Section titled “One flight per section”Each section of your page names what it shows; the plan follows the one in the middle of the screen.
<section data-viewpoint="tour-1">…</section><section data-zone="b3-z2">…</section><section data-level="level-p1">…</section><script> const plan = document.getElementById('plan'); const watcher = new IntersectionObserver( (entries) => { for (const { isIntersecting, target } of entries) { if (!isIntersecting || !plan.areaform) continue; const { viewpoint, zone, level } = target.dataset; if (viewpoint) plan.areaform.flyTo(viewpoint); else if (zone) plan.areaform.lookAt(zone); else if (level) plan.areaform.setLevel(level); } }, { rootMargin: '-50% 0px' }, // only the section across the middle ); document.querySelectorAll('section').forEach((section) => watcher.observe(section));</script>A list that shows each place
Section titled “A list that shows each place”for (const button of document.querySelectorAll('[data-room]')) button.addEventListener('click', () => el.areaform.lookAt(button.dataset.room));A tour of your own, step by step
Section titled “A tour of your own, step by step”for (const id of ['tour-1', 'tour-2', 'tour-3']) { const { interrupted } = await el.areaform.flyTo(id); if (interrupted) break; // the visitor took over await new Promise((wait) => setTimeout(wait, 2000));}Keep the view across page loads
Section titled “Keep the view across page loads”addEventListener('pagehide', () => localStorage.setItem('plan-camera', JSON.stringify(el.areaform.getCamera())),);el.addEventListener('areaform:ready', () => { const saved = JSON.parse(localStorage.getItem('plan-camera') ?? 'null'); if (saved) el.areaform.flyTo(saved, { seconds: 0 });});TypeScript
Section titled “TypeScript”The snippet comes from our CDN rather than npm, and so do its types: cdn.areaform.app/v1/areaform.d.ts. The file goes out with each release of the snippet, so it always describes the snippet your page runs. Save it into a folder your tsconfig.json includes:
curl -o src/areaform.d.ts https://cdn.areaform.app/v1/areaform.d.tsNothing else is needed. element.areaform and the areaform:… events are typed on every element, and their methods, options and event details autocomplete:
const el = document.querySelector<HTMLElement>('[data-areaform]')!;
el.addEventListener('areaform:ready', () => { el.areaform!.flyTo('vp-lobby', { seconds: 2 }); // typed: ids, options, the Promise});el.addEventListener('areaform:zoneclick', (e) => { console.log(e.detail.id, e.detail.data); // e is CustomEvent<{ id; levelId; data }>});areaform is optional, as only the snippet’s element has it, and only after areaform:ready. To name the types in your own code, import them from the file: import type { AreaformPlan, AreaformEvents, PlanCamera } from './areaform'.
Fetch it again to use what a later release adds. A file you saved earlier stays correct: /v1/ only ever adds methods, options and events, and never changes or removes one. One that is to go is deprecated first, with a warning in the browser’s console and its replacement here, and goes only with /v2/, which has types of its own.
The whole file, as the CDN serves it today:
// Types for the Areaform snippet's `element.areaform` and its `areaform:` events.// https://areaform.app/docs/camera///// Served beside the snippet, as https://cdn.areaform.app/v1/areaform.d.ts, so it always matches// the snippet that runs. Save it in your project, e.g. as `src/areaform.d.ts`, and// `element.areaform` and the `areaform:…` events are typed on every HTMLElement; the interfaces// can be imported from it too.
/** A point on the plan, in metres: x east, y south. */export type Vec2 = [number, number];/** A point in the 3D view, in metres: x east, y up, z south. */export type Vec3 = [number, number, number];
/** A camera, as `getCamera` gives it. With `levelId`, `flyTo` shows that Level first. */export interface PlanCamera { position: Vec3; /** The point it looks at. */ target: Vec3; levelId?: string;}
/** What a flight's Promise resolves with: `interrupted` when something took the camera on the way. */export interface FlightResult { interrupted: boolean;}
/** How long a flight takes. */export interface FlyToOptions { /** Seconds; 0 jumps there. A Viewpoint's own flight time, or 3, when absent (1.5 to turn). */ seconds?: number;}
/** How `lookAt` turns the camera, and how near it ends. */export interface LookAtOptions extends FlyToOptions { /** Metres from the camera to what it looks at, once there. */ distance?: number;}
/** What each event tells, as `on`'s handler gets it and as the `areaform:…` DOM event's `detail`. */export interface AreaformEvents { /** Another Level is shown, for any reason: the page, the Level switcher or a route. */ levelchange: { levelId: string }; /** A flight started with `flyTo` arrived: at a Viewpoint (its id) or at a camera (`id` null). */ viewpoint: { id: string | null; levelId: string }; /** A visitor clicked a Zone: its id, its Level and its `data`. */ zoneclick: { id: string; levelId: string; data: Record<string, unknown> | undefined }; /** The tour reached a Viewpoint and stays there a while. */ tourstep: { id: string; levelId: string; index: number }; /** * Nobody used the page for `data-idle-reset` seconds. The plan is then put back as it opened, * unless the DOM event's `preventDefault()` keeps it. */ idle: { seconds: number }; /** The plan was put back as it opened, by `reset()` or after `idle`. */ reset: undefined;}
/** `element.areaform`, on the snippet's element once the plan is drawn (`areaform:ready`). */export interface AreaformPlan { /** Flies to a Viewpoint by its id, or to a camera. */ flyTo(to: string | PlanCamera, options?: FlyToOptions): Promise<FlightResult>; /** Turns to a Zone by its id, or to a plan point. */ lookAt(to: string | Vec2, options?: LookAtOptions): Promise<FlightResult>; /** Puts the camera along the shown Level's Viewpoints, 0 to 1. */ setProgress(t: number): void; /** Shows another Level, by its id. */ setLevel(levelId: string): void; /** The id of the Level shown. */ getLevel(): string; /** The camera now, in the 3D view's coordinates; null before the plan is drawn. */ getCamera(): Required<PlanCamera> | null; /** Puts the plan back as it opened, for the next visitor at a kiosk. */ reset(): void; /** * Opens a Zone as `data-to` does: with Here, the way to it at once; without, its Level with * its card open; without Directions, highlighted on its Level. Resolves false when the plan has * no such Zone. */ showPlace(zoneId: string): Promise<boolean>; /** Draws these Zones, by id, in the highlight colour; `[]` clears it. */ highlight(zoneIds: string[]): void; /** * Brings these Zones forward on every Level, keeping their colours with a thick outline, and * fades every other Zone, e.g. the shops of one category; `[]` ends it. */ focus(zoneIds: string[]): void; /** Calls `handler` on each `event`; returns a function that stops it. */ on<E extends keyof AreaformEvents>( event: E, handler: (detail: AreaformEvents[E]) => void, ): () => void;}
/** * The DOM events on the snippet's element, which bubble: `areaform:ready` once the plan is drawn, * then one for each of `AreaformEvents`, with its `detail`. */export type AreaformDomEvents = { 'areaform:ready': CustomEvent<null> } & { [E in keyof AreaformEvents as `areaform:${E}`]: CustomEvent<AreaformEvents[E]>;};
declare global { interface HTMLElement { /** The plan's handle, once `areaform:ready` has been sent; absent on other elements. */ areaform?: AreaformPlan; } // Adds the events to the DOM's own map, so `addEventListener` knows each one's `detail`. // eslint-disable-next-line @typescript-eslint/no-empty-object-type interface HTMLElementEventMap extends AreaformDomEvents {}}With the packages, View, CameraTarget, FlightResult, FlyToOptions and LookAtOptions come from @areaform/view, and Camera3D from @areaform/render-3d.