# globe

An interactive globe of land dots with markers and arcs for a list of places.

A dark sphere in the box with the continents as dots (or a solid fill) and a thin glow along its rim. Each place is a marker on the surface; the one under the pointer or in focus grows and gets a ring. Arcs between places rise with their length and draw themselves along it once, or carry a dash that keeps running from the first place to the second. The globe turns slowly on its own, stops under the pointer and turns with a drag.

Module `globe`, category Maps and data. Import `@dioramajs/globe/globe`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/globe/src/globe.ts)

A dark globe of land dots with nine furniture showrooms as markers and arcs drawn from the Copenhagen workshop.

```html title="index.html"
<div class="showrooms" data-dio="globe" data-dio-lat="40" data-dio-lng="10" style="touch-action: pan-y">
  <ul aria-label="Showrooms, with deliveries from the Copenhagen workshop">
    <li id="copenhagen" data-dio-lat="55.7" data-dio-lng="12.54" data-dio-to="stockholm berlin amsterdam london milan new-york tokyo"><button type="button">Copenhagen</button></li>
    <li id="stockholm" data-dio-lat="59.31" data-dio-lng="18.07"><button type="button">Stockholm</button></li>
    <li id="berlin" data-dio-lat="52.5" data-dio-lng="13.4"><button type="button">Berlin</button></li>
    <li id="amsterdam" data-dio-lat="52.36" data-dio-lng="4.89"><button type="button">Amsterdam</button></li>
    <li id="london" data-dio-lat="51.52" data-dio-lng="-0.11"><button type="button">London</button></li>
    <li id="milan" data-dio-lat="45.47" data-dio-lng="9.19"><button type="button">Milan</button></li>
    <li id="new-york" data-dio-lat="40.72" data-dio-lng="-74.01"><button type="button">New York</button></li>
    <li id="tokyo" data-dio-lat="35.65" data-dio-lng="139.7"><button type="button">Tokyo</button></li>
    <li id="melbourne" data-dio-lat="-37.8" data-dio-lng="144.98"><button type="button">Melbourne</button></li>
  </ul>
</div>
```

## Usage

Put it on: an element with a list of places inside, or an empty element with `markers` pointing at a list elsewhere.

`start()` registers `globe`, so the attribute alone puts it on an element.

```html
<div class="globe" data-dio="globe" data-dio-lat="40" data-dio-lng="10" style="touch-action: pan-y"><ul><li id="ber" data-dio-lat="52.52" data-dio-lng="13.40" data-dio-to="nyc sgp"><a href="/offices/berlin">Berlin</a></li><li id="nyc" data-dio-lat="40.71" data-dio-lng="-74.01"><a href="/offices/new-york">New York</a></li><li id="sgp" data-dio-lat="1.35" data-dio-lng="103.82"><a href="/offices/singapore">Singapore</a></li></ul></div>
```

- `data-dio-lat`: Latitude of the place in degrees, -90 to 90.

- `data-dio-lng`: Longitude of the place in degrees, -180 to 180.

- `data-dio-to`: Ids of other places to draw arcs to, separated by spaces.

In page transitions: Leaves by dithering its pixels away while shrinking to 96%, so its opaque sphere never switches to transparent mid-transition.

- Event `dio:marker`, detail `{ marker: Element | null; id: string | null }`: A marker lights up under the pointer, from a hovered legend item or a focused link, or goes dark (marker and id null). Bubbles; tooltips and the pointer cursor are up to the site.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `markers` (rebuilds) | `data-dio-markers` | `string` | `'[data-dio-lat]'` | Selector of the places: inside the element, or in the document when none are inside. |
| `data` (rebuilds) | `data-dio-data` | `string` | `''` | URL of JSON `{ markers: [{ id, lat, lng }], arcs: [{ from, to }] }`, added to the places. |
| `lat` | `data-dio-lat` | `number` -90 to 90° | `20` | Latitude that faces the viewer; a change turns to it. |
| `lng` | `data-dio-lng` | `number` -180 to 180° | `0` | Longitude that faces the viewer. |
| `spin` | `data-dio-spin` | `number` -72 to 72 deg/s | `3.6` | Turns on its own around the poles; 0 stands still. |
| `drag` | `data-dio-drag` | `boolean` | `true` | Turn it by dragging, and with the arrow keys when it has a tabindex. |
| `style` (rebuilds) | `data-dio-style` | `'dots' \| 'solid'` | `'dots'` | Land as dots, or as a solid fill of the land mask. |
| `motion` (rebuilds) | `data-dio-motion` | `'draw' \| 'flow'` | `'draw'` | How the arcs move: draw draws each arc once along its length, for offices and showrooms; flow runs a dash along each arc again and again, from the place that names it in data-dio-to to the other, for routes, deliveries and traffic. |
| `dots` (rebuilds) | `data-dio-dots` | `number` 1000 to 20000 | `12000` | Dots over the whole sphere, about a third of them land up on land. At most low 4000, medium 8000. |
| `scale` | `data-dio-scale` | `number` 0.3 to 1 | `0.8` | Diameter as a fraction of the smaller side of the box. |
| `color` | `data-dio-color` | `color` | `'#0d1220'` | Color of the sphere, the ocean. |
| `land` | `data-dio-land` | `color` | `'#5b6b8c'` | Color of the land. |
| `marker` | `data-dio-marker` | `color` | `'#ffb547'` | Color of the markers. |
| `arc` | `data-dio-arc` | `color` | `'#ffb547'` | Color of the arcs. |
| `atmosphere` | `data-dio-atmosphere` | `color` | `'#6ea8ff'` | Color of the glow along the rim. |
| `glow` | `data-dio-glow` | `number` 0 to 1 | `0.4` | Strength of the glow along the rim. |
| `markerSize` | `data-dio-marker-size` | `number` 2 to 24 px | `6` | Diameter of a marker. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Most frames a second while it spins on its own; dragging runs at full rate. At most low 20. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `dots`, `solid`
- `motion`: `draw`, `flow`
  - `draw` draws each arc once along its length, for offices and showrooms.
  - `flow` runs a dash along each arc again and again, from the place that names it in data-dio-to to the other, for routes, deliveries and traffic.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size). Draws at a steady rate while it is on screen and stops when it leaves. While it spins or its arcs flow, at most `fps` frames a second; at full rate while dragged, coasting, turning to a place, drawing arcs or lighting a marker. None at rest without spin or flowing arcs, and none off screen. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. No spin, no coasting after a drag, arcs drawn whole at once without a flowing dash, and turns to a focused place made in one step. Dragging, arrow keys and lit markers still work: the visitor starts them.

Runs on every device. Nothing hovers: a tap on a marker works as a click. A finger turns the globe only when the element has `touch-action: pan-y` or `none`, otherwise the gesture scrolls the page.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { globe: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 10.0 KB the first time a page uses it, 3.6 KB of it data. GPU: Five draw calls: the sphere, all land dots as one instanced call, all markers, all arcs and the glow. Memory: About 12 bytes per land dot (40 KB at 12000 dots), or a 260 KB land mask texture for `solid`.

- low: 4000 dots, spin at 20 frames a second.
- medium: 8000 dots.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A store locator with a list beside it

Keep the places in a visible list next to the globe and point `markers` at its items. Hovering an item lights its marker, focusing its link turns the globe to the place, and a click on a marker follows the link. The globe itself is `aria-hidden`: the list already says everything it shows.

A globe turned to Europe beside a list of six showrooms; hovering or focusing a city lights its marker and turns the globe to it.

```html title="index.html"
<div class="locator">
  <div class="earth" data-dio="globe" data-dio-markers="#showroom-list li" data-dio-lat="48" data-dio-lng="8" data-dio-spin="0" aria-hidden="true" style="touch-action: pan-y"></div>
  <ul id="showroom-list" class="list">
    <li data-dio-lat="55.7" data-dio-lng="12.54"><a href="https://www.openstreetmap.org/#map=15/55.700/12.540">Copenhagen</a><span>Nordvest, Tue to Sat</span></li>
    <li data-dio-lat="59.31" data-dio-lng="18.07"><a href="https://www.openstreetmap.org/#map=15/59.314/18.072">Stockholm</a><span>Södermalm, Wed to Sun</span></li>
    <li data-dio-lat="52.5" data-dio-lng="13.4"><a href="https://www.openstreetmap.org/#map=15/52.499/13.403">Berlin</a><span>Kreuzberg, Tue to Sat</span></li>
    <li data-dio-lat="52.36" data-dio-lng="4.89"><a href="https://www.openstreetmap.org/#map=15/52.355/4.894">Amsterdam</a><span>De Pijp, Tue to Sat</span></li>
    <li data-dio-lat="51.52" data-dio-lng="-0.11"><a href="https://www.openstreetmap.org/#map=15/51.524/-0.106">London</a><span>Clerkenwell, Mon to Sat</span></li>
    <li data-dio-lat="45.47" data-dio-lng="9.19"><a href="https://www.openstreetmap.org/#map=15/45.472/9.187">Milan</a><span>Brera, Tue to Sat</span></li>
  </ul>
</div>
```

`dio:marker` fires on the globe each time a marker lights up or goes dark, with the element of the place in `detail.marker`. Use it for a tooltip or to show the hours of the showroom under the pointer.

```ts
document.querySelector('.earth').addEventListener('dio:marker', (e) => {
  tip.textContent =
    e.detail.marker?.querySelector('a')?.textContent ?? ''
})
```

### Routes from a JSON file

Hundreds of places do not belong in the markup. `data` loads `{ markers, arcs }` and adds them to any places in the page, and arcs may join places from both. `flow` runs a dash along each arc again and again, from the place that names the arc to the other, which reads as freight, flights or traffic.

```html
<div
  class="routes"
  data-dio="globe"
  data-dio-data="/data/routes.json"
  data-dio-motion="flow"
  data-dio-style="solid"
  data-dio-spin="1.8"
></div>
```

```json
{
  "markers": [
    { "id": "rtm", "lat": 51.95, "lng": 4.14 },
    { "id": "sin", "lat": 1.26, "lng": 103.84 },
    { "id": "lax", "lat": 33.74, "lng": -118.27 }
  ],
  "arcs": [
    { "from": "rtm", "to": "sin" },
    { "from": "sin", "to": "lax" }
  ]
}
```

### Brand colors

Every color is a param: the ocean in `color`, the land, markers, arcs and the glow along the rim. A light globe on a light page needs a darker `marker` and a stronger `glow` to keep its edge.

```html
<div
  class="globe"
  data-dio="globe"
  data-dio-color="#d3d7cf"
  data-dio-land="#6f766f"
  data-dio-marker="#1f2224"
  data-dio-arc="#1f2224"
  data-dio-atmosphere="#ffffff"
  data-dio-glow="0.5"
  data-dio-marker-size="9"
></div>
```

## Notes

It draws only while something moves: spin or flowing arcs at most `fps` frames a second, and a drag, a turn to a place or a marker lighting up at full rate. Set `spin` to 0 and keep `draw` for a globe that costs no frames at rest. The `low` tier draws 4000 dots and spins at 20 frames a second.

Land dots take about 12 bytes each, 40 KB at the default 12000 dots over the sphere. `solid` keeps a 260 KB land mask texture on the GPU instead. The land data is Natural Earth, in the public domain.

A finger turns the globe only with `touch-action: pan-y` or `none` on the element; otherwise it scrolls the page. Nothing hovers on touch screens, so a tap on a marker works as a click.

Under reduced motion the globe does not spin or coast, arcs appear whole without a flowing dash, and turns to a place happen in one step. Places inside the globe are hidden with it but keep their focus, so the keyboard reaches each marker through its button or link.

## Credits

Land data: Natural Earth (public domain)

## Related

- [chart](/docs/effects/chart.md): A plain HTML table or definition list drawn as an animated chart that grows in on scroll.
- [graph](/docs/effects/graph.md): A list or a table of links drawn as a network graph: partners, integrations, teams, knowledge maps.
- [heatmap](/docs/effects/heatmap.md): A table drawn as a heatmap: a matrix of rows and columns, or a calendar of days like a contributions chart.
- [map](/docs/effects/map.md): A flat map of the world or a region with markers for a list of places and arcs between them.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
