Skip to content

Commit 0cdc50d

Browse files
author
huanglii
committed
feature: add crossOrigin option to ImageLayer for the image.
1 parent baf494f commit 0cdc50d

File tree

9 files changed

+38
-10
lines changed

9 files changed

+38
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 0.3.1
2+
3+
### ✨ Features
4+
5+
- Add `crossOrigin` option to `ImageLayer` for the image.
6+
7+
### 🐞 Bug fixes
8+
9+
- Fix `ImageOption`'s type of the `ImageLayer` is incorrect.
10+
111
## 0.3.0
212

313
### ✨ Features

CHANGELOG.zh-CN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 0.3.1
2+
3+
### ✨ Features
4+
5+
- `ImageLayer` 新增 `crossOrigin` 选项
6+
7+
### 🐞 Bug fixes
8+
9+
- 修复 `ImageLayer``ImageOption` 错误
10+
111
## 0.3.0
212

313
### ✨ Features

dist/mapbox-gl-layers.es.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ class Arrugator {
220220
this._segment(v3, v2, t2);
221221
}
222222
}
223-
function loadImage(src) {
223+
function loadImage(src, crossOrigin) {
224224
return new Promise((res, rej) => {
225225
const img = new Image();
226+
img.crossOrigin = crossOrigin != null ? crossOrigin : "";
226227
img.src = src;
227228
img.onload = function() {
228229
res(img);
@@ -391,7 +392,7 @@ class ImageLayer {
391392
return arrugator.output();
392393
}
393394
_loadImage(map, gl) {
394-
loadImage(this._option.url).then((img) => {
395+
loadImage(this._option.url, this._option.crossOrigin).then((img) => {
395396
this._loaded = true;
396397
this._texture = gl.createTexture();
397398
gl.bindTexture(gl.TEXTURE_2D, this._texture);

dist/mapbox-gl-layers.umd.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/imagelayer.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface
2626
| --- | --- |
2727
| **option.url** <br />`(string)` | URL that points to an image. |
2828
| **option.projection** <br />`(string)` | Projection with EPSG code that points to the image. |
29-
| **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) |
3029
| **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) |
30+
| **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) |
31+
| **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. |
3132
3233
```ts
3334
export type ImageOption = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@naivemap/mapbox-gl-layers",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "mapbox-gl-layers",
55
"files": [
66
"dist",

src/ImageLayer/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export type ImageOption = {
1818
url: string
1919
projection: string
2020
coordinates: Coordinates
21-
resampling: 'linear' | 'nearest'
21+
resampling?: 'linear' | 'nearest'
22+
crossOrigin?: string
2223
}
2324

2425
export default class ImageLayer implements mapboxgl.CustomLayerInterface {
@@ -192,7 +193,7 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface {
192193
}
193194

194195
private _loadImage(map: mapboxgl.Map, gl: WebGLRenderingContext) {
195-
loadImage(this._option.url).then((img) => {
196+
loadImage(this._option.url, this._option.crossOrigin).then((img) => {
196197
this._loaded = true
197198

198199
// 创建纹理

src/util/image.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
22
* load image
33
* @param src
4+
* @param crossOrigin
45
* @returns
56
*/
6-
export function loadImage(src: string): Promise<HTMLImageElement> {
7+
export function loadImage(src: string, crossOrigin?: string): Promise<HTMLImageElement> {
78
return new Promise((res, rej) => {
89
const img = new Image()
10+
11+
img.crossOrigin = crossOrigin ?? ''
912
img.src = src
1013
img.onload = function () {
1114
res(img)

types/ImageLayer.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export declare type ImageOption = {
44
url: string
55
projection: string
66
coordinates: Coordinates
7-
resampling: 'linear' | 'nearest'
7+
resampling?: 'linear' | 'nearest'
8+
crossOrigin?: string
89
}
910
export default class ImageLayer implements mapboxgl.CustomLayerInterface {
1011
id: string
@@ -20,5 +21,6 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface {
2021
onAdd(map: mapboxgl.Map, gl: WebGLRenderingContext): void
2122
onRemove(map: mapboxgl.Map, gl: WebGLRenderingContext): void
2223
render(gl: WebGLRenderingContext, matrix: number[]): void
24+
update(url: string): void
2325
private _initArrugator
2426
}

0 commit comments

Comments
 (0)