@naivemap/mapbox-gl-image-layer 0.3.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/LICENSE ADDED
@@ -0,0 +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.
package/README.md ADDED
@@ -0,0 +1,66 @@
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
+ | **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. |
34
+
35
+ ```ts
36
+ export type ImageOption = {
37
+ url: string
38
+ projection: string
39
+ coordinates: Coordinates
40
+ resampling: 'linear' | 'nearest'
41
+ }
42
+ ```
43
+
44
+ ### Methods
45
+
46
+ | Method | Description |
47
+ | -------------------------- | --------------------- |
48
+ | **update** `(url: string)` | Update the image URL. |
49
+
50
+ ## Example
51
+
52
+ ```ts
53
+ const layer = new ImageLayer('layer-id', {
54
+ url: '/4326.png',
55
+ projection: 'EPSG:4326',
56
+ resampling: 'nearest',
57
+ coordinates: [
58
+ [105.289838, 32.204171], // top-left
59
+ [110.195632, 32.204171], // top-right
60
+ [110.195632, 28.164713], // bottom-right
61
+ [105.289838, 28.164713], // bottom-left
62
+ ],
63
+ })
64
+
65
+ map.addLayer(layer)
66
+ ```
@@ -0,0 +1,137 @@
1
+ // @ts-ignore
2
+ import Arrugator from 'arrugator';
3
+ import proj4 from 'proj4';
4
+ import { loadImage } from './utils/image';
5
+ import { createProgram } from './utils/webgl';
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
+ var arrugado = this._initArrugator(projection, coordinates);
16
+ this._arrugado = {
17
+ pos: arrugado.projected.flat(),
18
+ uv: arrugado.uv.flat(),
19
+ trigs: arrugado.trigs.flat(), // 三角形索引
20
+ };
21
+ this._map = null;
22
+ this._gl = null;
23
+ this._program = null;
24
+ this._texture = null;
25
+ this._positionBuffer = null;
26
+ this._uvBuffer = null;
27
+ this._verticesIndexBuffer = null;
28
+ }
29
+ ImageLayer.prototype.onAdd = function (map, gl) {
30
+ this._map = map;
31
+ this._gl = gl;
32
+ this._loadImage(map, gl);
33
+ 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 }";
34
+ var fragmentSource = "\n #ifdef GL_ES\n precision highp int;\n precision mediump float;\n #endif\n uniform sampler2D u_sampler;\n varying vec2 v_uv;\n void main() {\n gl_FragColor = texture2D(u_sampler, v_uv);\n }";
35
+ this._program = createProgram(gl, vertexSource, fragmentSource);
36
+ if (this._program) {
37
+ this._positionBuffer = gl.createBuffer();
38
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
39
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.pos), gl.STATIC_DRAW);
40
+ var a_pos = gl.getAttribLocation(this._program, 'a_pos');
41
+ gl.vertexAttribPointer(a_pos, 2, gl.FLOAT, false, 0, 0);
42
+ gl.enableVertexAttribArray(a_pos);
43
+ this._uvBuffer = gl.createBuffer();
44
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
45
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.uv), gl.STATIC_DRAW);
46
+ var a_uv = gl.getAttribLocation(this._program, 'a_uv');
47
+ gl.vertexAttribPointer(a_uv, 2, gl.FLOAT, false, 0, 0);
48
+ gl.enableVertexAttribArray(a_uv);
49
+ this._verticesIndexBuffer = gl.createBuffer();
50
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
51
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this._arrugado.trigs), gl.STATIC_DRAW);
52
+ }
53
+ };
54
+ ImageLayer.prototype.onRemove = function (map, gl) {
55
+ gl.deleteProgram(this._program);
56
+ gl.deleteTexture(this._texture);
57
+ gl.deleteBuffer(this._verticesIndexBuffer);
58
+ };
59
+ ImageLayer.prototype.render = function (gl, matrix) {
60
+ if (this._loaded && this._program) {
61
+ gl.useProgram(this._program);
62
+ // matrix
63
+ gl.uniformMatrix4fv(gl.getUniformLocation(this._program, 'u_matrix'), false, matrix);
64
+ // pos
65
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
66
+ gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_pos'), 2, gl.FLOAT, false, 0, 0);
67
+ // uv
68
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
69
+ gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_uv'), 2, gl.FLOAT, false, 0, 0);
70
+ // index
71
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
72
+ // texture
73
+ gl.activeTexture(gl.TEXTURE0);
74
+ gl.bindTexture(gl.TEXTURE_2D, this._texture);
75
+ gl.uniform1i(gl.getUniformLocation(this._program, 'u_sampler'), 0);
76
+ gl.drawElements(gl.TRIANGLES, this._arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
77
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
78
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
79
+ }
80
+ };
81
+ ImageLayer.prototype.update = function (url) {
82
+ this._loaded = false;
83
+ this._option.url = url;
84
+ if (this._gl && this._map) {
85
+ this._loadImage(this._map, this._gl);
86
+ }
87
+ };
88
+ ImageLayer.prototype._initArrugator = function (fromProj, coordinates) {
89
+ // 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
90
+ var origin = [-20037508.342789244, 20037508.342789244];
91
+ // 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
92
+ var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
93
+ // 转换为 EPSG:3857
94
+ var projector = proj4(fromProj, 'EPSG:3857').forward;
95
+ // 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
96
+ function forward(coors) {
97
+ // 墨卡托坐标
98
+ var coor_3857 = projector(coors);
99
+ // 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
100
+ var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
101
+ var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
102
+ return [mapbox_coor1, mapbox_coor2];
103
+ }
104
+ var epsilon = 0.00000000001;
105
+ // 纹理uv坐标
106
+ var sourceUV = [
107
+ [0, 0],
108
+ [0, 1],
109
+ [1, 0],
110
+ [1, 1], // bottom-right
111
+ ];
112
+ var arrugator = new Arrugator(forward, verts, sourceUV, [
113
+ [0, 1, 3],
114
+ [0, 3, 2],
115
+ ]);
116
+ arrugator.lowerEpsilon(epsilon);
117
+ return arrugator.output();
118
+ };
119
+ ImageLayer.prototype._loadImage = function (map, gl) {
120
+ var _this = this;
121
+ loadImage(this._option.url, this._option.crossOrigin).then(function (img) {
122
+ _this._loaded = true;
123
+ // 创建纹理
124
+ _this._texture = gl.createTexture();
125
+ gl.bindTexture(gl.TEXTURE_2D, _this._texture);
126
+ var textureFilter = _this._option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
127
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, textureFilter);
128
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, textureFilter);
129
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
130
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
131
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
132
+ map.triggerRepaint();
133
+ });
134
+ };
135
+ return ImageLayer;
136
+ }());
137
+ export default ImageLayer;
@@ -0,0 +1,19 @@
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
+ }
@@ -0,0 +1,64 @@
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
+ }
@@ -0,0 +1,32 @@
1
+ /// <reference types="mapbox-gl" />
2
+ declare type Coordinates = [[number, number], [number, number], [number, number], [number, number]];
3
+ export declare type ImageOption = {
4
+ url: string;
5
+ projection: string;
6
+ coordinates: Coordinates;
7
+ resampling?: 'linear' | 'nearest';
8
+ crossOrigin?: string;
9
+ };
10
+ export default class ImageLayer implements mapboxgl.CustomLayerInterface {
11
+ id: string;
12
+ type: 'custom';
13
+ renderingMode?: '2d' | '3d' | undefined;
14
+ private _option;
15
+ private _loaded;
16
+ private _arrugado;
17
+ private _map;
18
+ private _gl;
19
+ private _program;
20
+ private _texture;
21
+ private _positionBuffer;
22
+ private _uvBuffer;
23
+ private _verticesIndexBuffer;
24
+ constructor(id: string, option: ImageOption);
25
+ onAdd(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
26
+ onRemove(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
27
+ render(gl: WebGLRenderingContext, matrix: number[]): void;
28
+ update(url: string): void;
29
+ private _initArrugator;
30
+ private _loadImage;
31
+ }
32
+ export {};
@@ -0,0 +1,142 @@
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
+ // @ts-ignore
7
+ var arrugator_1 = __importDefault(require("arrugator"));
8
+ var proj4_1 = __importDefault(require("proj4"));
9
+ var image_1 = require("./utils/image");
10
+ var webgl_1 = require("./utils/webgl");
11
+ var ImageLayer = /** @class */ (function () {
12
+ function ImageLayer(id, option) {
13
+ this.id = id;
14
+ this.type = 'custom';
15
+ this.renderingMode = '2d';
16
+ this._option = option;
17
+ this._loaded = false;
18
+ // 初始化 Arrugator
19
+ var projection = option.projection, coordinates = option.coordinates;
20
+ var arrugado = this._initArrugator(projection, coordinates);
21
+ this._arrugado = {
22
+ pos: arrugado.projected.flat(),
23
+ uv: arrugado.uv.flat(),
24
+ trigs: arrugado.trigs.flat(), // 三角形索引
25
+ };
26
+ this._map = null;
27
+ this._gl = null;
28
+ this._program = null;
29
+ this._texture = null;
30
+ this._positionBuffer = null;
31
+ this._uvBuffer = null;
32
+ this._verticesIndexBuffer = null;
33
+ }
34
+ ImageLayer.prototype.onAdd = function (map, gl) {
35
+ this._map = map;
36
+ this._gl = gl;
37
+ this._loadImage(map, gl);
38
+ 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 }";
39
+ var fragmentSource = "\n #ifdef GL_ES\n precision highp int;\n precision mediump float;\n #endif\n uniform sampler2D u_sampler;\n varying vec2 v_uv;\n void main() {\n gl_FragColor = texture2D(u_sampler, v_uv);\n }";
40
+ this._program = (0, webgl_1.createProgram)(gl, vertexSource, fragmentSource);
41
+ if (this._program) {
42
+ this._positionBuffer = gl.createBuffer();
43
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
44
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.pos), gl.STATIC_DRAW);
45
+ var a_pos = gl.getAttribLocation(this._program, 'a_pos');
46
+ gl.vertexAttribPointer(a_pos, 2, gl.FLOAT, false, 0, 0);
47
+ gl.enableVertexAttribArray(a_pos);
48
+ this._uvBuffer = gl.createBuffer();
49
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
50
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this._arrugado.uv), gl.STATIC_DRAW);
51
+ var a_uv = gl.getAttribLocation(this._program, 'a_uv');
52
+ gl.vertexAttribPointer(a_uv, 2, gl.FLOAT, false, 0, 0);
53
+ gl.enableVertexAttribArray(a_uv);
54
+ this._verticesIndexBuffer = gl.createBuffer();
55
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
56
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this._arrugado.trigs), gl.STATIC_DRAW);
57
+ }
58
+ };
59
+ ImageLayer.prototype.onRemove = function (map, gl) {
60
+ gl.deleteProgram(this._program);
61
+ gl.deleteTexture(this._texture);
62
+ gl.deleteBuffer(this._verticesIndexBuffer);
63
+ };
64
+ ImageLayer.prototype.render = function (gl, matrix) {
65
+ if (this._loaded && this._program) {
66
+ gl.useProgram(this._program);
67
+ // matrix
68
+ gl.uniformMatrix4fv(gl.getUniformLocation(this._program, 'u_matrix'), false, matrix);
69
+ // pos
70
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
71
+ gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_pos'), 2, gl.FLOAT, false, 0, 0);
72
+ // uv
73
+ gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer);
74
+ gl.vertexAttribPointer(gl.getAttribLocation(this._program, 'a_uv'), 2, gl.FLOAT, false, 0, 0);
75
+ // index
76
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._verticesIndexBuffer);
77
+ // texture
78
+ gl.activeTexture(gl.TEXTURE0);
79
+ gl.bindTexture(gl.TEXTURE_2D, this._texture);
80
+ gl.uniform1i(gl.getUniformLocation(this._program, 'u_sampler'), 0);
81
+ gl.drawElements(gl.TRIANGLES, this._arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
82
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
83
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
84
+ }
85
+ };
86
+ ImageLayer.prototype.update = function (url) {
87
+ this._loaded = false;
88
+ this._option.url = url;
89
+ if (this._gl && this._map) {
90
+ this._loadImage(this._map, this._gl);
91
+ }
92
+ };
93
+ ImageLayer.prototype._initArrugator = function (fromProj, coordinates) {
94
+ // 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
95
+ var origin = [-20037508.342789244, 20037508.342789244];
96
+ // 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
97
+ var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
98
+ // 转换为 EPSG:3857
99
+ var projector = (0, proj4_1.default)(fromProj, 'EPSG:3857').forward;
100
+ // 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
101
+ function forward(coors) {
102
+ // 墨卡托坐标
103
+ var coor_3857 = projector(coors);
104
+ // 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
105
+ var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
106
+ var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
107
+ return [mapbox_coor1, mapbox_coor2];
108
+ }
109
+ var epsilon = 0.00000000001;
110
+ // 纹理uv坐标
111
+ var sourceUV = [
112
+ [0, 0],
113
+ [0, 1],
114
+ [1, 0],
115
+ [1, 1], // bottom-right
116
+ ];
117
+ var arrugator = new arrugator_1.default(forward, verts, sourceUV, [
118
+ [0, 1, 3],
119
+ [0, 3, 2],
120
+ ]);
121
+ arrugator.lowerEpsilon(epsilon);
122
+ return arrugator.output();
123
+ };
124
+ ImageLayer.prototype._loadImage = function (map, gl) {
125
+ var _this = this;
126
+ (0, image_1.loadImage)(this._option.url, this._option.crossOrigin).then(function (img) {
127
+ _this._loaded = true;
128
+ // 创建纹理
129
+ _this._texture = gl.createTexture();
130
+ gl.bindTexture(gl.TEXTURE_2D, _this._texture);
131
+ var textureFilter = _this._option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
132
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, textureFilter);
133
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, textureFilter);
134
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
135
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
136
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
137
+ map.triggerRepaint();
138
+ });
139
+ };
140
+ return ImageLayer;
141
+ }());
142
+ exports.default = ImageLayer;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * load image
3
+ * @param src
4
+ * @param crossOrigin
5
+ * @returns
6
+ */
7
+ export declare function loadImage(src: string, crossOrigin?: string): Promise<HTMLImageElement>;
@@ -0,0 +1,23 @@
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;
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,68 @@
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;
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@naivemap/mapbox-gl-image-layer",
3
+ "version": "0.3.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",
10
+ "exports": {
11
+ "./package.json": "./package.json",
12
+ ".": {
13
+ "import": "./dist/es/index.js",
14
+ "require": "./dist/js/index.js"
15
+ }
16
+ },
17
+ "types": "dist/js/index.d.ts",
18
+ "sideEffects": false,
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "mapbox-gl",
24
+ "reprojection",
25
+ "proj4"
26
+ ],
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
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"
43
+ },
44
+ "peerDependencies": {
45
+ "proj4": "^2.8.0"
46
+ },
47
+ "dependencies": {
48
+ "arrugator": "^1.0.1"
49
+ },
50
+ "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"
55
+ },
56
+ "gitHead": "0d34a954e38a0366112328e2cceaafebece75e3e"
57
+ }