update on rustjs

This commit is contained in:
Artem
2026-09-30 11:55:37 +03:00
parent 38d58bcf8d
commit 82318aaf6c
4 changed files with 222 additions and 99 deletions
-19
View File
@@ -8,7 +8,6 @@
"name": "gorodar-front", "name": "gorodar-front",
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"@2gis/mapgl": "^1.78.1",
"@angular/animations": "^20.3.32", "@angular/animations": "^20.3.32",
"@angular/cdk": "^20.2.14", "@angular/cdk": "^20.2.14",
"@angular/common": "^20.3.0", "@angular/common": "^20.3.0",
@@ -45,18 +44,6 @@
"typescript": "~5.9.2" "typescript": "~5.9.2"
} }
}, },
"node_modules/@2gis/mapgl": {
"version": "1.78.1",
"resolved": "https://registry.npmjs.org/@2gis/mapgl/-/mapgl-1.78.1.tgz",
"integrity": "sha512-SkbrqwBMPTLV4hihV+BgAtftIfyMeSru6yqlVF0IJEE6jaGQ8LFq0NJb1TTaXt3I1t3q8sjvU7X/hhHpxo5JXQ==",
"license": "BSD-2-Clause",
"dependencies": {
"@types/geojson": "^7946.0.7"
},
"engines": {
"node": ">=16.0.0"
}
},
"node_modules/@algolia/abtesting": { "node_modules/@algolia/abtesting": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/@algolia/abtesting/-/abtesting-1.1.0.tgz", "resolved": "https://registry.npmjs.org/@algolia/abtesting/-/abtesting-1.1.0.tgz",
@@ -4548,12 +4535,6 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/geojson": {
"version": "7946.0.16",
"resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.16.tgz",
"integrity": "sha512-6C8nqWur3j98U6+lXDfTUWIfgvZU+EumvpHKcYjujKH7woYyLj2sUmff0tRhrqM7BohUw7Pz3ZB1jj2gW9Fvmg==",
"license": "MIT"
},
"node_modules/@types/jasmine": { "node_modules/@types/jasmine": {
"version": "5.1.15", "version": "5.1.15",
"resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-5.1.15.tgz", "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-5.1.15.tgz",
-1
View File
@@ -22,7 +22,6 @@
}, },
"private": true, "private": true,
"dependencies": { "dependencies": {
"@2gis/mapgl": "^1.78.1",
"@angular/animations": "^20.3.32", "@angular/animations": "^20.3.32",
"@angular/cdk": "^20.2.14", "@angular/cdk": "^20.2.14",
"@angular/common": "^20.3.0", "@angular/common": "^20.3.0",
+202 -76
View File
@@ -10,7 +10,6 @@ import {
inject, inject,
signal, signal,
} from '@angular/core'; } from '@angular/core';
import { load } from '@2gis/mapgl';
import { Geolocation, type Position } from '@capacitor/geolocation'; import { Geolocation, type Position } from '@capacitor/geolocation';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
@@ -20,16 +19,104 @@ import type { Landmark } from '../../data/nizhny-novgorod';
import { Icon } from '../../shared/icon/icon'; import { Icon } from '../../shared/icon/icon';
import { selectCollectedIdSet } from '../../state'; import { selectCollectedIdSet } from '../../state';
const MAPGL_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b'; const MAP_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b';
const MAP_STYLE_NIGHT = 'e05ac437-fcc2-4845-ad74-b1de9ce07555';
const CITY_ZOOM = 13; const CITY_ZOOM = 13;
const USER_ZOOM = 16; const USER_ZOOM = 16;
const LANDMARK_SIZE = 40; const LANDMARK_SIZE = 40;
const USER_SIZE = 32; const USER_SIZE = 32;
type MapglApi = Awaited<ReturnType<typeof load>>; interface DgPoint {
type MapglMap = InstanceType<MapglApi['Map']>; x: number;
type MapglHtmlMarker = InstanceType<MapglApi['HtmlMarker']>; y: number;
}
interface DgMarker {
addTo(map: DgMap): this;
setLatLng(latLng: [number, number]): this;
setZIndexOffset(offset: number): this;
getElement(): HTMLElement | undefined;
on(type: string, handler: (event: Event) => void): this;
}
interface DgMap {
on(type: string, handler: () => void): this;
off(type: string, handler: () => void): this;
setView(
center: [number, number],
zoom: number,
options?: { animate?: boolean; duration?: number },
): this;
getZoom(): number;
latLngToContainerPoint(latLng: [number, number]): DgPoint;
remove(): void;
}
interface DgLoader {
then(resolve: () => void, reject?: (error: unknown) => void): Promise<void>;
}
interface DgApi {
map(element: HTMLElement, options: Record<string, unknown>): DgMap;
marker(latLng: [number, number], options: Record<string, unknown>): DgMarker;
divIcon(options: Record<string, unknown>): unknown;
}
declare global {
interface Window {
DG?: DgLoader & DgApi;
}
}
let rasterApi: Promise<DgApi> | null = null;
function loadRasterApi(): Promise<DgApi> {
if (rasterApi) {
return rasterApi;
}
rasterApi = new Promise<DgApi>((resolve, reject) => {
const ready = (): void => {
const api = window.DG;
if (!api) {
reject(new Error('RasterJS API не загрузился'));
return;
}
void api.then(
() => {
const readyApi = window.DG;
if (!readyApi?.map || !readyApi.marker || !readyApi.divIcon) {
reject(new Error('RasterJS API не загрузился'));
return;
}
resolve({
map: readyApi.map.bind(readyApi),
marker: readyApi.marker.bind(readyApi),
divIcon: readyApi.divIcon.bind(readyApi),
});
},
(error: unknown) => reject(error),
);
};
if (window.DG) {
ready();
return;
}
const script = document.createElement('script');
script.src = `https://maps.api.2gis.ru/2.0/loader.js?pkg=full&lazy=true&skin=dark&key=${MAP_KEY}`;
script.async = true;
script.onload = ready;
script.onerror = () => reject(new Error('Не удалось загрузить RasterJS API'));
document.body.appendChild(script);
});
return rasterApi;
}
@Component({ @Component({
selector: 'app-city-map', selector: 'app-city-map',
@@ -45,17 +132,32 @@ export class CityMap implements AfterViewInit, OnDestroy {
private readonly catalog = inject(CityCatalogService); private readonly catalog = inject(CityCatalogService);
private readonly store = inject(Store); private readonly store = inject(Store);
private readonly zone = inject(NgZone); private readonly zone = inject(NgZone);
private mapgl: MapglApi | null = null; private raster: DgApi | null = null;
private map: MapglMap | null = null; private map: DgMap | null = null;
private userMarker: MapglHtmlMarker | null = null; private userMarker: DgMarker | null = null;
private userMarkerElement: HTMLElement | null = null; private userMarkerElement: HTMLElement | null = null;
private watchId: string | null = null; private watchId: string | null = null;
private lastPosition: Position | null = null; private lastPosition: Position | null = null;
private zoomOnNextPan = true; private zoomOnNextPan = true;
private destroyed = false; private destroyed = false;
private programmaticMove = false;
private readonly markerElements = new Map<string, HTMLElement>(); private readonly markerElements = new Map<string, HTMLElement>();
private readonly markers = new Map<string, MapglHtmlMarker>(); private readonly markers = new Map<string, DgMarker>();
private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet); private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet);
private readonly stopFollowing = (): void => {
this.programmaticMove = false;
this.zone.run(() => this.following.set(false));
};
private readonly onZoomStart = (): void => {
if (this.programmaticMove) {
return;
}
this.stopFollowing();
};
private readonly onMoveEnd = (): void => {
this.programmaticMove = false;
};
protected readonly city = this.catalog.city; protected readonly city = this.catalog.city;
protected readonly selected = signal<Landmark | null>(null); protected readonly selected = signal<Landmark | null>(null);
@@ -72,49 +174,45 @@ export class CityMap implements AfterViewInit, OnDestroy {
} }
async ngAfterViewInit(): Promise<void> { async ngAfterViewInit(): Promise<void> {
const mapgl = await load(); const api = await loadRasterApi();
this.raster = api;
if (this.destroyed) { if (this.destroyed) {
return; return;
} }
this.mapgl = mapgl;
this.zone.runOutsideAngular(() => { this.zone.runOutsideAngular(() => {
if (this.destroyed) { const raster = this.raster;
if (this.destroyed || !raster) {
return; return;
} }
const map = new mapgl.Map(this.mapElement.nativeElement, { const map = raster.map(this.mapElement.nativeElement, {
center: this.city.center, center: this.toLatLng(this.city.center),
zoom: CITY_ZOOM, zoom: CITY_ZOOM,
key: MAPGL_KEY, key: MAP_KEY,
lang: 'ru', currentLang: 'ru',
style: MAP_STYLE_NIGHT,
defaultBackgroundColor: '#12110f',
zoomControl: false, zoomControl: false,
disablePitchByUserInteraction: true, fullscreenControl: false,
disableRotationByUserInteraction: true, poi: false,
enableTrackResize: true, geoclicker: false,
}); });
if (this.destroyed) { if (this.destroyed) {
map.destroy(); map.remove();
return; return;
} }
this.map = map; this.map = map;
map.on('movestart', (event) => { map.on('dragstart', this.stopFollowing);
if (!event.isUser) { map.on('zoomstart', this.onZoomStart);
return; map.on('zoom', () => this.syncRadiusSize());
}
this.zone.run(() => this.following.set(false));
});
map.on('move', () => this.syncRadiusSize()); map.on('move', () => this.syncRadiusSize());
map.on('moveend', this.onMoveEnd);
for (const landmark of this.city.objects) { for (const landmark of this.city.objects) {
this.markers.set(landmark.id, this.createMarker(mapgl, map, landmark)); this.markers.set(landmark.id, this.createMarker(raster, map, landmark));
} }
}); });
} }
@@ -122,7 +220,10 @@ export class CityMap implements AfterViewInit, OnDestroy {
ngOnDestroy(): void { ngOnDestroy(): void {
this.destroyed = true; this.destroyed = true;
void this.stopWatching(); void this.stopWatching();
this.map?.destroy(); this.map?.off('dragstart', this.stopFollowing);
this.map?.off('zoomstart', this.onZoomStart);
this.map?.off('moveend', this.onMoveEnd);
this.map?.remove();
this.map = null; this.map = null;
} }
@@ -184,25 +285,25 @@ export class CityMap implements AfterViewInit, OnDestroy {
const coordinates: [number, number] = [position.coords.longitude, position.coords.latitude]; const coordinates: [number, number] = [position.coords.longitude, position.coords.latitude];
const heading = position.coords.heading; const heading = position.coords.heading;
if (!this.userMarker && this.mapgl) { const raster = this.raster;
if (!this.userMarker && raster) {
const markerElement = document.createElement('div'); const markerElement = document.createElement('div');
markerElement.className = 'city-map__user'; markerElement.className = 'city-map__user';
markerElement.innerHTML = markerElement.innerHTML =
'<span class="city-map__user-body"><span class="city-map__user-heading"></span><span class="city-map__user-dot"></span></span>'; '<span class="city-map__user-body"><span class="city-map__user-heading"></span><span class="city-map__user-dot"></span></span>';
this.userMarkerElement = markerElement; this.userMarkerElement = markerElement;
this.userMarker = new this.mapgl.HtmlMarker(map, { this.userMarker = this.addHtmlMarker(
raster,
map,
coordinates, coordinates,
html: markerElement, markerElement,
anchor: [USER_SIZE / 2, USER_SIZE / 2], USER_SIZE,
interactive: false, 3,
preventMapInteractions: false, false,
disableRounding: true, );
labeling: { type: 'none' },
zIndex: 3,
});
this.userMarker.getContent().style.overflow = 'visible';
} else { } else {
this.userMarker?.setCoordinates(coordinates); this.userMarker?.setLatLng(this.toLatLng(coordinates));
} }
if (heading != null && !Number.isNaN(heading) && this.userMarkerElement) { if (heading != null && !Number.isNaN(heading) && this.userMarkerElement) {
@@ -225,7 +326,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
this.watchId = null; this.watchId = null;
} }
private createMarker(mapgl: MapglApi, map: MapglMap, landmark: Landmark): MapglHtmlMarker { private createMarker(api: DgApi, map: DgMap, landmark: Landmark): DgMarker {
const markerElement = document.createElement('button'); const markerElement = document.createElement('button');
markerElement.type = 'button'; markerElement.type = 'button';
markerElement.className = 'city-map__marker'; markerElement.className = 'city-map__marker';
@@ -240,21 +341,50 @@ export class CityMap implements AfterViewInit, OnDestroy {
this.markerElements.set(landmark.id, markerElement); this.markerElements.set(landmark.id, markerElement);
this.setMarkerIcon(markerElement, this.collectedIds().has(landmark.id)); this.setMarkerIcon(markerElement, this.collectedIds().has(landmark.id));
markerElement.addEventListener('click', (event) => { const marker = this.addHtmlMarker(
event.stopPropagation(); api,
map,
landmark.coordinates,
markerElement,
LANDMARK_SIZE,
1,
true,
);
marker.on('click', () => {
this.zone.run(() => this.selectLandmark(landmark)); this.zone.run(() => this.selectLandmark(landmark));
}); });
const marker = new mapgl.HtmlMarker(map, { return marker;
coordinates: landmark.coordinates, }
html: markerElement,
anchor: [LANDMARK_SIZE / 2, LANDMARK_SIZE / 2], private addHtmlMarker(
interactive: true, api: DgApi,
disableRounding: true, map: DgMap,
labeling: { type: 'none' }, coordinates: [number, number],
zIndex: 1, element: HTMLElement,
size: number,
zIndexOffset: number,
interactive: boolean,
): DgMarker {
const marker = api.marker(this.toLatLng(coordinates), {
icon: api.divIcon({
className: 'city-map__pin',
html: element,
iconSize: [size, size],
iconAnchor: [size / 2, size / 2],
}),
interactive,
keyboard: false,
zIndexOffset,
}); });
marker.getContent().style.overflow = 'visible';
marker.addTo(map);
const root = marker.getElement();
if (root) {
root.style.overflow = 'visible';
}
return marker; return marker;
} }
@@ -293,7 +423,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
for (const [id, element] of this.markerElements) { for (const [id, element] of this.markerElements) {
const active = id === selectedId; const active = id === selectedId;
element.classList.toggle('city-map__marker--active', active); element.classList.toggle('city-map__marker--active', active);
this.markers.get(id)?.setZIndex(active ? 2 : 1); this.markers.get(id)?.setZIndexOffset(active ? 2 : 1);
} }
} }
@@ -315,37 +445,33 @@ export class CityMap implements AfterViewInit, OnDestroy {
element.style.setProperty('--radius-px', `${diameter}px`); element.style.setProperty('--radius-px', `${diameter}px`);
} }
private metersToPixels( private metersToPixels(map: DgMap, meters: number, coordinates: [number, number]): number {
map: MapglMap,
meters: number,
coordinates: [number, number],
): number {
const [longitude, latitude] = coordinates; const [longitude, latitude] = coordinates;
const origin = map.project(coordinates); const origin = map.latLngToContainerPoint(this.toLatLng(coordinates));
const metersPerDegree = 111_320 * Math.cos((latitude * Math.PI) / 180); const metersPerDegree = 111_320 * Math.cos((latitude * Math.PI) / 180);
const shifted = map.project([ const shifted = map.latLngToContainerPoint([
longitude + meters / Math.max(metersPerDegree, 1),
latitude, latitude,
longitude + meters / Math.max(metersPerDegree, 1),
]); ]);
return Math.hypot(shifted[0] - origin[0], shifted[1] - origin[1]); return Math.hypot(shifted.x - origin.x, shifted.y - origin.y);
} }
private moveMap(center: [number, number], zoom: number | null, duration: number): void { private moveMap(center: [number, number], zoom: number | null, durationMs: number): void {
const map = this.map; const map = this.map;
if (!map) { if (!map) {
return; return;
} }
map.setCenter(center, { duration, easing: 'easeOutCubic' }); this.programmaticMove = true;
map.setView(this.toLatLng(center), zoom ?? map.getZoom(), {
animate: true,
duration: durationMs / 1000,
});
}
if (zoom != null) { private toLatLng(coordinates: [number, number]): [number, number] {
map.setZoom(zoom, { return [coordinates[1], coordinates[0]];
duration,
easing: 'easeOutCubic',
useHeightForAnimation: true,
});
}
} }
} }
+20 -3
View File
@@ -140,7 +140,24 @@
} }
} }
.city-map__canvas :has(> .city-map__marker), .city-map__canvas .leaflet-container {
.city-map__canvas :has(> .city-map__user) { width: 100%;
overflow: visible !important; height: 100%;
background: #12110f;
font: inherit;
}
.city-map__canvas .leaflet-tile-pane {
filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.92) saturate(0.65);
}
.city-map__canvas .leaflet-div-icon,
.city-map__pin {
background: transparent;
border: 0;
}
.city-map__canvas .leaflet-marker-icon,
.city-map__canvas .leaflet-marker-pane {
overflow: visible;
} }