@math.gl/web-mercator 3.6.0-alpha.8 → 3.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es5/index.js +6 -0
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/web-mercator-utils.js +24 -6
- package/dist/es5/web-mercator-utils.js.map +1 -1
- package/dist/es5/web-mercator-viewport.js +2 -0
- package/dist/es5/web-mercator-viewport.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web-mercator-utils.js +23 -7
- package/dist/esm/web-mercator-utils.js.map +1 -1
- package/dist/esm/web-mercator-viewport.js +2 -0
- package/dist/esm/web-mercator-viewport.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/web-mercator-utils.d.ts +19 -0
- package/dist/web-mercator-utils.d.ts.map +1 -1
- package/dist/web-mercator-viewport.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/web-mercator-utils.ts +59 -9
- package/src/web-mercator-viewport.ts +3 -1
- package/dist/assert.js +0 -8
- package/dist/fit-bounds.js +0 -63
- package/dist/fly-to-viewport.js +0 -87
- package/dist/get-bounds.js +0 -51
- package/dist/index.js +0 -8
- package/dist/math-utils.js +0 -26
- package/dist/normalize-viewport-props.js +0 -41
- package/dist/web-mercator-utils.js +0 -290
- package/dist/web-mercator-viewport.js +0 -275
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/web-mercator-viewport.ts"],"names":["createMat4","zoomToScale","pixelsToWorld","lngLatToWorld","worldToLngLat","worldToPixels","altitudeToFovy","fovyToAltitude","DEFAULT_ALTITUDE","getProjectionMatrix","getDistanceScales","getViewMatrix","fitBounds","getBounds","mat4","vec2","vec3","WebMercatorViewport","constructor","props","width","height","viewport","equals","projectionMatrix","viewMatrix","lngLatZ","options","topLeft","worldPosition","projectPosition","coord","pixelProjectionMatrix","x","y","y2","length","xyz","targetZ","undefined","z","targetZWorld","distanceScales","unitsPerMeter","pixelUnprojectionMatrix","X","Y","Z","unprojectPosition","Number","isFinite","metersPerUnit","altitude","fovy","latitude","longitude","zoom","pitch","bearing","position","nearZMultiplier","farZMultiplier","scale","Math","max","center","push","add","mul","meterOffset","_initMatrices","Object","freeze","vpm","multiply","viewProjectionMatrix","m","translate","mInverse","invert","Error","projectFlat","lngLat","unprojectFlat","xy","getMapCenterByLngLatPosition","pos","fromLocation","toLocation","negate","newCenter","bounds","assign","corners","getBoundingRegion","west","min","map","p","east","south","north","getLocationAtPoint"],"mappings":";AACA,SAAQA,UAAR,QAAyB,cAAzB;AAEA,SACEC,WADF,EAEEC,aAFF,EAGEC,aAHF,EAIEC,aAJF,EAKEC,aALF,EAMEC,cANF,EAOEC,cAPF,EAQEC,gBARF,EASEC,mBATF,EAUEC,iBAVF,EAWEC,aAXF,QAaO,sBAbP;AAcA,OAAOC,SAAP,MAAsB,cAAtB;AACA,OAAOC,SAAP,MAAsB,cAAtB;AAGA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AACA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AACA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AAiDA,eAAe,MAAMC,mBAAN,CAA0B;AA+BvCC,EAAAA,WAAW,CAACC,KAA+B,GAAG;AAACC,IAAAA,KAAK,EAAE,CAAR;AAAWC,IAAAA,MAAM,EAAE;AAAnB,GAAnC,EAA0D;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA,oCAgI3DC,QAAD,IAAmD;AAC1D,UAAI,EAAEA,QAAQ,YAAYL,mBAAtB,CAAJ,EAAgD;AAC9C,eAAO,KAAP;AACD;;AAED,aACEK,QAAQ,CAACF,KAAT,KAAmB,KAAKA,KAAxB,IACAE,QAAQ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ACA,UAAMG,KAAK,GAAGtC,IAAI,CAACC,GAAL,CAAS,GAAG6B,OAAO,CAACI,GAAR,CAAaC,CAAD,IAAOA,CAAC,CAAC,CAAD,CAApB,CAAZ,CAAd;AACA,WAAO,CACL,CAACH,IAAD,EAAOK,KAAP,CADK,EAEL,CAACD,IAAD,EAAOE,KAAP,CAFK,CAAP;AAID;;AAQDP,EAAAA,iBAAiB,CAACnE,OAAqB,GAAG,EAAzB,EAAyC;AACxD,WAAOd,SAAS,CAAC,IAAD,EAAOc,OAAO,CAACa,CAAR,IAAa,CAApB,CAAhB;AACD;;AAKD8D,EAAAA,kBAAkB,CAAC;AAACpB,IAAAA,MAAD;AAASI,IAAAA;AAAT,GAAD,EAA6D;AAC7E,WAAO,KAAKD,4BAAL,CAAkC;AAACH,MAAAA,MAAD;AAASI,MAAAA;AAAT,KAAlC,CAAP;AACD;;AA9UsC","sourcesContent":["// View and Projection Matrix calculations for mapbox-js style map view properties\nimport {createMat4} from './math-utils';\n\nimport {\n zoomToScale,\n pixelsToWorld,\n lngLatToWorld,\n worldToLngLat,\n worldToPixels,\n altitudeToFovy,\n fovyToAltitude,\n DEFAULT_ALTITUDE,\n getProjectionMatrix,\n getDistanceScales,\n getViewMatrix,\n DistanceScales\n} from './web-mercator-utils';\nimport fitBounds from './fit-bounds';\nimport getBounds from './get-bounds';\nimport type {FitBoundsOptions} from './fit-bounds';\n\nimport * as mat4 from 'gl-matrix/mat4';\nimport * as vec2 from 'gl-matrix/vec2';\nimport * as vec3 from 'gl-matrix/vec3';\n\n/**\n * @param width=1 - Width of \"viewport\" or window\n * @param height=1 - Height of \"viewport\" or window\n * @param scale=1 - Either use scale or zoom\n * @param pitch=0 - Camera angle in degrees (0 is straight down)\n * @param bearing=0 - Map rotation in degrees (0 means north is up)\n * @param fovy= - Field of view of camera in degrees\n * @param altitude= - Altitude of camera in screen units \n *\n * Web mercator projection short-hand parameters\n * @param latitude - Center of viewport on map\n * @param longitude - Center of viewport on map\n * @param zoom - Scale = Math.pow(2,zoom) on map\n\n * Notes:\n * - Only one of center or [latitude, longitude] can be specified\n * - [latitude, longitude] can only be specified when \"mercator\" is true\n * - Altitude has a default value that matches assumptions in mapbox-gl\n * - Field of view is independent from altitude, provide `altitudeToFovy(1.5)` (default value) to match assumptions in mapbox-gl\n * - width and height are forced to 1 if supplied as 0, to avoid\n * division by zero. This is intended to reduce the burden of apps to\n * to check values before instantiating a Viewport.\n */\nexport type WebMercatorViewportProps = {\n // Map state\n width: number;\n height: number;\n latitude?: number;\n longitude?: number;\n position?: number[];\n zoom?: number;\n pitch?: number;\n bearing?: number;\n altitude?: number;\n fovy?: number;\n nearZMultiplier?: number;\n farZMultiplier?: number;\n};\n\n/**\n * The WebMercatorViewport class creates\n * - view/projection matrices\n * - \"uniform values\" (for shaders) from mercator params\n *\n * Note: Instances are immutable in the sense that they only have accessors.\n * A new viewport instance should be created if any parameters have changed.\n */\nexport default class WebMercatorViewport {\n readonly latitude: number;\n readonly longitude: number;\n readonly zoom: number;\n readonly pitch: number;\n readonly bearing: number;\n readonly altitude: number;\n readonly fovy: number;\n\n readonly meterOffset: number[];\n readonly center: number[];\n\n readonly width: number;\n readonly height: number;\n readonly scale: number;\n readonly distanceScales: DistanceScales;\n\n readonly viewMatrix: number[];\n readonly projectionMatrix: number[];\n\n viewProjectionMatrix: number[];\n pixelProjectionMatrix: number[];\n pixelUnprojectionMatrix: number[];\n\n /**\n * @classdesc\n * Creates view/projection matrices from mercator params\n * Note: The Viewport is immutable in the sense that it only has accessors.\n * A new viewport instance should be created if any parameters have changed.\n */\n // eslint-disable-next-line max-statements\n constructor(props: WebMercatorViewportProps = {width: 1, height: 1}) {\n let {\n // Map state\n width,\n height,\n altitude = null,\n fovy = null\n } = props;\n const {\n latitude = 0,\n longitude = 0,\n zoom = 0,\n pitch = 0,\n bearing = 0,\n position = null,\n nearZMultiplier = 0.02,\n farZMultiplier = 1.01\n } = props;\n\n // Silently allow apps to send in 0,0 to facilitate isomorphic render etc\n width = width || 1;\n height = height || 1;\n\n // `fovy` & `altitude` are independent parameters, one for the\n // projection and the latter for the view matrix. In the past,\n // the `fovy` was always derived from the `altitude`\n if (fovy === null && altitude === null) {\n altitude = DEFAULT_ALTITUDE;\n fovy = altitudeToFovy(altitude);\n } else if (fovy === null) {\n fovy = altitudeToFovy(altitude);\n } else if (altitude === null) {\n altitude = fovyToAltitude(fovy);\n }\n\n const scale = zoomToScale(zoom);\n // Altitude - prevent division by 0\n // TODO - just throw an Error instead?\n altitude = Math.max(0.75, altitude);\n\n const distanceScales = getDistanceScales({longitude, latitude});\n\n const center = lngLatToWorld([longitude, latitude]);\n center.push(0);\n\n if (position) {\n vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));\n }\n\n this.projectionMatrix = getProjectionMatrix({\n width,\n height,\n pitch,\n fovy,\n nearZMultiplier,\n farZMultiplier\n });\n\n this.viewMatrix = getViewMatrix({\n height,\n scale,\n center,\n pitch,\n bearing,\n altitude\n });\n\n // Save parameters\n this.width = width;\n this.height = height;\n this.scale = scale;\n\n this.latitude = latitude;\n this.longitude = longitude;\n this.zoom = zoom;\n this.pitch = pitch;\n this.bearing = bearing;\n this.altitude = altitude;\n this.fovy = fovy;\n this.center = center;\n this.meterOffset = position || [0, 0, 0];\n\n this.distanceScales = distanceScales;\n\n this._initMatrices();\n\n Object.freeze(this);\n }\n\n _initMatrices(): void {\n const {width, height, projectionMatrix, viewMatrix} = this;\n\n // Note: As usual, matrix operations should be applied in \"reverse\" order\n // since vectors will be multiplied in from the right during transformation\n const vpm = createMat4();\n mat4.multiply(vpm, vpm, projectionMatrix);\n mat4.multiply(vpm, vpm, viewMatrix);\n this.viewProjectionMatrix = vpm;\n\n // Calculate matrices and scales needed for projection\n /**\n * Builds matrices that converts preprojected lngLats to screen pixels\n * and vice versa.\n * Note: Currently returns bottom-left coordinates!\n * Note: Starts with the GL projection matrix and adds steps to the\n * scale and translate that matrix onto the window.\n * Note: WebGL controls clip space to screen projection with gl.viewport\n * and does not need this step.\n */\n const m = createMat4();\n\n // matrix for conversion from location to screen coordinates\n mat4.scale(m, m, [width / 2, -height / 2, 1]);\n mat4.translate(m, m, [1, -1, 0]);\n mat4.multiply(m, m, vpm);\n\n const mInverse = mat4.invert(createMat4(), m);\n if (!mInverse) {\n throw new Error('Pixel project matrix not invertible');\n }\n\n this.pixelProjectionMatrix = m;\n this.pixelUnprojectionMatrix = mInverse;\n }\n\n /** Two viewports are equal if width and height are identical, and if\n * their view and projection matrices are (approximately) equal.\n */\n equals = (viewport: WebMercatorViewport | null): boolean => {\n if (!(viewport instanceof WebMercatorViewport)) {\n return false;\n }\n\n return (\n viewport.width === this.width &&\n viewport.height === this.height &&\n mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&\n mat4.equals(viewport.viewMatrix, this.viewMatrix)\n );\n };\n\n /**\n * Projects xyz (possibly latitude and longitude) to pixel coordinates in window\n * using viewport projection parameters\n * - [longitude, latitude] to [x, y]\n * - [longitude, latitude, Z] => [x, y, z]\n * Note: By default, returns top-left coordinates for canvas/SVG type render\n *\n * @param lngLatZ - [lng, lat] or [lng, lat, Z]\n * @param options - options\n * @param options.topLeft=true - Whether projected coords are top left\n * @return - screen coordinates [x, y] or [x, y, z], z as pixel depth\n */\n project = (lngLatZ: number[], options: {topLeft?: boolean} = {}): number[] => {\n const {topLeft = true} = options;\n const worldPosition = this.projectPosition(lngLatZ);\n const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);\n\n const [x, y] = coord;\n const y2 = topLeft ? y : this.height - y;\n return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];\n };\n\n /**\n * Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.\n *\n * - [x, y] => [lng, lat]\n * - [x, y, z] => [lng, lat, Z]\n *\n * @param xyz - screen coordinates, z as pixel depth\n * @param options - options\n * @param options.topLeft=true - Whether projected coords are top left\n * @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as\n * the elevation plane to unproject onto\n * @return - [lng, lat, Z] or [X, Y, Z]\n */\n unproject = (xyz: number[], options: {topLeft?: boolean; targetZ?: number} = {}): number[] => {\n const {topLeft = true, targetZ = undefined} = options;\n const [x, y, z] = xyz;\n\n const y2 = topLeft ? y : this.height - y;\n const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];\n const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);\n const [X, Y, Z] = this.unprojectPosition(coord);\n\n if (Number.isFinite(z)) {\n return [X, Y, Z];\n }\n return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];\n };\n\n // NON_LINEAR PROJECTION HOOKS\n // Used for web meractor projection\n\n projectPosition = (xyz: number[]): [number, number, number] => {\n const [X, Y] = lngLatToWorld(xyz);\n const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];\n return [X, Y, Z];\n };\n\n unprojectPosition = (xyz: number[]): [number, number, number] => {\n const [X, Y] = worldToLngLat(xyz);\n const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];\n return [X, Y, Z];\n };\n\n /**\n * Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.\n * Performs the nonlinear part of the web mercator projection.\n * Remaining projection is done with 4x4 matrices which also handles\n * perspective.\n *\n * @param lngLat - [lng, lat] coordinates\n * Specifies a point on the sphere to project onto the map.\n * @return [x,y] coordinates.\n */\n projectFlat(lngLat: number[]): number[] {\n return lngLatToWorld(lngLat);\n }\n\n /**\n * Unproject world point [x,y] on map onto {lat, lon} on sphere\n *\n * @param xy - array with [x,y] members\n * representing point on projected map plane\n * @return - array with [lat,lon] of point on sphere.\n * Has toArray method if you need a GeoJSON Array.\n * Per cartographic tradition, lat and lon are specified as degrees.\n */\n unprojectFlat(xy: number[]): number[] {\n return worldToLngLat(xy);\n }\n\n /**\n * Get the map center that place a given [lng, lat] coordinate at screen point [x, y]\n * @param opt\n * @param opt.lngLat - [lng,lat] coordinates\n * Specifies a point on the sphere.\n * @param opt.pos - [x,y] coordinates\n * Specifies a point on the screen.\n * @return [lng,lat] new map center.\n */\n getMapCenterByLngLatPosition({lngLat, pos}: {lngLat: number[]; pos: number[]}): number[] {\n const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);\n const toLocation = lngLatToWorld(lngLat);\n const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));\n const newCenter = vec2.add([], this.center, translate);\n return worldToLngLat(newCenter);\n }\n\n /**\n * Returns a new viewport that fit around the given rectangle.\n * Only supports non-perspective mode.\n * @param bounds - [[lon, lat], [lon, lat]]\n * @param [options]\n * @param [options.padding] - The amount of padding in pixels to add to the given bounds.\n * @param [options.offset] - The center of the given bounds relative to the map's center,\n * [x, y] measured in pixels.\n * @returns {WebMercatorViewport}\n */\n fitBounds(\n bounds: [[number, number], [number, number]],\n options: Omit<FitBoundsOptions, 'width' | 'height' | 'bounds'> = {}\n ): WebMercatorViewport {\n const {width, height} = this;\n const {longitude, latitude, zoom} = fitBounds(Object.assign({width, height, bounds}, options));\n return new WebMercatorViewport({width, height, longitude, latitude, zoom});\n }\n\n /**\n * Returns the bounding box of the viewport.\n * @param [options]\n * @param [options.z] - The altitude at which the bounds should be calculated.\n * @returns {Array} bounds - [[lon, lat], [lon, lat]]\n */\n getBounds(options?: {z?: number}): number[][] {\n const corners = this.getBoundingRegion(options);\n\n const west = Math.min(...corners.map((p) => p[0]));\n const east = Math.max(...corners.map((p) => p[0]));\n const south = Math.min(...corners.map((p) => p[1]));\n const north = Math.max(...corners.map((p) => p[1]));\n return [\n [west, south],\n [east, north]\n ];\n }\n\n /**\n * Returns the bounding box of the viewport.\n * @param [options]\n * @param [options.z] - The altitude at which the bounds should be calculated.\n * @returns {Array} an array of 4 points that define the visible region\n */\n getBoundingRegion(options: {z?: number} = {}): number[][] {\n return getBounds(this, options.z || 0);\n }\n\n // DEPRECATED\n\n /** @deprecated Legacy method name */\n getLocationAtPoint({lngLat, pos}: {lngLat: number[]; pos: number[]}): number[] {\n return this.getMapCenterByLngLatPosition({lngLat, pos});\n }\n}\n"],"file":"web-mercator-viewport.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/web-mercator-viewport.ts"],"names":["createMat4","zoomToScale","pixelsToWorld","lngLatToWorld","worldToLngLat","worldToPixels","altitudeToFovy","fovyToAltitude","DEFAULT_ALTITUDE","getProjectionMatrix","getDistanceScales","getViewMatrix","fitBounds","getBounds","mat4","vec2","vec3","WebMercatorViewport","constructor","props","width","height","viewport","equals","projectionMatrix","viewMatrix","lngLatZ","options","topLeft","worldPosition","projectPosition","coord","pixelProjectionMatrix","x","y","y2","length","xyz","targetZ","undefined","z","targetZWorld","distanceScales","unitsPerMeter","pixelUnprojectionMatrix","X","Y","Z","unprojectPosition","Number","isFinite","metersPerUnit","altitude","fovy","latitude","longitude","zoom","pitch","bearing","position","nearZMultiplier","farZMultiplier","scale","Math","max","center","push","add","mul","meterOffset","_initMatrices","Object","freeze","vpm","multiply","viewProjectionMatrix","m","translate","mInverse","invert","Error","projectFlat","lngLat","unprojectFlat","xy","getMapCenterByLngLatPosition","pos","fromLocation","toLocation","negate","newCenter","bounds","assign","corners","getBoundingRegion","west","min","map","p","east","south","north","getLocationAtPoint"],"mappings":";AACA,SAAQA,UAAR,QAAyB,cAAzB;AAEA,SACEC,WADF,EAEEC,aAFF,EAGEC,aAHF,EAIEC,aAJF,EAKEC,aALF,EAMEC,cANF,EAOEC,cAPF,EAQEC,gBARF,EASEC,mBATF,EAUEC,iBAVF,EAWEC,aAXF,QAaO,sBAbP;AAcA,OAAOC,SAAP,MAAsB,cAAtB;AACA,OAAOC,SAAP,MAAsB,cAAtB;AAGA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AACA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AACA,OAAO,KAAKC,IAAZ,MAAsB,gBAAtB;AAiDA,eAAe,MAAMC,mBAAN,CAA0B;AA+BvCC,EAAAA,WAAW,CAACC,KAA+B,GAAG;AAACC,IAAAA,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AAvB,CAAhB;AAEA,UAAMoE,IAAI,GAAGhC,IAAI,CAACiC,GAAL,CAAS,GAAGH,OAAO,CAACI,GAAR,CAAaC,CAAD,IAAOA,CAAC,CAAC,CAAD,CAApB,CAAZ,CAAb;AACA,UAAMC,IAAI,GAAGpC,IAAI,CAACC,GAAL,CAAS,GAAG6B,OAAO,CAACI,GAAR,CAAaC,CAAD,IAAOA,CAAC,CAAC,CAAD,CAApB,CAAZ,CAAb;AACA,UAAME,KAAK,GAAGrC,IAAI,CAACiC,GAAL,CAAS,GAAGH,OAAO,CAACI,GAAR,CAAaC,CAAD,IAAOA,CAAC,CAAC,CAAD,CAApB,CAAZ,CAAd;AACA,UAAMG,KAAK,GAAGtC,IAAI,CAACC,GAAL,CAAS,GAAG6B,OAAO,CAACI,GAAR,CAAaC,CAAD,IAAOA,CAAC,CAAC,CAAD,CAApB,CAAZ,CAAd;AACA,WAAO,CACL,CAACH,IAAD,EAAOK,KAAP,CADK,EAEL,CAACD,IAAD,EAAOE,KAAP,CAFK,CAAP;AAID;;AAQDP,EAAAA,iBAAiB,CAACnE,OAAqB,GAAG,EAAzB,EAAyC;AACxD,WAAOd,SAAS,CAAC,IAAD,EAAOc,OAAO,CAACa,CAAR,IAAa,CAApB,CAAhB;AACD;;AAKD8D,EAAAA,kBAAkB,CAAC;AAACpB,IAAAA,MAAD;AAASI,IAAAA;AAAT,GAAD,EAA6D;AAC7E,WAAO,KAAKD,4BAAL,CAAkC;AAACH,MAAAA,MAAD;AAASI,MAAAA;AAAT,KAAlC,CAAP;AACD;;AAhVsC","sourcesContent":["// View and Projection Matrix calculations for mapbox-js style map view properties\nimport {createMat4} from './math-utils';\n\nimport {\n zoomToScale,\n pixelsToWorld,\n lngLatToWorld,\n worldToLngLat,\n worldToPixels,\n altitudeToFovy,\n fovyToAltitude,\n DEFAULT_ALTITUDE,\n getProjectionMatrix,\n getDistanceScales,\n getViewMatrix,\n DistanceScales\n} from './web-mercator-utils';\nimport fitBounds from './fit-bounds';\nimport getBounds from './get-bounds';\nimport type {FitBoundsOptions} from './fit-bounds';\n\nimport * as mat4 from 'gl-matrix/mat4';\nimport * as vec2 from 'gl-matrix/vec2';\nimport * as vec3 from 'gl-matrix/vec3';\n\n/**\n * @param width=1 - Width of \"viewport\" or window\n * @param height=1 - Height of \"viewport\" or window\n * @param scale=1 - Either use scale or zoom\n * @param pitch=0 - Camera angle in degrees (0 is straight down)\n * @param bearing=0 - Map rotation in degrees (0 means north is up)\n * @param fovy= - Field of view of camera in degrees\n * @param altitude= - Altitude of camera in screen units \n *\n * Web mercator projection short-hand parameters\n * @param latitude - Center of viewport on map\n * @param longitude - Center of viewport on map\n * @param zoom - Scale = Math.pow(2,zoom) on map\n\n * Notes:\n * - Only one of center or [latitude, longitude] can be specified\n * - [latitude, longitude] can only be specified when \"mercator\" is true\n * - Altitude has a default value that matches assumptions in mapbox-gl\n * - Field of view is independent from altitude, provide `altitudeToFovy(1.5)` (default value) to match assumptions in mapbox-gl\n * - width and height are forced to 1 if supplied as 0, to avoid\n * division by zero. This is intended to reduce the burden of apps to\n * to check values before instantiating a Viewport.\n */\nexport type WebMercatorViewportProps = {\n // Map state\n width: number;\n height: number;\n latitude?: number;\n longitude?: number;\n position?: number[];\n zoom?: number;\n pitch?: number;\n bearing?: number;\n altitude?: number;\n fovy?: number;\n nearZMultiplier?: number;\n farZMultiplier?: number;\n};\n\n/**\n * The WebMercatorViewport class creates\n * - view/projection matrices\n * - \"uniform values\" (for shaders) from mercator params\n *\n * Note: Instances are immutable in the sense that they only have accessors.\n * A new viewport instance should be created if any parameters have changed.\n */\nexport default class WebMercatorViewport {\n readonly latitude: number;\n readonly longitude: number;\n readonly zoom: number;\n readonly pitch: number;\n readonly bearing: number;\n readonly altitude: number;\n readonly fovy: number;\n\n readonly meterOffset: number[];\n readonly center: number[];\n\n readonly width: number;\n readonly height: number;\n readonly scale: number;\n readonly distanceScales: DistanceScales;\n\n readonly viewMatrix: number[];\n readonly projectionMatrix: number[];\n\n viewProjectionMatrix: number[];\n pixelProjectionMatrix: number[];\n pixelUnprojectionMatrix: number[];\n\n /**\n * @classdesc\n * Creates view/projection matrices from mercator params\n * Note: The Viewport is immutable in the sense that it only has accessors.\n * A new viewport instance should be created if any parameters have changed.\n */\n // eslint-disable-next-line max-statements\n constructor(props: WebMercatorViewportProps = {width: 1, height: 1}) {\n let {\n // Map state\n width,\n height,\n altitude = null,\n fovy = null\n } = props;\n const {\n latitude = 0,\n longitude = 0,\n zoom = 0,\n pitch = 0,\n bearing = 0,\n position = null,\n nearZMultiplier = 0.02,\n farZMultiplier = 1.01\n } = props;\n\n // Silently allow apps to send in 0,0 to facilitate isomorphic render etc\n width = width || 1;\n height = height || 1;\n\n // `fovy` & `altitude` are independent parameters, one for the\n // projection and the latter for the view matrix. In the past,\n // the `fovy` was always derived from the `altitude`\n if (fovy === null && altitude === null) {\n altitude = DEFAULT_ALTITUDE;\n fovy = altitudeToFovy(altitude);\n } else if (fovy === null) {\n fovy = altitudeToFovy(altitude);\n } else if (altitude === null) {\n altitude = fovyToAltitude(fovy);\n }\n\n const scale = zoomToScale(zoom);\n // Altitude - prevent division by 0\n // TODO - just throw an Error instead?\n altitude = Math.max(0.75, altitude);\n\n const distanceScales = getDistanceScales({longitude, latitude});\n\n const center: number[] = lngLatToWorld([longitude, latitude]);\n center.push(0);\n\n if (position) {\n vec3.add(center, center, vec3.mul([], position, distanceScales.unitsPerMeter));\n }\n\n this.projectionMatrix = getProjectionMatrix({\n width,\n height,\n scale,\n center,\n pitch,\n fovy,\n nearZMultiplier,\n farZMultiplier\n });\n\n this.viewMatrix = getViewMatrix({\n height,\n scale,\n center,\n pitch,\n bearing,\n altitude\n });\n\n // Save parameters\n this.width = width;\n this.height = height;\n this.scale = scale;\n\n this.latitude = latitude;\n this.longitude = longitude;\n this.zoom = zoom;\n this.pitch = pitch;\n this.bearing = bearing;\n this.altitude = altitude;\n this.fovy = fovy;\n this.center = center;\n this.meterOffset = position || [0, 0, 0];\n\n this.distanceScales = distanceScales;\n\n this._initMatrices();\n\n Object.freeze(this);\n }\n\n _initMatrices(): void {\n const {width, height, projectionMatrix, viewMatrix} = this;\n\n // Note: As usual, matrix operations should be applied in \"reverse\" order\n // since vectors will be multiplied in from the right during transformation\n const vpm = createMat4();\n mat4.multiply(vpm, vpm, projectionMatrix);\n mat4.multiply(vpm, vpm, viewMatrix);\n this.viewProjectionMatrix = vpm;\n\n // Calculate matrices and scales needed for projection\n /**\n * Builds matrices that converts preprojected lngLats to screen pixels\n * and vice versa.\n * Note: Currently returns bottom-left coordinates!\n * Note: Starts with the GL projection matrix and adds steps to the\n * scale and translate that matrix onto the window.\n * Note: WebGL controls clip space to screen projection with gl.viewport\n * and does not need this step.\n */\n const m = createMat4();\n\n // matrix for conversion from location to screen coordinates\n mat4.scale(m, m, [width / 2, -height / 2, 1]);\n mat4.translate(m, m, [1, -1, 0]);\n mat4.multiply(m, m, vpm);\n\n const mInverse = mat4.invert(createMat4(), m);\n if (!mInverse) {\n throw new Error('Pixel project matrix not invertible');\n }\n\n this.pixelProjectionMatrix = m;\n this.pixelUnprojectionMatrix = mInverse;\n }\n\n /** Two viewports are equal if width and height are identical, and if\n * their view and projection matrices are (approximately) equal.\n */\n equals = (viewport: WebMercatorViewport | null): boolean => {\n if (!(viewport instanceof WebMercatorViewport)) {\n return false;\n }\n\n return (\n viewport.width === this.width &&\n viewport.height === this.height &&\n mat4.equals(viewport.projectionMatrix, this.projectionMatrix) &&\n mat4.equals(viewport.viewMatrix, this.viewMatrix)\n );\n };\n\n /**\n * Projects xyz (possibly latitude and longitude) to pixel coordinates in window\n * using viewport projection parameters\n * - [longitude, latitude] to [x, y]\n * - [longitude, latitude, Z] => [x, y, z]\n * Note: By default, returns top-left coordinates for canvas/SVG type render\n *\n * @param lngLatZ - [lng, lat] or [lng, lat, Z]\n * @param options - options\n * @param options.topLeft=true - Whether projected coords are top left\n * @return - screen coordinates [x, y] or [x, y, z], z as pixel depth\n */\n project = (lngLatZ: number[], options: {topLeft?: boolean} = {}): number[] => {\n const {topLeft = true} = options;\n const worldPosition = this.projectPosition(lngLatZ);\n const coord = worldToPixels(worldPosition, this.pixelProjectionMatrix);\n\n const [x, y] = coord;\n const y2 = topLeft ? y : this.height - y;\n return lngLatZ.length === 2 ? [x, y2] : [x, y2, coord[2]];\n };\n\n /**\n * Unproject pixel coordinates on screen onto world coordinates, possibly `[lon, lat]` on map.\n *\n * - [x, y] => [lng, lat]\n * - [x, y, z] => [lng, lat, Z]\n *\n * @param xyz - screen coordinates, z as pixel depth\n * @param options - options\n * @param options.topLeft=true - Whether projected coords are top left\n * @param options.targetZ=0 - If pixel depth is unknown, targetZ is used as\n * the elevation plane to unproject onto\n * @return - [lng, lat, Z] or [X, Y, Z]\n */\n unproject = (xyz: number[], options: {topLeft?: boolean; targetZ?: number} = {}): number[] => {\n const {topLeft = true, targetZ = undefined} = options;\n const [x, y, z] = xyz;\n\n const y2 = topLeft ? y : this.height - y;\n const targetZWorld = targetZ && targetZ * this.distanceScales.unitsPerMeter[2];\n const coord = pixelsToWorld([x, y2, z], this.pixelUnprojectionMatrix, targetZWorld);\n const [X, Y, Z] = this.unprojectPosition(coord);\n\n if (Number.isFinite(z)) {\n return [X, Y, Z];\n }\n return Number.isFinite(targetZ) ? [X, Y, targetZ] : [X, Y];\n };\n\n // NON_LINEAR PROJECTION HOOKS\n // Used for web meractor projection\n\n projectPosition = (xyz: number[]): [number, number, number] => {\n const [X, Y] = lngLatToWorld(xyz);\n const Z = (xyz[2] || 0) * this.distanceScales.unitsPerMeter[2];\n return [X, Y, Z];\n };\n\n unprojectPosition = (xyz: number[]): [number, number, number] => {\n const [X, Y] = worldToLngLat(xyz);\n const Z = (xyz[2] || 0) * this.distanceScales.metersPerUnit[2];\n return [X, Y, Z];\n };\n\n /**\n * Project [lng,lat] on sphere onto [x,y] on 512*512 Mercator Zoom 0 tile.\n * Performs the nonlinear part of the web mercator projection.\n * Remaining projection is done with 4x4 matrices which also handles\n * perspective.\n *\n * @param lngLat - [lng, lat] coordinates\n * Specifies a point on the sphere to project onto the map.\n * @return [x,y] coordinates.\n */\n projectFlat(lngLat: number[]): number[] {\n return lngLatToWorld(lngLat);\n }\n\n /**\n * Unproject world point [x,y] on map onto {lat, lon} on sphere\n *\n * @param xy - array with [x,y] members\n * representing point on projected map plane\n * @return - array with [lat,lon] of point on sphere.\n * Has toArray method if you need a GeoJSON Array.\n * Per cartographic tradition, lat and lon are specified as degrees.\n */\n unprojectFlat(xy: number[]): number[] {\n return worldToLngLat(xy);\n }\n\n /**\n * Get the map center that place a given [lng, lat] coordinate at screen point [x, y]\n * @param opt\n * @param opt.lngLat - [lng,lat] coordinates\n * Specifies a point on the sphere.\n * @param opt.pos - [x,y] coordinates\n * Specifies a point on the screen.\n * @return [lng,lat] new map center.\n */\n getMapCenterByLngLatPosition({lngLat, pos}: {lngLat: number[]; pos: number[]}): number[] {\n const fromLocation = pixelsToWorld(pos, this.pixelUnprojectionMatrix);\n const toLocation = lngLatToWorld(lngLat);\n const translate = vec2.add([], toLocation, vec2.negate([], fromLocation));\n const newCenter = vec2.add([], this.center, translate);\n return worldToLngLat(newCenter);\n }\n\n /**\n * Returns a new viewport that fit around the given rectangle.\n * Only supports non-perspective mode.\n * @param bounds - [[lon, lat], [lon, lat]]\n * @param [options]\n * @param [options.padding] - The amount of padding in pixels to add to the given bounds.\n * @param [options.offset] - The center of the given bounds relative to the map's center,\n * [x, y] measured in pixels.\n * @returns {WebMercatorViewport}\n */\n fitBounds(\n bounds: [[number, number], [number, number]],\n options: Omit<FitBoundsOptions, 'width' | 'height' | 'bounds'> = {}\n ): WebMercatorViewport {\n const {width, height} = this;\n const {longitude, latitude, zoom} = fitBounds(Object.assign({width, height, bounds}, options));\n return new WebMercatorViewport({width, height, longitude, latitude, zoom});\n }\n\n /**\n * Returns the bounding box of the viewport.\n * @param [options]\n * @param [options.z] - The altitude at which the bounds should be calculated.\n * @returns {Array} bounds - [[lon, lat], [lon, lat]]\n */\n getBounds(options?: {z?: number}): number[][] {\n const corners = this.getBoundingRegion(options);\n\n const west = Math.min(...corners.map((p) => p[0]));\n const east = Math.max(...corners.map((p) => p[0]));\n const south = Math.min(...corners.map((p) => p[1]));\n const north = Math.max(...corners.map((p) => p[1]));\n return [\n [west, south],\n [east, north]\n ];\n }\n\n /**\n * Returns the bounding box of the viewport.\n * @param [options]\n * @param [options.z] - The altitude at which the bounds should be calculated.\n * @returns {Array} an array of 4 points that define the visible region\n */\n getBoundingRegion(options: {z?: number} = {}): number[][] {\n return getBounds(this, options.z || 0);\n }\n\n // DEPRECATED\n\n /** @deprecated Legacy method name */\n getLocationAtPoint({lngLat, pos}: {lngLat: number[]; pos: number[]}): number[] {\n return this.getMapCenterByLngLatPosition({lngLat, pos});\n }\n}\n"],"file":"web-mercator-viewport.js"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export { default as getBounds } from './get-bounds';
|
|
|
4
4
|
export { default as fitBounds } from './fit-bounds';
|
|
5
5
|
export { default as normalizeViewportProps } from './normalize-viewport-props';
|
|
6
6
|
export { default as flyToViewport, getFlyToDuration } from './fly-to-viewport';
|
|
7
|
-
export { MAX_LATITUDE, lngLatToWorld, worldToLngLat, worldToPixels, pixelsToWorld, zoomToScale, scaleToZoom, altitudeToFovy, fovyToAltitude, getMeterZoom, getDistanceScales, addMetersToLngLat, getViewMatrix, getProjectionMatrix, getProjectionParameters } from './web-mercator-utils';
|
|
7
|
+
export { MAX_LATITUDE, lngLatToWorld, worldToLngLat, worldToPixels, pixelsToWorld, zoomToScale, scaleToZoom, altitudeToFovy, fovyToAltitude, getMeterZoom, unitsPerMeter, getDistanceScales, addMetersToLngLat, getViewMatrix, getProjectionMatrix, getProjectionParameters } from './web-mercator-utils';
|
|
8
8
|
/** Types */
|
|
9
9
|
export type { FitBoundsOptions } from './fit-bounds';
|
|
10
10
|
export type { DistanceScales } from './web-mercator-utils';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAC,OAAO,IAAI,mBAAmB,EAAC,MAAM,yBAAyB,CAAC;AAEvE,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,sBAAsB,EAAC,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAC,OAAO,IAAI,aAAa,EAAE,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAE7E,OAAO,EACL,YAAY,EACZ,aAAa,EACb,aAAa,EACb,aAAa,EACb,aAAa,EACb,WAAW,EACX,WAAW,EACX,cAAc,EACd,cAAc,EACd,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,mBAAmB,EACnB,uBAAuB,EACxB,MAAM,sBAAsB,CAAC;AAE9B,YAAY;AACZ,YAAY,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACnD,YAAY,EAAC,cAAc,EAAC,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAC,OAAO,IAAI,mBAAmB,EAAC,MAAM,yBAAyB,CAAC;AAEvE,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,sBAAsB,EAAC,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAC,OAAO,IAAI,aAAa,EAAE,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAE7E,OAAO,EACL,YAAY,EACZ,aAAa,EACb,aAAa,EACb,aAAa,EACb,aAAa,EACb,WAAW,EACX,WAAW,EACX,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,mBAAmB,EACnB,uBAAuB,EACxB,MAAM,sBAAsB,CAAC;AAE9B,YAAY;AACZ,YAAY,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACnD,YAAY,EAAC,cAAc,EAAC,MAAM,sBAAsB,CAAC"}
|
|
@@ -57,6 +57,13 @@ export declare function worldToLngLat(xy: number[]): [number, number];
|
|
|
57
57
|
export declare function getMeterZoom(options: {
|
|
58
58
|
latitude: number;
|
|
59
59
|
}): number;
|
|
60
|
+
/**
|
|
61
|
+
* Calculate the conversion from meter to common units at a given latitude
|
|
62
|
+
* This is a cheaper version of `getDistanceScales`
|
|
63
|
+
* @param latitude center latitude in degrees
|
|
64
|
+
* @returns common units per meter
|
|
65
|
+
*/
|
|
66
|
+
export declare function unitsPerMeter(latitude: number): number;
|
|
60
67
|
/**
|
|
61
68
|
* Calculate distance scales in meters around current lat/lon, both for
|
|
62
69
|
* degrees and pixels.
|
|
@@ -91,6 +98,9 @@ export declare function getViewMatrix(options: {
|
|
|
91
98
|
*
|
|
92
99
|
* @param options.width Width of "viewport" or window
|
|
93
100
|
* @param options.height Height of "viewport" or window
|
|
101
|
+
* @param options.scale Scale at the current zoom
|
|
102
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
103
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
94
104
|
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
95
105
|
* @param options.fovy field of view in degrees
|
|
96
106
|
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
@@ -101,6 +111,9 @@ export declare function getViewMatrix(options: {
|
|
|
101
111
|
export declare function getProjectionParameters(options: {
|
|
102
112
|
width: number;
|
|
103
113
|
height: number;
|
|
114
|
+
scale?: number;
|
|
115
|
+
center?: number[];
|
|
116
|
+
offset?: [number, number];
|
|
104
117
|
fovy?: number;
|
|
105
118
|
altitude?: number;
|
|
106
119
|
pitch?: number;
|
|
@@ -117,6 +130,9 @@ export declare function getProjectionParameters(options: {
|
|
|
117
130
|
* @param options Viewport options
|
|
118
131
|
* @param options.width Width of "viewport" or window
|
|
119
132
|
* @param options.height Height of "viewport" or window
|
|
133
|
+
* @param options.scale Scale at the current zoom
|
|
134
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
135
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
120
136
|
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
121
137
|
* @param options.fovy field of view in degrees
|
|
122
138
|
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
@@ -128,6 +144,9 @@ export declare function getProjectionMatrix(options: {
|
|
|
128
144
|
width: number;
|
|
129
145
|
height: number;
|
|
130
146
|
pitch: number;
|
|
147
|
+
scale?: number;
|
|
148
|
+
center?: number[];
|
|
149
|
+
offset?: [number, number];
|
|
131
150
|
fovy?: number;
|
|
132
151
|
altitude?: number;
|
|
133
152
|
nearZMultiplier: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-mercator-utils.d.ts","sourceRoot":"","sources":["../src/web-mercator-utils.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,YAAY,YAAY,CAAC;AAGtC,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAEpC,oBAAY,cAAc,GAAG;IAC3B,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;GASG;AACH,aAAK,oB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,MAAM,EAAE,MAAM,CAAC,CAAC,EAC5C,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAM,GAClE,mBAAmB;IAMtB;;;;;OAKG;IACH,SAAS,CAAC,OAAO,CAAC,EAAE;QAAC,CAAC,CAAC,EAAE,MAAM,CAAA;KAAC,GAAG,MAAM,EAAE,EAAE;IAa7C;;;;;OAKG;IACH,iBAAiB,CAAC,OAAO,GAAE;QAAC,CAAC,CAAC,EAAE,MAAM,CAAA;KAAM,GAAG,MAAM,EAAE,EAAE;IAMzD,qCAAqC;IACrC,kBAAkB,CAAC,EAAC,MAAM,EAAE,GAAG,EAAC,EAAE;QAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,GAAG,EAAE,MAAM,EAAE,CAAA;KAAC,GAAG,MAAM,EAAE;CAG/E"}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Utilities for perspective-enabled Web Mercator projections",
|
|
4
4
|
"author": "Uber Technologies, Inc.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.6.
|
|
6
|
+
"version": "3.6.2",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"webgl",
|
|
9
9
|
"javascript",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"mapbox-gl": "^1.0.0",
|
|
34
34
|
"mock-browser": "^0.92.14"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "517015873bbd7143bfc4bb761e70071c7a86771b"
|
|
37
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// TODO - THE UTILITIES IN THIS FILE SHOULD BE IMPORTED FROM WEB-MERCATOR-VIEWPORT MODULE
|
|
2
2
|
|
|
3
|
-
import {createMat4, transformVector, log2} from './math-utils';
|
|
3
|
+
import {createMat4, transformVector, clamp, log2} from './math-utils';
|
|
4
4
|
|
|
5
5
|
import * as mat4 from 'gl-matrix/mat4';
|
|
6
6
|
import * as vec2 from 'gl-matrix/vec2';
|
|
@@ -107,6 +107,17 @@ export function getMeterZoom(options: {latitude: number}): number {
|
|
|
107
107
|
return scaleToZoom(EARTH_CIRCUMFERENCE * latCosine) - 9;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Calculate the conversion from meter to common units at a given latitude
|
|
112
|
+
* This is a cheaper version of `getDistanceScales`
|
|
113
|
+
* @param latitude center latitude in degrees
|
|
114
|
+
* @returns common units per meter
|
|
115
|
+
*/
|
|
116
|
+
export function unitsPerMeter(latitude: number): number {
|
|
117
|
+
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
118
|
+
return TILE_SIZE / EARTH_CIRCUMFERENCE / latCosine;
|
|
119
|
+
}
|
|
120
|
+
|
|
110
121
|
/**
|
|
111
122
|
* Calculate distance scales in meters around current lat/lon, both for
|
|
112
123
|
* degrees and pixels.
|
|
@@ -252,6 +263,9 @@ export function getViewMatrix(options: {
|
|
|
252
263
|
*
|
|
253
264
|
* @param options.width Width of "viewport" or window
|
|
254
265
|
* @param options.height Height of "viewport" or window
|
|
266
|
+
* @param options.scale Scale at the current zoom
|
|
267
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
268
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
255
269
|
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
256
270
|
* @param options.fovy field of view in degrees
|
|
257
271
|
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
@@ -262,13 +276,26 @@ export function getViewMatrix(options: {
|
|
|
262
276
|
export function getProjectionParameters(options: {
|
|
263
277
|
width: number;
|
|
264
278
|
height: number;
|
|
279
|
+
scale?: number;
|
|
280
|
+
center?: number[];
|
|
281
|
+
offset?: [number, number];
|
|
265
282
|
fovy?: number;
|
|
266
283
|
altitude?: number;
|
|
267
284
|
pitch?: number;
|
|
268
285
|
nearZMultiplier?: number;
|
|
269
286
|
farZMultiplier?: number;
|
|
270
287
|
}): ProjectionParameters {
|
|
271
|
-
const {
|
|
288
|
+
const {
|
|
289
|
+
width,
|
|
290
|
+
height,
|
|
291
|
+
altitude,
|
|
292
|
+
pitch = 0,
|
|
293
|
+
offset,
|
|
294
|
+
center,
|
|
295
|
+
scale,
|
|
296
|
+
nearZMultiplier = 1,
|
|
297
|
+
farZMultiplier = 1
|
|
298
|
+
} = options;
|
|
272
299
|
let {fovy = altitudeToFovy(DEFAULT_ALTITUDE)} = options;
|
|
273
300
|
|
|
274
301
|
// For back-compatibility allow field of view to be
|
|
@@ -276,25 +303,42 @@ export function getProjectionParameters(options: {
|
|
|
276
303
|
if (altitude !== undefined) {
|
|
277
304
|
fovy = altitudeToFovy(altitude);
|
|
278
305
|
}
|
|
279
|
-
|
|
306
|
+
|
|
307
|
+
const fovRadians = fovy * DEGREES_TO_RADIANS;
|
|
308
|
+
const pitchRadians = pitch * DEGREES_TO_RADIANS;
|
|
309
|
+
|
|
310
|
+
// Distance from camera to the target
|
|
280
311
|
const focalDistance = fovyToAltitude(fovy);
|
|
281
312
|
|
|
313
|
+
let cameraToSeaLevelDistance = focalDistance;
|
|
314
|
+
|
|
315
|
+
if (center) {
|
|
316
|
+
cameraToSeaLevelDistance += (center[2] * scale) / Math.cos(pitchRadians) / height;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const fovAboveCenter = fovRadians * (0.5 + (offset ? offset[1] : 0) / height);
|
|
320
|
+
|
|
282
321
|
// Find the distance from the center point to the center top
|
|
283
322
|
// in focal distance units using law of sines.
|
|
284
|
-
const pitchRadians = pitch * DEGREES_TO_RADIANS;
|
|
285
323
|
const topHalfSurfaceDistance =
|
|
286
|
-
(Math.sin(
|
|
287
|
-
Math.sin(
|
|
324
|
+
(Math.sin(fovAboveCenter) * cameraToSeaLevelDistance) /
|
|
325
|
+
Math.sin(clamp(Math.PI / 2 - pitchRadians - fovAboveCenter, 0.01, Math.PI - 0.01));
|
|
326
|
+
|
|
327
|
+
// Calculate z distance of the farthest fragment that should be rendered.
|
|
328
|
+
const furthestDistance =
|
|
329
|
+
Math.sin(pitchRadians) * topHalfSurfaceDistance + cameraToSeaLevelDistance;
|
|
330
|
+
// Matches mapbox limit
|
|
331
|
+
const horizonDistance = cameraToSeaLevelDistance * 10;
|
|
288
332
|
|
|
289
333
|
// Calculate z value of the farthest fragment that should be rendered.
|
|
290
|
-
const farZ = Math.
|
|
334
|
+
const farZ = Math.min(furthestDistance * farZMultiplier, horizonDistance);
|
|
291
335
|
|
|
292
336
|
return {
|
|
293
|
-
fov:
|
|
337
|
+
fov: fovRadians,
|
|
294
338
|
aspect: width / height,
|
|
295
339
|
focalDistance,
|
|
296
340
|
near: nearZMultiplier,
|
|
297
|
-
far: farZ
|
|
341
|
+
far: farZ
|
|
298
342
|
};
|
|
299
343
|
}
|
|
300
344
|
|
|
@@ -308,6 +352,9 @@ export function getProjectionParameters(options: {
|
|
|
308
352
|
* @param options Viewport options
|
|
309
353
|
* @param options.width Width of "viewport" or window
|
|
310
354
|
* @param options.height Height of "viewport" or window
|
|
355
|
+
* @param options.scale Scale at the current zoom
|
|
356
|
+
* @param options.center Offset of the target, vec3 in world space
|
|
357
|
+
* @param options.offset Offset of the focal point, vec2 in screen space
|
|
311
358
|
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
312
359
|
* @param options.fovy field of view in degrees
|
|
313
360
|
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
@@ -319,6 +366,9 @@ export function getProjectionMatrix(options: {
|
|
|
319
366
|
width: number;
|
|
320
367
|
height: number;
|
|
321
368
|
pitch: number;
|
|
369
|
+
scale?: number;
|
|
370
|
+
center?: number[];
|
|
371
|
+
offset?: [number, number];
|
|
322
372
|
fovy?: number;
|
|
323
373
|
altitude?: number;
|
|
324
374
|
nearZMultiplier: number;
|
|
@@ -143,7 +143,7 @@ export default class WebMercatorViewport {
|
|
|
143
143
|
|
|
144
144
|
const distanceScales = getDistanceScales({longitude, latitude});
|
|
145
145
|
|
|
146
|
-
const center = lngLatToWorld([longitude, latitude]);
|
|
146
|
+
const center: number[] = lngLatToWorld([longitude, latitude]);
|
|
147
147
|
center.push(0);
|
|
148
148
|
|
|
149
149
|
if (position) {
|
|
@@ -153,6 +153,8 @@ export default class WebMercatorViewport {
|
|
|
153
153
|
this.projectionMatrix = getProjectionMatrix({
|
|
154
154
|
width,
|
|
155
155
|
height,
|
|
156
|
+
scale,
|
|
157
|
+
center,
|
|
156
158
|
pitch,
|
|
157
159
|
fovy,
|
|
158
160
|
nearZMultiplier,
|
package/dist/assert.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// Replacement for the external assert method to reduce bundle size
|
|
2
|
-
// Note: We don't use the second "message" argument in calling code,
|
|
3
|
-
// so no need to support it here
|
|
4
|
-
export default function assert(condition, message) {
|
|
5
|
-
if (!condition) {
|
|
6
|
-
throw new Error(message || '@math.gl/web-mercator: assertion failed.');
|
|
7
|
-
}
|
|
8
|
-
}
|
package/dist/fit-bounds.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import assert from './assert';
|
|
2
|
-
import { log2, clamp } from './math-utils';
|
|
3
|
-
import { MAX_LATITUDE, lngLatToWorld, worldToLngLat } from './web-mercator-utils';
|
|
4
|
-
/**
|
|
5
|
-
* Returns map settings {latitude, longitude, zoom}
|
|
6
|
-
* that will contain the provided corners within the provided width.
|
|
7
|
-
*
|
|
8
|
-
* > _Note: Only supports non-perspective mode._
|
|
9
|
-
*
|
|
10
|
-
* @param options fit bounds parameters
|
|
11
|
-
* @returns - latitude, longitude and zoom
|
|
12
|
-
*/
|
|
13
|
-
export default function fitBounds(options) {
|
|
14
|
-
const { width, height, bounds, minExtent = 0, // 0.01 would be about 1000 meters (degree is ~110KM)
|
|
15
|
-
maxZoom = 24, // ~x4,000,000 => About 10 meter extents
|
|
16
|
-
offset = [0, 0] } = options;
|
|
17
|
-
const [[west, south], [east, north]] = bounds;
|
|
18
|
-
const padding = getPaddingObject(options.padding);
|
|
19
|
-
const nw = lngLatToWorld([west, clamp(north, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
20
|
-
const se = lngLatToWorld([east, clamp(south, -MAX_LATITUDE, MAX_LATITUDE)]);
|
|
21
|
-
// width/height on the Web Mercator plane
|
|
22
|
-
const size = [
|
|
23
|
-
Math.max(Math.abs(se[0] - nw[0]), minExtent),
|
|
24
|
-
Math.max(Math.abs(se[1] - nw[1]), minExtent)
|
|
25
|
-
];
|
|
26
|
-
const targetSize = [
|
|
27
|
-
width - padding.left - padding.right - Math.abs(offset[0]) * 2,
|
|
28
|
-
height - padding.top - padding.bottom - Math.abs(offset[1]) * 2
|
|
29
|
-
];
|
|
30
|
-
assert(targetSize[0] > 0 && targetSize[1] > 0);
|
|
31
|
-
// scale = screen pixels per unit on the Web Mercator plane
|
|
32
|
-
const scaleX = targetSize[0] / size[0];
|
|
33
|
-
const scaleY = targetSize[1] / size[1];
|
|
34
|
-
// Find how much we need to shift the center
|
|
35
|
-
const offsetX = (padding.right - padding.left) / 2 / scaleX;
|
|
36
|
-
const offsetY = (padding.bottom - padding.top) / 2 / scaleY;
|
|
37
|
-
const center = [(se[0] + nw[0]) / 2 + offsetX, (se[1] + nw[1]) / 2 + offsetY];
|
|
38
|
-
const centerLngLat = worldToLngLat(center);
|
|
39
|
-
const zoom = Math.min(maxZoom, log2(Math.abs(Math.min(scaleX, scaleY))));
|
|
40
|
-
assert(Number.isFinite(zoom));
|
|
41
|
-
return {
|
|
42
|
-
longitude: centerLngLat[0],
|
|
43
|
-
latitude: centerLngLat[1],
|
|
44
|
-
zoom
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
// Helpers
|
|
48
|
-
function getPaddingObject(padding = 0) {
|
|
49
|
-
if (typeof padding === 'number') {
|
|
50
|
-
return {
|
|
51
|
-
top: padding,
|
|
52
|
-
bottom: padding,
|
|
53
|
-
left: padding,
|
|
54
|
-
right: padding
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
// Make sure all the required properties are set
|
|
58
|
-
assert(Number.isFinite(padding.top) &&
|
|
59
|
-
Number.isFinite(padding.bottom) &&
|
|
60
|
-
Number.isFinite(padding.left) &&
|
|
61
|
-
Number.isFinite(padding.right));
|
|
62
|
-
return padding;
|
|
63
|
-
}
|
package/dist/fly-to-viewport.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { lerp } from './math-utils';
|
|
2
|
-
import { scaleToZoom, zoomToScale, lngLatToWorld, worldToLngLat } from './web-mercator-utils';
|
|
3
|
-
import * as vec2 from 'gl-matrix/vec2';
|
|
4
|
-
const EPSILON = 0.01;
|
|
5
|
-
const VIEWPORT_TRANSITION_PROPS = ['longitude', 'latitude', 'zoom'];
|
|
6
|
-
const DEFAULT_OPTS = {
|
|
7
|
-
curve: 1.414,
|
|
8
|
-
speed: 1.2
|
|
9
|
-
// screenSpeed and maxDuration are used only if specified
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* mapbox-gl-js flyTo : https://www.mapbox.com/mapbox-gl-js/api/#map#flyto.
|
|
13
|
-
* It implements “Smooth and efficient zooming and panning.” algorithm by
|
|
14
|
-
* "Jarke J. van Wijk and Wim A.A. Nuij"
|
|
15
|
-
*/
|
|
16
|
-
export default function flyToViewport(startProps, endProps, t, options) {
|
|
17
|
-
// Equations from above paper are referred where needed.
|
|
18
|
-
const { startZoom, startCenterXY, uDelta, w0, u1, S, rho, rho2, r0 } = getFlyToTransitionParams(startProps, endProps, options);
|
|
19
|
-
// If change in center is too small, do linear interpolaiton.
|
|
20
|
-
if (u1 < EPSILON) {
|
|
21
|
-
const viewport = {};
|
|
22
|
-
for (const key of VIEWPORT_TRANSITION_PROPS) {
|
|
23
|
-
const startValue = startProps[key];
|
|
24
|
-
const endValue = endProps[key];
|
|
25
|
-
viewport[key] = lerp(startValue, endValue, t);
|
|
26
|
-
}
|
|
27
|
-
// @ts-expect-error properties are populated dynamically
|
|
28
|
-
return viewport;
|
|
29
|
-
}
|
|
30
|
-
const s = t * S;
|
|
31
|
-
const w = Math.cosh(r0) / Math.cosh(r0 + rho * s);
|
|
32
|
-
const u = (w0 * ((Math.cosh(r0) * Math.tanh(r0 + rho * s) - Math.sinh(r0)) / rho2)) / u1;
|
|
33
|
-
const scaleIncrement = 1 / w; // Using w method for scaling.
|
|
34
|
-
const newZoom = startZoom + scaleToZoom(scaleIncrement);
|
|
35
|
-
const newCenterWorld = vec2.scale([], uDelta, u);
|
|
36
|
-
vec2.add(newCenterWorld, newCenterWorld, startCenterXY);
|
|
37
|
-
const newCenter = worldToLngLat(newCenterWorld);
|
|
38
|
-
return {
|
|
39
|
-
longitude: newCenter[0],
|
|
40
|
-
latitude: newCenter[1],
|
|
41
|
-
zoom: newZoom
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
// returns transition duration in milliseconds
|
|
45
|
-
export function getFlyToDuration(startProps, endProps, options) {
|
|
46
|
-
const opts = { ...DEFAULT_OPTS, ...options };
|
|
47
|
-
const { screenSpeed, speed, maxDuration } = opts;
|
|
48
|
-
const { S, rho } = getFlyToTransitionParams(startProps, endProps, opts);
|
|
49
|
-
const length = 1000 * S;
|
|
50
|
-
let duration;
|
|
51
|
-
if (Number.isFinite(screenSpeed)) {
|
|
52
|
-
duration = length / (screenSpeed / rho);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
duration = length / speed;
|
|
56
|
-
}
|
|
57
|
-
return Number.isFinite(maxDuration) && duration > maxDuration ? 0 : duration;
|
|
58
|
-
}
|
|
59
|
-
// Private Methods
|
|
60
|
-
// Calculate all parameters that are static for given startProps and endProps
|
|
61
|
-
function getFlyToTransitionParams(startProps, endProps, opts) {
|
|
62
|
-
opts = Object.assign({}, DEFAULT_OPTS, opts);
|
|
63
|
-
const rho = opts.curve;
|
|
64
|
-
const startZoom = startProps.zoom;
|
|
65
|
-
const startCenter = [startProps.longitude, startProps.latitude];
|
|
66
|
-
const startScale = zoomToScale(startZoom);
|
|
67
|
-
const endZoom = endProps.zoom;
|
|
68
|
-
const endCenter = [endProps.longitude, endProps.latitude];
|
|
69
|
-
const scale = zoomToScale(endZoom - startZoom);
|
|
70
|
-
const startCenterXY = lngLatToWorld(startCenter);
|
|
71
|
-
const endCenterXY = lngLatToWorld(endCenter);
|
|
72
|
-
const uDelta = vec2.sub([], endCenterXY, startCenterXY);
|
|
73
|
-
const w0 = Math.max(startProps.width, startProps.height);
|
|
74
|
-
const w1 = w0 / scale;
|
|
75
|
-
const u1 = vec2.length(uDelta) * startScale;
|
|
76
|
-
// u0 is treated as '0' in Eq (9).
|
|
77
|
-
// If u1 is too small, will generate invalid number
|
|
78
|
-
const _u1 = Math.max(u1, EPSILON);
|
|
79
|
-
// Implement Equation (9) from above algorithm.
|
|
80
|
-
const rho2 = rho * rho;
|
|
81
|
-
const b0 = (w1 * w1 - w0 * w0 + rho2 * rho2 * _u1 * _u1) / (2 * w0 * rho2 * _u1);
|
|
82
|
-
const b1 = (w1 * w1 - w0 * w0 - rho2 * rho2 * _u1 * _u1) / (2 * w1 * rho2 * _u1);
|
|
83
|
-
const r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0);
|
|
84
|
-
const r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
|
|
85
|
-
const S = (r1 - r0) / rho;
|
|
86
|
-
return { startZoom, startCenterXY, uDelta, w0, u1, S, rho, rho2, r0, r1 };
|
|
87
|
-
}
|
package/dist/get-bounds.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { worldToLngLat } from './web-mercator-utils';
|
|
2
|
-
import * as vec2 from 'gl-matrix/vec2';
|
|
3
|
-
import { transformVector } from './math-utils';
|
|
4
|
-
const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
5
|
-
/*
|
|
6
|
-
* Returns the quad at the intersection of the frustum and the given z plane
|
|
7
|
-
* @param {WebMercatorViewport} viewport
|
|
8
|
-
* @param {Number} z - elevation in meters
|
|
9
|
-
*/
|
|
10
|
-
export default function getBounds(viewport, z = 0) {
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
12
|
-
const { width, height, unproject } = viewport;
|
|
13
|
-
const unprojectOps = { targetZ: z };
|
|
14
|
-
const bottomLeft = unproject([0, height], unprojectOps);
|
|
15
|
-
const bottomRight = unproject([width, height], unprojectOps);
|
|
16
|
-
let topLeft;
|
|
17
|
-
let topRight;
|
|
18
|
-
const halfFov = viewport.fovy
|
|
19
|
-
? 0.5 * viewport.fovy * DEGREES_TO_RADIANS
|
|
20
|
-
: Math.atan(0.5 / viewport.altitude);
|
|
21
|
-
const angleToGround = (90 - viewport.pitch) * DEGREES_TO_RADIANS;
|
|
22
|
-
// The top plane is parallel to the ground if halfFov == angleToGround
|
|
23
|
-
if (halfFov > angleToGround - 0.01) {
|
|
24
|
-
// intersect with the far plane
|
|
25
|
-
topLeft = unprojectOnFarPlane(viewport, 0, z);
|
|
26
|
-
topRight = unprojectOnFarPlane(viewport, width, z);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
// intersect with the top plane
|
|
30
|
-
topLeft = unproject([0, 0], unprojectOps);
|
|
31
|
-
topRight = unproject([width, 0], unprojectOps);
|
|
32
|
-
}
|
|
33
|
-
return [bottomLeft, bottomRight, topRight, topLeft];
|
|
34
|
-
}
|
|
35
|
-
/*
|
|
36
|
-
* Find a point on the far clipping plane of the viewport
|
|
37
|
-
* @param {WebMercatorViewport} viewport
|
|
38
|
-
* @param {Number} x - projected x in screen space
|
|
39
|
-
* @param {Number} targetZ - the elevation of the point in meters
|
|
40
|
-
*/
|
|
41
|
-
function unprojectOnFarPlane(viewport, x, targetZ) {
|
|
42
|
-
const { pixelUnprojectionMatrix } = viewport;
|
|
43
|
-
const coord0 = transformVector(pixelUnprojectionMatrix, [x, 0, 1, 1]);
|
|
44
|
-
const coord1 = transformVector(pixelUnprojectionMatrix, [x, viewport.height, 1, 1]);
|
|
45
|
-
const z = targetZ * viewport.distanceScales.unitsPerMeter[2];
|
|
46
|
-
const t = (z - coord0[2]) / (coord1[2] - coord0[2]);
|
|
47
|
-
const coord = vec2.lerp([], coord0, coord1, t);
|
|
48
|
-
const result = worldToLngLat(coord);
|
|
49
|
-
result.push(targetZ);
|
|
50
|
-
return result;
|
|
51
|
-
}
|
package/dist/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// Classic web-mercator-project
|
|
2
|
-
export { default } from './web-mercator-viewport';
|
|
3
|
-
export { default as WebMercatorViewport } from './web-mercator-viewport';
|
|
4
|
-
export { default as getBounds } from './get-bounds';
|
|
5
|
-
export { default as fitBounds } from './fit-bounds';
|
|
6
|
-
export { default as normalizeViewportProps } from './normalize-viewport-props';
|
|
7
|
-
export { default as flyToViewport, getFlyToDuration } from './fly-to-viewport';
|
|
8
|
-
export { MAX_LATITUDE, lngLatToWorld, worldToLngLat, worldToPixels, pixelsToWorld, zoomToScale, scaleToZoom, altitudeToFovy, fovyToAltitude, getMeterZoom, getDistanceScales, addMetersToLngLat, getViewMatrix, getProjectionMatrix, getProjectionParameters } from './web-mercator-utils';
|
package/dist/math-utils.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { transformMat4, scale } from 'gl-matrix/vec4';
|
|
2
|
-
// Helper, avoids low-precision 32 bit matrices from gl-matrix mat4.create()
|
|
3
|
-
export function createMat4() {
|
|
4
|
-
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
5
|
-
}
|
|
6
|
-
// Transforms a vec4 with a projection matrix
|
|
7
|
-
export function transformVector(matrix, vector) {
|
|
8
|
-
const result = transformMat4([], vector, matrix);
|
|
9
|
-
scale(result, result, 1 / result[3]);
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
12
|
-
export function mod(value, divisor) {
|
|
13
|
-
const modulus = value % divisor;
|
|
14
|
-
return modulus < 0 ? divisor + modulus : modulus;
|
|
15
|
-
}
|
|
16
|
-
export function lerp(start, end, step) {
|
|
17
|
-
return step * end + (1 - step) * start;
|
|
18
|
-
}
|
|
19
|
-
export function clamp(x, min, max) {
|
|
20
|
-
return x < min ? min : x > max ? max : x;
|
|
21
|
-
}
|
|
22
|
-
function ieLog2(x) {
|
|
23
|
-
return Math.log(x) * Math.LOG2E;
|
|
24
|
-
}
|
|
25
|
-
// Handle missing log2 in IE 11
|
|
26
|
-
export const log2 = Math.log2 || ieLog2;
|