@swr-data-lab/components 2.30.0 → 2.31.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- <script lang="ts">let { weight = 'regular', as = 'div', children } = $props();
1
+ <script lang="ts">let { weight = 'regular', as = 'div', children, ...rest } = $props();
2
2
  export {};
3
3
  </script>
4
4
 
5
- <svelte:element this={as} class={['container', weight]}>
5
+ <svelte:element this={as} class={['container', weight]} {...rest}>
6
6
  {@render children?.()}
7
7
  </svelte:element>
8
8
 
@@ -1,8 +1,8 @@
1
- <script lang="ts">let { as = 'div', weight = 'regular', children } = $props();
1
+ <script lang="ts">let { as = 'div', weight = 'regular', children, ...rest } = $props();
2
2
  export {};
3
3
  </script>
4
4
 
5
- <svelte:element this={as} class={['container', weight]}>
5
+ <svelte:element this={as} class={['container', weight]} {...rest}>
6
6
  {@render children?.()}
7
7
  </svelte:element>
8
8
 
@@ -1,8 +1,8 @@
1
- <script lang="ts">let { as = 'h2', children } = $props();
1
+ <script lang="ts">let { as = 'h2', children, ...rest } = $props();
2
2
  export {};
3
3
  </script>
4
4
 
5
- <svelte:element this={as} class="container">
5
+ <svelte:element this={as} class="container" {...rest}>
6
6
  {@render children?.()}
7
7
  </svelte:element>
8
8
 
@@ -1,8 +1,8 @@
1
- <script lang="ts">let { as = 'div', children } = $props();
1
+ <script lang="ts">let { as = 'div', children, ...rest } = $props();
2
2
  export {};
3
3
  </script>
4
4
 
5
- <svelte:element this={as} class="container">
5
+ <svelte:element this={as} class="container" {...rest}>
6
6
  {@render children?.()}
7
7
  </svelte:element>
8
8
 
@@ -1,14 +1,14 @@
1
1
  <script lang="ts">import {} from 'maplibre-gl';
2
+ import { onDestroy, onMount } from 'svelte';
2
3
  import MapSource from '../Source';
3
4
  import { getMapContext } from '../context.svelte.js';
4
5
  import quadraticToPoints from './quadraticToPoints';
5
- import { onDestroy } from 'svelte';
6
6
  const { map } = $derived(getMapContext());
7
7
  const { id, arrows = [], attribution = '' } = $props();
8
8
  const ars = arrows.map((a) => {
9
9
  return {
10
10
  width: a.width || 10,
11
- points: quadraticToPoints(a.a, a.b, a.c, 10),
11
+ points: quadraticToPoints(a.a, a.b, a.c),
12
12
  headScale: a.headScale
13
13
  };
14
14
  });
@@ -58,7 +58,7 @@ let sourceSpec = $state({
58
58
  const onZoom = () => {
59
59
  sourceSpec = { ...sourceSpec, data: arrowsToJson(ars) };
60
60
  };
61
- $effect(() => {
61
+ onMount(() => {
62
62
  map?.on('zoom', onZoom);
63
63
  });
64
64
  onDestroy(() => {
@@ -1,4 +1,4 @@
1
- type V2 = [number, number];
1
+ import type { V2 } from '../types';
2
2
  interface ArrowSpec {
3
3
  a: V2;
4
4
  b: V2;
@@ -1,12 +1,11 @@
1
+ import type { V2 } from '../types';
1
2
  /**
2
- Given a quadratic bezier defined by points a, b and c,
3
- returns a series of n points on the curve
4
-
5
- The quadratic bezier is: B(t) = (1-t)[(1-t)P0 + tP1] + t[(1-t)P1+tP2]
6
- Source: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves
7
-
8
- Below is a naive implementation but good enough for now. For a better approach see:
9
- https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves
3
+ * Given a quadratic Bezier defined by points a, b and c
4
+ * returns a series of evenly-spaced points on the curve
5
+ *
6
+ * Sources:
7
+ * - https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves
8
+ * - https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves
10
9
  */
11
- declare const quadraticToPoints: (a: [number, number], b: [number, number], c: [number, number], n?: number) => [number, number][];
10
+ declare const quadraticToPoints: (a: V2, b: V2, c: V2, n?: number) => V2[];
12
11
  export default quadraticToPoints;
@@ -1,21 +1,64 @@
1
1
  /**
2
- Given a quadratic bezier defined by points a, b and c,
3
- returns a series of n points on the curve
4
-
5
- The quadratic bezier is: B(t) = (1-t)[(1-t)P0 + tP1] + t[(1-t)P1+tP2]
6
- Source: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves
7
-
8
- Below is a naive implementation but good enough for now. For a better approach see:
9
- https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves
2
+ * Returns the euclidian distance between two points
10
3
  */
11
- const quadraticToPoints = (a, b, c, n = 10) => {
4
+ const distance = (a, b) => {
5
+ return Math.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2);
6
+ };
7
+ /**
8
+ * Given a sorted array arr and a value, returns the index i of arr
9
+ * such that arr[i] < value && arr[i + 1] > value using binary search
10
+ */
11
+ const getSortedIndex = (arr, v) => {
12
+ let low = 0, high = arr.length;
13
+ while (low < high) {
14
+ let mid = (low + high) >>> 1; // === Math.abs((low + high) * .5) but faster
15
+ if (arr[mid] < v)
16
+ low = mid + 1;
17
+ else
18
+ high = mid;
19
+ }
20
+ return low;
21
+ };
22
+ /**
23
+ * Given a polyline, returns n evenly-spaced points on the polyline
24
+ */
25
+ const interpolatePolyline = (points, n) => {
26
+ let res = [], totalLength = 0, segments = [0];
27
+ for (let i = 0; i < points.length - 2; i++) {
28
+ totalLength += distance(points[i], points[i + 1]);
29
+ segments.push(totalLength);
30
+ }
31
+ for (let i = 0; i < n; i++) {
32
+ const d = totalLength * (i / n);
33
+ const si = getSortedIndex(segments, d);
34
+ const p0 = points[si];
35
+ const p1 = points[si + 1];
36
+ const sn = [p1[0] - p0[0], p1[1] - p0[1]];
37
+ const segmentFraction = (d - segments[si]) / distance(p0, p1);
38
+ res.push([p1[0] + sn[0] * segmentFraction, p1[1] + sn[1] * segmentFraction]);
39
+ }
40
+ return res;
41
+ };
42
+ /**
43
+ * Given a quadratic Bezier defined by points a, b and c
44
+ * returns a series of evenly-spaced points on the curve
45
+ *
46
+ * Sources:
47
+ * - https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves
48
+ * - https://bit-101.com/blog/posts/2024-09-29/evenly-placed-points-on-bezier-curves
49
+ */
50
+ const quadraticToPoints = (a, b, c, n = 25) => {
12
51
  let points = [];
52
+ // 1. Interpolate the quadratic bezier function to obtain an uneven polyline
13
53
  for (let i = 0; i < n; i++) {
14
54
  const t = i / n;
15
- const x = (1 - t) * ((1 - t) * a[0] + t * c[0]) + t * ((1 - t) * c[0] + t * b[0]);
16
- const y = (1 - t) * ((1 - t) * a[1] + t * c[1]) + t * ((1 - t) * c[1] + t * b[1]);
17
- points.push([x, y]);
55
+ points.push([
56
+ (1 - t) * ((1 - t) * a[0] + t * c[0]) + t * ((1 - t) * c[0] + t * b[0]),
57
+ (1 - t) * ((1 - t) * a[1] + t * c[1]) + t * ((1 - t) * c[1] + t * b[1])
58
+ ]);
18
59
  }
19
- return [...points, b];
60
+ // 2. Interpolate the polyline from (1) to get evenly-spaced points
61
+ points = interpolatePolyline([...points, b], n);
62
+ return points;
20
63
  };
21
64
  export default quadraticToPoints;
@@ -3,6 +3,7 @@ export type Layer = LineLayerSpecification | FillLayerSpecification | FillExtrus
3
3
  export type GeocodingService = 'maptiler';
4
4
  export type GeocodingCountry = 'de' | 'at';
5
5
  export type GeocodingLanguage = 'de' | 'en';
6
+ export type V2 = [number, number];
6
7
  export interface Location {
7
8
  lat: number;
8
9
  lng: number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@swr-data-lab/components",
3
3
  "description": "SWR Data Lab component library",
4
- "version": "2.30.0",
4
+ "version": "2.31.1",
5
5
  "author": "SWR Data Lab",
6
6
  "license": "UNLICENSED",
7
7
  "type": "module",