@orca-fe/x-map
Version:
38 lines (37 loc) • 1.47 kB
JavaScript
import { SVG_OFFSET_TOP } from '../../defs';
import { AbstractMarker } from '../AbstractLayer';
const svgNS = 'http://www.w3.org/2000/svg';
export default class SvgPolygon extends AbstractMarker {
constructor(options) {
super();
this.setPath = (path) => {
this.path = path;
this.updatePosition();
};
this.setStyle = (style) => {
Object.entries(style).forEach(([key, value]) => {
this.dom.style[key] = value;
});
this.updatePosition();
};
this.updatePosition = () => {
var _a;
if ((_a = this.layer) === null || _a === void 0 ? void 0 : _a.map) {
const { map } = this.layer;
const pixels = this.path.map(position => map.lnglatToPixel(position));
const path = pixels.map(([x, y], index) => `${index === 0 ? 'M' : 'L'}${x} ${y - SVG_OFFSET_TOP} `).concat(['Z'])
.join('');
this.dom.setAttribute('d', this.path.length ? path : '');
}
};
const { path, style = {} } = options;
const dom = document.createElementNS(svgNS, 'path');
this.dom = dom;
this.path = path;
dom.style.fill = 'none';
dom.style.stroke = 'black';
Object.entries(style).forEach(([key, value]) => {
this.dom.style[key] = value;
});
}
}