@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
package/src/canvas.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AbstractCanvas, IShape } from '@antv/g-base';
|
|
2
|
+
import { ChangeType } from '@antv/g-base';
|
|
3
|
+
import { IElement } from './interfaces';
|
|
4
|
+
import { SHAPE_TO_TAGS } from './constant';
|
|
5
|
+
import { drawChildren } from './util/draw';
|
|
6
|
+
import { setTransform, setClip } from './util/svg';
|
|
7
|
+
import { sortDom, createSVGElement } from './util/dom';
|
|
8
|
+
import * as Shape from './shape';
|
|
9
|
+
import Group from './group';
|
|
10
|
+
import Defs from './defs';
|
|
11
|
+
|
|
12
|
+
class Canvas extends AbstractCanvas {
|
|
13
|
+
constructor(cfg) {
|
|
14
|
+
super({
|
|
15
|
+
...cfg,
|
|
16
|
+
autoDraw: true,
|
|
17
|
+
// 设置渲染引擎为 canvas,只读属性
|
|
18
|
+
renderer: 'svg',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getShapeBase() {
|
|
23
|
+
return Shape;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getGroupBase() {
|
|
27
|
+
return Group;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 覆盖 Container 中通过遍历的方式获取 shape 对象的逻辑,直接走 SVG 的 dom 拾取即可
|
|
31
|
+
getShape(x: number, y: number, ev: Event): IShape {
|
|
32
|
+
let target = <Element>ev.target || <Element>ev.srcElement;
|
|
33
|
+
if (!SHAPE_TO_TAGS[target.tagName]) {
|
|
34
|
+
let parent = <Element>target.parentNode;
|
|
35
|
+
while (parent && !SHAPE_TO_TAGS[parent.tagName]) {
|
|
36
|
+
parent = <Element>parent.parentNode;
|
|
37
|
+
}
|
|
38
|
+
target = parent;
|
|
39
|
+
}
|
|
40
|
+
return this.find((child) => child.get('el') === target) as IShape;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 复写基类的方法生成标签
|
|
44
|
+
createDom() {
|
|
45
|
+
const element = createSVGElement('svg') as SVGSVGElement;
|
|
46
|
+
const context = new Defs(element);
|
|
47
|
+
element.setAttribute('width', `${this.get('width')}`);
|
|
48
|
+
element.setAttribute('height', `${this.get('height')}`);
|
|
49
|
+
// 缓存 context 对象
|
|
50
|
+
this.set('context', context);
|
|
51
|
+
return element;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 一些方法调用会引起画布变化
|
|
56
|
+
* @param {ChangeType} changeType 改变的类型
|
|
57
|
+
*/
|
|
58
|
+
onCanvasChange(changeType: ChangeType) {
|
|
59
|
+
const context = this.get('context');
|
|
60
|
+
const el = this.get('el');
|
|
61
|
+
if (changeType === 'sort') {
|
|
62
|
+
const children = this.get('children');
|
|
63
|
+
if (children && children.length) {
|
|
64
|
+
sortDom(this, (a: IElement, b: IElement) => {
|
|
65
|
+
return children.indexOf(a) - children.indexOf(b) ? 1 : 0;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
} else if (changeType === 'clear') {
|
|
69
|
+
// el maybe null for canvas
|
|
70
|
+
if (el) {
|
|
71
|
+
// 清空 SVG 元素
|
|
72
|
+
el.innerHTML = '';
|
|
73
|
+
const defsEl = context.el;
|
|
74
|
+
// 清空 defs 元素
|
|
75
|
+
defsEl.innerHTML = '';
|
|
76
|
+
// 将清空后的 defs 元素挂载到 el 下
|
|
77
|
+
el.appendChild(defsEl);
|
|
78
|
+
}
|
|
79
|
+
} else if (changeType === 'matrix') {
|
|
80
|
+
setTransform(this);
|
|
81
|
+
} else if (changeType === 'clip') {
|
|
82
|
+
setClip(this, context);
|
|
83
|
+
} else if (changeType === 'changeSize') {
|
|
84
|
+
el.setAttribute('width', `${this.get('width')}`);
|
|
85
|
+
el.setAttribute('height', `${this.get('height')}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 复写基类的 draw 方法
|
|
90
|
+
draw() {
|
|
91
|
+
const context = this.get('context');
|
|
92
|
+
const children = this.getChildren() as IElement[];
|
|
93
|
+
setClip(this, context);
|
|
94
|
+
if (children.length) {
|
|
95
|
+
drawChildren(context, children);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default Canvas;
|
package/src/constant.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const SHAPE_TO_TAGS = {
|
|
2
|
+
rect: 'path',
|
|
3
|
+
circle: 'circle',
|
|
4
|
+
line: 'line',
|
|
5
|
+
path: 'path',
|
|
6
|
+
marker: 'path',
|
|
7
|
+
text: 'text',
|
|
8
|
+
polyline: 'polyline',
|
|
9
|
+
polygon: 'polygon',
|
|
10
|
+
image: 'image',
|
|
11
|
+
ellipse: 'ellipse',
|
|
12
|
+
dom: 'foreignObject',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const SVG_ATTR_MAP = {
|
|
16
|
+
opacity: 'opacity',
|
|
17
|
+
fillStyle: 'fill',
|
|
18
|
+
fill: 'fill',
|
|
19
|
+
fillOpacity: 'fill-opacity',
|
|
20
|
+
strokeStyle: 'stroke',
|
|
21
|
+
strokeOpacity: 'stroke-opacity',
|
|
22
|
+
stroke: 'stroke',
|
|
23
|
+
x: 'x',
|
|
24
|
+
y: 'y',
|
|
25
|
+
r: 'r',
|
|
26
|
+
rx: 'rx',
|
|
27
|
+
ry: 'ry',
|
|
28
|
+
width: 'width',
|
|
29
|
+
height: 'height',
|
|
30
|
+
x1: 'x1',
|
|
31
|
+
x2: 'x2',
|
|
32
|
+
y1: 'y1',
|
|
33
|
+
y2: 'y2',
|
|
34
|
+
lineCap: 'stroke-linecap',
|
|
35
|
+
lineJoin: 'stroke-linejoin',
|
|
36
|
+
lineWidth: 'stroke-width',
|
|
37
|
+
lineDash: 'stroke-dasharray',
|
|
38
|
+
lineDashOffset: 'stroke-dashoffset',
|
|
39
|
+
miterLimit: 'stroke-miterlimit',
|
|
40
|
+
font: 'font',
|
|
41
|
+
fontSize: 'font-size',
|
|
42
|
+
fontStyle: 'font-style',
|
|
43
|
+
fontVariant: 'font-variant',
|
|
44
|
+
fontWeight: 'font-weight',
|
|
45
|
+
fontFamily: 'font-family',
|
|
46
|
+
startArrow: 'marker-start',
|
|
47
|
+
endArrow: 'marker-end',
|
|
48
|
+
path: 'd',
|
|
49
|
+
class: 'class',
|
|
50
|
+
id: 'id',
|
|
51
|
+
style: 'style',
|
|
52
|
+
preserveAspectRatio: 'preserveAspectRatio',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const EVENTS = [
|
|
56
|
+
'click',
|
|
57
|
+
'mousedown',
|
|
58
|
+
'mouseup',
|
|
59
|
+
'dblclick',
|
|
60
|
+
'contextmenu',
|
|
61
|
+
'mouseenter',
|
|
62
|
+
'mouseleave',
|
|
63
|
+
'mouseover',
|
|
64
|
+
'mouseout',
|
|
65
|
+
'mousemove',
|
|
66
|
+
'wheel',
|
|
67
|
+
];
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview arrow
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { isArray, uniqueId } from '@antv/util';
|
|
7
|
+
import { createSVGElement } from '../util/dom';
|
|
8
|
+
|
|
9
|
+
class Arrow {
|
|
10
|
+
id: string;
|
|
11
|
+
el: SVGMarkerElement;
|
|
12
|
+
child: SVGPathElement | any; // TODO G.Shape, not any
|
|
13
|
+
stroke: string;
|
|
14
|
+
cfg: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
} = {};
|
|
17
|
+
|
|
18
|
+
constructor(attrs, type) {
|
|
19
|
+
const el = createSVGElement('marker') as SVGMarkerElement;
|
|
20
|
+
const id = uniqueId('marker_');
|
|
21
|
+
el.setAttribute('id', id);
|
|
22
|
+
const shape = createSVGElement('path');
|
|
23
|
+
shape.setAttribute('stroke', attrs.stroke || 'none');
|
|
24
|
+
shape.setAttribute('fill', attrs.fill || 'none');
|
|
25
|
+
el.appendChild(shape);
|
|
26
|
+
el.setAttribute('overflow', 'visible');
|
|
27
|
+
el.setAttribute('orient', 'auto-start-reverse');
|
|
28
|
+
this.el = el;
|
|
29
|
+
this.child = shape;
|
|
30
|
+
this.id = id;
|
|
31
|
+
const cfg = attrs[type === 'marker-start' ? 'startArrow' : 'endArrow'];
|
|
32
|
+
this.stroke = attrs.stroke || '#000';
|
|
33
|
+
if (cfg === true) {
|
|
34
|
+
this._setDefaultPath(type, shape);
|
|
35
|
+
} else {
|
|
36
|
+
this.cfg = cfg; // when arrow config exists
|
|
37
|
+
this._setMarker(attrs.lineWidth, shape);
|
|
38
|
+
}
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
match() {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_setDefaultPath(type, el) {
|
|
47
|
+
const parent = this.el;
|
|
48
|
+
// 默认箭头的边长为 10,夹角为 60 度
|
|
49
|
+
el.setAttribute('d', `M0,0 L${10 * Math.cos(Math.PI / 6)},5 L0,10`);
|
|
50
|
+
parent.setAttribute('refX', `${10 * Math.cos(Math.PI / 6)}`);
|
|
51
|
+
parent.setAttribute('refY', `${5}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_setMarker(r, el) {
|
|
55
|
+
const parent = this.el;
|
|
56
|
+
let path = this.cfg.path;
|
|
57
|
+
const d = this.cfg.d;
|
|
58
|
+
|
|
59
|
+
if (isArray(path)) {
|
|
60
|
+
path = path
|
|
61
|
+
.map((segment) => {
|
|
62
|
+
return segment.join(' ');
|
|
63
|
+
})
|
|
64
|
+
.join('');
|
|
65
|
+
}
|
|
66
|
+
el.setAttribute('d', path);
|
|
67
|
+
parent.appendChild(el);
|
|
68
|
+
if (d) {
|
|
69
|
+
parent.setAttribute('refX', `${d / r}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
update(fill) {
|
|
74
|
+
const child = this.child;
|
|
75
|
+
if (child.attr) {
|
|
76
|
+
child.attr('fill', fill);
|
|
77
|
+
} else {
|
|
78
|
+
child.setAttribute('fill', fill);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default Arrow;
|
package/src/defs/clip.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview clip
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { uniqueId } from '@antv/util';
|
|
7
|
+
import { createSVGElement } from '../util/dom';
|
|
8
|
+
|
|
9
|
+
class Clip {
|
|
10
|
+
type: string = 'clip';
|
|
11
|
+
id: string;
|
|
12
|
+
el: SVGClipPathElement;
|
|
13
|
+
cfg: {
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
} = {};
|
|
16
|
+
|
|
17
|
+
constructor(cfg) {
|
|
18
|
+
const el = createSVGElement('clipPath') as SVGClipPathElement;
|
|
19
|
+
this.el = el;
|
|
20
|
+
this.id = uniqueId('clip_');
|
|
21
|
+
el.id = this.id;
|
|
22
|
+
const shapeEl = cfg.cfg.el;
|
|
23
|
+
el.appendChild(shapeEl);
|
|
24
|
+
this.cfg = cfg;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
match() {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
remove() {
|
|
33
|
+
const el = this.el;
|
|
34
|
+
el.parentNode.removeChild(el);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default Clip;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview gradient
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each, mod, toRadian, uniqueId } from '@antv/util';
|
|
7
|
+
import { createSVGElement } from '../util/dom';
|
|
8
|
+
|
|
9
|
+
const regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i;
|
|
10
|
+
const regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i;
|
|
11
|
+
const regexColorStop = /[\d.]+:(#[^\s]+|[^)]+\))/gi;
|
|
12
|
+
|
|
13
|
+
function addStop(steps) {
|
|
14
|
+
const arr = steps.match(regexColorStop);
|
|
15
|
+
if (!arr) {
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
let stops = '';
|
|
19
|
+
arr.sort((a, b) => {
|
|
20
|
+
a = a.split(':');
|
|
21
|
+
b = b.split(':');
|
|
22
|
+
return Number(a[0]) - Number(b[0]);
|
|
23
|
+
});
|
|
24
|
+
each(arr, (item: any) => {
|
|
25
|
+
item = item.split(':');
|
|
26
|
+
stops += `<stop offset="${item[0]}" stop-color="${item[1]}"></stop>`;
|
|
27
|
+
});
|
|
28
|
+
return stops;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseLineGradient(color, el) {
|
|
32
|
+
const arr = regexLG.exec(color);
|
|
33
|
+
const angle = mod(toRadian(parseFloat(arr[1])), Math.PI * 2);
|
|
34
|
+
const steps = arr[2];
|
|
35
|
+
let start;
|
|
36
|
+
let end;
|
|
37
|
+
|
|
38
|
+
if (angle >= 0 && angle < 0.5 * Math.PI) {
|
|
39
|
+
start = {
|
|
40
|
+
x: 0,
|
|
41
|
+
y: 0,
|
|
42
|
+
};
|
|
43
|
+
end = {
|
|
44
|
+
x: 1,
|
|
45
|
+
y: 1,
|
|
46
|
+
};
|
|
47
|
+
} else if (0.5 * Math.PI <= angle && angle < Math.PI) {
|
|
48
|
+
start = {
|
|
49
|
+
x: 1,
|
|
50
|
+
y: 0,
|
|
51
|
+
};
|
|
52
|
+
end = {
|
|
53
|
+
x: 0,
|
|
54
|
+
y: 1,
|
|
55
|
+
};
|
|
56
|
+
} else if (Math.PI <= angle && angle < 1.5 * Math.PI) {
|
|
57
|
+
start = {
|
|
58
|
+
x: 1,
|
|
59
|
+
y: 1,
|
|
60
|
+
};
|
|
61
|
+
end = {
|
|
62
|
+
x: 0,
|
|
63
|
+
y: 0,
|
|
64
|
+
};
|
|
65
|
+
} else {
|
|
66
|
+
start = {
|
|
67
|
+
x: 0,
|
|
68
|
+
y: 1,
|
|
69
|
+
};
|
|
70
|
+
end = {
|
|
71
|
+
x: 1,
|
|
72
|
+
y: 0,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const tanTheta = Math.tan(angle);
|
|
77
|
+
const tanTheta2 = tanTheta * tanTheta;
|
|
78
|
+
|
|
79
|
+
const x = (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.x;
|
|
80
|
+
const y = (tanTheta * (end.x - start.x + tanTheta * (end.y - start.y))) / (tanTheta2 + 1) + start.y;
|
|
81
|
+
el.setAttribute('x1', start.x);
|
|
82
|
+
el.setAttribute('y1', start.y);
|
|
83
|
+
el.setAttribute('x2', x);
|
|
84
|
+
el.setAttribute('y2', y);
|
|
85
|
+
el.innerHTML = addStop(steps);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseRadialGradient(color, self) {
|
|
89
|
+
const arr = regexRG.exec(color);
|
|
90
|
+
const cx = parseFloat(arr[1]);
|
|
91
|
+
const cy = parseFloat(arr[2]);
|
|
92
|
+
const r = parseFloat(arr[3]);
|
|
93
|
+
const steps = arr[4];
|
|
94
|
+
self.setAttribute('cx', cx);
|
|
95
|
+
self.setAttribute('cy', cy);
|
|
96
|
+
self.setAttribute('r', r);
|
|
97
|
+
self.innerHTML = addStop(steps);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
class Gradient {
|
|
101
|
+
el: SVGGradientElement;
|
|
102
|
+
id: string;
|
|
103
|
+
cfg: {
|
|
104
|
+
[key: string]: any;
|
|
105
|
+
} = {};
|
|
106
|
+
|
|
107
|
+
constructor(cfg) {
|
|
108
|
+
let el = null;
|
|
109
|
+
const id = uniqueId('gradient_');
|
|
110
|
+
if (cfg.toLowerCase()[0] === 'l') {
|
|
111
|
+
el = createSVGElement('linearGradient');
|
|
112
|
+
parseLineGradient(cfg, el);
|
|
113
|
+
} else {
|
|
114
|
+
el = createSVGElement('radialGradient');
|
|
115
|
+
parseRadialGradient(cfg, el);
|
|
116
|
+
}
|
|
117
|
+
el.setAttribute('id', id);
|
|
118
|
+
this.el = el;
|
|
119
|
+
this.id = id;
|
|
120
|
+
this.cfg = cfg;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
match(type, attr) {
|
|
125
|
+
return this.cfg === attr;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export default Gradient;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview defs
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { uniqueId } from '@antv/util';
|
|
7
|
+
import Gradient from './gradient';
|
|
8
|
+
import Shadow from './shadow';
|
|
9
|
+
import Arrow from './arrow';
|
|
10
|
+
import Clip from './clip';
|
|
11
|
+
import Pattern from './pattern';
|
|
12
|
+
import { createSVGElement } from '../util/dom';
|
|
13
|
+
|
|
14
|
+
class Defs {
|
|
15
|
+
id: string;
|
|
16
|
+
defaultArrow: {};
|
|
17
|
+
children: any[];
|
|
18
|
+
el: SVGDefsElement;
|
|
19
|
+
canvas: SVGSVGElement;
|
|
20
|
+
|
|
21
|
+
constructor(canvas) {
|
|
22
|
+
const el = createSVGElement('defs') as SVGDefsElement;
|
|
23
|
+
const id = uniqueId('defs_');
|
|
24
|
+
el.id = id;
|
|
25
|
+
canvas.appendChild(el);
|
|
26
|
+
this.children = [];
|
|
27
|
+
this.defaultArrow = {};
|
|
28
|
+
this.el = el;
|
|
29
|
+
this.canvas = canvas;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
find(type, attr) {
|
|
33
|
+
const children = this.children;
|
|
34
|
+
let result = null;
|
|
35
|
+
for (let i = 0; i < children.length; i++) {
|
|
36
|
+
if (children[i].match(type, attr)) {
|
|
37
|
+
result = children[i].id;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
findById(id) {
|
|
45
|
+
const children = this.children;
|
|
46
|
+
let flag = null;
|
|
47
|
+
for (let i = 0; i < children.length; i++) {
|
|
48
|
+
if (children[i].id === id) {
|
|
49
|
+
flag = children[i];
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return flag;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
add(item) {
|
|
57
|
+
this.children.push(item);
|
|
58
|
+
item.canvas = this.canvas;
|
|
59
|
+
item.parent = this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getDefaultArrow(attrs, name) {
|
|
63
|
+
const stroke = attrs.stroke || attrs.strokeStyle;
|
|
64
|
+
if (this.defaultArrow[stroke]) {
|
|
65
|
+
return this.defaultArrow[stroke].id;
|
|
66
|
+
}
|
|
67
|
+
const arrow = new Arrow(attrs, name);
|
|
68
|
+
this.defaultArrow[stroke] = arrow;
|
|
69
|
+
this.el.appendChild(arrow.el);
|
|
70
|
+
this.add(arrow);
|
|
71
|
+
return arrow.id;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
addGradient(cfg) {
|
|
75
|
+
const gradient = new Gradient(cfg);
|
|
76
|
+
this.el.appendChild(gradient.el);
|
|
77
|
+
this.add(gradient);
|
|
78
|
+
return gradient.id;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addArrow(attrs, name) {
|
|
82
|
+
const arrow = new Arrow(attrs, name);
|
|
83
|
+
this.el.appendChild(arrow.el);
|
|
84
|
+
this.add(arrow);
|
|
85
|
+
return arrow.id;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
addShadow(cfg) {
|
|
89
|
+
const shadow = new Shadow(cfg);
|
|
90
|
+
this.el.appendChild(shadow.el);
|
|
91
|
+
this.add(shadow);
|
|
92
|
+
return shadow.id;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
addPattern(cfg) {
|
|
96
|
+
const pattern = new Pattern(cfg);
|
|
97
|
+
this.el.appendChild(pattern.el);
|
|
98
|
+
this.add(pattern);
|
|
99
|
+
return pattern.id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
addClip(cfg) {
|
|
103
|
+
const clip = new Clip(cfg);
|
|
104
|
+
this.el.appendChild(clip.el);
|
|
105
|
+
this.add(clip);
|
|
106
|
+
return clip.id;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default Defs;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview pattern
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { uniqueId } from '@antv/util';
|
|
7
|
+
import { createSVGElement } from '../util/dom';
|
|
8
|
+
|
|
9
|
+
const regexPR = /^p\s*\(\s*([axyn])\s*\)\s*(.*)/i;
|
|
10
|
+
|
|
11
|
+
class Pattern {
|
|
12
|
+
el: SVGPatternElement;
|
|
13
|
+
id: string;
|
|
14
|
+
cfg: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
} = {};
|
|
17
|
+
|
|
18
|
+
constructor(cfg) {
|
|
19
|
+
const el = createSVGElement('pattern') as SVGPatternElement;
|
|
20
|
+
el.setAttribute('patternUnits', 'userSpaceOnUse');
|
|
21
|
+
const child = createSVGElement('image');
|
|
22
|
+
el.appendChild(child);
|
|
23
|
+
const id = uniqueId('pattern_');
|
|
24
|
+
el.id = id;
|
|
25
|
+
this.el = el;
|
|
26
|
+
this.id = id;
|
|
27
|
+
this.cfg = cfg;
|
|
28
|
+
const arr = regexPR.exec(cfg);
|
|
29
|
+
const source = arr[2];
|
|
30
|
+
child.setAttribute('href', source);
|
|
31
|
+
const img = new Image();
|
|
32
|
+
if (!source.match(/^data:/i)) {
|
|
33
|
+
img.crossOrigin = 'Anonymous';
|
|
34
|
+
}
|
|
35
|
+
img.src = source;
|
|
36
|
+
function onload() {
|
|
37
|
+
el.setAttribute('width', `${img.width}`);
|
|
38
|
+
el.setAttribute('height', `${img.height}`);
|
|
39
|
+
}
|
|
40
|
+
if (img.complete) {
|
|
41
|
+
onload();
|
|
42
|
+
} else {
|
|
43
|
+
img.onload = onload;
|
|
44
|
+
// Fix onload() bug in IE9
|
|
45
|
+
img.src = img.src;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
match(type, attr) {
|
|
52
|
+
return this.cfg === attr;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default Pattern;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview shadow
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each, uniqueId } from '@antv/util';
|
|
7
|
+
import { createSVGElement } from '../util/dom';
|
|
8
|
+
|
|
9
|
+
const ATTR_MAP = {
|
|
10
|
+
shadowColor: 'color',
|
|
11
|
+
shadowOpacity: 'opacity',
|
|
12
|
+
shadowBlur: 'blur',
|
|
13
|
+
shadowOffsetX: 'dx',
|
|
14
|
+
shadowOffsetY: 'dy',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const SHADOW_DIMENSION = {
|
|
18
|
+
x: '-40%',
|
|
19
|
+
y: '-40%',
|
|
20
|
+
width: '200%',
|
|
21
|
+
height: '200%',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
class Shadow {
|
|
25
|
+
type: string = 'filter';
|
|
26
|
+
id: string;
|
|
27
|
+
el: SVGFilterElement;
|
|
28
|
+
cfg: {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
} = {};
|
|
31
|
+
|
|
32
|
+
constructor(cfg) {
|
|
33
|
+
this.type = 'filter';
|
|
34
|
+
const el = createSVGElement('filter') as SVGFilterElement;
|
|
35
|
+
// expand the filter region to fill in shadows
|
|
36
|
+
each(SHADOW_DIMENSION, (v, k) => {
|
|
37
|
+
el.setAttribute(k, v);
|
|
38
|
+
});
|
|
39
|
+
this.el = el;
|
|
40
|
+
this.id = uniqueId('filter_');
|
|
41
|
+
this.el.id = this.id;
|
|
42
|
+
this.cfg = cfg;
|
|
43
|
+
this._parseShadow(cfg, el);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
match(type, cfg) {
|
|
48
|
+
if (this.type !== type) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
let flag = true;
|
|
52
|
+
const config = this.cfg;
|
|
53
|
+
each(Object.keys(config), (attr) => {
|
|
54
|
+
if (config[attr] !== cfg[attr]) {
|
|
55
|
+
flag = false;
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return flag;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
update(name, value) {
|
|
63
|
+
const config = this.cfg;
|
|
64
|
+
config[ATTR_MAP[name]] = value;
|
|
65
|
+
this._parseShadow(config, this.el);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_parseShadow(config, el) {
|
|
70
|
+
const child = `<feDropShadow
|
|
71
|
+
dx="${config.dx || 0}"
|
|
72
|
+
dy="${config.dy || 0}"
|
|
73
|
+
stdDeviation="${config.blur ? config.blur / 10 : 0}"
|
|
74
|
+
flood-color="${config.color ? config.color : '#000'}"
|
|
75
|
+
flood-opacity="${config.opacity ? config.opacity : 1}"
|
|
76
|
+
/>`;
|
|
77
|
+
el.innerHTML = child;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default Shadow;
|