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 ImageSource.updateImage() #7342

Merged
merged 11 commits into from
Oct 2, 2018
Merged

Conversation

dcervelli
Copy link
Contributor

Fixes #4050.

update-image

H/t to the folks on the #4050 thread who showed the way to do this.

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • post benchmark scores
  • manually test the debug page
  • tagged @mapbox/studio and/or @mapbox/maps-design if this PR includes style spec changes

@dcervelli dcervelli changed the title Feature/update image Add ImageSource.updateImage() Sep 26, 2018
@dcervelli
Copy link
Contributor Author

dcervelli commented Sep 26, 2018

As pointed out by the original submitter of the bug, I need to test changing the image size before merging.

Edit: looks like this is no problem.

Copy link
Contributor

@ryanhamley ryanhamley left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! It works well. I left some feedback on a few things. I'd also suggest updating the Animate a series of images example to use this new approach. I'd also love to have some unit tests for this functionality; we probably need one to test changing the URL, one to test changing the coordinates and one to test what happens when you change the image size.

* They do not have to represent a rectangle.
* @returns {ImageSource} this
*/
updateImage(options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add some Flow typing here. See here and refer to the setCoordinates method in this file to what your coordinates param would look like. Make sure that coordinates is optional.


var updateCoords = Boolean(options.coordinates);

getImage(this.map._transformRequest(options.url, ResourceType.Image), (err, image) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a lot of repetition of code in this. We could DRY it up quite a bit by using the existing load and _finishLoading functions which has the added bonus of firing the appropriate events and updating this.url, etc. You have to change _finishLoading so it can accept the coordinates as a parameter and it looks like you'd have to change load to accept a callback so you can pass in the necessary functionality like this.texture = null but otherwise it seems to work pretty well.

this.fire(new Event('data', {dataType:'source', sourceDataType: 'content'}));
}

if (updateCoords && this.map) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just check options.coordinates here instead of casting to a Boolean and saving the result as a variable.

});

const path = "/test/integration/image/";
const images = [
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we need quite so many gifs to be added to the library. You can illustrate this with just a few. In fact, you can probably modify this to use 'https://www.mapbox.com/mapbox-gl-js/assets/radar' + currentImage + '.gif' as seen here and eliminate the need for these gifs entirely.

map.addSource("radar", {
type: "image",
url: getPath(),
coordinates: [
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to also update the coordinates on each iteration so that can easily be debugged.

*
* // update url.
* // Note: this is most effective if your layer has the `raster-fade-duration` paint property set to 0.
* mySource.updateImage({
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably only need one example here. Just show it with url and coordinates. The docs should note that coordinates is optional.

@dcervelli
Copy link
Contributor Author

@ryanhamley I think I got all of the comments addressed except for a unit test around an image update with a size change. Not really sure how to do that one. FWIW, the code in image_source.js doesn't ever do anything with width/height -- all of that is handled in the Texture/ImageData code -- and I've covered this pretty thoroughly in manually testing.

});

// To illustrate moving coordinates at the same time
coordinates.forEach(c => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This moves the image off the screen pretty rapidly and eventually will cause an error when the latitude goes above 90°. I know I suggested this but maybe it would be better to remove it. Sorry about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem.

@ryanhamley
Copy link
Contributor

Hey @dcervelli sorry it took a few days to get back to you. I think it's looking pretty good now. I made a comment on the debug page. I think now that I see it, we should either remove the coordinate updates or loop through them so the image doesn't move off the page and eventually off the map. Other than that, I'm ready to give this a 👍

@ryanhamley ryanhamley merged commit efb91b6 into mapbox:master Oct 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants