Skip to content

Commit

Permalink
feature: add crossOrigin option to ImageLayer for the image.
Browse files Browse the repository at this point in the history
  • Loading branch information
huanglii committed May 19, 2022
1 parent baf494f commit 0cdc50d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.3.1

### ✨ Features

- Add `crossOrigin` option to `ImageLayer` for the image.

### 🐞 Bug fixes

- Fix `ImageOption`'s type of the `ImageLayer` is incorrect.

## 0.3.0

### ✨ Features
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.3.1

### ✨ Features

- `ImageLayer` 新增 `crossOrigin` 选项

### 🐞 Bug fixes

- 修复 `ImageLayer``ImageOption` 错误

## 0.3.0

### ✨ Features
Expand Down
5 changes: 3 additions & 2 deletions dist/mapbox-gl-layers.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ class Arrugator {
this._segment(v3, v2, t2);
}
}
function loadImage(src) {
function loadImage(src, crossOrigin) {
return new Promise((res, rej) => {
const img = new Image();
img.crossOrigin = crossOrigin != null ? crossOrigin : "";
img.src = src;
img.onload = function() {
res(img);
Expand Down Expand Up @@ -391,7 +392,7 @@ class ImageLayer {
return arrugator.output();
}
_loadImage(map, gl) {
loadImage(this._option.url).then((img) => {
loadImage(this._option.url, this._option.crossOrigin).then((img) => {
this._loaded = true;
this._texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this._texture);
Expand Down
4 changes: 2 additions & 2 deletions dist/mapbox-gl-layers.umd.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion doc/imagelayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface
| --- | --- |
| **option.url** <br />`(string)` | URL that points to an image. |
| **option.projection** <br />`(string)` | Projection with EPSG code that points to the image. |
| **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) |
| **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) |
| **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) |
| **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. |
```ts
export type ImageOption = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@naivemap/mapbox-gl-layers",
"version": "0.3.0",
"version": "0.3.1",
"description": "mapbox-gl-layers",
"files": [
"dist",
Expand Down
5 changes: 3 additions & 2 deletions src/ImageLayer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export type ImageOption = {
url: string
projection: string
coordinates: Coordinates
resampling: 'linear' | 'nearest'
resampling?: 'linear' | 'nearest'
crossOrigin?: string
}

export default class ImageLayer implements mapboxgl.CustomLayerInterface {
Expand Down Expand Up @@ -192,7 +193,7 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface {
}

private _loadImage(map: mapboxgl.Map, gl: WebGLRenderingContext) {
loadImage(this._option.url).then((img) => {
loadImage(this._option.url, this._option.crossOrigin).then((img) => {
this._loaded = true

// 创建纹理
Expand Down
5 changes: 4 additions & 1 deletion src/util/image.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
* load image
* @param src
* @param crossOrigin
* @returns
*/
export function loadImage(src: string): Promise<HTMLImageElement> {
export function loadImage(src: string, crossOrigin?: string): Promise<HTMLImageElement> {
return new Promise((res, rej) => {
const img = new Image()

img.crossOrigin = crossOrigin ?? ''
img.src = src
img.onload = function () {
res(img)
Expand Down
4 changes: 3 additions & 1 deletion types/ImageLayer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export declare type ImageOption = {
url: string
projection: string
coordinates: Coordinates
resampling: 'linear' | 'nearest'
resampling?: 'linear' | 'nearest'
crossOrigin?: string
}
export default class ImageLayer implements mapboxgl.CustomLayerInterface {
id: string
Expand All @@ -20,5 +21,6 @@ export default class ImageLayer implements mapboxgl.CustomLayerInterface {
onAdd(map: mapboxgl.Map, gl: WebGLRenderingContext): void
onRemove(map: mapboxgl.Map, gl: WebGLRenderingContext): void
render(gl: WebGLRenderingContext, matrix: number[]): void
update(url: string): void
private _initArrugator
}

0 comments on commit 0cdc50d

Please sign in to comment.