maplibre-gl-layers 0.1.0
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/README.md +112 -0
- package/dist/SpriteLayer.d.ts +256 -0
- package/dist/easing.d.ts +19 -0
- package/dist/index.cjs +24163 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +24163 -0
- package/dist/index.mjs.map +1 -0
- package/dist/interpolation.d.ts +90 -0
- package/dist/location.d.ts +24 -0
- package/dist/math.d.ts +467 -0
- package/dist/numericInterpolation.d.ts +80 -0
- package/dist/rotationInterpolation.d.ts +44 -0
- package/dist/types.d.ts +351 -0
- package/dist/utils.d.ts +16 -0
- package/images/demo1.png +0 -0
- package/images/demo2.png +0 -0
- package/images/maplibre-gl-layers-120.png +0 -0
- package/package.json +75 -0
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# maplibre-gl-layers
|
|
2
|
+
|
|
3
|
+
MapLibre's layer extension library enabling the display, movement, and modification of large numbers of dynamic sprite images
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
[](https://www.repostatus.org/#concept)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
----
|
|
11
|
+
|
|
12
|
+
## What is this?
|
|
13
|
+
|
|
14
|
+
With [MapLibre GL JS](https://maplibre.org/maplibre-gl-js/docs/), you can place markers on a map, decorate their appearance, and move them freely.
|
|
15
|
+
Markers often need to move smoothly, appear and disappear over time, and you may have countless coordinates to render.
|
|
16
|
+
|
|
17
|
+
**maplibre-gl-layers** is designed to meet that need.
|
|
18
|
+
|
|
19
|
+
Using this package, you can place and adjust large collections of sprites (marker images) through a straightforward API:
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
Here is a minimal example that places a single sprite:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
// Use MapLibre GL JS together with maplibre-gl-layers
|
|
27
|
+
import { Map } from 'maplibre-gl';
|
|
28
|
+
import { createSpriteLayer } from 'maplibre-gl-layers';
|
|
29
|
+
|
|
30
|
+
// Create the MapLibre instance
|
|
31
|
+
const map = new Map({
|
|
32
|
+
container: 'map',
|
|
33
|
+
style: 'https://demotiles.maplibre.org/style.json',
|
|
34
|
+
center: [136.885202573, 35.170006912],
|
|
35
|
+
zoom: 13,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Create the SpriteLayer
|
|
39
|
+
const spriteLayer = createSpriteLayer({ id: 'vehicles' });
|
|
40
|
+
|
|
41
|
+
// Add the layer after the map finishes loading
|
|
42
|
+
map.on('load', async () => {
|
|
43
|
+
map.addLayer(spriteLayer);
|
|
44
|
+
|
|
45
|
+
// Register an image that can be referenced by sprites
|
|
46
|
+
const MARKER_IMAGE_ID = 'marker';
|
|
47
|
+
await spriteLayer.registerImage(MARKER_IMAGE_ID, '/assets/marker.png');
|
|
48
|
+
|
|
49
|
+
// Place a sprite that uses the registered image
|
|
50
|
+
const SPRITE_ID = 'vehicle-1';
|
|
51
|
+
spriteLayer.addSprite(SPRITE_ID, {
|
|
52
|
+
// Specific location
|
|
53
|
+
location: { lng: 136.8852, lat: 35.17 },
|
|
54
|
+
images: [
|
|
55
|
+
{
|
|
56
|
+
subLayer: 0,
|
|
57
|
+
order: 0,
|
|
58
|
+
imageId: MARKER_IMAGE_ID, // The image ID
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// ...continue manipulating sprites through the SpriteLayer API
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can place, update, and remove sprites or the images assigned to them at any time through the API.
|
|
68
|
+
|
|
69
|
+
In addition to images, you can render text alongside sprites and animate them together. That makes it easy to build the kinds of visualizations typically required for assets, vehicles, or other moving features:
|
|
70
|
+
|
|
71
|
+

|
|
72
|
+
|
|
73
|
+
### Main Features
|
|
74
|
+
|
|
75
|
+
- Place, update, and remove large numbers of sprites.
|
|
76
|
+
- Move each sprite's coordinate freely, making it easy to represent moving objects.
|
|
77
|
+
- Specify per-sprite anchor positions for precise rendering.
|
|
78
|
+
- Add multiple images and text to the same sprite, adjusting rotation, offset, scale, opacity, and more.
|
|
79
|
+
- Animate sprite movement, rotation, and offsets with interpolation controls.
|
|
80
|
+
- Control draw order via sub-layers and per-sprite ordering.
|
|
81
|
+
- Bulk update API for many sprite states.
|
|
82
|
+
- No package dependencies except MapLibre.
|
|
83
|
+
|
|
84
|
+
### Requirements
|
|
85
|
+
|
|
86
|
+
- MapLibre GL JS 5.9 or higher
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Installation
|
|
91
|
+
|
|
92
|
+
The library is published as an npm package. Install it in your project with:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm install maplibre-gl-layers
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
----
|
|
99
|
+
|
|
100
|
+
## Documentation
|
|
101
|
+
|
|
102
|
+
[See the repository documentation](http://github.com/kekyo/maplibre-gl-layers/).
|
|
103
|
+
|
|
104
|
+
## Discussions and Pull Requests
|
|
105
|
+
|
|
106
|
+
For discussions, please refer to the [GitHub Discussions page](https://github.com/kekyo/maplibre-gl-layers/discussions). We have currently stopped issue-based discussions.
|
|
107
|
+
|
|
108
|
+
Pull requests are welcome! Please submit them as diffs against the `develop` branch and squashed changes before send.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
Under MIT.
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: maplibre-gl-layers
|
|
3
|
+
* version: 0.1.0
|
|
4
|
+
* description: MapLibre's layer extension library enabling the display, movement, and modification of large numbers of dynamic sprite images
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/maplibre-gl-layers.git
|
|
8
|
+
* git.commit.hash: 88b09a93779279947d9c6b1e3a7538d4d197dbcb
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Map as MapLibreMap } from 'maplibre-gl';
|
|
12
|
+
import { SpriteMode, SpriteLayerInterface, SpriteLayerOptions, SpriteAnchor, SpriteLocation, SpriteImageDefinitionInit, SpriteImageOffset, SpriteInterpolationOptions, SpriteNumericInterpolationOptions, SpriteImageOriginLocation } from './types';
|
|
13
|
+
import { SpriteInterpolationState } from './interpolation';
|
|
14
|
+
import { NumericInterpolationState } from './numericInterpolation';
|
|
15
|
+
/**
|
|
16
|
+
* Compact representation of an Array-like 4x4 matrix.
|
|
17
|
+
* Accepts typed arrays sourced from MapLibre internals.
|
|
18
|
+
*/
|
|
19
|
+
type MatrixInput = ArrayLike<number>;
|
|
20
|
+
/**
|
|
21
|
+
* Cached clip-space context containing the mercator matrix required to project coordinates.
|
|
22
|
+
* @property {MatrixInput} mercatorMatrix - Matrix mapping Mercator coordinates to clip space.
|
|
23
|
+
*/
|
|
24
|
+
type ClipContext = {
|
|
25
|
+
readonly mercatorMatrix: MatrixInput;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Computes the perspective ratio from MapLibre's internal transform.
|
|
29
|
+
* Used to calculate distance-based scaling that responds to pitch, zoom, and altitude.
|
|
30
|
+
* @param {MapLibreMap} mapInstance - MapLibre map providing the transform and camera distance.
|
|
31
|
+
* @param {SpriteLocation} location - Location used to derive mercator coordinates for scaling.
|
|
32
|
+
* @returns {number} Perspective ratio applied when scaling sprites; defaults to 1 if unavailable.
|
|
33
|
+
*/
|
|
34
|
+
export declare const calculatePerspectiveRatio: (mapInstance: MapLibreMap, location: SpriteLocation) => number;
|
|
35
|
+
/**
|
|
36
|
+
* Multiplies a 4x4 matrix with a 4-component vector using row-major indexing.
|
|
37
|
+
* @param {MatrixInput} matrix - Matrix to multiply.
|
|
38
|
+
* @param {number} x - X component of the vector.
|
|
39
|
+
* @param {number} y - Y component of the vector.
|
|
40
|
+
* @param {number} z - Z component of the vector.
|
|
41
|
+
* @param {number} w - W component of the vector.
|
|
42
|
+
* @returns {[number, number, number, number]} Resulting homogeneous coordinate.
|
|
43
|
+
*/
|
|
44
|
+
export declare const multiplyMatrixAndVector: (matrix: MatrixInput, x: number, y: number, z: number, w: number) => [number, number, number, number];
|
|
45
|
+
/**
|
|
46
|
+
* Projects a longitude/latitude/elevation tuple into clip space using the provided context.
|
|
47
|
+
* @param {number} lng - Longitude in degrees.
|
|
48
|
+
* @param {number} lat - Latitude in degrees.
|
|
49
|
+
* @param {number} elevationMeters - Elevation above the ellipsoid in meters.
|
|
50
|
+
* @param {ClipContext | null} context - Clip-space context; `null` skips projection.
|
|
51
|
+
* @returns {[number, number, number, number] | null} Clip coordinates or `null` when projection fails.
|
|
52
|
+
*/
|
|
53
|
+
export declare const projectLngLatToClipSpace: (lng: number, lat: number, elevationMeters: number, context: ClipContext | null) => [number, number, number, number] | null;
|
|
54
|
+
/**
|
|
55
|
+
* Applies auto-rotation to all images within a sprite when movement exceeds the configured threshold.
|
|
56
|
+
* @template T Arbitrary sprite tag type.
|
|
57
|
+
* @param {InternalSpriteCurrentState<T>} sprite - Sprite undergoing potential rotation update.
|
|
58
|
+
* @param {SpriteLocation} nextLocation - Destination location used to derive bearing and distance.
|
|
59
|
+
* @returns {boolean} `true` when auto-rotation was applied, `false` otherwise.
|
|
60
|
+
*/
|
|
61
|
+
export declare const applyAutoRotation: <T>(sprite: InternalSpriteCurrentState<T>, nextLocation: SpriteLocation) => boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Compiles a shader from source, throwing if compilation fails.
|
|
64
|
+
* @param {WebGLRenderingContext} glContext - Active WebGL context.
|
|
65
|
+
* @param {number} type - Shader type (`VERTEX_SHADER` or `FRAGMENT_SHADER`).
|
|
66
|
+
* @param {string} source - GLSL source code.
|
|
67
|
+
* @returns {WebGLShader} Compiled shader object.
|
|
68
|
+
* @throws When shader creation or compilation fails.
|
|
69
|
+
*/
|
|
70
|
+
export declare const compileShader: (glContext: WebGLRenderingContext, type: number, source: string) => WebGLShader;
|
|
71
|
+
/**
|
|
72
|
+
* Links a vertex and fragment shader into a WebGL program.
|
|
73
|
+
* @param {WebGLRenderingContext} glContext - Active WebGL context.
|
|
74
|
+
* @param {string} vertexSource - Vertex shader GLSL source.
|
|
75
|
+
* @param {string} fragmentSource - Fragment shader GLSL source.
|
|
76
|
+
* @returns {WebGLProgram} Linked shader program ready for use.
|
|
77
|
+
* @throws When linking fails or a program cannot be created.
|
|
78
|
+
*/
|
|
79
|
+
export declare const createShaderProgram: (glContext: WebGLRenderingContext, vertexSource: string, fragmentSource: string) => WebGLProgram;
|
|
80
|
+
/**
|
|
81
|
+
* Base attributes for an image that composes a sprite.
|
|
82
|
+
*/
|
|
83
|
+
interface InternalSpriteImageState {
|
|
84
|
+
/**
|
|
85
|
+
* Sub-layer identifier.
|
|
86
|
+
*/
|
|
87
|
+
subLayer: number;
|
|
88
|
+
/**
|
|
89
|
+
* Ordering value within the sub-layer.
|
|
90
|
+
*/
|
|
91
|
+
order: number;
|
|
92
|
+
/**
|
|
93
|
+
* Image ID referenced for rendering.
|
|
94
|
+
*/
|
|
95
|
+
imageId: string;
|
|
96
|
+
/**
|
|
97
|
+
* Rendering mode. Defaults to surface.
|
|
98
|
+
*/
|
|
99
|
+
mode: SpriteMode;
|
|
100
|
+
/**
|
|
101
|
+
* Opacity applied to the image alpha. Defaults to 1.0.
|
|
102
|
+
*/
|
|
103
|
+
opacity: number;
|
|
104
|
+
/**
|
|
105
|
+
* Multiplier for real-world meters corresponding to one image pixel. 1.0 = 1 meter. Defaults to 1.0.
|
|
106
|
+
*/
|
|
107
|
+
scale: number;
|
|
108
|
+
/**
|
|
109
|
+
* Anchor position within the sprite. Defaults to [0.0, 0.0].
|
|
110
|
+
*/
|
|
111
|
+
anchor: SpriteAnchor;
|
|
112
|
+
/**
|
|
113
|
+
* Offset from the sprite coordinate. Defaults to no offset.
|
|
114
|
+
*/
|
|
115
|
+
offset: SpriteImageOffset;
|
|
116
|
+
/**
|
|
117
|
+
* Requested rotation angle in degrees. Defaults to 0.
|
|
118
|
+
* Billboard mode: Clockwise rotation relative to the viewport.
|
|
119
|
+
* Surface mode: Clockwise azimuth from geographic north.
|
|
120
|
+
*/
|
|
121
|
+
rotateDeg: number;
|
|
122
|
+
/**
|
|
123
|
+
* Rotation currently applied during rendering.
|
|
124
|
+
*/
|
|
125
|
+
displayedRotateDeg: number;
|
|
126
|
+
/**
|
|
127
|
+
* Whether auto-rotation is enabled. Defaults to true in surface mode and false in billboard mode.
|
|
128
|
+
* The sprite orientation is derived from its movement vector when enabled.
|
|
129
|
+
*/
|
|
130
|
+
autoRotation: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Minimum distance in meters required before auto-rotation updates. Defaults to 20 m.
|
|
133
|
+
* Values <= 0 trigger immediate updates.
|
|
134
|
+
*/
|
|
135
|
+
autoRotationMinDistanceMeters: number;
|
|
136
|
+
/**
|
|
137
|
+
* Base rotation determined by auto-rotation. Initially 0.
|
|
138
|
+
*/
|
|
139
|
+
resolvedBaseRotateDeg: number;
|
|
140
|
+
/**
|
|
141
|
+
* Reference sub-layer used as the origin for offsets. Defaults to sprite coordinates.
|
|
142
|
+
*/
|
|
143
|
+
originLocation?: SpriteImageOriginLocation;
|
|
144
|
+
/**
|
|
145
|
+
* Interpolation state for display rotation.
|
|
146
|
+
*/
|
|
147
|
+
rotationInterpolationState: NumericInterpolationState | null;
|
|
148
|
+
/**
|
|
149
|
+
* Default interpolation options for display rotation.
|
|
150
|
+
*/
|
|
151
|
+
rotationInterpolationOptions: SpriteNumericInterpolationOptions | null;
|
|
152
|
+
/**
|
|
153
|
+
* Interpolation state used for offset.offsetDeg.
|
|
154
|
+
*/
|
|
155
|
+
offsetInterpolationState: NumericInterpolationState | null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Sprite position interpolation state.
|
|
159
|
+
*/
|
|
160
|
+
type InternalSpriteInterpolationState = SpriteInterpolationState;
|
|
161
|
+
/**
|
|
162
|
+
* Current sprite state.
|
|
163
|
+
* @param TTag Tag type.
|
|
164
|
+
*/
|
|
165
|
+
interface InternalSpriteCurrentState<TTag> {
|
|
166
|
+
/**
|
|
167
|
+
* Sprite identifier.
|
|
168
|
+
*/
|
|
169
|
+
spriteId: string;
|
|
170
|
+
/**
|
|
171
|
+
* Whether the sprite is enabled.
|
|
172
|
+
*/
|
|
173
|
+
isEnabled: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Current location (interpolated position when moving).
|
|
176
|
+
*/
|
|
177
|
+
currentLocation: SpriteLocation;
|
|
178
|
+
/**
|
|
179
|
+
* Source location used for movement interpolation.
|
|
180
|
+
* Feedback mode: previous command location.
|
|
181
|
+
* Feed-forward mode: current command location.
|
|
182
|
+
*/
|
|
183
|
+
fromLocation?: SpriteLocation;
|
|
184
|
+
/**
|
|
185
|
+
* Destination location used for movement interpolation.
|
|
186
|
+
* Feedback mode: current command location.
|
|
187
|
+
* Feed-forward mode: predicted location.
|
|
188
|
+
*/
|
|
189
|
+
toLocation?: SpriteLocation;
|
|
190
|
+
/**
|
|
191
|
+
* Map of image states currently associated with the sprite.
|
|
192
|
+
*/
|
|
193
|
+
images: Map<number, Map<number, InternalSpriteImageState>>;
|
|
194
|
+
/**
|
|
195
|
+
* Optional tag (null when not set).
|
|
196
|
+
*/
|
|
197
|
+
tag: TTag | null;
|
|
198
|
+
/**
|
|
199
|
+
* Active interpolation state, or null when idle.
|
|
200
|
+
*/
|
|
201
|
+
interpolationState: InternalSpriteInterpolationState | null;
|
|
202
|
+
/**
|
|
203
|
+
* Most recently requested interpolation options, or null if none pending.
|
|
204
|
+
*/
|
|
205
|
+
pendingInterpolationOptions: SpriteInterpolationOptions | null;
|
|
206
|
+
/**
|
|
207
|
+
* Most recently commanded location, regardless of interpolation.
|
|
208
|
+
*/
|
|
209
|
+
lastCommandLocation: SpriteLocation;
|
|
210
|
+
/**
|
|
211
|
+
* Latest location used as the basis for auto-rotation calculation.
|
|
212
|
+
*/
|
|
213
|
+
lastAutoRotationLocation: SpriteLocation;
|
|
214
|
+
/**
|
|
215
|
+
* Last resolved base angle from auto-rotation in degrees, reused as the next initial value.
|
|
216
|
+
*/
|
|
217
|
+
lastAutoRotationAngleDeg: number;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Clones a sprite anchor, defaulting to the origin when none supplied.
|
|
221
|
+
* @param {SpriteAnchor} [anchor] - Anchor to clone.
|
|
222
|
+
* @returns {SpriteAnchor} Safe copy for mutation within the layer state.
|
|
223
|
+
*/
|
|
224
|
+
export declare const cloneAnchor: (anchor?: SpriteAnchor) => SpriteAnchor;
|
|
225
|
+
/**
|
|
226
|
+
* Clones an image offset, applying defaults when missing.
|
|
227
|
+
* @param {SpriteImageOffset} [offset] - Offset definition to copy.
|
|
228
|
+
* @returns {SpriteImageOffset} Cloned offset structure.
|
|
229
|
+
*/
|
|
230
|
+
export declare const cloneOffset: (offset?: SpriteImageOffset) => SpriteImageOffset;
|
|
231
|
+
/**
|
|
232
|
+
* Deep-clones movement interpolation options to prevent shared references between sprites.
|
|
233
|
+
* @param {SpriteInterpolationOptions} options - Options provided by the user.
|
|
234
|
+
* @returns {SpriteInterpolationOptions} Cloned options object.
|
|
235
|
+
*/
|
|
236
|
+
export declare const cloneInterpolationOptions: (options: SpriteInterpolationOptions) => SpriteInterpolationOptions;
|
|
237
|
+
/**
|
|
238
|
+
* Creates internal sprite image state from initialization data and layer bookkeeping fields.
|
|
239
|
+
* @param {SpriteImageDefinitionInit} imageInit - Caller-provided image definition.
|
|
240
|
+
* @param {number} subLayer - Sub-layer index the image belongs to.
|
|
241
|
+
* @param {number} order - Ordering slot within the sub-layer.
|
|
242
|
+
* @returns {InternalSpriteImageState} Normalized internal state ready for rendering.
|
|
243
|
+
*/
|
|
244
|
+
export declare const createImageStateFromInit: (imageInit: SpriteImageDefinitionInit, subLayer: number, order: number) => InternalSpriteImageState;
|
|
245
|
+
/**
|
|
246
|
+
* Factory that creates the MapLibre layer interface for the sprite layer.
|
|
247
|
+
* Implements the CustomLayerInterface lifecycle (init -> render -> dispose), packing thousands
|
|
248
|
+
* of sprites into GPU buffers for efficient rendering. Supports optional scaling controls for
|
|
249
|
+
* billboard and surface modes.
|
|
250
|
+
*
|
|
251
|
+
* @template T Arbitrary tag type for per-sprite metadata.
|
|
252
|
+
* @param {SpriteLayerOptions} [options] Initial layer options such as ID or scaling settings.
|
|
253
|
+
* @returns {SpriteLayerInterface<T>} Interface for sprite add/update/remove operations and MapLibre hooks.
|
|
254
|
+
*/
|
|
255
|
+
export declare const createSpriteLayer: <T = any>(options?: SpriteLayerOptions) => SpriteLayerInterface<T>;
|
|
256
|
+
export {};
|
package/dist/easing.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: maplibre-gl-layers
|
|
3
|
+
* version: 0.1.0
|
|
4
|
+
* description: MapLibre's layer extension library enabling the display, movement, and modification of large numbers of dynamic sprite images
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/maplibre-gl-layers.git
|
|
8
|
+
* git.commit.hash: 88b09a93779279947d9c6b1e3a7538d4d197dbcb
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { EasingFunction } from './types';
|
|
12
|
+
/**
|
|
13
|
+
* Linear interpolation that clamps the value to the [0, 1] range.
|
|
14
|
+
*/
|
|
15
|
+
export declare const linearEasing: EasingFunction;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the provided easing function or falls back to linear interpolation.
|
|
18
|
+
*/
|
|
19
|
+
export declare const resolveEasing: (easing?: EasingFunction) => EasingFunction;
|