@shenyong/mapbox-gl-image
v0.0.5
Published
Move, scale and rotate image on a map
Downloads
3
Readme
🏙️ @shenyong/mapbox-gl-image
Control to move, scale, rotate image (raster layer) on top of a map. Very handy to represent information from the raster to geojson, for example, the contours of buildings.
npm i @shenyong/mapbox-gl-image
import ImageControl from '@shenyong/mapbox-gl-image';
import '@shenyong/mapbox-gl-image/src/index.css';
const imageControl = new ImageControl();
map.addControl(imageControl, 'bottom-right');
Options
export type ImageControlOptions = {
removeButton?: boolean;
};
Events
| event | description | | -------------- | -------------------------- | | image.select | selected new image | | image.deselect | image was deselected | | image.mode | transform mode was changed | | image.update | position was updated | | image.add | new image added | | image.remove | image removed |
Methods
Methods are useful for programmatic control (when option invisible
is true
):
addFile(file: File, coordinates?: [number, number][] | undefined): Promise<string>;
- add new image by file. raster id is returnedaddUrl(url: string, coordinates?: [number, number][] | undefined): Promise<string>;
- add new image by url. raster id is returnedsetLock: (id: string, isLocked: boolean) => void;
- lock or unlock image. locked image can't be selectedremoveRaster: () => void;
- removes selected raster from the map
If image was added without coordinates
parameter, the image is scaled down to be fully visible and placed at the center of the viewport.
Other methods may help to use this control without buttons, these methods are described in type definitions .d.ts
.
Change paint properties
Paint properties can be changed dynamically. Below is an example how to control image opacity by slider (full implementation is available in preview).
map.on('image.select', ({ id }) => {
const rasterLayerId = image.rasters[id].rasterLayer.id;
const range = document.createElement('input');
range.style.position = 'absolute';
range.style.left = '50%';
range.style.transform = 'translateX(-50%)';
range.style.bottom = '16px';
range.type = 'range';
range.min = 0;
range.step = 0.05;
range.max = 1;
range.value = map.getPaintProperty(rasterLayerId, 'raster-opacity');
range.addEventListener('input', () => {
map.setPaintProperty(rasterLayerId, 'raster-opacity', Number(range.value));
});
document.body.appendChild(range);
map.once('image.deselect', () => {
document.body.removeChild(range);
});
});