diff --git a/src/style/style.js b/src/style/style.js index a2b944ee198..d91e449f9db 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -58,6 +58,7 @@ class Style extends Evented { this.animationLoop = (map && map.animationLoop) || new AnimationLoop(); this.dispatcher = new Dispatcher(getWorkerPool(), this); this.spriteAtlas = new SpriteAtlas(1024, 1024); + this.spriteAtlas.setEventedParent(this); this.lineAtlas = new LineAtlas(256, 512); this._layers = {}; diff --git a/src/symbol/sprite_atlas.js b/src/symbol/sprite_atlas.js index f70a60380f0..8e0b700b649 100644 --- a/src/symbol/sprite_atlas.js +++ b/src/symbol/sprite_atlas.js @@ -4,6 +4,7 @@ const ShelfPack = require('@mapbox/shelf-pack'); const browser = require('../util/browser'); const util = require('../util/util'); const window = require('../util/window'); +const Evented = require('../util/evented'); class AtlasImage { constructor(rect, width, height, sdf, pixelRatio) { @@ -15,9 +16,11 @@ class AtlasImage { } } -class SpriteAtlas { +class SpriteAtlas extends Evented { constructor(width, height) { + super(); + this.width = width; this.height = height; @@ -64,22 +67,24 @@ class SpriteAtlas { } if (!(pixels instanceof Uint32Array)) { - throw new Error('Image provided in an invalid format. Supported formats are HTMLImageElement, ImageData, and ArrayBufferView.'); + this.fire('error', {error: new Error('Image provided in an invalid format. Supported formats are HTMLImageElement, ImageData, and ArrayBufferView.')}); } if (this.images[name]) { - throw new Error('An image with this name already exists.'); + this.fire('error', {error: new Error('An image with this name already exists.')}); } const rect = this.allocateImage(width, height); if (!rect) { - throw new Error('There is not enough space to add this image.'); + this.fire('error', {error: new Error('There is not enough space to add this image.')}); } const image = new AtlasImage(rect, width / this.pixelRatio, height / this.pixelRatio, false, 1); this.images[name] = image; this.copy(pixels, width, rect, {pixelRatio: this.pixelRatio, x: 0, y: 0, width, height}, false); + + this.fire('data', {dataType: 'style'}); } getImage(name, wrap) {