Skip to content

Commit

Permalink
Support adding SDF images at runtime (#5181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick authored Aug 23, 2017
1 parent b203a40 commit 0cbc71d
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/symbol/sprite_atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class SpriteAtlas extends Evented {
return rect;
}

addImage(name: string, pixels: HTMLImageElement | $ArrayBufferView, options: { width: number, height: number, pixelRatio?: number }) {
addImage(name: string, pixels: HTMLImageElement | $ArrayBufferView, options: { width: number, height: number, pixelRatio?: number, sdf?: boolean }) {
let width, height, pixelRatio;
if (pixels instanceof HTMLImageElement) {
width = pixels.width;
Expand Down Expand Up @@ -151,7 +151,7 @@ class SpriteAtlas extends Evented {
width,
height,
pixelRatio,
sdf: false
sdf: options.sdf || false
};

this.copy(pixels, width, rect, {x: 0, y: 0, width, height}, false);
Expand Down
3 changes: 2 additions & 1 deletion src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,11 +1147,12 @@ class Map extends Camera {
* @param {number} [options.width] The pixel width of the `ArrayBufferView` image
* @param {number} [options.height] The pixel height of the `ArrayBufferView` image
* @param {number} [options.pixelRatio] The ratio of pixels in the `ArrayBufferView` image to physical pixels on the screen
* @param {boolean} [options.sdf] Whether the image should be interpreted as an SDF image
*/
addImage(
name: string,
image: HTMLImageElement | $ArrayBufferView,
options?: {width: number, height: number, pixelRatio: number}
options?: {width: number, height: number, pixelRatio: number, sdf: boolean}
) {
this.style.spriteAtlas.addImage(name, image, (options: any));
}
Expand Down
Binary file added test/integration/image/line.sdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"addImage",
"marker",
"./sprites/1.5x.png",
1.5
{
"pixelRatio": 1.5
}
],
[
"addLayer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"addImage",
"marker",
"./sprites/1.5x.png",
1.5
{
"pixelRatio": 1.5
}
],
[
"addLayer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"addImage",
"marker",
"./sprites/2x.png",
2
{
"pixelRatio": 2
}
],
[
"addLayer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"addImage",
"marker",
"./sprites/2x.png",
2
{
"pixelRatio": 2
}
],
[
"addLayer",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"operations": [
[
"addImage",
"line.sdf",
"./image/line.sdf.png"
],
[
"addLayer",
{
"id": "geometry",
"type": "symbol",
"source": "geometry",
"layout": {
"icon-image": "line.sdf"
}
}
],
[
"wait"
]
]
}
},
"sources": {
"geometry": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [0, 0]
}
}
},
"layers": []
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"operations": [
[
"addImage",
"line.sdf",
"./image/line.sdf.png",
{
"sdf": true
}
],
[
"addLayer",
{
"id": "geometry",
"type": "symbol",
"source": "geometry",
"layout": {
"icon-image": "line.sdf"
},
"paint": {
"icon-color": "red"
}
}
],
[
"wait"
]
]
}
},
"sources": {
"geometry": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [0, 0]
}
}
},
"layers": []
}

5 changes: 3 additions & 2 deletions test/suite_implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PNG = require('pngjs').PNG;
const Map = require('../src/ui/map');
const window = require('../src/util/window');
const browser = require('../src/util/browser');
const util = require('../src/util/util');
const rtlTextPlugin = require('../src/source/rtl_text_plugin');
const rtlText = require('@mapbox/mapbox-gl-rtl-text');
const fs = require('fs');
Expand Down Expand Up @@ -107,8 +108,8 @@ function applyOperations(map, operations, callback) {

} else if (operation[0] === 'addImage') {
const img = PNG.sync.read(fs.readFileSync(path.join(__dirname, './integration', operation[2])));
const pixelRatio = operation.length > 3 ? operation[3] : 1;
map.addImage(operation[1], img.data, {height: img.height, width: img.width, pixelRatio: pixelRatio});
const options = util.extend({}, {height: img.height, width: img.width, pixelRatio: 1}, operation.length > 3 ? operation[3] : {});
map.addImage(operation[1], img.data, options);
applyOperations(map, operations.slice(1), callback);

} else {
Expand Down

0 comments on commit 0cbc71d

Please sign in to comment.