@naivemap/mapbox-gl-image-layer 0.4.2 → 0.6.0-alpha.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Naive Map
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Naive Map
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,76 +1,11 @@
1
- ## ImageLayer class
2
-
3
- ```bash
4
- npm i @naivemap/mapbox-gl-image-layer proj4
5
- ```
6
-
7
- <b>Signature:</b>
8
-
9
- ```typescript
10
- export default class ImageLayer implements mapboxgl.CustomLayerInterface
11
- ```
12
-
13
- <b>Implements:</b> mapboxgl.CustomLayerInterface
14
-
15
- ## Constructors
16
-
17
- | Constructor | Description |
18
- | --- | --- |
19
- | (constructor)(`id`: `string`, `option`: `ImageOption`) | Constructs a new instance of the <code>ImageLayer</code> class |
20
-
21
- ### Parameters
22
-
23
- **id** `(string)` The ID of the layer.
24
-
25
- **option** `(ImageOption)` The option of the image.
26
-
27
- | Name | Description |
28
- | --- | --- |
29
- | **option.url** <br />(`string`) | URL that points to an image. |
30
- | **option.projection** <br />(`string`) | Projection with EPSG code that points to the image. |
31
- | **option.coordinates** <br />(`Array<Array<number>>`) | Corners of image specified in longitude, latitude pairs: top left, top right, bottom right, bottom left. ref: [coordinates](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#image-coordinates) |
32
- | **option.resampling** <br />(Optional `enum`. One of `"linear"`, `"nearest"`. Defaults to `"linear"`) | The resampling/interpolation method to use for overscaling, also known as texture magnification filter. ref: [raster-resampling](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-raster-raster-resampling) |
33
- | **option.opacity** <br />(Optional `number` between 0 and 1 inclusive. Defaults to 1. | The opacity at which the image will be drawn. |
34
- | **options.crossOrigin** <br />(`string`) | The crossOrigin attribute is a string which specifies the Cross-Origin Resource Sharing ([CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS)) setting to use when retrieving the image. |
35
-
36
- ```ts
37
- export type ImageOption = {
38
- url: string
39
- projection: string
40
- coordinates: Coordinates
41
- resampling: 'linear' | 'nearest'
42
- }
43
- ```
44
-
45
- ### Methods
46
-
47
- #### updateImage
48
-
49
- Updates the image URL and, optionally, the projection, the coordinates and the resampling.
50
-
51
- ```ts
52
- updateImage(option: {
53
- url: string
54
- projection?: string
55
- coordinates?: Coordinates
56
- resampling?: 'linear' | 'nearest'
57
- }): this
58
- ```
59
-
60
- ## Example
61
-
62
- ```ts
63
- const layer = new ImageLayer('layer-id', {
64
- url: '/4326.png',
65
- projection: 'EPSG:4326',
66
- resampling: 'nearest',
67
- coordinates: [
68
- [105.289838, 32.204171], // top-left
69
- [110.195632, 32.204171], // top-right
70
- [110.195632, 28.164713], // bottom-right
71
- [105.289838, 28.164713], // bottom-left
72
- ],
73
- })
74
-
75
- map.addLayer(layer)
76
- ```
1
+ # @naivemap/mapbox-gl-image-layer
2
+
3
+ ImageLayer ([@naivemap/mapbox-gl-image-layer](https://www.npmjs.com/package/@naivemap/mapbox-gl-image-layer)): A versatile layer for displaying georeferenced images with **various projections** on the map. It supports loading and correctly positioning images from different coordinate systems, making it ideal for integrating non-standard raster data, historical maps, or aerial imagery.
4
+
5
+ [API References](https://www.naivemap.com/map-gl-layers/api/image-layer/)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @naivemap/mapbox-gl-image-layer proj4
11
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";const o=require("@naivemap/image-layer-core"),a=require("@naivemap/map-gl-layer-adaptor");class d{id;type="custom";renderingMode="2d";core;adaptor;constructor(e,r){this.id=e,this.core=new o(r),this.adaptor=a.createMapboxLayerAdaptor(this.core,{id:e,renderingMode:this.renderingMode,prepareStencilMask:t=>a.clearTileClippingMask(t)})}onAdd(e,r){this.adaptor.onAdd(e,r)}onRemove(e,r){this.adaptor.onRemove(e,r)}render(e,r){this.adaptor.render(e,r)}updateImage(e){return this.core.updateImage(e)}updateMask(e){return this.core.updateMask(e)}}module.exports=d;
@@ -0,0 +1,6 @@
1
+ export * from './mapbox-gl-image-layer/src/index'
2
+ export {}
3
+ import MapboxImageLayer from './mapbox-gl-image-layer/src/index'
4
+ export default MapboxImageLayer
5
+ export * from './mapbox-gl-image-layer/src/index'
6
+ export {}
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ import t from "@naivemap/image-layer-core";
2
+ import { createMapboxLayerAdaptor as o, clearTileClippingMask as d } from "@naivemap/map-gl-layer-adaptor";
3
+ class n {
4
+ id;
5
+ /**
6
+ * @ignore
7
+ */
8
+ type = "custom";
9
+ /**
10
+ * @ignore
11
+ */
12
+ renderingMode = "2d";
13
+ core;
14
+ adaptor;
15
+ /**
16
+ * @param id - A unique layer id
17
+ * @param option - ImageLayer options
18
+ */
19
+ constructor(e, r) {
20
+ this.id = e, this.core = new t(r), this.adaptor = o(this.core, {
21
+ id: e,
22
+ renderingMode: this.renderingMode,
23
+ prepareStencilMask: (a) => d(a)
24
+ });
25
+ }
26
+ /**
27
+ * @ignore
28
+ */
29
+ onAdd(e, r) {
30
+ this.adaptor.onAdd(e, r);
31
+ }
32
+ /**
33
+ * @ignore
34
+ */
35
+ onRemove(e, r) {
36
+ this.adaptor.onRemove(e, r);
37
+ }
38
+ /**
39
+ * @ignore
40
+ */
41
+ render(e, r) {
42
+ this.adaptor.render(e, r);
43
+ }
44
+ /**
45
+ * Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
46
+ * @param {Object} option Options object.
47
+ */
48
+ updateImage(e) {
49
+ return this.core.updateImage(e);
50
+ }
51
+ /**
52
+ * Updates the mask property of the image layer.
53
+ * @param mask
54
+ */
55
+ updateMask(e) {
56
+ return this.core.updateMask(e);
57
+ }
58
+ }
59
+ export {
60
+ n as default
61
+ };
@@ -0,0 +1,46 @@
1
+ import { default as ImageLayerCore, ImageLayerOption, MaskProperty } from '../../image-layer-core/src/index.ts';
2
+ import { CustomLayerInterface, Map } from 'mapbox-gl';
3
+ export type { ImageLayerOption } from '../../image-layer-core/src/index.ts';
4
+ /**
5
+ * A custom Mapbox GL JS layer for rendering georeferenced images with arbitrary projections.
6
+ */
7
+ export default class ImageLayer implements CustomLayerInterface {
8
+ id: string;
9
+ /**
10
+ * @ignore
11
+ */
12
+ type: 'custom';
13
+ /**
14
+ * @ignore
15
+ */
16
+ renderingMode?: '2d' | '3d' | undefined;
17
+ private core;
18
+ private adaptor;
19
+ /**
20
+ * @param id - A unique layer id
21
+ * @param option - ImageLayer options
22
+ */
23
+ constructor(id: string, option: ImageLayerOption);
24
+ /**
25
+ * @ignore
26
+ */
27
+ onAdd(map: Map, gl: WebGLRenderingContext): void;
28
+ /**
29
+ * @ignore
30
+ */
31
+ onRemove(map: Map, gl: WebGLRenderingContext): void;
32
+ /**
33
+ * @ignore
34
+ */
35
+ render(gl: WebGLRenderingContext, args: unknown): void;
36
+ /**
37
+ * Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
38
+ * @param {Object} option Options object.
39
+ */
40
+ updateImage(option: Partial<Omit<ImageLayerOption, 'mask'>>): ImageLayerCore;
41
+ /**
42
+ * Updates the mask property of the image layer.
43
+ * @param mask
44
+ */
45
+ updateMask(mask: Partial<MaskProperty>): ImageLayerCore;
46
+ }
@@ -0,0 +1,4 @@
1
+ import { default as ImageLayer } from './ImageLayer';
2
+ export type { ImageLayerOption } from '../../image-layer-core/src/index.ts';
3
+ export default ImageLayer;
4
+ export type { MaskProperty } from '../../image-layer-core/src/index.ts';
package/package.json CHANGED
@@ -1,57 +1,47 @@
1
1
  {
2
2
  "name": "@naivemap/mapbox-gl-image-layer",
3
- "version": "0.4.2",
4
- "description": "Load a static image of any projection via Arrugator and Proj4js.",
5
- "author": "huanglii <li.huangli@qq.com>",
6
- "homepage": "https://github.com/naivemap/mapbox-gl-layers#readme",
7
- "license": "MIT",
8
- "main": "dist/js/index.js",
9
- "module": "dist/es/index.js",
3
+ "version": "0.6.0-alpha.0",
4
+ "description": "Load a static image of any projection via Arrugator and Proj4js for Mapbox GL JS.",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "type": "module",
10
8
  "exports": {
11
- "./package.json": "./package.json",
12
9
  ".": {
13
- "import": "./dist/es/index.js",
14
- "require": "./dist/js/index.js"
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
15
13
  }
16
14
  },
17
- "types": "dist/js/index.d.ts",
18
- "sideEffects": false,
19
15
  "files": [
20
16
  "dist"
21
17
  ],
22
18
  "keywords": [
23
- "mapbox-gl",
24
- "reprojection",
25
- "proj4"
19
+ "mapbox",
20
+ "image",
21
+ "layer",
22
+ "custom"
26
23
  ],
24
+ "author": "naivemap",
25
+ "license": "MIT",
26
+ "types": "./dist/index.d.ts",
27
+ "repository": "https://github.com/naivemap/maplibre-gl-layers",
27
28
  "publishConfig": {
28
29
  "access": "public"
29
30
  },
30
- "repository": {
31
- "type": "git",
32
- "url": "git+https://github.com/naivemap/mapbox-gl-layers.git"
33
- },
34
- "scripts": {
35
- "test": "echo \"Error: run tests from root\" && exit 1",
36
- "build": "npm-run-all build:*",
37
- "build:es": "tsc --outDir dist/es --module esnext --declaration false",
38
- "build:js": "tsc",
39
- "prepublishOnly": "npm-run-all build:*"
40
- },
41
- "bugs": {
42
- "url": "https://github.com/naivemap/mapbox-gl-layers/issues"
31
+ "sideEffects": false,
32
+ "dependencies": {
33
+ "@naivemap/map-gl-layer-adaptor": "0.1.0",
34
+ "@naivemap/image-layer-core": "0.1.0"
43
35
  },
44
36
  "peerDependencies": {
37
+ "mapbox-gl": "^2.0.0 || ^3.0.0",
45
38
  "proj4": "^2.8.0"
46
39
  },
47
- "dependencies": {
48
- "arrugator": "^1.0.1"
49
- },
50
40
  "devDependencies": {
51
- "@types/mapbox-gl": "^2.7.2",
52
- "@types/node": "^17.0.36",
53
- "@types/proj4": "^2.5.2",
54
- "npm-run-all": "^4.1.5"
41
+ "mapbox-gl": "^3.10.0"
55
42
  },
56
- "gitHead": "5fc47667682399fe76c56d6808a44098bbd46636"
57
- }
43
+ "scripts": {
44
+ "build": "vite build",
45
+ "typedocs": "pnpm typedoc"
46
+ }
47
+ }
package/dist/es/index.js DELETED
@@ -1,132 +0,0 @@
1
- import { loadImage } from './utils/image';
2
- import { createProgram } from './utils/webgl';
3
- import { initArrugator } from './utils/arrugator';
4
- var ImageLayer = /** @class */ (function () {
5
- function ImageLayer(id, option) {
6
- this.id = id;
7
- this.type = 'custom';
8
- this.renderingMode = '2d';
9
- this._option = option;
10
- this._loaded = false;
11
- // 初始化 Arrugator
12
- var projection = option.projection, coordinates = option.coordinates;
13
- this._arrugado = initArrugator(projection, coordinates);
14
- this._map = null;
15
- this._gl = null;
16
- this._program = null;
17
- this._texture = null;
18
- this._positionBuffer = null;
19
- this._uvBuffer = null;
20
- this._verticesIndexBuffer = null;
21
- }
22
- ImageLayer.prototype.onAdd = function (map, gl) {
23
- this._map = map;
24
- this._gl = gl;
25
- this._loadImage(map, gl);
26
- var vertexSource = "\n uniform mat4 u_matrix;\n attribute vec2 a_pos;\n attribute vec2 a_uv;\n varying vec2 v_uv;\n void main() {\n gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n v_uv = a_uv;\n }";
27
- var fragmentSource = "\n #ifdef GL_ES\n precision highp int;\n precision mediump float;\n #endif\n uniform sampler2D u_sampler;\n uniform float u_opacity;\n varying vec2 v_uv;\n void main() {\n vec4 color = texture2D(u_sampler, v_uv);\n gl_FragColor = color * u_opacity;\n }";
28
- this._program = createProgram(gl, vertexSource, fragmentSource);
29
- if (this._program) {
30
- this._positionBuffer = gl.createBuffer();
31
- // gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer)
32
- // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.pos), gl.STATIC_DRAW)
33
- var a_pos = gl.getAttribLocation(this._program, 'a_pos');
34
- gl.vertexAttribPointer(a_pos, 2, gl.FLOAT, false, 0, 0);
35
- gl.enableVertexAttribArray(a_pos);
36
- this._uvBuffer = gl.createBuffer();
37
- // gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer)
38
- // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.uv), gl.STATIC_DRAW)
39
- var a_uv = gl.getAttribLocation(this._program, 'a_uv');
40
- gl.vertexAttribPointer(a_uv, 2, gl.FLOAT, false, 0, 0);
41
- gl.enableVertexAttribArray(a_uv);
42
- this._verticesIndexBuffer = gl.createBuffer();
43
- // gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer)
44
- // gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this._arrugado.trigs), gl.STATIC_DRAW)
45
- this._bindData(gl, this._arrugado);
46
- }
47
- };
48
- ImageLayer.prototype.onRemove = function (map, gl) {
49
- gl.deleteProgram(this._program);
50
- gl.deleteTexture(this._texture);
51
- gl.deleteBuffer(this._verticesIndexBuffer);
52
- };
53
- ImageLayer.prototype.render = function (gl, matrix) {
54
- if (this._loaded && this._program) {
55
- gl.useProgram(this._program);
56
- // matrix
57
- gl.uniformMatrix4fv(gl.getUniformLocation(this._program, 'u_matrix'), false, matrix);
58
- // pos
59
- gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
60
- gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_pos'), 2, gl.FLOAT, false, 0, 0);
61
- // uv
62
- gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
63
- gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_uv'), 2, gl.FLOAT, false, 0, 0);
64
- // index
65
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
66
- // texture
67
- gl.activeTexture(gl.TEXTURE0);
68
- gl.bindTexture(gl.TEXTURE_2D, this._texture);
69
- gl.uniform1i(gl.getUniformLocation(this._program, 'u_sampler'), 0);
70
- // opacity
71
- gl.uniform1f(gl.getUniformLocation(this._program, 'u_opacity'), this._option.opacity || 1);
72
- // blend
73
- gl.enable(gl.BLEND);
74
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
75
- gl.drawElements(gl.TRIANGLES, this._arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
76
- gl.bindBuffer(gl.ARRAY_BUFFER, null);
77
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
78
- }
79
- };
80
- /**
81
- * Updates the image URL and, optionally, the projection, the coordinates and the resampling.
82
- * @param {Object} option Options object.
83
- * @param {string} [option.url] Required image URL.
84
- * @param {string} [option.projection] Projection with EPSG code that points to the image..
85
- * @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
86
- * @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
87
- */
88
- ImageLayer.prototype.updateImage = function (option) {
89
- var _a, _b, _c;
90
- this._loaded = false;
91
- this._option.url = option.url;
92
- if (this._gl && this._map) {
93
- if (option.projection || option.coordinates) {
94
- this._option.projection = (_a = option.projection) !== null && _a !== void 0 ? _a : this._option.projection;
95
- this._option.coordinates = (_b = option.coordinates) !== null && _b !== void 0 ? _b : this._option.coordinates;
96
- // reinit arrugator
97
- this._arrugado = initArrugator(this._option.projection, this._option.coordinates);
98
- this._bindData(this._gl, this._arrugado);
99
- }
100
- this._option.resampling = (_c = option.resampling) !== null && _c !== void 0 ? _c : this._option.resampling;
101
- // reload image
102
- this._loadImage(this._map, this._gl);
103
- }
104
- return this;
105
- };
106
- ImageLayer.prototype._bindData = function (gl, arrugado) {
107
- gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
108
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arrugado.pos), gl.STATIC_DRAW);
109
- gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
110
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arrugado.uv), gl.STATIC_DRAW);
111
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
112
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(arrugado.trigs), gl.STATIC_DRAW);
113
- };
114
- ImageLayer.prototype._loadImage = function (map, gl) {
115
- var _this = this;
116
- loadImage(this._option.url, this._option.crossOrigin).then(function (img) {
117
- _this._loaded = true;
118
- // 创建纹理
119
- _this._texture = gl.createTexture();
120
- gl.bindTexture(gl.TEXTURE_2D, _this._texture);
121
- var textureFilter = _this._option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
122
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, textureFilter);
123
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, textureFilter);
124
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
125
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
126
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
127
- map.triggerRepaint();
128
- });
129
- };
130
- return ImageLayer;
131
- }());
132
- export default ImageLayer;
@@ -1,39 +0,0 @@
1
- // @ts-ignore
2
- import Arrugator from 'arrugator';
3
- import proj4 from 'proj4';
4
- export function initArrugator(fromProj, coordinates) {
5
- // 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
6
- var origin = [-20037508.342789244, 20037508.342789244];
7
- // 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
8
- var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
9
- // 转换为 EPSG:3857
10
- var projector = proj4(fromProj, 'EPSG:3857').forward;
11
- // 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
12
- function forward(coors) {
13
- // 墨卡托坐标
14
- var coor_3857 = projector(coors);
15
- // 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
16
- var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
17
- var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
18
- return [mapbox_coor1, mapbox_coor2];
19
- }
20
- var epsilon = 0.00000000001;
21
- // 纹理uv坐标
22
- var sourceUV = [
23
- [0, 0],
24
- [0, 1],
25
- [1, 0],
26
- [1, 1], // bottom-right
27
- ];
28
- var arrugator = new Arrugator(forward, verts, sourceUV, [
29
- [0, 1, 3],
30
- [0, 3, 2],
31
- ]);
32
- arrugator.lowerEpsilon(epsilon);
33
- var arrugado = arrugator.output();
34
- return {
35
- pos: arrugado.projected.flat(),
36
- uv: arrugado.uv.flat(),
37
- trigs: arrugado.trigs.flat(), // 三角形索引
38
- };
39
- }
@@ -1,19 +0,0 @@
1
- /**
2
- * load image
3
- * @param src
4
- * @param crossOrigin
5
- * @returns
6
- */
7
- export function loadImage(src, crossOrigin) {
8
- return new Promise(function (res, rej) {
9
- var img = new Image();
10
- img.crossOrigin = crossOrigin !== null && crossOrigin !== void 0 ? crossOrigin : '';
11
- img.src = src;
12
- img.onload = function () {
13
- res(img);
14
- };
15
- img.onerror = function () {
16
- rej('error');
17
- };
18
- });
19
- }
@@ -1,64 +0,0 @@
1
- /**
2
- * Create a shader object
3
- * @param gl GL context
4
- * @param type the type of the shader object to be created
5
- * @param source shader program (string)
6
- * @return created shader object, or null if the creation has failed.
7
- */
8
- function createShader(gl, type, source) {
9
- // Create shader object
10
- var shader = gl.createShader(type);
11
- if (shader == null) {
12
- console.log('unable to create shader');
13
- return null;
14
- }
15
- // Set the shader program
16
- gl.shaderSource(shader, source);
17
- // Compile the shader
18
- gl.compileShader(shader);
19
- // Check the result of compilation
20
- var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
21
- if (!compiled) {
22
- var error = gl.getShaderInfoLog(shader);
23
- console.log('Failed to compile shader: ' + error);
24
- gl.deleteShader(shader);
25
- return null;
26
- }
27
- return shader;
28
- }
29
- /**
30
- * Create the linked program object
31
- * @param gl GL context
32
- * @param vshader a vertex shader program (string)
33
- * @param fshader a fragment shader program (string)
34
- * @return created program object, or null if the creation has failed
35
- */
36
- export function createProgram(gl, vshader, fshader) {
37
- // Create shader object
38
- var vertexShader = createShader(gl, gl.VERTEX_SHADER, vshader);
39
- var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fshader);
40
- if (!vertexShader || !fragmentShader) {
41
- return null;
42
- }
43
- // Create a program object
44
- var program = gl.createProgram();
45
- if (!program) {
46
- return null;
47
- }
48
- // Attach the shader objects
49
- gl.attachShader(program, vertexShader);
50
- gl.attachShader(program, fragmentShader);
51
- // Link the program object
52
- gl.linkProgram(program);
53
- // Check the result of linking
54
- var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
55
- if (!linked) {
56
- var error = gl.getProgramInfoLog(program);
57
- console.log('Failed to link program: ' + error);
58
- gl.deleteProgram(program);
59
- gl.deleteShader(fragmentShader);
60
- gl.deleteShader(vertexShader);
61
- return null;
62
- }
63
- return program;
64
- }
@@ -1,45 +0,0 @@
1
- /// <reference types="mapbox-gl" />
2
- import type { Coordinates } from './utils/arrugator';
3
- export declare type ImageOption = {
4
- url: string;
5
- projection: string;
6
- coordinates: Coordinates;
7
- resampling?: 'linear' | 'nearest';
8
- opacity?: number;
9
- crossOrigin?: string;
10
- };
11
- export default class ImageLayer implements mapboxgl.CustomLayerInterface {
12
- id: string;
13
- type: 'custom';
14
- renderingMode?: '2d' | '3d' | undefined;
15
- private _option;
16
- private _loaded;
17
- private _arrugado;
18
- private _map;
19
- private _gl;
20
- private _program;
21
- private _texture;
22
- private _positionBuffer;
23
- private _uvBuffer;
24
- private _verticesIndexBuffer;
25
- constructor(id: string, option: ImageOption);
26
- onAdd(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
27
- onRemove(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
28
- render(gl: WebGLRenderingContext, matrix: number[]): void;
29
- /**
30
- * Updates the image URL and, optionally, the projection, the coordinates and the resampling.
31
- * @param {Object} option Options object.
32
- * @param {string} [option.url] Required image URL.
33
- * @param {string} [option.projection] Projection with EPSG code that points to the image..
34
- * @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
35
- * @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
36
- */
37
- updateImage(option: {
38
- url: string;
39
- projection?: string;
40
- coordinates?: Coordinates;
41
- resampling?: 'linear' | 'nearest';
42
- }): this;
43
- private _bindData;
44
- private _loadImage;
45
- }
package/dist/js/index.js DELETED
@@ -1,134 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var image_1 = require("./utils/image");
4
- var webgl_1 = require("./utils/webgl");
5
- var arrugator_1 = require("./utils/arrugator");
6
- var ImageLayer = /** @class */ (function () {
7
- function ImageLayer(id, option) {
8
- this.id = id;
9
- this.type = 'custom';
10
- this.renderingMode = '2d';
11
- this._option = option;
12
- this._loaded = false;
13
- // 初始化 Arrugator
14
- var projection = option.projection, coordinates = option.coordinates;
15
- this._arrugado = (0, arrugator_1.initArrugator)(projection, coordinates);
16
- this._map = null;
17
- this._gl = null;
18
- this._program = null;
19
- this._texture = null;
20
- this._positionBuffer = null;
21
- this._uvBuffer = null;
22
- this._verticesIndexBuffer = null;
23
- }
24
- ImageLayer.prototype.onAdd = function (map, gl) {
25
- this._map = map;
26
- this._gl = gl;
27
- this._loadImage(map, gl);
28
- var vertexSource = "\n uniform mat4 u_matrix;\n attribute vec2 a_pos;\n attribute vec2 a_uv;\n varying vec2 v_uv;\n void main() {\n gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n v_uv = a_uv;\n }";
29
- var fragmentSource = "\n #ifdef GL_ES\n precision highp int;\n precision mediump float;\n #endif\n uniform sampler2D u_sampler;\n uniform float u_opacity;\n varying vec2 v_uv;\n void main() {\n vec4 color = texture2D(u_sampler, v_uv);\n gl_FragColor = color * u_opacity;\n }";
30
- this._program = (0, webgl_1.createProgram)(gl, vertexSource, fragmentSource);
31
- if (this._program) {
32
- this._positionBuffer = gl.createBuffer();
33
- // gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer)
34
- // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.pos), gl.STATIC_DRAW)
35
- var a_pos = gl.getAttribLocation(this._program, 'a_pos');
36
- gl.vertexAttribPointer(a_pos, 2, gl.FLOAT, false, 0, 0);
37
- gl.enableVertexAttribArray(a_pos);
38
- this._uvBuffer = gl.createBuffer();
39
- // gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer)
40
- // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.uv), gl.STATIC_DRAW)
41
- var a_uv = gl.getAttribLocation(this._program, 'a_uv');
42
- gl.vertexAttribPointer(a_uv, 2, gl.FLOAT, false, 0, 0);
43
- gl.enableVertexAttribArray(a_uv);
44
- this._verticesIndexBuffer = gl.createBuffer();
45
- // gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer)
46
- // gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this._arrugado.trigs), gl.STATIC_DRAW)
47
- this._bindData(gl, this._arrugado);
48
- }
49
- };
50
- ImageLayer.prototype.onRemove = function (map, gl) {
51
- gl.deleteProgram(this._program);
52
- gl.deleteTexture(this._texture);
53
- gl.deleteBuffer(this._verticesIndexBuffer);
54
- };
55
- ImageLayer.prototype.render = function (gl, matrix) {
56
- if (this._loaded && this._program) {
57
- gl.useProgram(this._program);
58
- // matrix
59
- gl.uniformMatrix4fv(gl.getUniformLocation(this._program, 'u_matrix'), false, matrix);
60
- // pos
61
- gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
62
- gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_pos'), 2, gl.FLOAT, false, 0, 0);
63
- // uv
64
- gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
65
- gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_uv'), 2, gl.FLOAT, false, 0, 0);
66
- // index
67
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
68
- // texture
69
- gl.activeTexture(gl.TEXTURE0);
70
- gl.bindTexture(gl.TEXTURE_2D, this._texture);
71
- gl.uniform1i(gl.getUniformLocation(this._program, 'u_sampler'), 0);
72
- // opacity
73
- gl.uniform1f(gl.getUniformLocation(this._program, 'u_opacity'), this._option.opacity || 1);
74
- // blend
75
- gl.enable(gl.BLEND);
76
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
77
- gl.drawElements(gl.TRIANGLES, this._arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
78
- gl.bindBuffer(gl.ARRAY_BUFFER, null);
79
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
80
- }
81
- };
82
- /**
83
- * Updates the image URL and, optionally, the projection, the coordinates and the resampling.
84
- * @param {Object} option Options object.
85
- * @param {string} [option.url] Required image URL.
86
- * @param {string} [option.projection] Projection with EPSG code that points to the image..
87
- * @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
88
- * @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
89
- */
90
- ImageLayer.prototype.updateImage = function (option) {
91
- var _a, _b, _c;
92
- this._loaded = false;
93
- this._option.url = option.url;
94
- if (this._gl && this._map) {
95
- if (option.projection || option.coordinates) {
96
- this._option.projection = (_a = option.projection) !== null && _a !== void 0 ? _a : this._option.projection;
97
- this._option.coordinates = (_b = option.coordinates) !== null && _b !== void 0 ? _b : this._option.coordinates;
98
- // reinit arrugator
99
- this._arrugado = (0, arrugator_1.initArrugator)(this._option.projection, this._option.coordinates);
100
- this._bindData(this._gl, this._arrugado);
101
- }
102
- this._option.resampling = (_c = option.resampling) !== null && _c !== void 0 ? _c : this._option.resampling;
103
- // reload image
104
- this._loadImage(this._map, this._gl);
105
- }
106
- return this;
107
- };
108
- ImageLayer.prototype._bindData = function (gl, arrugado) {
109
- gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
110
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arrugado.pos), gl.STATIC_DRAW);
111
- gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
112
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arrugado.uv), gl.STATIC_DRAW);
113
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
114
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(arrugado.trigs), gl.STATIC_DRAW);
115
- };
116
- ImageLayer.prototype._loadImage = function (map, gl) {
117
- var _this = this;
118
- (0, image_1.loadImage)(this._option.url, this._option.crossOrigin).then(function (img) {
119
- _this._loaded = true;
120
- // 创建纹理
121
- _this._texture = gl.createTexture();
122
- gl.bindTexture(gl.TEXTURE_2D, _this._texture);
123
- var textureFilter = _this._option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
124
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, textureFilter);
125
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, textureFilter);
126
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
127
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
128
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
129
- map.triggerRepaint();
130
- });
131
- };
132
- return ImageLayer;
133
- }());
134
- exports.default = ImageLayer;
@@ -1,7 +0,0 @@
1
- export declare type Coordinates = [[number, number], [number, number], [number, number], [number, number]];
2
- export declare type ArrugadoFlat = {
3
- pos: number[];
4
- uv: number[];
5
- trigs: number[];
6
- };
7
- export declare function initArrugator(fromProj: string, coordinates: Coordinates): ArrugadoFlat;
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.initArrugator = void 0;
7
- // @ts-ignore
8
- var arrugator_1 = __importDefault(require("arrugator"));
9
- var proj4_1 = __importDefault(require("proj4"));
10
- function initArrugator(fromProj, coordinates) {
11
- // 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
12
- var origin = [-20037508.342789244, 20037508.342789244];
13
- // 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
14
- var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
15
- // 转换为 EPSG:3857
16
- var projector = (0, proj4_1.default)(fromProj, 'EPSG:3857').forward;
17
- // 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
18
- function forward(coors) {
19
- // 墨卡托坐标
20
- var coor_3857 = projector(coors);
21
- // 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
22
- var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
23
- var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
24
- return [mapbox_coor1, mapbox_coor2];
25
- }
26
- var epsilon = 0.00000000001;
27
- // 纹理uv坐标
28
- var sourceUV = [
29
- [0, 0],
30
- [0, 1],
31
- [1, 0],
32
- [1, 1], // bottom-right
33
- ];
34
- var arrugator = new arrugator_1.default(forward, verts, sourceUV, [
35
- [0, 1, 3],
36
- [0, 3, 2],
37
- ]);
38
- arrugator.lowerEpsilon(epsilon);
39
- var arrugado = arrugator.output();
40
- return {
41
- pos: arrugado.projected.flat(),
42
- uv: arrugado.uv.flat(),
43
- trigs: arrugado.trigs.flat(), // 三角形索引
44
- };
45
- }
46
- exports.initArrugator = initArrugator;
@@ -1,7 +0,0 @@
1
- /**
2
- * load image
3
- * @param src
4
- * @param crossOrigin
5
- * @returns
6
- */
7
- export declare function loadImage(src: string, crossOrigin?: string): Promise<HTMLImageElement>;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadImage = void 0;
4
- /**
5
- * load image
6
- * @param src
7
- * @param crossOrigin
8
- * @returns
9
- */
10
- function loadImage(src, crossOrigin) {
11
- return new Promise(function (res, rej) {
12
- var img = new Image();
13
- img.crossOrigin = crossOrigin !== null && crossOrigin !== void 0 ? crossOrigin : '';
14
- img.src = src;
15
- img.onload = function () {
16
- res(img);
17
- };
18
- img.onerror = function () {
19
- rej('error');
20
- };
21
- });
22
- }
23
- exports.loadImage = loadImage;
@@ -1,8 +0,0 @@
1
- /**
2
- * Create the linked program object
3
- * @param gl GL context
4
- * @param vshader a vertex shader program (string)
5
- * @param fshader a fragment shader program (string)
6
- * @return created program object, or null if the creation has failed
7
- */
8
- export declare function createProgram(gl: WebGLRenderingContext, vshader: string, fshader: string): WebGLProgram | null;
@@ -1,68 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createProgram = void 0;
4
- /**
5
- * Create a shader object
6
- * @param gl GL context
7
- * @param type the type of the shader object to be created
8
- * @param source shader program (string)
9
- * @return created shader object, or null if the creation has failed.
10
- */
11
- function createShader(gl, type, source) {
12
- // Create shader object
13
- var shader = gl.createShader(type);
14
- if (shader == null) {
15
- console.log('unable to create shader');
16
- return null;
17
- }
18
- // Set the shader program
19
- gl.shaderSource(shader, source);
20
- // Compile the shader
21
- gl.compileShader(shader);
22
- // Check the result of compilation
23
- var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
24
- if (!compiled) {
25
- var error = gl.getShaderInfoLog(shader);
26
- console.log('Failed to compile shader: ' + error);
27
- gl.deleteShader(shader);
28
- return null;
29
- }
30
- return shader;
31
- }
32
- /**
33
- * Create the linked program object
34
- * @param gl GL context
35
- * @param vshader a vertex shader program (string)
36
- * @param fshader a fragment shader program (string)
37
- * @return created program object, or null if the creation has failed
38
- */
39
- function createProgram(gl, vshader, fshader) {
40
- // Create shader object
41
- var vertexShader = createShader(gl, gl.VERTEX_SHADER, vshader);
42
- var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fshader);
43
- if (!vertexShader || !fragmentShader) {
44
- return null;
45
- }
46
- // Create a program object
47
- var program = gl.createProgram();
48
- if (!program) {
49
- return null;
50
- }
51
- // Attach the shader objects
52
- gl.attachShader(program, vertexShader);
53
- gl.attachShader(program, fragmentShader);
54
- // Link the program object
55
- gl.linkProgram(program);
56
- // Check the result of linking
57
- var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
58
- if (!linked) {
59
- var error = gl.getProgramInfoLog(program);
60
- console.log('Failed to link program: ' + error);
61
- gl.deleteProgram(program);
62
- gl.deleteShader(fragmentShader);
63
- gl.deleteShader(vertexShader);
64
- return null;
65
- }
66
- return program;
67
- }
68
- exports.createProgram = createProgram;