@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/group.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AbstractGroup } from '@antv/g-base';
|
|
2
|
+
import { ChangeType } from '@antv/g-base';
|
|
3
|
+
import { each } from '@antv/util';
|
|
4
|
+
import { IElement, IGroup } from './interfaces';
|
|
5
|
+
import * as Shape from './shape';
|
|
6
|
+
import Defs from './defs';
|
|
7
|
+
import { drawChildren, refreshElement } from './util/draw';
|
|
8
|
+
import { setClip, setTransform } from './util/svg';
|
|
9
|
+
import { SVG_ATTR_MAP } from './constant';
|
|
10
|
+
import { createSVGElement } from './util/dom';
|
|
11
|
+
|
|
12
|
+
class Group extends AbstractGroup {
|
|
13
|
+
// SVG 中分组对应实体标签 <g>
|
|
14
|
+
isEntityGroup() {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
createDom() {
|
|
19
|
+
const element = createSVGElement('g');
|
|
20
|
+
this.set('el', element);
|
|
21
|
+
const parent = this.getParent();
|
|
22
|
+
if (parent) {
|
|
23
|
+
let parentNode = parent.get('el');
|
|
24
|
+
if (parentNode) {
|
|
25
|
+
parentNode.appendChild(element);
|
|
26
|
+
} else {
|
|
27
|
+
// parentNode maybe null for group
|
|
28
|
+
parentNode = (parent as IGroup).createDom();
|
|
29
|
+
parent.set('el', parentNode);
|
|
30
|
+
parentNode.appendChild(element);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 覆盖基类的 afterAttrsChange 方法
|
|
37
|
+
afterAttrsChange(targetAttrs) {
|
|
38
|
+
super.afterAttrsChange(targetAttrs);
|
|
39
|
+
const canvas = this.get('canvas');
|
|
40
|
+
// 只有挂载到画布下,才对元素进行实际渲染
|
|
41
|
+
if (canvas && canvas.get('autoDraw')) {
|
|
42
|
+
const context = canvas.get('context');
|
|
43
|
+
this.createPath(context, targetAttrs);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 一些方法调用会引起画布变化
|
|
49
|
+
* @param {ChangeType} changeType 改变的类型
|
|
50
|
+
*/
|
|
51
|
+
onCanvasChange(changeType: ChangeType) {
|
|
52
|
+
refreshElement(this, changeType);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getShapeBase() {
|
|
56
|
+
return Shape;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getGroupBase() {
|
|
60
|
+
return Group;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
draw(context: Defs) {
|
|
64
|
+
const children = this.getChildren() as IElement[];
|
|
65
|
+
const el = this.get('el');
|
|
66
|
+
if (this.get('destroyed')) {
|
|
67
|
+
if (el) {
|
|
68
|
+
el.parentNode.removeChild(el);
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
if (!el) {
|
|
72
|
+
this.createDom();
|
|
73
|
+
}
|
|
74
|
+
setClip(this, context);
|
|
75
|
+
this.createPath(context);
|
|
76
|
+
if (children.length) {
|
|
77
|
+
drawChildren(context, children);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 绘制分组的路径
|
|
84
|
+
* @param {Defs} context 上下文
|
|
85
|
+
* @param {ShapeAttrs} targetAttrs 渲染的目标属性
|
|
86
|
+
*/
|
|
87
|
+
createPath(context: Defs, targetAttrs?) {
|
|
88
|
+
const attrs = this.attr();
|
|
89
|
+
const el = this.get('el');
|
|
90
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
91
|
+
if (SVG_ATTR_MAP[attr]) {
|
|
92
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
setTransform(this);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default Group;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as Shape from './shape';
|
|
2
|
+
|
|
3
|
+
export * from '@antv/g-base';
|
|
4
|
+
export { IElement, IGroup, IShape } from './interfaces';
|
|
5
|
+
export { default as Canvas } from './canvas';
|
|
6
|
+
export { default as Group } from './group';
|
|
7
|
+
export { Shape };
|
|
8
|
+
|
|
9
|
+
export const version = '0.5.6';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IElement as IBaseElement, IGroup as IBaseGroup, IShape as IBaseShape } from '@antv/g-base';
|
|
2
|
+
import Defs from './defs';
|
|
3
|
+
|
|
4
|
+
// 导出 g-base 中的 interfaces
|
|
5
|
+
export * from '@antv/g-base';
|
|
6
|
+
|
|
7
|
+
export interface IElement extends IBaseElement {
|
|
8
|
+
/**
|
|
9
|
+
* 裁剪和绘制图形元素
|
|
10
|
+
* @param {Defs} context 上下文
|
|
11
|
+
*/
|
|
12
|
+
draw(context: Defs, targetAttrs?);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface IGroup extends IBaseGroup {
|
|
16
|
+
/**
|
|
17
|
+
* 创建分组容器,对应 <g> 元素
|
|
18
|
+
* @return {SVGGElement} 分组容器
|
|
19
|
+
*/
|
|
20
|
+
createDom(): SVGGElement;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface IShape extends IBaseShape {
|
|
24
|
+
type: string;
|
|
25
|
+
canFill: boolean;
|
|
26
|
+
canStroke: boolean;
|
|
27
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { AbstractShape } from '@antv/g-base';
|
|
2
|
+
import { ShapeAttrs, ChangeType, BBox } from '@antv/g-base';
|
|
3
|
+
import { IShape } from '../interfaces';
|
|
4
|
+
import Defs from '../defs';
|
|
5
|
+
import { setShadow, setTransform, setClip } from '../util/svg';
|
|
6
|
+
import { createDom } from '../util/dom';
|
|
7
|
+
import { refreshElement } from '../util/draw';
|
|
8
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
9
|
+
import * as Shape from './index';
|
|
10
|
+
import Group from '../group';
|
|
11
|
+
import { getBBoxMethod } from '@antv/g-base';
|
|
12
|
+
|
|
13
|
+
class ShapeBase extends AbstractShape implements IShape {
|
|
14
|
+
type: string = 'svg';
|
|
15
|
+
canFill: boolean = false;
|
|
16
|
+
canStroke: boolean = false;
|
|
17
|
+
|
|
18
|
+
getDefaultAttrs() {
|
|
19
|
+
const attrs = super.getDefaultAttrs();
|
|
20
|
+
// 设置默认值
|
|
21
|
+
return {
|
|
22
|
+
...attrs,
|
|
23
|
+
lineWidth: 1,
|
|
24
|
+
lineAppendWidth: 0,
|
|
25
|
+
strokeOpacity: 1,
|
|
26
|
+
fillOpacity: 1,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 覆盖基类的 afterAttrsChange 方法
|
|
31
|
+
afterAttrsChange(targetAttrs: ShapeAttrs) {
|
|
32
|
+
super.afterAttrsChange(targetAttrs);
|
|
33
|
+
const canvas = this.get('canvas');
|
|
34
|
+
// 只有挂载到画布下,才对元素进行实际渲染
|
|
35
|
+
if (canvas && canvas.get('autoDraw')) {
|
|
36
|
+
const context = canvas.get('context');
|
|
37
|
+
this.draw(context, targetAttrs);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getShapeBase() {
|
|
42
|
+
return Shape;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getGroupBase() {
|
|
46
|
+
return Group;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 一些方法调用会引起画布变化
|
|
51
|
+
* @param {ChangeType} changeType 改变的类型
|
|
52
|
+
*/
|
|
53
|
+
onCanvasChange(changeType: ChangeType) {
|
|
54
|
+
refreshElement(this, changeType);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
calculateBBox(): BBox {
|
|
58
|
+
const el = this.get('el');
|
|
59
|
+
let bbox = null;
|
|
60
|
+
// 包围盒计算依赖于绘制,如果还没有生成对应的 Dom 元素,则包围盒的长宽均为 0
|
|
61
|
+
if (el) {
|
|
62
|
+
bbox = el.getBBox();
|
|
63
|
+
} else {
|
|
64
|
+
const bboxMethod = getBBoxMethod(this.get('type'));
|
|
65
|
+
if (bboxMethod) {
|
|
66
|
+
bbox = bboxMethod(this);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (bbox) {
|
|
70
|
+
const { x, y, width, height } = bbox;
|
|
71
|
+
const lineWidth = this.getHitLineWidth();
|
|
72
|
+
const halfWidth = lineWidth / 2;
|
|
73
|
+
const minX = x - halfWidth;
|
|
74
|
+
const minY = y - halfWidth;
|
|
75
|
+
const maxX = x + width + halfWidth;
|
|
76
|
+
const maxY = y + height + halfWidth;
|
|
77
|
+
return {
|
|
78
|
+
x: minX,
|
|
79
|
+
y: minY,
|
|
80
|
+
minX,
|
|
81
|
+
minY,
|
|
82
|
+
maxX,
|
|
83
|
+
maxY,
|
|
84
|
+
width: width + lineWidth,
|
|
85
|
+
height: height + lineWidth,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
x: 0,
|
|
90
|
+
y: 0,
|
|
91
|
+
minX: 0,
|
|
92
|
+
minY: 0,
|
|
93
|
+
maxX: 0,
|
|
94
|
+
maxY: 0,
|
|
95
|
+
width: 0,
|
|
96
|
+
height: 0,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
isFill() {
|
|
101
|
+
const { fill, fillStyle } = this.attr();
|
|
102
|
+
return (fill || fillStyle || this.isClipShape()) && this.canFill;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
isStroke() {
|
|
106
|
+
const { stroke, strokeStyle } = this.attr();
|
|
107
|
+
return (stroke || strokeStyle) && this.canStroke;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
draw(context, targetAttrs) {
|
|
111
|
+
const el = this.get('el');
|
|
112
|
+
if (this.get('destroyed')) {
|
|
113
|
+
if (el) {
|
|
114
|
+
el.parentNode.removeChild(el);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
if (!el) {
|
|
118
|
+
createDom(this);
|
|
119
|
+
}
|
|
120
|
+
setClip(this, context);
|
|
121
|
+
this.createPath(context, targetAttrs);
|
|
122
|
+
this.shadow(context, targetAttrs);
|
|
123
|
+
this.strokeAndFill(context, targetAttrs);
|
|
124
|
+
this.transform(targetAttrs);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @protected
|
|
130
|
+
* 绘制图形的路径
|
|
131
|
+
* @param {Defs} context 上下文
|
|
132
|
+
* @param {ShapeAttrs} targetAttrs 渲染的目标属性
|
|
133
|
+
*/
|
|
134
|
+
createPath(context: Defs, targetAttrs?: ShapeAttrs) {}
|
|
135
|
+
|
|
136
|
+
// stroke and fill
|
|
137
|
+
strokeAndFill(context, targetAttrs?) {
|
|
138
|
+
const attrs = targetAttrs || this.attr();
|
|
139
|
+
const { fill, fillStyle, stroke, strokeStyle, fillOpacity, strokeOpacity, lineWidth } = attrs;
|
|
140
|
+
const el = this.get('el');
|
|
141
|
+
|
|
142
|
+
if (this.canFill) {
|
|
143
|
+
// 初次渲染和更新渲染的逻辑有所不同: 初次渲染值为空时,需要设置为 none,否则就会是黑色,而更新渲染则不需要
|
|
144
|
+
if (!targetAttrs) {
|
|
145
|
+
this._setColor(context, 'fill', fill || fillStyle);
|
|
146
|
+
} else if ('fill' in attrs) {
|
|
147
|
+
this._setColor(context, 'fill', fill);
|
|
148
|
+
} else if ('fillStyle' in attrs) {
|
|
149
|
+
// compatible with fillStyle
|
|
150
|
+
this._setColor(context, 'fill', fillStyle);
|
|
151
|
+
}
|
|
152
|
+
if (fillOpacity) {
|
|
153
|
+
el.setAttribute(SVG_ATTR_MAP['fillOpacity'], fillOpacity);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (this.canStroke && lineWidth > 0) {
|
|
158
|
+
if (!targetAttrs) {
|
|
159
|
+
this._setColor(context, 'stroke', stroke || strokeStyle);
|
|
160
|
+
} else if ('stroke' in attrs) {
|
|
161
|
+
this._setColor(context, 'stroke', stroke);
|
|
162
|
+
} else if ('strokeStyle' in attrs) {
|
|
163
|
+
// compatible with strokeStyle
|
|
164
|
+
this._setColor(context, 'stroke', strokeStyle);
|
|
165
|
+
}
|
|
166
|
+
if (strokeOpacity) {
|
|
167
|
+
el.setAttribute(SVG_ATTR_MAP['strokeOpacity'], strokeOpacity);
|
|
168
|
+
}
|
|
169
|
+
if (lineWidth) {
|
|
170
|
+
el.setAttribute(SVG_ATTR_MAP['lineWidth'], lineWidth);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
_setColor(context, attr, value) {
|
|
176
|
+
const el = this.get('el');
|
|
177
|
+
if (!value) {
|
|
178
|
+
// need to set `none` to avoid default value
|
|
179
|
+
el.setAttribute(SVG_ATTR_MAP[attr], 'none');
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
value = value.trim();
|
|
183
|
+
if (/^[r,R,L,l]{1}[\s]*\(/.test(value)) {
|
|
184
|
+
let id = context.find('gradient', value);
|
|
185
|
+
if (!id) {
|
|
186
|
+
id = context.addGradient(value);
|
|
187
|
+
}
|
|
188
|
+
el.setAttribute(SVG_ATTR_MAP[attr], `url(#${id})`);
|
|
189
|
+
} else if (/^[p,P]{1}[\s]*\(/.test(value)) {
|
|
190
|
+
let id = context.find('pattern', value);
|
|
191
|
+
if (!id) {
|
|
192
|
+
id = context.addPattern(value);
|
|
193
|
+
}
|
|
194
|
+
el.setAttribute(SVG_ATTR_MAP[attr], `url(#${id})`);
|
|
195
|
+
} else {
|
|
196
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
shadow(context, targetAttrs?) {
|
|
201
|
+
const attrs = this.attr();
|
|
202
|
+
const { shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor } = targetAttrs || attrs;
|
|
203
|
+
if (shadowOffsetX || shadowOffsetY || shadowBlur || shadowColor) {
|
|
204
|
+
setShadow(this, context);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
transform(targetAttrs?) {
|
|
209
|
+
const attrs = this.attr();
|
|
210
|
+
const { matrix } = targetAttrs || attrs;
|
|
211
|
+
if (matrix) {
|
|
212
|
+
setTransform(this);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
isInShape(refX: number, refY: number): boolean {
|
|
217
|
+
return this.isPointInPath(refX, refY);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
isPointInPath(refX: number, refY: number): boolean {
|
|
221
|
+
const el = this.get('el');
|
|
222
|
+
const canvas = this.get('canvas');
|
|
223
|
+
const bbox = canvas.get('el').getBoundingClientRect();
|
|
224
|
+
const clientX = refX + bbox.left;
|
|
225
|
+
const clientY = refY + bbox.top;
|
|
226
|
+
const element = document.elementFromPoint(clientX, clientY);
|
|
227
|
+
if (element && element.isEqualNode(el)) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 获取线拾取的宽度
|
|
235
|
+
* @returns {number} 线的拾取宽度
|
|
236
|
+
*/
|
|
237
|
+
getHitLineWidth() {
|
|
238
|
+
const { lineWidth, lineAppendWidth } = this.attrs;
|
|
239
|
+
if (this.isStroke()) {
|
|
240
|
+
return lineWidth + lineAppendWidth;
|
|
241
|
+
}
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export default ShapeBase;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview circle
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Circle extends ShapeBase {
|
|
11
|
+
type: string = 'circle';
|
|
12
|
+
canFill: boolean = true;
|
|
13
|
+
canStroke: boolean = true;
|
|
14
|
+
|
|
15
|
+
getDefaultAttrs() {
|
|
16
|
+
const attrs = super.getDefaultAttrs();
|
|
17
|
+
return {
|
|
18
|
+
...attrs,
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
r: 0,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
createPath(context, targetAttrs) {
|
|
26
|
+
const attrs = this.attr();
|
|
27
|
+
const el = this.get('el');
|
|
28
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
29
|
+
// 圆和椭圆的点坐标属性不是 x, y,而是 cx, cy
|
|
30
|
+
if (attr === 'x' || attr === 'y') {
|
|
31
|
+
el.setAttribute(`c${attr}`, value);
|
|
32
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
33
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default Circle;
|
package/src/shape/dom.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview dom
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Dom extends ShapeBase {
|
|
11
|
+
type: string = 'dom';
|
|
12
|
+
canFill: boolean = false;
|
|
13
|
+
canStroke: boolean = false;
|
|
14
|
+
|
|
15
|
+
createPath(context, targetAttrs) {
|
|
16
|
+
const attrs = this.attr();
|
|
17
|
+
const el = this.get('el');
|
|
18
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
19
|
+
if (SVG_ATTR_MAP[attr]) {
|
|
20
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (typeof attrs['html'] === 'function') {
|
|
24
|
+
const element = attrs['html'].call(this, attrs);
|
|
25
|
+
if (element instanceof Element || element instanceof HTMLDocument) {
|
|
26
|
+
const children = el.childNodes;
|
|
27
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
28
|
+
el.removeChild(children[i]);
|
|
29
|
+
}
|
|
30
|
+
el.appendChild(element); // append to el if it's an element
|
|
31
|
+
} else {
|
|
32
|
+
el.innerHTML = element; // set innerHTML
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
el.innerHTML = attrs['html']; // set innerHTML
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default Dom;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview ellipse
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Ellipse extends ShapeBase {
|
|
11
|
+
type: string = 'ellipse';
|
|
12
|
+
canFill: boolean = true;
|
|
13
|
+
canStroke: boolean = true;
|
|
14
|
+
|
|
15
|
+
getDefaultAttrs() {
|
|
16
|
+
const attrs = super.getDefaultAttrs();
|
|
17
|
+
return {
|
|
18
|
+
...attrs,
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
rx: 0,
|
|
22
|
+
ry: 0,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
createPath(context, targetAttrs) {
|
|
27
|
+
const attrs = this.attr();
|
|
28
|
+
const el = this.get('el');
|
|
29
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
30
|
+
// 圆和椭圆的点坐标属性不是 x, y,而是 cx, cy
|
|
31
|
+
if (attr === 'x' || attr === 'y') {
|
|
32
|
+
el.setAttribute(`c${attr}`, value);
|
|
33
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
34
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default Ellipse;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview image
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { each, isString } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Image extends ShapeBase {
|
|
11
|
+
type: string = 'image';
|
|
12
|
+
canFill: boolean = false;
|
|
13
|
+
canStroke: boolean = false;
|
|
14
|
+
|
|
15
|
+
getDefaultAttrs() {
|
|
16
|
+
const attrs = super.getDefaultAttrs();
|
|
17
|
+
return {
|
|
18
|
+
...attrs,
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
width: 0,
|
|
22
|
+
height: 0,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
createPath(context, targetAttrs) {
|
|
27
|
+
const attrs = this.attr();
|
|
28
|
+
const el = this.get('el');
|
|
29
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
30
|
+
if (attr === 'img') {
|
|
31
|
+
this._setImage(attrs.img);
|
|
32
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
33
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
setAttr(name: string, value: any) {
|
|
39
|
+
this.attrs[name] = value;
|
|
40
|
+
if (name === 'img') {
|
|
41
|
+
this._setImage(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_setImage(img) {
|
|
46
|
+
const attrs = this.attr();
|
|
47
|
+
const el = this.get('el');
|
|
48
|
+
if (isString(img)) {
|
|
49
|
+
el.setAttribute('href', img);
|
|
50
|
+
} else if (img instanceof (window as any).Image) {
|
|
51
|
+
if (!attrs.width) {
|
|
52
|
+
el.setAttribute('width', img.width);
|
|
53
|
+
this.attr('width', img.width);
|
|
54
|
+
}
|
|
55
|
+
if (!attrs.height) {
|
|
56
|
+
el.setAttribute('height', img.height);
|
|
57
|
+
this.attr('height', img.height);
|
|
58
|
+
}
|
|
59
|
+
el.setAttribute('href', img.src);
|
|
60
|
+
} else if (img instanceof HTMLElement && isString(img.nodeName) && img.nodeName.toUpperCase() === 'CANVAS') {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
el.setAttribute('href', img.toDataURL());
|
|
63
|
+
} else if (img instanceof ImageData) {
|
|
64
|
+
const canvas = document.createElement('canvas');
|
|
65
|
+
canvas.setAttribute('width', `${img.width}`);
|
|
66
|
+
canvas.setAttribute('height', `${img.height}`);
|
|
67
|
+
canvas.getContext('2d').putImageData(img, 0, 0);
|
|
68
|
+
if (!attrs.width) {
|
|
69
|
+
el.setAttribute('width', `${img.width}`);
|
|
70
|
+
this.attr('width', img.width);
|
|
71
|
+
}
|
|
72
|
+
if (!attrs.height) {
|
|
73
|
+
el.setAttribute('height', `${img.height}`);
|
|
74
|
+
this.attr('height', img.height);
|
|
75
|
+
}
|
|
76
|
+
el.setAttribute('href', canvas.toDataURL());
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default Image;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { default as Base } from './base';
|
|
2
|
+
export { default as Circle } from './circle';
|
|
3
|
+
export { default as Dom } from './dom';
|
|
4
|
+
export { default as Ellipse } from './ellipse';
|
|
5
|
+
export { default as Image } from './image';
|
|
6
|
+
export { default as Line } from './line';
|
|
7
|
+
export { default as Marker } from './marker';
|
|
8
|
+
export { default as Path } from './path';
|
|
9
|
+
export { default as Polygon } from './polygon';
|
|
10
|
+
export { default as Polyline } from './polyline';
|
|
11
|
+
export { default as Rect } from './rect';
|
|
12
|
+
export { default as Text } from './text';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview line
|
|
3
|
+
* @author dengfuping_develop@163.com
|
|
4
|
+
*/
|
|
5
|
+
import { Line as LineUtil } from '@antv/g-math';
|
|
6
|
+
import { each, isObject } from '@antv/util';
|
|
7
|
+
import { SVG_ATTR_MAP } from '../constant';
|
|
8
|
+
import ShapeBase from './base';
|
|
9
|
+
|
|
10
|
+
class Line extends ShapeBase {
|
|
11
|
+
type: string = 'line';
|
|
12
|
+
canFill: boolean = false;
|
|
13
|
+
canStroke: boolean = true;
|
|
14
|
+
|
|
15
|
+
getDefaultAttrs() {
|
|
16
|
+
const attrs = super.getDefaultAttrs();
|
|
17
|
+
return {
|
|
18
|
+
...attrs,
|
|
19
|
+
x1: 0,
|
|
20
|
+
y1: 0,
|
|
21
|
+
x2: 0,
|
|
22
|
+
y2: 0,
|
|
23
|
+
startArrow: false,
|
|
24
|
+
endArrow: false,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
createPath(context, targetAttrs) {
|
|
29
|
+
const attrs = this.attr();
|
|
30
|
+
const el = this.get('el');
|
|
31
|
+
each(targetAttrs || attrs, (value, attr) => {
|
|
32
|
+
if (attr === 'startArrow' || attr === 'endArrow') {
|
|
33
|
+
if (value) {
|
|
34
|
+
const id = isObject(value)
|
|
35
|
+
? context.addArrow(attrs, SVG_ATTR_MAP[attr])
|
|
36
|
+
: context.getDefaultArrow(attrs, SVG_ATTR_MAP[attr]);
|
|
37
|
+
el.setAttribute(SVG_ATTR_MAP[attr], `url(#${id})`);
|
|
38
|
+
} else {
|
|
39
|
+
el.removeAttribute(SVG_ATTR_MAP[attr]);
|
|
40
|
+
}
|
|
41
|
+
} else if (SVG_ATTR_MAP[attr]) {
|
|
42
|
+
el.setAttribute(SVG_ATTR_MAP[attr], value);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Use math calculation to get length of line
|
|
49
|
+
* @return {number} length
|
|
50
|
+
*/
|
|
51
|
+
getTotalLength() {
|
|
52
|
+
const { x1, y1, x2, y2 } = this.attr();
|
|
53
|
+
return LineUtil.length(x1, y1, x2, y2);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Use math calculation to get point according to ratio as same sa Canvas version
|
|
58
|
+
* @param {number} ratio
|
|
59
|
+
* @return {Point} point
|
|
60
|
+
*/
|
|
61
|
+
getPoint(ratio: number) {
|
|
62
|
+
const { x1, y1, x2, y2 } = this.attr();
|
|
63
|
+
return LineUtil.pointAt(x1, y1, x2, y2, ratio);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default Line;
|