Curved lines between pairs of places, for flights, trade links or any from-to data.
<script lang="ts">
import {
Map,
MapArc,
MapMarker,
MapMarkerLabel,
distanceMeters,
type LngLatTuple,
type MapArcItem
} from '$lib/components/ui/map/index.js';
interface Flight extends MapArcItem {
code: string;
city: string;
}
const HUB: LngLatTuple = [8.5706, 50.0333];
const DESTINATIONS = [
{ code: 'LIS', city: 'Lisbon', lngLat: [-9.1359, 38.7742] },
{ code: 'DUB', city: 'Dublin', lngLat: [-6.2499, 53.4264] },
{ code: 'OSL', city: 'Oslo', lngLat: [11.1004, 60.1976] },
{ code: 'ATH', city: 'Athens', lngLat: [23.9484, 37.9364] },
{ code: 'IST', city: 'Istanbul', lngLat: [28.8146, 40.9769] },
{ code: 'MAD', city: 'Madrid', lngLat: [-3.5676, 40.4983] }
] satisfies { code: string; city: string; lngLat: LngLatTuple }[];
const METRES_PER_KM = 1000;
const flights: Flight[] = DESTINATIONS.map((place) => ({
from: HUB,
to: place.lngLat,
code: place.code,
city: place.city
}));
let selected = $state<Flight | null>(null);
let hovered = $state<Flight | null>(null);
const shown = $derived(hovered ?? selected);
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [9, 48], zoom: 3.4, bearing: 0, pitch: 0 }}>
<MapArc
arcs={flights}
width={3}
onclick={(flight) => (selected = flight)}
onhover={(flight) => (hovered = flight)}
/>
<MapMarker lngLat={HUB} aria-label="Frankfurt hub">
<MapMarkerLabel>FRA</MapMarkerLabel>
</MapMarker>
</Map>
<p
class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
aria-live="polite"
>
{#if shown}
FRA to {shown.code} ({shown.city}):
{Math.round(distanceMeters(shown.from, shown.to) / METRES_PER_KM)} km
{:else}
Point at or click an arc
{/if}
</p>
</div>
MapArc draws one curved line for each item in arcs. Each item needs a from and a to position as [longitude, latitude]. All arcs share one layer, so hundreds of them stay fast.
Usage
<script lang="ts">
import { Map, MapArc } from '$lib/components/ui/map/index.js';
const arcs = [
{ from: [8.5706, 50.0333], to: [-9.1359, 38.7742] },
{ from: [8.5706, 50.0333], to: [23.9484, 37.9364] }
] satisfies { from: [number, number]; to: [number, number] }[];
</script>
<Map class="h-96">
<MapArc {arcs} />
</Map>Curve
curvature sets how far each arc bends to the side, as a share of the straight distance between its ends. It is measured in longitude and latitude, so arcs far from the equator look a little flatter or steeper than ones near it. 0 draws a straight line. The default is 0.2.
Arcs bend to the left of the direction of travel. To bend the other way, use a negative curvature.
An arc that crosses the antimeridian, such as Tokyo to San Francisco, goes the short way over the Pacific.
Clicks and hover
Items can carry any extra fields. onclick gets the clicked item back with its index in arcs, so you can look up whatever you stored on it:
<MapArc
arcs={flights}
onclick={(flight, index) => {
selected = flight.code;
}}
/>The arc under the pointer is highlighted: it turns fully opaque and 2 pixels wider. Change that with hoverColor, hoverWidth and hoverOpacity. onhover gets the item and its index while the pointer is on an arc, and null for both when it leaves. When the arcs change, the highlight is cleared, because indexes then point at different items.
API Reference
MapArc
Props
| Name | Type | Description |
|---|---|---|
arcs required | T[] | Arcs to draw. Extra fields on each item come back in `onclick`. |
id | string | Source id; the line layer is `<id>-line`. Read once. |
curvature | number | How far each arc bends sideways, as a share of its length. 0 is straight. Defaults to 0.2. |
steps | number | Points along each arc. More is smoother. |
color | string | Line colour. Defaults to the theme's `--primary`. |
width | number | Line width in pixels. Defaults to 2. |
opacity | number | Line opacity, 0 to 1. Defaults to 0.8. |
dashArray | number[] | Dash pattern in line widths. |
hoverColor | string | Colour of the arc under the pointer. Defaults to `color`. |
hoverWidth | number | Width of the arc under the pointer. Defaults to `width` plus 2. |
hoverOpacity | number | Opacity of the arc under the pointer, 0 to 1. Defaults to 1. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
Events
| Name | Type | Description |
|---|---|---|
onclick | (arc: T, index: number, event: MapMouseEvent) => void | Called with the clicked arc and its index in `arcs`. |
onhover | (arc: T | null, index: number | null) => void | Called with the arc under the pointer and its index, and with `null` when it leaves. |