Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ImageBitmap in Map#addImage and Map#updateImage #9139

Merged
merged 2 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {version} from '../../package.json';
import {extend, bindAll, warnOnce, uniqueId} from '../util/util';
import browser from '../util/browser';
import window from '../util/window';
const {HTMLImageElement, HTMLElement} = window;
const {HTMLImageElement, HTMLElement, ImageBitmap} = window;
import DOM from '../util/dom';
import {getImage, getJSON, ResourceType} from '../util/ajax';
import {RequestManager} from '../util/mapbox';
Expand Down Expand Up @@ -1411,7 +1411,7 @@ class Map extends Camera {
* A {@link Map#error} event will be fired if there is not enough space in the sprite to add this image.
*
* @param id The ID of the image.
* @param image The image as an `HTMLImageElement`, `ImageData`, or object with `width`, `height`, and `data`
* @param image The image as an `HTMLImageElement`, `ImageData`, `ImageBitmap` or object with `width`, `height`, and `data`
* properties with the same format as `ImageData`.
* @param options
* @param options.pixelRatio The ratio of pixels in the image to physical pixels on the screen
Expand Down Expand Up @@ -1447,17 +1447,17 @@ class Map extends Camera {
* @see Use `ImageData`: [Add a generated icon to the map](https://www.mapbox.com/mapbox-gl-js/example/add-image-generated/)
*/
addImage(id: string,
image: HTMLImageElement | ImageData | {width: number, height: number, data: Uint8Array | Uint8ClampedArray} | StyleImageInterface,
image: HTMLImageElement | ImageBitmap | ImageData | {width: number, height: number, data: Uint8Array | Uint8ClampedArray} | StyleImageInterface,
{pixelRatio = 1, sdf = false, stretchX, stretchY, content}: $Shape<StyleImageMetadata> = {}) {
this._lazyInitEmptyStyle();
const version = 0;

if (image instanceof HTMLImageElement) {
if (image instanceof HTMLImageElement || (ImageBitmap && image instanceof ImageBitmap)) {
const {width, height, data} = browser.getImageData(image);
this.style.addImage(id, {data: new RGBAImage({width, height}, data), pixelRatio, stretchX, stretchY, content, sdf, version});
} else if (image.width === undefined || image.height === undefined) {
return this.fire(new ErrorEvent(new Error(
'Invalid arguments to map.addImage(). The second argument must be an `HTMLImageElement`, `ImageData`, ' +
'Invalid arguments to map.addImage(). The second argument must be an `HTMLImageElement`, `ImageData`, `ImageBitmap`, ' +
'or object with `width`, `height`, and `data` properties with the same format as `ImageData`')));
} else {
const {width, height, data} = image;
Expand Down Expand Up @@ -1489,7 +1489,7 @@ class Map extends Camera {
* or [`line-pattern`](https://docs.mapbox.com/mapbox-gl-js/style-spec/#paint-line-line-pattern).
*
* @param id The ID of the image.
* @param image The image as an `HTMLImageElement`, `ImageData`, or object with `width`, `height`, and `data`
* @param image The image as an `HTMLImageElement`, `ImageData`, `ImageBitmap` or object with `width`, `height`, and `data`
* properties with the same format as `ImageData`.
*
* @example
Expand All @@ -1498,20 +1498,19 @@ class Map extends Camera {
* if (map.hasImage('cat')) map.updateImage('cat', './other-cat-icon.png');
*/
updateImage(id: string,
image: HTMLImageElement | ImageData | {width: number, height: number, data: Uint8Array | Uint8ClampedArray} | StyleImageInterface) {
image: HTMLImageElement | ImageBitmap | ImageData | {width: number, height: number, data: Uint8Array | Uint8ClampedArray} | StyleImageInterface) {

const existingImage = this.style.getImage(id);
if (!existingImage) {
return this.fire(new ErrorEvent(new Error(
'The map has no image with that id. If you are adding a new image use `map.addImage(...)` instead.')));
}

const imageData = image instanceof HTMLImageElement ? browser.getImageData(image) : image;
const imageData = (image instanceof HTMLImageElement || (ImageBitmap && image instanceof ImageBitmap)) ? browser.getImageData(image) : image;
const {width, height, data} = imageData;

if (width === undefined || height === undefined) {
return this.fire(new ErrorEvent(new Error(
'Invalid arguments to map.updateImage(). The second argument must be an `HTMLImageElement`, `ImageData`, ' +
'Invalid arguments to map.updateImage(). The second argument must be an `HTMLImageElement`, `ImageData`, `ImageBitmap`, ' +
'or object with `width`, `height`, and `data` properties with the same format as `ImageData`')));
}

Expand All @@ -1520,7 +1519,7 @@ class Map extends Camera {
'The width and height of the updated image must be that same as the previous version of the image')));
}

const copy = !(image instanceof HTMLImageElement);
const copy = !(image instanceof HTMLImageElement || (ImageBitmap && image instanceof ImageBitmap));
existingImage.data.replace(data, copy);

this.style.updateImage(id, existingImage);
Expand Down
4 changes: 2 additions & 2 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ function arrayBufferToImageBitmap(data: ArrayBuffer, callback: (err: ?Error, ima
const blob: Blob = new window.Blob([new Uint8Array(data)], {type: 'image/png'});
window.createImageBitmap(blob).then((imgBitmap) => {
callback(null, imgBitmap);
}).catch(() => {
callback(new Error('Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.'));
}).catch((e) => {
callback(new Error(`Could not load image because of ${e.message}. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.`));
});
}

Expand Down