Skip to content

Commit

Permalink
Make SpriteAtlas fire error and data events
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Mar 10, 2017
1 parent 478bbe4 commit 59408f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
13 changes: 9 additions & 4 deletions src/symbol/sprite_atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -15,9 +16,11 @@ class AtlasImage {
}
}

class SpriteAtlas {
class SpriteAtlas extends Evented {

constructor(width, height) {
super();

this.width = width;
this.height = height;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 59408f9

Please sign in to comment.