Skip to content

Commit

Permalink
Add "Map#removeImage"
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Mar 13, 2017
1 parent 565c9da commit e555657
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
46 changes: 26 additions & 20 deletions debug/addimage.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,35 @@

map.on('load', () => {
const img = document.createElement('img');
img.src = '/test/integration/image/marker.png';
img.src = '/test/integration/image/0.png';
img.onload = () => {
map.addImage('img', img);
map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.414, 37.776]
}
}]
const img1 = document.createElement('img');
img1.src = '/test/integration/image/marker.png';
img1.onload = () => {
map.removeImage('img');
map.addImage('img', img1);
map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.414, 37.776]
}
}]
}
},
"layout": {
"icon-image": "img"
}
},
"layout": {
"icon-image": "img"
}
});
});
};
};
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"test": "run-s lint test-unit test-flow",
"test-suite": "run-s test-render test-query",
"test-suite-clean": "find test/integration/*-tests -mindepth 2 -type d -not \\( -exec test -e \"{}/style.json\" \\; \\) -print | xargs -t rm -r",
"test-unit": "tap --reporter dot --no-coverage test/unit",
"test-unit": "tap --no-coverage test/unit",
"test-render": "node test/render.test.js",
"test-query": "node test/query.test.js",
"test-flow": "flow .",
Expand Down
16 changes: 16 additions & 0 deletions src/symbol/sprite_atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ class SpriteAtlas extends Evented {
this.fire('data', {dataType: 'style'});
}

removeImage(name) {
const image = this.images[name];
delete this.images[name];

if (!image) {
this.fire('error', {error: new Error('No image with this name exists.')});
}

const rect = image.rect;

this.copy(new Uint32Array(image.width * image.height), image.width, rect, {pixelRatio: this.pixelRatio, x: 0, y: 0, width: image.width, height: image.height}, false);
this.atlas.unref(rect);

this.fire('data', {dataType: 'style'});
}

getImage(name, wrap) {
if (this.images[name]) {
return this.images[name];
Expand Down
4 changes: 4 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,10 @@ class Map extends Camera {
this.style.spriteAtlas.addImage(name, image, width, height);
}

removeImage(name) {
this.style.spriteAtlas.removeImage(name);
}

/**
* Adds a [Mapbox style layer](https://www.mapbox.com/mapbox-gl-style-spec/#layers)
* to the map's style.
Expand Down

0 comments on commit e555657

Please sign in to comment.