@antv/g-svg 0.5.5 → 0.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/g.min.js +1 -31
- package/dist/g.min.js.map +1 -1
- package/esm/canvas.d.ts +1 -1
- package/esm/group.d.ts +1 -1
- package/esm/index.d.ts +3 -4
- package/esm/index.js +2 -5
- package/esm/index.js.map +1 -1
- package/esm/interfaces.d.ts +2 -2
- package/esm/interfaces.js +1 -1
- package/esm/interfaces.js.map +1 -1
- package/esm/shape/base.d.ts +1 -1
- package/esm/shape/base.js +1 -1
- package/esm/shape/base.js.map +1 -1
- package/esm/shape/line.js +1 -1
- package/esm/shape/line.js.map +1 -1
- package/esm/shape/path.d.ts +1 -1
- package/esm/shape/polyline.d.ts +1 -1
- package/esm/shape/polyline.js +2 -2
- package/esm/shape/polyline.js.map +1 -1
- package/esm/types.d.ts +1 -1
- package/esm/types.js +1 -1
- package/esm/types.js.map +1 -1
- package/esm/util/draw.d.ts +1 -1
- package/lib/canvas.d.ts +1 -1
- package/lib/group.d.ts +1 -1
- package/lib/index.d.ts +3 -4
- package/lib/index.js +3 -7
- package/lib/index.js.map +1 -1
- package/lib/interfaces.d.ts +2 -2
- package/lib/interfaces.js +1 -1
- package/lib/interfaces.js.map +1 -1
- package/lib/shape/base.d.ts +1 -1
- package/lib/shape/base.js +2 -2
- package/lib/shape/base.js.map +1 -1
- package/lib/shape/line.js +3 -3
- package/lib/shape/line.js.map +1 -1
- package/lib/shape/path.d.ts +1 -1
- package/lib/shape/polyline.d.ts +1 -1
- package/lib/shape/polyline.js +5 -5
- package/lib/shape/polyline.js.map +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/types.js +1 -1
- package/lib/types.js.map +1 -1
- package/lib/util/draw.d.ts +1 -1
- package/package.json +9 -8
- package/src/canvas.ts +100 -0
- package/src/constant.ts +67 -0
- package/src/defs/arrow.ts +83 -0
- package/src/defs/clip.ts +38 -0
- package/src/defs/gradient.ts +129 -0
- package/src/defs/index.ts +110 -0
- package/src/defs/pattern.ts +56 -0
- package/src/defs/shadow.ts +81 -0
- package/src/group.ts +99 -0
- package/src/index.ts +9 -0
- package/src/interfaces.ts +27 -0
- package/src/shape/base.ts +246 -0
- package/src/shape/circle.ts +39 -0
- package/src/shape/dom.ts +40 -0
- package/src/shape/ellipse.ts +40 -0
- package/src/shape/image.ts +81 -0
- package/src/shape/index.ts +12 -0
- package/src/shape/line.ts +67 -0
- package/src/shape/marker/index.ts +56 -0
- package/src/shape/marker/symbols.ts +49 -0
- package/src/shape/path.ts +90 -0
- package/src/shape/polygon.ts +27 -0
- package/src/shape/polyline.ts +146 -0
- package/src/shape/rect.ts +91 -0
- package/src/shape/text.ts +126 -0
- package/src/types.ts +2 -0
- package/src/util/dom.ts +95 -0
- package/src/util/draw.ts +67 -0
- package/src/util/format.ts +62 -0
- package/src/util/svg.ts +51 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview marker
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { isArray, isFunction } from '@antv/util';
|
|
7
|
+
import ShapeBase from '../base';
|
|
8
|
+
import symbolsFactory from './symbols';
|
|
9
|
+
|
|
10
|
+
class Marker extends ShapeBase {
|
|
11
|
+
type: string = 'marker';
|
|
12
|
+
canFill: boolean = true;
|
|
13
|
+
canStroke: boolean = true;
|
|
14
|
+
|
|
15
|
+
// 作为其静态属性
|
|
16
|
+
public static symbolsFactory = symbolsFactory;
|
|
17
|
+
|
|
18
|
+
createPath(context) {
|
|
19
|
+
const el = this.get('el');
|
|
20
|
+
el.setAttribute('d', this._assembleMarker());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
_assembleMarker() {
|
|
24
|
+
const d = this._getPath();
|
|
25
|
+
if (isArray(d)) {
|
|
26
|
+
return d
|
|
27
|
+
.map((path) => {
|
|
28
|
+
return path.join(' ');
|
|
29
|
+
})
|
|
30
|
+
.join('');
|
|
31
|
+
}
|
|
32
|
+
return d;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_getPath(): any[] {
|
|
36
|
+
const attrs = this.attr();
|
|
37
|
+
const { x, y } = attrs;
|
|
38
|
+
// 兼容 r 和 radius 两种写法,推荐使用 r
|
|
39
|
+
const r = attrs.r || attrs.radius;
|
|
40
|
+
const symbol = attrs.symbol || 'circle';
|
|
41
|
+
let method;
|
|
42
|
+
if (isFunction(symbol)) {
|
|
43
|
+
method = symbol;
|
|
44
|
+
} else {
|
|
45
|
+
method = symbolsFactory.get(symbol);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!method) {
|
|
49
|
+
console.warn(`${method} symbol is not exist.`);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
return method(x, y, r);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default Marker;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const Symbols = {
|
|
2
|
+
// 圆
|
|
3
|
+
circle(x: number, y: number, r: number): any[] {
|
|
4
|
+
return [
|
|
5
|
+
['M', x, y],
|
|
6
|
+
['m', -r, 0],
|
|
7
|
+
['a', r, r, 0, 1, 0, r * 2, 0],
|
|
8
|
+
['a', r, r, 0, 1, 0, -r * 2, 0],
|
|
9
|
+
];
|
|
10
|
+
},
|
|
11
|
+
// 正方形
|
|
12
|
+
square(x: number, y: number, r: number): any[] {
|
|
13
|
+
return [['M', x - r, y - r], ['L', x + r, y - r], ['L', x + r, y + r], ['L', x - r, y + r], ['Z']];
|
|
14
|
+
},
|
|
15
|
+
// 菱形
|
|
16
|
+
diamond(x: number, y: number, r: number): any[] {
|
|
17
|
+
return [['M', x - r, y], ['L', x, y - r], ['L', x + r, y], ['L', x, y + r], ['Z']];
|
|
18
|
+
},
|
|
19
|
+
// 三角形
|
|
20
|
+
triangle(x: number, y: number, r: number): any[] {
|
|
21
|
+
const diffY = r * Math.sin((1 / 3) * Math.PI);
|
|
22
|
+
return [['M', x - r, y + diffY], ['L', x, y - diffY], ['L', x + r, y + diffY], ['z']];
|
|
23
|
+
},
|
|
24
|
+
// 倒三角形
|
|
25
|
+
triangleDown(x: number, y: number, r: number): any[] {
|
|
26
|
+
const diffY = r * Math.sin((1 / 3) * Math.PI);
|
|
27
|
+
return [['M', x - r, y - diffY], ['L', x + r, y - diffY], ['L', x, y + diffY], ['Z']];
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type SymbolFunc = (x: number, y: number, r: number) => any[];
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
get(type: string): SymbolFunc {
|
|
35
|
+
return Symbols[type];
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
register(type: string, func: SymbolFunc) {
|
|
39
|
+
Symbols[type] = func;
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
remove(type: string) {
|
|
43
|
+
delete Symbols[type];
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
getAll() {
|
|
47
|
+
return Symbols;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview path
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
import { Point } from '@antv/g-base';
|
|
6
|
+
import { each, isArray, isObject } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Path extends ShapeBase {
|
|
11
|
+
type: string = 'path';
|
|
12
|
+
canFill: boolean = true;
|
|
13
|
+
canStroke: boolean = true;
|
|
14
|
+
|
|
15
|
+
getDefaultAttrs() {
|
|
16
|
+
const attrs = super.getDefaultAttrs();
|
|
17
|
+
return {
|
|
18
|
+
...attrs,
|
|
19
|
+
startArrow: false,
|
|
20
|
+
endArrow: false,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
createPath(context, targetAttrs) {
|
|
25
|
+
const attrs = this.attr();
|
|
26
|
+
const el = this.get('el');
|
|
27
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
28
|
+
if (attr === 'path' && isArray(value)) {
|
|
29
|
+
el.setAttribute('d', this._formatPath(value));
|
|
30
|
+
} else if (attr === 'startArrow' || attr === 'endArrow') {
|
|
31
|
+
if (value) {
|
|
32
|
+
const id = isObject(value)
|
|
33
|
+
? context.addArrow(attrs, SVG_ATTR_MAP[attr])
|
|
34
|
+
: context.getDefaultArrow(attrs, SVG_ATTR_MAP[attr]);
|
|
35
|
+
el.setAttribute(SVG_ATTR_MAP[attr], `url(#${id})`);
|
|
36
|
+
} else {
|
|
37
|
+
el.removeAttribute(SVG_ATTR_MAP[attr]);
|
|
38
|
+
}
|
|
39
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
40
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_formatPath(value) {
|
|
46
|
+
const newValue = value
|
|
47
|
+
.map((path) => {
|
|
48
|
+
return path.join(' ');
|
|
49
|
+
})
|
|
50
|
+
.join('');
|
|
51
|
+
if (~newValue.indexOf('NaN')) {
|
|
52
|
+
return '';
|
|
53
|
+
}
|
|
54
|
+
return newValue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Get total length of path
|
|
59
|
+
* 尽管通过浏览器的 SVGPathElement.getTotalLength() 接口获取的 path 长度,
|
|
60
|
+
* 与 Canvas 版本通过数学计算的方式得到的长度有一些细微差异,但最大误差在个位数像素,精度上可以能接受
|
|
61
|
+
* @return {number} length
|
|
62
|
+
*/
|
|
63
|
+
getTotalLength() {
|
|
64
|
+
const el = this.get('el');
|
|
65
|
+
return el ? el.getTotalLength() : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get point according to ratio
|
|
70
|
+
* @param {number} ratio
|
|
71
|
+
* @return {Point} point
|
|
72
|
+
*/
|
|
73
|
+
getPoint(ratio: number): Point {
|
|
74
|
+
const el = this.get('el');
|
|
75
|
+
const totalLength = this.getTotalLength();
|
|
76
|
+
// @see https://github.com/antvis/g/issues/634
|
|
77
|
+
if (totalLength === 0) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const point = el ? el.getPointAtLength(ratio * totalLength) : null;
|
|
81
|
+
return point
|
|
82
|
+
? {
|
|
83
|
+
x: point.x,
|
|
84
|
+
y: point.y,
|
|
85
|
+
}
|
|
86
|
+
: null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default Path;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview polygon
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
import { each, isArray } from '@antv/util';
|
|
6
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
7
|
+
import ShapeBase from './base';
|
|
8
|
+
|
|
9
|
+
class Polygon extends ShapeBase {
|
|
10
|
+
type: string = 'polygon';
|
|
11
|
+
canFill: boolean = true;
|
|
12
|
+
canStroke: boolean = true;
|
|
13
|
+
|
|
14
|
+
createPath(context, targetAttrs) {
|
|
15
|
+
const attrs = this.attr();
|
|
16
|
+
const el = this.get('el');
|
|
17
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
18
|
+
if (attr === 'points' && isArray(value) && value.length >= 2) {
|
|
19
|
+
el.setAttribute('points', value.map((point) => `${point[0]},${point[1]}`).join(' '));
|
|
20
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
21
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default Polygon;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview polyline
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
import { Point } from '@antv/g-base';
|
|
6
|
+
import { Polyline as PolylineUtil } from '@antv/g-math';
|
|
7
|
+
import { Line as LineUtil } from '@antv/g-math';
|
|
8
|
+
import { each, isArray, isNil } from '@antv/util';
|
|
9
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
10
|
+
import ShapeBase from './base';
|
|
11
|
+
|
|
12
|
+
class Polyline extends ShapeBase {
|
|
13
|
+
type: string = 'polyline';
|
|
14
|
+
canFill: boolean = true;
|
|
15
|
+
canStroke: boolean = true;
|
|
16
|
+
|
|
17
|
+
getDefaultAttrs() {
|
|
18
|
+
const attrs = super.getDefaultAttrs();
|
|
19
|
+
return {
|
|
20
|
+
...attrs,
|
|
21
|
+
startArrow: false,
|
|
22
|
+
endArrow: false,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 更新属性时,检测是否更改了 points
|
|
27
|
+
onAttrChange(name: string, value: any, originValue: any) {
|
|
28
|
+
super.onAttrChange(name, value, originValue);
|
|
29
|
+
if (['points'].indexOf(name) !== -1) {
|
|
30
|
+
this._resetCache();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_resetCache() {
|
|
35
|
+
this.set('totalLength', null);
|
|
36
|
+
this.set('tCache', null);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
createPath(context, targetAttrs) {
|
|
40
|
+
const attrs = this.attr();
|
|
41
|
+
const el = this.get('el');
|
|
42
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
43
|
+
if (attr === 'points' && isArray(value) && value.length >= 2) {
|
|
44
|
+
el.setAttribute('points', value.map((point) => `${point[0]},${point[1]}`).join(' '));
|
|
45
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
46
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get length of polyline
|
|
53
|
+
* @return {number} length
|
|
54
|
+
*/
|
|
55
|
+
getTotalLength() {
|
|
56
|
+
const { points } = this.attr();
|
|
57
|
+
// get totalLength from cache
|
|
58
|
+
const totalLength = this.get('totalLength');
|
|
59
|
+
if (!isNil(totalLength)) {
|
|
60
|
+
return totalLength;
|
|
61
|
+
}
|
|
62
|
+
this.set('totalLength', PolylineUtil.length(points));
|
|
63
|
+
return this.get('totalLength');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get point according to ratio
|
|
68
|
+
* @param {number} ratio
|
|
69
|
+
* @return {Point} point
|
|
70
|
+
*/
|
|
71
|
+
getPoint(ratio: number): Point {
|
|
72
|
+
const { points } = this.attr();
|
|
73
|
+
// get tCache from cache
|
|
74
|
+
let tCache = this.get('tCache');
|
|
75
|
+
if (!tCache) {
|
|
76
|
+
this._setTcache();
|
|
77
|
+
tCache = this.get('tCache');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let subt;
|
|
81
|
+
let index;
|
|
82
|
+
each(tCache, (v, i) => {
|
|
83
|
+
if (ratio >= v[0] && ratio <= v[1]) {
|
|
84
|
+
subt = (ratio - v[0]) / (v[1] - v[0]);
|
|
85
|
+
index = i;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return LineUtil.pointAt(points[index][0], points[index][1], points[index + 1][0], points[index + 1][1], subt);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_setTcache() {
|
|
92
|
+
const { points } = this.attr();
|
|
93
|
+
if (!points || points.length === 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const totalLength = this.getTotalLength();
|
|
98
|
+
if (totalLength <= 0) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let tempLength = 0;
|
|
103
|
+
const tCache = [];
|
|
104
|
+
let segmentT;
|
|
105
|
+
let segmentL;
|
|
106
|
+
|
|
107
|
+
each(points, (p, i) => {
|
|
108
|
+
if (points[i + 1]) {
|
|
109
|
+
segmentT = [];
|
|
110
|
+
segmentT[0] = tempLength / totalLength;
|
|
111
|
+
segmentL = LineUtil.length(p[0], p[1], points[i + 1][0], points[i + 1][1]);
|
|
112
|
+
tempLength += segmentL;
|
|
113
|
+
segmentT[1] = tempLength / totalLength;
|
|
114
|
+
tCache.push(segmentT);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
this.set('tCache', tCache);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get start tangent vector
|
|
122
|
+
* @return {Array}
|
|
123
|
+
*/
|
|
124
|
+
getStartTangent(): number[][] {
|
|
125
|
+
const { points } = this.attr();
|
|
126
|
+
const result = [];
|
|
127
|
+
result.push([points[1][0], points[1][1]]);
|
|
128
|
+
result.push([points[0][0], points[0][1]]);
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Get end tangent vector
|
|
134
|
+
* @return {Array}
|
|
135
|
+
*/
|
|
136
|
+
getEndTangent(): number[][] {
|
|
137
|
+
const { points } = this.attr();
|
|
138
|
+
const l = points.length - 1;
|
|
139
|
+
const result = [];
|
|
140
|
+
result.push([points[l - 1][0], points[l - 1][1]]);
|
|
141
|
+
result.push([points[l][0], points[l][1]]);
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export default Polyline;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview rect
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each, isArray } from '@antv/util';
|
|
7
|
+
import ShapeBase from './base';
|
|
8
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
9
|
+
import { parseRadius } from '../util/format';
|
|
10
|
+
|
|
11
|
+
class Rect extends ShapeBase {
|
|
12
|
+
type: string = 'rect';
|
|
13
|
+
canFill: boolean = true;
|
|
14
|
+
canStroke: boolean = true;
|
|
15
|
+
|
|
16
|
+
getDefaultAttrs() {
|
|
17
|
+
const attrs = super.getDefaultAttrs();
|
|
18
|
+
return {
|
|
19
|
+
...attrs,
|
|
20
|
+
x: 0,
|
|
21
|
+
y: 0,
|
|
22
|
+
width: 0,
|
|
23
|
+
height: 0,
|
|
24
|
+
radius: 0,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
createPath(context, targetAttrs) {
|
|
29
|
+
const attrs = this.attr();
|
|
30
|
+
const el = this.get('el');
|
|
31
|
+
// 加上状态量,用来标记 path 是否已组装
|
|
32
|
+
let completed = false;
|
|
33
|
+
// 和组装 path 相关的绘图属性
|
|
34
|
+
const pathRelatedAttrs = ['x', 'y', 'width', 'height', 'radius'];
|
|
35
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
36
|
+
if (pathRelatedAttrs.indexOf(attr) !== -1 && !completed) {
|
|
37
|
+
el.setAttribute('d', this._assembleRect(attrs));
|
|
38
|
+
completed = true;
|
|
39
|
+
} else if (pathRelatedAttrs.indexOf(attr) === -1 && SVG_ATTR_MAP[attr]) {
|
|
40
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_assembleRect(attrs) {
|
|
46
|
+
const x = attrs.x;
|
|
47
|
+
const y = attrs.y;
|
|
48
|
+
const w = attrs.width;
|
|
49
|
+
const h = attrs.height;
|
|
50
|
+
const radius = attrs.radius;
|
|
51
|
+
|
|
52
|
+
if (!radius) {
|
|
53
|
+
return `M ${x},${y} l ${w},0 l 0,${h} l${-w} 0 z`;
|
|
54
|
+
}
|
|
55
|
+
const r = parseRadius(radius);
|
|
56
|
+
if (isArray(radius)) {
|
|
57
|
+
if (radius.length === 1) {
|
|
58
|
+
r.r1 = r.r2 = r.r3 = r.r4 = radius[0];
|
|
59
|
+
} else if (radius.length === 2) {
|
|
60
|
+
r.r1 = r.r3 = radius[0];
|
|
61
|
+
r.r2 = r.r4 = radius[1];
|
|
62
|
+
} else if (radius.length === 3) {
|
|
63
|
+
r.r1 = radius[0];
|
|
64
|
+
r.r2 = r.r4 = radius[1];
|
|
65
|
+
r.r3 = radius[2];
|
|
66
|
+
} else {
|
|
67
|
+
r.r1 = radius[0];
|
|
68
|
+
r.r2 = radius[1];
|
|
69
|
+
r.r3 = radius[2];
|
|
70
|
+
r.r4 = radius[3];
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
r.r1 = r.r2 = r.r3 = r.r4 = radius;
|
|
74
|
+
}
|
|
75
|
+
const d = [
|
|
76
|
+
[`M ${x + r.r1},${y}`],
|
|
77
|
+
[`l ${w - r.r1 - r.r2},0`],
|
|
78
|
+
[`a ${r.r2},${r.r2},0,0,1,${r.r2},${r.r2}`],
|
|
79
|
+
[`l 0,${h - r.r2 - r.r3}`],
|
|
80
|
+
[`a ${r.r3},${r.r3},0,0,1,${-r.r3},${r.r3}`],
|
|
81
|
+
[`l ${r.r3 + r.r4 - w},0`],
|
|
82
|
+
[`a ${r.r4},${r.r4},0,0,1,${-r.r4},${-r.r4}`],
|
|
83
|
+
[`l 0,${r.r4 + r.r1 - h}`],
|
|
84
|
+
[`a ${r.r1},${r.r1},0,0,1,${r.r1},${-r.r1}`],
|
|
85
|
+
['z'],
|
|
86
|
+
];
|
|
87
|
+
return d.join(' ');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default Rect;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview text
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each } from '@antv/util';
|
|
7
|
+
import { detect } from 'detect-browser';
|
|
8
|
+
import { setTransform } from '../util/svg';
|
|
9
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
10
|
+
import ShapeBase from './base';
|
|
11
|
+
|
|
12
|
+
const LETTER_SPACING = 0.3;
|
|
13
|
+
|
|
14
|
+
const BASELINE_MAP = {
|
|
15
|
+
top: 'before-edge',
|
|
16
|
+
middle: 'central',
|
|
17
|
+
bottom: 'after-edge',
|
|
18
|
+
alphabetic: 'baseline',
|
|
19
|
+
hanging: 'hanging',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// for FireFox
|
|
23
|
+
const BASELINE_MAP_FOR_FIREFOX = {
|
|
24
|
+
top: 'text-before-edge',
|
|
25
|
+
middle: 'central',
|
|
26
|
+
bottom: 'text-after-edge',
|
|
27
|
+
alphabetic: 'alphabetic',
|
|
28
|
+
hanging: 'hanging',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const ANCHOR_MAP = {
|
|
32
|
+
left: 'left',
|
|
33
|
+
start: 'left',
|
|
34
|
+
center: 'middle',
|
|
35
|
+
right: 'end',
|
|
36
|
+
end: 'end',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
class Text extends ShapeBase {
|
|
40
|
+
type: string = 'text';
|
|
41
|
+
canFill: boolean = true;
|
|
42
|
+
canStroke: boolean = true;
|
|
43
|
+
|
|
44
|
+
getDefaultAttrs() {
|
|
45
|
+
const attrs = super.getDefaultAttrs();
|
|
46
|
+
return {
|
|
47
|
+
...attrs,
|
|
48
|
+
x: 0,
|
|
49
|
+
y: 0,
|
|
50
|
+
text: null,
|
|
51
|
+
fontSize: 12,
|
|
52
|
+
fontFamily: 'sans-serif',
|
|
53
|
+
fontStyle: 'normal',
|
|
54
|
+
fontWeight: 'normal',
|
|
55
|
+
fontVariant: 'normal',
|
|
56
|
+
textAlign: 'start',
|
|
57
|
+
textBaseline: 'bottom',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
createPath(context, targetAttrs) {
|
|
62
|
+
const attrs = this.attr();
|
|
63
|
+
const el = this.get('el');
|
|
64
|
+
this._setFont();
|
|
65
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
66
|
+
if (attr === 'text') {
|
|
67
|
+
this._setText(`${value}`);
|
|
68
|
+
} else if (attr === 'matrix' && value) {
|
|
69
|
+
setTransform(this);
|
|
70
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
71
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
el.setAttribute('paint-order', 'stroke');
|
|
75
|
+
el.setAttribute('style', 'stroke-linecap:butt; stroke-linejoin:miter;');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
_setFont() {
|
|
79
|
+
const el = this.get('el');
|
|
80
|
+
const { textBaseline, textAlign } = this.attr();
|
|
81
|
+
|
|
82
|
+
const browser = detect();
|
|
83
|
+
if (browser && browser.name === 'firefox') {
|
|
84
|
+
// compatible with FireFox browser, ref: https://github.com/antvis/g/issues/119
|
|
85
|
+
el.setAttribute('dominant-baseline', BASELINE_MAP_FOR_FIREFOX[textBaseline] || 'alphabetic');
|
|
86
|
+
} else {
|
|
87
|
+
el.setAttribute('alignment-baseline', BASELINE_MAP[textBaseline] || 'baseline');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
el.setAttribute('text-anchor', ANCHOR_MAP[textAlign] || 'left');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_setText(text) {
|
|
94
|
+
const el = this.get('el');
|
|
95
|
+
const { x, textBaseline: baseline = 'bottom' } = this.attr();
|
|
96
|
+
if (!text) {
|
|
97
|
+
el.innerHTML = '';
|
|
98
|
+
} else if (~text.indexOf('\n')) {
|
|
99
|
+
const textArr = text.split('\n');
|
|
100
|
+
const textLen = textArr.length - 1;
|
|
101
|
+
let arr = '';
|
|
102
|
+
each(textArr, (segment, i: number) => {
|
|
103
|
+
if (i === 0) {
|
|
104
|
+
if (baseline === 'alphabetic') {
|
|
105
|
+
arr += `<tspan x="${x}" dy="${-textLen}em">${segment}</tspan>`;
|
|
106
|
+
} else if (baseline === 'top') {
|
|
107
|
+
arr += `<tspan x="${x}" dy="0.9em">${segment}</tspan>`;
|
|
108
|
+
} else if (baseline === 'middle') {
|
|
109
|
+
arr += `<tspan x="${x}" dy="${-(textLen - 1) / 2}em">${segment}</tspan>`;
|
|
110
|
+
} else if (baseline === 'bottom') {
|
|
111
|
+
arr += `<tspan x="${x}" dy="-${textLen + LETTER_SPACING}em">${segment}</tspan>`;
|
|
112
|
+
} else if (baseline === 'hanging') {
|
|
113
|
+
arr += `<tspan x="${x}" dy="${-(textLen - 1) - LETTER_SPACING}em">${segment}</tspan>`;
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
arr += `<tspan x="${x}" dy="1em">${segment}</tspan>`;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
el.innerHTML = arr;
|
|
120
|
+
} else {
|
|
121
|
+
el.innerHTML = text;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export default Text;
|
package/src/types.ts
ADDED
package/src/util/dom.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { toArray } from '@antv/util';
|
|
2
|
+
import { IShape, IGroup, IElement } from '../interfaces';
|
|
3
|
+
import { SHAPE_TO_TAGS } from '../constant';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 创建并返回图形的 svg 元素
|
|
7
|
+
* @param type svg类型
|
|
8
|
+
*/
|
|
9
|
+
export function createSVGElement(type: string): SVGElement {
|
|
10
|
+
return document.createElementNS('http://www.w3.org/2000/svg', type);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 创建并返回图形的 dom 元素
|
|
15
|
+
* @param {IShape} shape 图形
|
|
16
|
+
* @return {SVGElement}
|
|
17
|
+
*/
|
|
18
|
+
export function createDom(shape: IShape) {
|
|
19
|
+
const type = SHAPE_TO_TAGS[shape.type];
|
|
20
|
+
const parent = shape.getParent();
|
|
21
|
+
if (!type) {
|
|
22
|
+
throw new Error(`the type ${shape.type} is not supported by svg`);
|
|
23
|
+
}
|
|
24
|
+
const element = createSVGElement(type);
|
|
25
|
+
if (shape.get('id')) {
|
|
26
|
+
element.id = shape.get('id');
|
|
27
|
+
}
|
|
28
|
+
shape.set('el', element);
|
|
29
|
+
shape.set('attrs', {});
|
|
30
|
+
// 对于 defs 下的 dom 节点,parent 为空,通过 context 统一挂载到 defs 节点下
|
|
31
|
+
if (parent) {
|
|
32
|
+
let parentNode = parent.get('el');
|
|
33
|
+
if (parentNode) {
|
|
34
|
+
parentNode.appendChild(element);
|
|
35
|
+
} else {
|
|
36
|
+
// parentNode maybe null for group
|
|
37
|
+
parentNode = (parent as IGroup).createDom();
|
|
38
|
+
parent.set('el', parentNode);
|
|
39
|
+
parentNode.appendChild(element);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return element;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 对 dom 元素进行排序
|
|
47
|
+
* @param {IElement} element 元素
|
|
48
|
+
* @param {sorter} function 排序函数
|
|
49
|
+
*/
|
|
50
|
+
export function sortDom(element: IElement, sorter: (a: IElement, b: IElement) => number) {
|
|
51
|
+
const el = element.get('el');
|
|
52
|
+
const childList = toArray(el.children).sort(sorter);
|
|
53
|
+
// create empty fragment
|
|
54
|
+
const fragment = document.createDocumentFragment();
|
|
55
|
+
childList.forEach((child) => {
|
|
56
|
+
fragment.appendChild(child);
|
|
57
|
+
});
|
|
58
|
+
el.appendChild(fragment);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 将 dom 元素移动到父元素下的指定位置
|
|
63
|
+
* @param {SVGElement} element dom 元素
|
|
64
|
+
* @param {number} targetIndex 目标位置(从 0 开始)
|
|
65
|
+
*/
|
|
66
|
+
export function moveTo(element: SVGElement, targetIndex: number) {
|
|
67
|
+
const parentNode = element.parentNode;
|
|
68
|
+
const siblings = Array.from(parentNode.childNodes).filter(
|
|
69
|
+
// 要求为元素节点,且不能为 defs 节点
|
|
70
|
+
(node: Node) => node.nodeType === 1 && node.nodeName.toLowerCase() !== 'defs'
|
|
71
|
+
);
|
|
72
|
+
// 获取目标节点
|
|
73
|
+
const target = siblings[targetIndex];
|
|
74
|
+
const currentIndex = siblings.indexOf(element);
|
|
75
|
+
// 如果目标节点存在
|
|
76
|
+
if (target) {
|
|
77
|
+
// 当前索引 > 目标索引,直接插入到目标节点之前即可
|
|
78
|
+
if (currentIndex > targetIndex) {
|
|
79
|
+
parentNode.insertBefore(element, target);
|
|
80
|
+
} else if (currentIndex < targetIndex) {
|
|
81
|
+
// 当前索引 < 目标索引
|
|
82
|
+
// 获取目标节点的下一个节点
|
|
83
|
+
const targetNext = siblings[targetIndex + 1];
|
|
84
|
+
// 如果目标节点的下一个节点存在,插入到该节点之前
|
|
85
|
+
if (targetNext) {
|
|
86
|
+
parentNode.insertBefore(element, targetNext);
|
|
87
|
+
} else {
|
|
88
|
+
// 如果该节点不存在,则追加到末尾
|
|
89
|
+
parentNode.appendChild(element);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
parentNode.appendChild(element);
|
|
94
|
+
}
|
|
95
|
+
}
|