tons of feature
This commit is contained in:
37
lib/pointEngine.ts
Normal file
37
lib/pointEngine.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import maplibregl from "maplibre-gl";
|
||||
import { Geometry } from "@/lib/useEditorState";
|
||||
|
||||
type ModeGetter = () => "idle" | "draw" | "select" | "add-point";
|
||||
|
||||
export function initPoint(
|
||||
map: maplibregl.Map,
|
||||
getMode: ModeGetter,
|
||||
onComplete: (geometry: Geometry) => void
|
||||
) {
|
||||
/**
|
||||
* Add a new point when in add-point mode.
|
||||
*/
|
||||
function onClick(e: maplibregl.MapLayerMouseEvent) {
|
||||
if (getMode() !== "add-point") return;
|
||||
|
||||
const geometry = {
|
||||
type: "Point",
|
||||
coordinates: [e.lngLat.lng, e.lngLat.lat],
|
||||
};
|
||||
|
||||
onComplete?.(geometry);
|
||||
}
|
||||
|
||||
function onMove() {
|
||||
if (getMode() !== "add-point") return;
|
||||
map.getCanvas().style.cursor = "crosshair";
|
||||
}
|
||||
|
||||
map.on("click", onClick);
|
||||
map.on("mousemove", onMove);
|
||||
|
||||
return () => {
|
||||
map.off("click", onClick);
|
||||
map.off("mousemove", onMove);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user