-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
+page.svelte
275 lines (233 loc) · 7.78 KB
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<script lang="ts">
import { geoOrthographic, geoCentroid } from 'd3-geo';
import { feature } from 'topojson-client';
import { index } from 'd3-array';
import { mdiPlay, mdiStop } from '@mdi/js';
import {
Canvas,
Chart,
GeoPath,
Graticule,
HitCanvas,
Tooltip,
TransformContext,
Svg,
} from 'layerchart';
import { Button, scrollIntoView, cls, sortFunc, ButtonGroup, timerStore } from 'svelte-ux';
import Preview from '$lib/docs/Preview.svelte';
import GeoDebug from '$lib/docs/GeoDebug.svelte';
import TransformDebug from '$lib/docs/TransformDebug.svelte';
import { timings } from './timings.js';
export let data;
const countries = feature(data.geojson, data.geojson.objects.countries);
let transformContext: TransformContext;
let transformContext2: TransformContext; // second instance for canvas example
let selectedFeature;
$: if (selectedFeature) {
const centroid = geoCentroid(selectedFeature);
transformContext.setTranslate({
x: -centroid[0],
y: -centroid[1],
});
transformContext2.setTranslate({
x: -centroid[0],
y: -centroid[1],
});
}
// Animate to Yakko's song
// https://animaniacs.fandom.com/wiki/Yakko%27s_World_(song)#New_Updated_Verse
// https://www.youtube.com/watch?v=BoaLSUKeGWw
// https://www.youtube.com/watch?v=5pOFKmk7ytU
const countryFeaturesByName = index(countries.features, (f) => f.properties.name);
const countryTimings = Object.entries(timings).map(([timing, country], index) => {
const [hours, minutes, seconds, milli] = timing.split(':');
return {
country,
audioTime: +hours * 60 * 60 + +minutes * 60 + +seconds + +milli / 1000,
};
});
// Set to jump to a country
let currentIndex = -1;
let isPlaying = false;
$: if (isPlaying && $audioCurrentTime >= countryTimings[currentIndex + 1]?.audioTime) {
const countryName = countryTimings[currentIndex + 1].country;
selectedFeature = countryFeaturesByName.get(countryName);
currentIndex += 1;
}
const audioFile = new Audio('/audio/yakko_world.mp3');
audioFile.addEventListener('ended', () => stop());
const audioCurrentTime = timerStore({
initial: 0,
delay: 100,
onTick: () => audioFile.currentTime,
});
async function play() {
isPlaying = true;
audioFile.currentTime = currentIndex !== -1 ? countryTimings[currentIndex].audioTime : 0;
audioFile.play();
}
function stop() {
isPlaying = false;
audioFile.pause();
audioFile.currentTime = 0;
currentIndex = -1;
selectedFeature = null;
}
let debug = false;
</script>
<h1>Examples</h1>
<h2>SVG</h2>
<Preview data={countries}>
<div class="h-[600px] grid grid-cols-[224px,1fr] relative">
<div class="absolute top-0 right-0 z-10 flex items-center gap-3">
{#if isPlaying && selectedFeature}
<span class="text-sm px-2 py-1 font-semibold text-primary bg-primary/5 rounded-full">
{selectedFeature?.properties.name ?? ''}
</span>
{/if}
<ButtonGroup variant="fill-light" color="primary" size="sm">
<Button icon={mdiPlay} on:click={play} disabled={isPlaying} />
<Button icon={mdiStop} on:click={stop} disabled={!isPlaying} />
</ButtonGroup>
</div>
<div class="overflow-auto scrollbar-none">
{#each countries.features.sort(sortFunc('properties.name')) as country}
{@const isSelected = selectedFeature === country}
<div use:scrollIntoView={{ condition: isSelected }}>
<Button
variant={isSelected ? 'fill-light' : 'default'}
color={isSelected ? 'primary' : 'default'}
fullWidth
on:click={() => (selectedFeature = country)}
>
{country.properties.name}
</Button>
</div>
{/each}
</div>
<Chart
geo={{
projection: geoOrthographic,
fitGeojson: countries,
applyTransform: ['rotate'],
}}
transform={{
spring: { stiffness: 0.04 },
}}
bind:transformContext
let:tooltip
>
{#if debug}
<div class="absolute bottom-0 right-0 z-10 grid gap-1">
<GeoDebug />
<TransformDebug />
</div>
{/if}
<Svg>
<GeoPath geojson={{ type: 'Sphere' }} class="fill-blue-400/50" />
<Graticule class="stroke-surface-content/20" />
{#each countries.features as country}
<GeoPath
geojson={country}
class={cls(
'stroke-surface-content/50 fill-white cursor-pointer',
selectedFeature === country
? 'stroke-primary-900 fill-primary'
: 'hover:fill-gray-200'
)}
on:click={(e) => (selectedFeature = country)}
{tooltip}
/>
{/each}
</Svg>
<Tooltip>
<div slot="header" let:data>{data.properties.name}</div>
</Tooltip>
</Chart>
</div>
</Preview>
<h2>Canvas</h2>
<Preview data={countries}>
<div class="h-[600px] grid grid-cols-[224px,1fr] relative">
<div class="absolute top-0 right-0 z-10 flex items-center gap-3">
{#if isPlaying && selectedFeature}
<span class="text-sm px-2 py-1 font-semibold text-primary bg-primary/5 rounded-full">
{selectedFeature?.properties.name ?? ''}
</span>
{/if}
<ButtonGroup variant="fill-light" color="primary" size="sm">
<Button icon={mdiPlay} on:click={play} disabled={isPlaying} />
<Button icon={mdiStop} on:click={stop} disabled={!isPlaying} />
</ButtonGroup>
</div>
<div class="overflow-auto scrollbar-none">
{#each countries.features.sort(sortFunc('properties.name')) as country}
{@const isSelected = selectedFeature === country}
<div use:scrollIntoView={{ condition: isSelected }}>
<Button
variant={isSelected ? 'fill-light' : 'default'}
color={isSelected ? 'primary' : 'default'}
fullWidth
on:click={() => (selectedFeature = country)}
>
{country.properties.name}
</Button>
</div>
{/each}
</div>
<Chart
geo={{
projection: geoOrthographic,
fitGeojson: countries,
applyTransform: ['rotate'],
}}
transform={{ spring: { stiffness: 0.04 } }}
bind:transformContext={transformContext2}
let:tooltip
>
<Canvas>
<GeoPath geojson={{ type: 'Sphere' }} class="fill-blue-400/50" />
</Canvas>
<Canvas>
<Graticule class="stroke-surface-content/20" />
</Canvas>
<Canvas>
<GeoPath geojson={countries} class="stroke-surface-content/50 fill-white" />
</Canvas>
<Canvas>
<GeoPath geojson={selectedFeature} class="stroke-primary-900 fill-primary" />
</Canvas>
{#if tooltip.data}
<Canvas>
<GeoPath geojson={tooltip.data} class="fill-surface-content/20" />
</Canvas>
{/if}
<HitCanvas
let:nextColor
let:setColorData
on:pointermove={(e) => tooltip.show(e.detail.event, e.detail.data)}
on:pointerleave={tooltip.hide}
on:click={(e) => {
selectedFeature = e.detail.data;
}}
>
<GeoPath
render={(ctx, { geoPath }) => {
for (var feature of countries.features) {
const color = nextColor();
ctx.beginPath();
geoPath(feature);
ctx.fillStyle = color;
ctx.fill();
// Stroking shape seems to help with dark border, but there is still antialising and thus gaps
ctx.strokeStyle = color;
ctx.stroke();
setColorData(color, feature);
}
}}
/>
</HitCanvas>
<Tooltip header={(data) => data.properties.name} />
</Chart>
</div>
</Preview>