mapbox / mapbox/mapbox-gl-js

`setImage` convenience method

Open
#10,941 0 comments 0 reactions 1 assignee View on GitHub

@avpeery is already working on this.

Since Aug 18, 2021.

  • #10978 by @ashishshroti14 — closed without merging
api :memo: needs discussion :speech_balloon:
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.