-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmarker.ts
More file actions
67 lines (57 loc) · 1.51 KB
/
Copy pathmarker.ts
File metadata and controls
67 lines (57 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { DisplayObjectConfig, BaseStyleProps } from '@antv/g-lite';
import { Path } from '@antv/g-lite';
import { PathArray } from '@antv/util';
export interface MarkerStyleProps extends BaseStyleProps {
x?: string | number;
y?: string | number;
symbol?: 'circle' | 'square' | 'arrow';
radius?: string | number;
}
const SYMBOLS = {
circle(x, y, r): PathArray {
return [
['M', x - r, y],
['A', r, r, 0, 1, 0, x + r, y],
['A', r, r, 0, 1, 0, x - r, y],
];
},
square(x, y, r): PathArray {
return [
['M', x - r, y - r],
['L', x + r, y - r],
['L', x + r, y + r],
['L', x - r, y + r],
['Z'],
];
},
arrow(x, y, r): PathArray {
return [
['M', x - r, y + (2 * r) / Math.sqrt(3)],
['L', x + r, y + (2 * r) / Math.sqrt(3)],
['L', x, y - (2 * r) / Math.sqrt(3)],
['Z'],
];
},
};
export class Marker extends Path {
parsedStyle: any;
constructor(config: DisplayObjectConfig<MarkerStyleProps>) {
super(config);
this.updatePath();
}
setAttribute(name, value, force?: boolean) {
super.setAttribute(name, value, force);
if (['x', 'y', 'symbol', 'radius'].indexOf(name) > -1) {
this.updatePath();
}
}
updatePath() {
const { x = 0, y = 0 } = this.parsedStyle;
const { radius, symbol } = this.attributes as MarkerStyleProps;
if (!symbol) return;
const method = SYMBOLS[symbol];
if (!method) return;
const path = method(x, y, radius);
super.setAttribute('d', path);
}
}