|
| 1 | +import { Tyria, TileLayer, MarkerLayer } from 'tyria'; |
| 2 | + |
| 3 | +const map = new Tyria(document.getElementById('map')!, { |
| 4 | + backgroundColor: '#051626', |
| 5 | + maxZoom: 7, |
| 6 | + minZoom: 1, |
| 7 | + zoomSnap: .5, |
| 8 | + padding: 80, |
| 9 | + bounds: [[0, 0], [81920, 114688]], |
| 10 | + // padding: { top: 16, bottom: 80, left: 16, right: 80 }, |
| 11 | +}); |
| 12 | + |
| 13 | +map.addLayer(new TileLayer({ |
| 14 | + source: (x, y, z) => `https://tiles.gw2.io/1/1/${z}/${x}/${y}.jpg`, |
| 15 | + bounds: [[0, 0], [81920, 114688]], |
| 16 | +})); |
| 17 | + |
| 18 | +const markers = new MarkerLayer({ |
| 19 | + icon: 'https://render.guildwars2.com/file/32633AF8ADEA696A1EF56D3AE32D617B10D3AC57/157353.png', |
| 20 | + iconSize: [32, 32], |
| 21 | + minZoom: 3, |
| 22 | +}); |
| 23 | +map.addLayer(markers) |
| 24 | + |
| 25 | +const waypoints = new Map(); |
| 26 | + |
| 27 | +fetch('https://api.guildwars2.com/v2/continents/1/floors/1').then((r) => r.json()).then((data) => { |
| 28 | + // @ts-expect-error test |
| 29 | + const wps = Object.values(data.regions ?? []).flatMap((region) => Object.values(region.maps ?? {}).flatMap((map) => Object.values(map.points_of_interest ?? {}).filter((poi) => poi.type === 'waypoint').map((poi) => ({ ...poi, map })))); |
| 30 | + wps.forEach((wp) => waypoints.set(wp.id, wp)); |
| 31 | + markers.add(...wps.map((wp) => ({ |
| 32 | + id: wp.id, |
| 33 | + position: wp.coord, |
| 34 | + }))) |
| 35 | +}); |
| 36 | + |
| 37 | +map.jumpTo({ center: [49432, 31440], zoom: 2.5 }); |
| 38 | +map.easeTo({ zoom: 3 }, { duration: 2000, easing: (x) => 1 - Math.pow(1 - x, 3) }); |
| 39 | + |
| 40 | +map.addEventListener('marker.over', (e) => document.getElementById('over')!.innerText = waypoints.get(e.markerId).name); |
| 41 | +map.addEventListener('marker.leave', () => document.getElementById('over')!.innerText = ''); |
| 42 | +map.addEventListener('marker.click', (e) => { |
| 43 | + const wp = waypoints.get(e.markerId); |
| 44 | + console.log(wp); |
| 45 | + map.easeTo({ contain: wp.map.continent_rect }) |
| 46 | +}); |
| 47 | + |
| 48 | +// @ts-expect-error test |
| 49 | +document.getElementById('debug')!.addEventListener('change', (e) => map.setDebug(e.target.checked)); |
| 50 | + |
| 51 | +document.getElementById('lionsarch')!.addEventListener('click', () => map.easeTo({ contain: [[48130, 30720], [50430, 32250]] })) |
| 52 | +document.getElementById('ascalon')!.addEventListener('click', () => map.easeTo({ contain: [[56682, 24700], [64500, 35800]], zoom: 3 })) |
| 53 | +document.getElementById('horn')!.addEventListener('click', () => map.easeTo({ contain: [[19328, 19048], [27296, 24800]] })) |
| 54 | +document.getElementById('cantha')!.addEventListener('click', () => map.easeTo({ contain: [[20576, 97840], [39056, 106256]] })) |
| 55 | + |
| 56 | +// @ts-expect-error debug |
| 57 | +window.map = map; |
0 commit comments