UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 3.17 kB
import{LngLatBounds as R}from"maplibre-gl";import{fpGeo as T}from"../../../data/fpGeo";import{svgArea as c}from"../../../data/svg";import{uiState as d}from"../../../store";import{convertLocalToGps as f}from"../../../utils/gps";import{planBearing as v}from"../../../utils/planBearing";export const MAP_BOUNDS_PADDING_FACTOR=3,MAP_BOUNDS_MIN_PADDING_DEGREES=.02,INITIAL_FIT_ZOOM_ADJUSTMENT=.15;const p=1.42,M=.35,z=.9,D=.85,E=8,A=.05;export function deriveVenueMapBounds(t){const n=[[c.min.x,c.min.y],[c.max.x,c.min.y],[c.max.x,c.max.y],[c.min.x,c.max.y]].map(([a,m])=>f(a,m,t));if(!n.every(([a,m])=>Number.isFinite(a)&&Number.isFinite(m)))return;const e=n.map(([a])=>a),o=n.map(([,a])=>a),i=Math.min(...e),r=Math.max(...e),s=Math.min(...o),u=Math.max(...o),x=Math.max((r-i)*MAP_BOUNDS_PADDING_FACTOR,MAP_BOUNDS_MIN_PADDING_DEGREES),h=Math.max((u-s)*MAP_BOUNDS_PADDING_FACTOR,MAP_BOUNDS_MIN_PADDING_DEGREES);return[[i-x,s-h],[r+x,u+h]]}function _(t,n,e,o){const i=Math.min(t.getZoom(),n),r=(e-i)*Math.min(1,o/p);return i+r>=t.getMinZoom()-A}function I(t,n,e){if(_(t,n,e,p))return p;if(!_(t,n,e,M))return null;let o=M,i=p;for(let r=0;r<E;r++){const s=(o+i)/2;_(t,n,e,s)?o=s:i=s}return o}export function getLocalRectGeoBounds(t,n){const e=[f(t.min.x,t.min.y,n),f(t.max.x,t.min.y,n),f(t.max.x,t.max.y,n),f(t.min.x,t.max.y,n)],o=e.map(([r])=>r),i=e.map(([,r])=>r);return[[Math.min(...o),Math.min(...i)],[Math.max(...o),Math.max(...i)]]}export function getVisibleRectFitPadding(t=0){const n=d.canvasVisibleRectPx,e=d.screenSize;return!n||!e?{top:t,bottom:t,left:t,right:t}:{top:Math.max(0,n.min.y)+t,bottom:Math.max(0,e.height-n.max.y)+t,left:Math.max(0,n.min.x)+t,right:Math.max(0,e.width-n.max.x)+t}}export function isLngLatInVisibleViewport(t,n,e){const o=t.project(n),i=t.getCanvas();return o.x>=e.left&&o.x<=i.clientWidth-e.right&&o.y>=e.top&&o.y<=i.clientHeight-e.bottom}export function deriveGeoConfig(){if(!T?.properties?.config)return null;const t=T.properties.config,n=(t.p0.lng+t.p2.lng)/2,e=(t.p0.lat+t.p2.lat)/2,o=v(t.p0,t.p2),i=Number.isFinite(o)?o:t.bearing??0;return{center:[n,e],bearing:i,zoom:16,gpsConfig:t}}export function fitMapToLocalRect(t,n,e,o={}){if(!t||!n||!e||n.size.height===1/0||n.size.width===1/0||n.size.height<=0||n.size.width<=0)return;const i=getLocalRectGeoBounds(n,e.gpsConfig),r=getVisibleRectFitPadding(o.padding??50),s=t.cameraForBounds(i,{padding:r,bearing:e.bearing});if(!s?.center||typeof s.zoom!="number"){t.fitBounds(i,{padding:r,bearing:e.bearing,essential:!0});return}const u=t.project(t.getCenter()),x=t.project(s.center),h=Math.max(t.getCanvas().clientWidth,t.getCanvas().clientHeight,1),a=Math.hypot(x.x-u.x,x.y-u.y)/h,m=Math.abs(t.getZoom()-s.zoom),y=o.duration??Math.min(2800,500+a*450+m*150),l={center:s.center,zoom:s.zoom,bearing:e.bearing,duration:y,essential:!0};if(a<=1.2&&m<=1.5){t.easeTo(l);return}const L=t.getBounds().extend(R.convert(i)),g=t.cameraForBounds(L,{padding:r,bearing:e.bearing});if(!g?.center||typeof g.zoom!="number"||g.zoom>=t.getZoom()-.2){t.easeTo(l);return}const b=I(t,s.zoom,g.zoom);if(b!==null){const O=Math.max(M,b*z),F=Math.round(y*D);t.flyTo({...l,curve:O,duration:F});return}t.easeTo(l)}