@orca-fe/x-map
Version:
39 lines (38 loc) • 1.48 kB
JavaScript
import { SVG_OFFSET_TOP } from '../../defs';
import { AbstractMarker } from '../AbstractLayer';
const svgNS = 'http://www.w3.org/2000/svg';
export default class SvgLine extends AbstractMarker {
constructor(options) {
super();
this.setLine = (from, to) => {
this.from = from;
this.to = to;
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.from, this.to].map(position => map.lnglatToPixel(position));
const path = pixels.map(([x, y], index) => `${index === 0 ? 'M' : 'L'}${x} ${y - SVG_OFFSET_TOP} `).join('');
this.dom.setAttribute('d', path);
}
};
const { from, to, style = {} } = options;
const dom = document.createElementNS(svgNS, 'path');
this.dom = dom;
this.from = from;
this.to = to;
dom.style.fill = 'none';
dom.style.stroke = 'black';
Object.entries(style).forEach(([key, value]) => {
this.dom.style[key] = value;
});
}
}