`setImage` convenience method
@avpeery is already working on this.
Since Aug 18, 2021.
- #10978 by @ashishshroti14 — closed without merging
- Dominant language
- TypeScript
- Stars
- 12.4k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
## Motivation
In Studio we have the following pattern that feels common across GL JS devs. We could potentially integrate this pattern into a convenience method:
```typescript
if (map.hasImage(imageId)) {
if (this.imageSizeChanged(imageId, rasterized)) {
// Detect if icon size has changed, and if so,
// use remove/add instead of updateImage, since
// updateImage disallows size changes
map.removeImage(imageId);
map.addImage(imageId, rasterized, {
pixelRatio: dimensions.pixelRatio,
});
} else {
map.updateImage(imageId, rasterized);
}
} else {
map.addImage(imageId, rasterized, {
pixelRatio: dimensions.pixelRatio,
});
}
```
## Design Alternatives
We could do nothing (current) and users would just write this block of code themselves.
Potentially a new API should be added with this functionality, but would be faster since integrated with the [`ImageManager`](https://github.com/mapbox/mapbox-gl-js/blob/36533f3683dd48837ab07095fb6484165d9ee400/src/render/image_manager.js#L40).
## Design
Add a `map.setImage(id, image, opts)` convenience method that:
- Adds the image if `id` doesn't exist
- Updates the image if `id` exists but is of the same size.
- Removes/re-adds the image if `id` exists but is of a different size.
### Mock-Up
```typescript
// First call adds image.
map.setImage(id, image)
// Second call updates image.
map.setImage(id, image)
// Third call removes then re-adds image since size is different.
map.setImage(id, imageOfNewSize)
```
### Concepts
This adds an additional concept to sprites of "setting," in addition to add/update/remove.
### Implementation
An implementation would probably be most performant if implemented directly in `ImageManager`/`Style` instead of a wrapper around the existing methods.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.