tile-painter
Version:
Parse Mapbox style documents into rendering functions
30 lines (21 loc) • 841 B
JavaScript
export function initIconLabeler(zoom, layout, paint, sprite) {
const pad = layout["icon-padding"](zoom);
return { measure, draw };
function measure(feature, scale) {
let spriteID = feature.properties.spriteID;
if (!spriteID || !sprite) return;
let meta = sprite.meta[spriteID];
if (!meta) return;
let { x: cx, y: cy, width, height } = meta;
let crop = [cx, cy, width, height];
let coords = feature.geometry.coordinates.map(c => c * scale);
let x = Math.round(coords[0] - width / 2);
let y = Math.round(coords[1] - height / 2);
let position = [x, y, width, height];
let bbox = [ [x - pad, y - pad], [x + width + pad, y + height + pad] ];
return { crop, position, bbox };
}
function draw(ctx, { crop, position }) {
ctx.drawImage(sprite.image, ...crop, ...position);
}
}