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

Add Map#loadImage method and Map#addImage examples #4478

Merged
merged 3 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion docs/_posts/examples/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"MapboxGeocoder": true,
"MapboxDirections": true,
"turf": true,
"d3": true
"d3": true,
"Uint8Array": true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember why we have es6 env set to false a few lines below? As far as I remember, turning this back to true would allow not specifying common globals like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As best I remember we were just to avoid using es6 features with limited browser support. This is less of a concern now. I have enabled the "es6" flag and removed this global in the latest commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to continue to write examples without using ES6 features such as let/const or arrow functions (not supported by iOS 9) and for...of (not supported by IE 11). I think it's useful to keep this configuration option to catch accidental uses of these types of features. I suggest we revert the latest commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right -- the examples don't get bubleifyied. I'll revert the last commit and leave a comment about this in the eslint file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfirebaugh as far as I remember, the env switches are only for predefined globals like Uint8Array — they're not used for catching keywords like let.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. And "parserOptions": { "ecmaVersion": 5 } doesn't work either. 😞

},
"rules": {
"strict": "off",
Expand Down
59 changes: 59 additions & 0 deletions docs/_posts/examples/3400-01-31-add-image-generated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
layout: example
category: example
title: Add a generated icon to the map
description: Add an icon to the map that was generated at runtime.
tags:
- styles
- layers
---
<div id='map'></div>

<script>

var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9'
});

map.on('load', () => {

const width = 64; // The image will be 64 pixels square
const bytesPerPixel = 4; // Each pixel is represented by 4 bytes: red, green, blue, and alpha.
const image = new Uint8Array(width * width * bytesPerPixel);

for (var x = 0; x < width; x++) {
for (var y = 0; y < width; y++) {
const offset = (y * width + x) * bytesPerPixel;
image[offset + 0] = y / width * 255; // red
image[offset + 1] = x / width * 255; // green
image[offset + 2] = 128; // blue
image[offset + 3] = 255; // alpha
}
}

map.addImage('gradient', image, {width: width, height: width});

map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
}]
}
},
"layout": {
"icon-image": "gradient"
}
});
});

</script>
47 changes: 47 additions & 0 deletions docs/_posts/examples/3400-01-31-add-image.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: example
category: example
title: Add an icon to the map
description: Add an icon to the map from an external URL and use it in a symbol layer.
tags:
- styles
- layers
---
<div id='map'></div>

<script>

var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9'
});

map.on('load', () => {
map.loadImage('https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/400px-Cat_silhouette.svg.png', (error, image) => {
if (error) throw error;
map.addImage('cat', image);
map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
}]
}
},
"layout": {
"icon-image": "cat",
"icon-size": 0.25
}
});
});
});

</script>
15 changes: 15 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const util = require('../util/util');
const browser = require('../util/browser');
const window = require('../util/window');
const DOM = require('../util/dom');
const ajax = require('../util/ajax');

const Style = require('../style/style');
const AnimationLoop = require('../style/animation_loop');
Expand Down Expand Up @@ -823,6 +824,8 @@ class Map extends Camera {
* {@link Map#error} event will be fired if there is not enough space in the
* sprite to add this image.
*
* @see [Add an icon to the map](https://www.mapbox.com/mapbox-gl-js/example/add-image/)
* @see [Add a generated icon to the map](https://www.mapbox.com/mapbox-gl-js/example/add-image-generated/)
* @param {string} name The name of the image.
* @param {HTMLImageElement|ArrayBufferView} image The image as an `HTMLImageElement` or `ArrayBufferView` (using the format of [`ImageData#data`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData/data))
* @param {Object} [options] Required if and only if passing an `ArrayBufferView`
Expand All @@ -843,6 +846,18 @@ class Map extends Camera {
this.style.spriteAtlas.removeImage(name);
}

/**
* Load an image from an external URL for use with `Map#addImage`. External
* domains must support [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS).
*
* @param {string} url The URL of the image.
* @param {Function} callback Called when the image has loaded or with an error argument if there is an error.
* @see [Add an icon to the map](https://www.mapbox.com/mapbox-gl-js/example/add-image/)
*/
loadImage(url, callback) {
ajax.getImage(url, callback);
}

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