@idraw/renderer 0.4.0-beta.4 → 0.4.0-beta.40
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/README.md +2 -2
- package/dist/esm/calculator.d.ts +43 -0
- package/dist/esm/calculator.js +168 -0
- package/dist/esm/draw/box.d.ts +17 -3
- package/dist/esm/draw/box.js +102 -49
- package/dist/esm/draw/circle.js +55 -31
- package/dist/esm/draw/elements.js +5 -2
- package/dist/esm/draw/global.d.ts +2 -0
- package/dist/esm/draw/global.js +9 -0
- package/dist/esm/draw/group.js +20 -8
- package/dist/esm/draw/html.js +7 -7
- package/dist/esm/draw/image.js +50 -9
- package/dist/esm/draw/index.d.ts +3 -0
- package/dist/esm/draw/index.js +3 -0
- package/dist/esm/draw/layout.d.ts +2 -0
- package/dist/esm/draw/layout.js +44 -0
- package/dist/esm/draw/path.js +48 -8
- package/dist/esm/draw/rect.js +4 -3
- package/dist/esm/draw/svg.js +7 -7
- package/dist/esm/draw/text.js +34 -84
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.js +42 -7
- package/dist/esm/loader.d.ts +2 -0
- package/dist/esm/loader.js +50 -26
- package/dist/esm/view-visible/index.d.ts +22 -0
- package/dist/esm/view-visible/index.js +63 -0
- package/dist/esm/virtual-flat/index.d.ts +7 -0
- package/dist/esm/virtual-flat/index.js +45 -0
- package/dist/esm/virtual-flat/text.d.ts +2 -0
- package/dist/esm/virtual-flat/text.js +151 -0
- package/dist/index.global.js +1474 -312
- package/dist/index.global.min.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -64,5 +64,5 @@ renderer.on('drawFrameComplete', (e) => {
|
|
|
64
64
|
|
|
65
65
|
## Documents
|
|
66
66
|
|
|
67
|
-
- [Documents](https://
|
|
68
|
-
- [Online Playground](https://
|
|
67
|
+
- [Documents](https://idrawjs.com/docs/en/) | [中文文档](https://idrawjs.com/docs/zh/)
|
|
68
|
+
- [Online Playground](https://idrawjs.com/playground/) | [在线API示例](https://idrawjs.com/playground/)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Point, Data, Element, ElementType, ViewCalculator, ViewCalculatorOptions, ViewScaleInfo, ViewSizeInfo, ViewRectInfo, ModifyOptions, VirtualFlatItem } from '@idraw/types';
|
|
2
|
+
export declare class Calculator implements ViewCalculator {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(opts: ViewCalculatorOptions);
|
|
5
|
+
toGridNum(num: number, opts?: {
|
|
6
|
+
ignore?: boolean;
|
|
7
|
+
}): number;
|
|
8
|
+
destroy(): void;
|
|
9
|
+
needRender(elem: Element<ElementType>): boolean;
|
|
10
|
+
getPointElement(p: Point, opts: {
|
|
11
|
+
data: Data;
|
|
12
|
+
viewScaleInfo: ViewScaleInfo;
|
|
13
|
+
viewSizeInfo: ViewSizeInfo;
|
|
14
|
+
}): {
|
|
15
|
+
index: number;
|
|
16
|
+
element: null | Element<ElementType>;
|
|
17
|
+
groupQueueIndex: number;
|
|
18
|
+
};
|
|
19
|
+
resetVirtualFlatItemMap(data: Data, opts: {
|
|
20
|
+
viewScaleInfo: ViewScaleInfo;
|
|
21
|
+
viewSizeInfo: ViewSizeInfo;
|
|
22
|
+
}): void;
|
|
23
|
+
updateVisiableStatus(opts: {
|
|
24
|
+
viewScaleInfo: ViewScaleInfo;
|
|
25
|
+
viewSizeInfo: ViewSizeInfo;
|
|
26
|
+
}): void;
|
|
27
|
+
calcViewRectInfoFromOrigin(uuid: string, opts: {
|
|
28
|
+
checkVisible?: boolean;
|
|
29
|
+
viewScaleInfo: ViewScaleInfo;
|
|
30
|
+
viewSizeInfo: ViewSizeInfo;
|
|
31
|
+
}): ViewRectInfo | null;
|
|
32
|
+
calcViewRectInfoFromRange(uuid: string, opts: {
|
|
33
|
+
checkVisible?: boolean;
|
|
34
|
+
viewScaleInfo: ViewScaleInfo;
|
|
35
|
+
viewSizeInfo: ViewSizeInfo;
|
|
36
|
+
}): ViewRectInfo | null;
|
|
37
|
+
modifyVirtualFlatItemMap(data: Data, opts: {
|
|
38
|
+
modifyOptions: ModifyOptions;
|
|
39
|
+
viewScaleInfo: ViewScaleInfo;
|
|
40
|
+
viewSizeInfo: ViewSizeInfo;
|
|
41
|
+
}): void;
|
|
42
|
+
getVirtualFlatItem(uuid: string): VirtualFlatItem | null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Calculator_opts, _Calculator_store;
|
|
13
|
+
import { is, getViewPointAtElement, Store, calcViewPointSize, findElementFromListByPosition, getGroupQueueByElementPosition, calcElementOriginRectInfo, originRectInfoToRangeRectInfo } from '@idraw/util';
|
|
14
|
+
import { sortElementsViewVisiableInfoMap, updateVirtualFlatItemMapStatus } from './view-visible';
|
|
15
|
+
import { calcVirtualFlatDetail } from './virtual-flat';
|
|
16
|
+
export class Calculator {
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
_Calculator_opts.set(this, void 0);
|
|
19
|
+
_Calculator_store.set(this, void 0);
|
|
20
|
+
__classPrivateFieldSet(this, _Calculator_opts, opts, "f");
|
|
21
|
+
__classPrivateFieldSet(this, _Calculator_store, new Store({
|
|
22
|
+
defaultStorage: {
|
|
23
|
+
virtualFlatItemMap: {},
|
|
24
|
+
visibleCount: 0,
|
|
25
|
+
invisibleCount: 0
|
|
26
|
+
}
|
|
27
|
+
}), "f");
|
|
28
|
+
}
|
|
29
|
+
toGridNum(num, opts) {
|
|
30
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.ignore) === true) {
|
|
31
|
+
return num;
|
|
32
|
+
}
|
|
33
|
+
return Math.round(num);
|
|
34
|
+
}
|
|
35
|
+
destroy() {
|
|
36
|
+
__classPrivateFieldSet(this, _Calculator_opts, null, "f");
|
|
37
|
+
}
|
|
38
|
+
needRender(elem) {
|
|
39
|
+
const virtualFlatItemMap = __classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap');
|
|
40
|
+
const info = virtualFlatItemMap[elem.uuid];
|
|
41
|
+
if (!info) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return info.isVisibleInView;
|
|
45
|
+
}
|
|
46
|
+
getPointElement(p, opts) {
|
|
47
|
+
const context2d = __classPrivateFieldGet(this, _Calculator_opts, "f").tempContext;
|
|
48
|
+
return getViewPointAtElement(p, Object.assign(Object.assign({}, opts), { context2d }));
|
|
49
|
+
}
|
|
50
|
+
resetVirtualFlatItemMap(data, opts) {
|
|
51
|
+
if (data) {
|
|
52
|
+
const { virtualFlatItemMap, invisibleCount, visibleCount } = sortElementsViewVisiableInfoMap(data.elements, Object.assign(Object.assign({}, opts), {
|
|
53
|
+
tempContext: __classPrivateFieldGet(this, _Calculator_opts, "f").tempContext
|
|
54
|
+
}));
|
|
55
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('virtualFlatItemMap', virtualFlatItemMap);
|
|
56
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('invisibleCount', invisibleCount);
|
|
57
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('visibleCount', visibleCount);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
updateVisiableStatus(opts) {
|
|
61
|
+
const { virtualFlatItemMap, invisibleCount, visibleCount } = updateVirtualFlatItemMapStatus(__classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap'), opts);
|
|
62
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('virtualFlatItemMap', virtualFlatItemMap);
|
|
63
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('invisibleCount', invisibleCount);
|
|
64
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('visibleCount', visibleCount);
|
|
65
|
+
}
|
|
66
|
+
calcViewRectInfoFromOrigin(uuid, opts) {
|
|
67
|
+
const infoData = __classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap')[uuid];
|
|
68
|
+
if (!(infoData === null || infoData === void 0 ? void 0 : infoData.originRectInfo)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const { checkVisible, viewScaleInfo, viewSizeInfo } = opts;
|
|
72
|
+
const { center, left, right, bottom, top, topLeft, topRight, bottomLeft, bottomRight } = infoData.originRectInfo;
|
|
73
|
+
if (checkVisible === true && infoData.isVisibleInView === false) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const calcOpts = { viewScaleInfo, viewSizeInfo };
|
|
77
|
+
const viewRectInfo = {
|
|
78
|
+
center: calcViewPointSize(center, calcOpts),
|
|
79
|
+
left: calcViewPointSize(left, calcOpts),
|
|
80
|
+
right: calcViewPointSize(right, calcOpts),
|
|
81
|
+
bottom: calcViewPointSize(bottom, calcOpts),
|
|
82
|
+
top: calcViewPointSize(top, calcOpts),
|
|
83
|
+
topLeft: calcViewPointSize(topLeft, calcOpts),
|
|
84
|
+
topRight: calcViewPointSize(topRight, calcOpts),
|
|
85
|
+
bottomLeft: calcViewPointSize(bottomLeft, calcOpts),
|
|
86
|
+
bottomRight: calcViewPointSize(bottomRight, calcOpts)
|
|
87
|
+
};
|
|
88
|
+
return viewRectInfo;
|
|
89
|
+
}
|
|
90
|
+
calcViewRectInfoFromRange(uuid, opts) {
|
|
91
|
+
const infoData = __classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap')[uuid];
|
|
92
|
+
if (!(infoData === null || infoData === void 0 ? void 0 : infoData.originRectInfo)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const { checkVisible, viewScaleInfo, viewSizeInfo } = opts;
|
|
96
|
+
const { center, left, right, bottom, top, topLeft, topRight, bottomLeft, bottomRight } = infoData.rangeRectInfo;
|
|
97
|
+
if (checkVisible === true && infoData.isVisibleInView === false) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const calcOpts = { viewScaleInfo, viewSizeInfo };
|
|
101
|
+
const viewRectInfo = {
|
|
102
|
+
center: calcViewPointSize(center, calcOpts),
|
|
103
|
+
left: calcViewPointSize(left, calcOpts),
|
|
104
|
+
right: calcViewPointSize(right, calcOpts),
|
|
105
|
+
bottom: calcViewPointSize(bottom, calcOpts),
|
|
106
|
+
top: calcViewPointSize(top, calcOpts),
|
|
107
|
+
topLeft: calcViewPointSize(topLeft, calcOpts),
|
|
108
|
+
topRight: calcViewPointSize(topRight, calcOpts),
|
|
109
|
+
bottomLeft: calcViewPointSize(bottomLeft, calcOpts),
|
|
110
|
+
bottomRight: calcViewPointSize(bottomRight, calcOpts)
|
|
111
|
+
};
|
|
112
|
+
return viewRectInfo;
|
|
113
|
+
}
|
|
114
|
+
modifyVirtualFlatItemMap(data, opts) {
|
|
115
|
+
const { modifyOptions, viewScaleInfo, viewSizeInfo } = opts;
|
|
116
|
+
const { type, content } = modifyOptions;
|
|
117
|
+
const list = data.elements;
|
|
118
|
+
const virtualFlatItemMap = __classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap');
|
|
119
|
+
if (type === 'deleteElement') {
|
|
120
|
+
const { element } = content;
|
|
121
|
+
const uuids = [];
|
|
122
|
+
const _walk = (e) => {
|
|
123
|
+
uuids.push(e.uuid);
|
|
124
|
+
if (e.type === 'group' && Array.isArray(e.detail.children)) {
|
|
125
|
+
e.detail.children.forEach((child) => {
|
|
126
|
+
_walk(child);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
_walk(element);
|
|
131
|
+
uuids.forEach((uuid) => {
|
|
132
|
+
delete virtualFlatItemMap[uuid];
|
|
133
|
+
});
|
|
134
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('virtualFlatItemMap', virtualFlatItemMap);
|
|
135
|
+
}
|
|
136
|
+
else if (type === 'addElement' || type === 'updateElement') {
|
|
137
|
+
const { position } = content;
|
|
138
|
+
const element = findElementFromListByPosition(position, data.elements);
|
|
139
|
+
const groupQueue = getGroupQueueByElementPosition(list, position);
|
|
140
|
+
if (element) {
|
|
141
|
+
if (type === 'updateElement' && element.type === 'group') {
|
|
142
|
+
this.resetVirtualFlatItemMap(data, { viewScaleInfo, viewSizeInfo });
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const originRectInfo = calcElementOriginRectInfo(element, {
|
|
146
|
+
groupQueue: groupQueue || []
|
|
147
|
+
});
|
|
148
|
+
const newVirtualFlatItem = Object.assign({ type: element.type, originRectInfo, rangeRectInfo: is.angle(element.angle) ? originRectInfoToRangeRectInfo(originRectInfo) : originRectInfo, isVisibleInView: true, position: [...position] }, calcVirtualFlatDetail(element, {
|
|
149
|
+
tempContext: __classPrivateFieldGet(this, _Calculator_opts, "f").tempContext
|
|
150
|
+
}));
|
|
151
|
+
virtualFlatItemMap[element.uuid] = newVirtualFlatItem;
|
|
152
|
+
__classPrivateFieldGet(this, _Calculator_store, "f").set('virtualFlatItemMap', virtualFlatItemMap);
|
|
153
|
+
if (type === 'updateElement') {
|
|
154
|
+
this.updateVisiableStatus({ viewScaleInfo, viewSizeInfo });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if (type === 'moveElement') {
|
|
160
|
+
this.resetVirtualFlatItemMap(data, { viewScaleInfo, viewSizeInfo });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
getVirtualFlatItem(uuid) {
|
|
164
|
+
const itemMap = __classPrivateFieldGet(this, _Calculator_store, "f").get('virtualFlatItemMap');
|
|
165
|
+
return itemMap[uuid] || null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
_Calculator_opts = new WeakMap(), _Calculator_store = new WeakMap();
|
package/dist/esm/draw/box.d.ts
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import { ViewContext2D, Element, ElementType, ElementSize, ViewScaleInfo, ViewSizeInfo } from '@idraw/types';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { Calculator } from '../calculator';
|
|
3
|
+
export declare function getOpacity(elem: Element): number;
|
|
4
|
+
export declare function drawBox(ctx: ViewContext2D, viewElem: Element, opts: {
|
|
5
|
+
originElem: Element;
|
|
4
6
|
calcElemSize: ElementSize;
|
|
5
7
|
pattern?: string | CanvasPattern | null;
|
|
6
|
-
renderContent
|
|
8
|
+
renderContent?: () => void;
|
|
9
|
+
viewScaleInfo: ViewScaleInfo;
|
|
10
|
+
viewSizeInfo: ViewSizeInfo;
|
|
11
|
+
parentOpacity: number;
|
|
12
|
+
}): void;
|
|
13
|
+
export declare function drawBoxBackground(ctx: ViewContext2D, viewElem: Element<ElementType>, opts: {
|
|
14
|
+
pattern?: string | CanvasPattern | null;
|
|
15
|
+
viewScaleInfo: ViewScaleInfo;
|
|
16
|
+
viewSizeInfo: ViewSizeInfo;
|
|
17
|
+
}): void;
|
|
18
|
+
export declare function drawBoxBorder(ctx: ViewContext2D, viewElem: Element<ElementType>, opts: {
|
|
19
|
+
originElem: Element;
|
|
7
20
|
viewScaleInfo: ViewScaleInfo;
|
|
8
21
|
viewSizeInfo: ViewSizeInfo;
|
|
22
|
+
calculator?: Calculator;
|
|
9
23
|
}): void;
|
|
10
24
|
export declare function drawBoxShadow(ctx: ViewContext2D, viewElem: Element<ElementType>, opts: {
|
|
11
25
|
viewScaleInfo: ViewScaleInfo;
|
package/dist/esm/draw/box.js
CHANGED
|
@@ -1,31 +1,53 @@
|
|
|
1
1
|
import { istype, isColorStr, generateSVGPath, rotateElement, is, getDefaultElementDetailConfig, calcViewBoxSize } from '@idraw/util';
|
|
2
2
|
import { createColorStyle } from './color';
|
|
3
3
|
const defaultElemConfig = getDefaultElementDetailConfig();
|
|
4
|
+
export function getOpacity(elem) {
|
|
5
|
+
var _a, _b, _c, _d;
|
|
6
|
+
let opacity = 1;
|
|
7
|
+
if (((_a = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _a === void 0 ? void 0 : _a.opacity) !== undefined && ((_b = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _b === void 0 ? void 0 : _b.opacity) >= 0 && ((_c = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _c === void 0 ? void 0 : _c.opacity) <= 1) {
|
|
8
|
+
opacity = (_d = elem === null || elem === void 0 ? void 0 : elem.detail) === null || _d === void 0 ? void 0 : _d.opacity;
|
|
9
|
+
}
|
|
10
|
+
return opacity;
|
|
11
|
+
}
|
|
4
12
|
export function drawBox(ctx, viewElem, opts) {
|
|
5
13
|
const { pattern, renderContent, originElem, calcElemSize, viewScaleInfo, viewSizeInfo } = opts || {};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
const { parentOpacity } = opts;
|
|
15
|
+
const opacity = getOpacity(originElem) * parentOpacity;
|
|
16
|
+
const { clipPath, clipPathStrokeColor, clipPathStrokeWidth } = originElem.detail;
|
|
17
|
+
const mainRender = () => {
|
|
18
|
+
ctx.globalAlpha = opacity;
|
|
19
|
+
drawBoxBackground(ctx, viewElem, { pattern, viewScaleInfo, viewSizeInfo });
|
|
20
|
+
renderContent === null || renderContent === void 0 ? void 0 : renderContent();
|
|
21
|
+
drawBoxBorder(ctx, viewElem, { originElem, viewScaleInfo, viewSizeInfo });
|
|
22
|
+
ctx.globalAlpha = parentOpacity;
|
|
23
|
+
};
|
|
24
|
+
if (clipPath) {
|
|
25
|
+
drawClipPath(ctx, viewElem, {
|
|
26
|
+
originElem,
|
|
27
|
+
calcElemSize,
|
|
28
|
+
viewScaleInfo,
|
|
29
|
+
viewSizeInfo,
|
|
30
|
+
renderContent: () => {
|
|
31
|
+
mainRender();
|
|
18
32
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
});
|
|
34
|
+
if (typeof clipPathStrokeWidth === 'number' && clipPathStrokeWidth > 0 && clipPathStrokeColor) {
|
|
35
|
+
drawClipPathStroke(ctx, viewElem, {
|
|
36
|
+
originElem,
|
|
37
|
+
calcElemSize,
|
|
38
|
+
viewScaleInfo,
|
|
39
|
+
viewSizeInfo,
|
|
40
|
+
parentOpacity
|
|
41
|
+
});
|
|
23
42
|
}
|
|
24
|
-
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
mainRender();
|
|
46
|
+
}
|
|
25
47
|
}
|
|
26
48
|
function drawClipPath(ctx, viewElem, opts) {
|
|
27
|
-
const { renderContent, originElem, calcElemSize,
|
|
28
|
-
const totalScale =
|
|
49
|
+
const { renderContent, originElem, calcElemSize, viewSizeInfo } = opts;
|
|
50
|
+
const totalScale = viewSizeInfo.devicePixelRatio;
|
|
29
51
|
const { clipPath } = (originElem === null || originElem === void 0 ? void 0 : originElem.detail) || {};
|
|
30
52
|
if (clipPath && calcElemSize && clipPath.commands) {
|
|
31
53
|
const { x, y, w, h } = calcElemSize;
|
|
@@ -53,15 +75,48 @@ function drawClipPath(ctx, viewElem, opts) {
|
|
|
53
75
|
renderContent === null || renderContent === void 0 ? void 0 : renderContent();
|
|
54
76
|
}
|
|
55
77
|
}
|
|
56
|
-
function
|
|
78
|
+
function drawClipPathStroke(ctx, viewElem, opts) {
|
|
79
|
+
const { renderContent, originElem, calcElemSize, viewSizeInfo, parentOpacity } = opts;
|
|
80
|
+
const totalScale = viewSizeInfo.devicePixelRatio;
|
|
81
|
+
const { clipPath, clipPathStrokeColor, clipPathStrokeWidth } = (originElem === null || originElem === void 0 ? void 0 : originElem.detail) || {};
|
|
82
|
+
if (clipPath &&
|
|
83
|
+
calcElemSize &&
|
|
84
|
+
clipPath.commands &&
|
|
85
|
+
typeof clipPathStrokeWidth === 'number' &&
|
|
86
|
+
clipPathStrokeWidth > 0 &&
|
|
87
|
+
clipPathStrokeColor) {
|
|
88
|
+
const { x, y, w, h } = calcElemSize;
|
|
89
|
+
const { originW, originH, originX, originY } = clipPath;
|
|
90
|
+
const scaleW = w / originW;
|
|
91
|
+
const scaleH = h / originH;
|
|
92
|
+
const viewOriginX = originX * scaleW;
|
|
93
|
+
const viewOriginY = originY * scaleH;
|
|
94
|
+
const internalX = x - viewOriginX;
|
|
95
|
+
const internalY = y - viewOriginY;
|
|
96
|
+
ctx.save();
|
|
97
|
+
ctx.globalAlpha = parentOpacity;
|
|
98
|
+
ctx.translate(internalX, internalY);
|
|
99
|
+
ctx.scale(totalScale * scaleW, totalScale * scaleH);
|
|
100
|
+
const pathStr = generateSVGPath(clipPath.commands || []);
|
|
101
|
+
const path2d = new Path2D(pathStr);
|
|
102
|
+
ctx.strokeStyle = clipPathStrokeColor;
|
|
103
|
+
ctx.lineWidth = clipPathStrokeWidth;
|
|
104
|
+
ctx.stroke(path2d);
|
|
105
|
+
ctx.translate(0 - internalX, 0 - internalY);
|
|
106
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
107
|
+
rotateElement(ctx, Object.assign({}, viewElem), () => {
|
|
108
|
+
renderContent === null || renderContent === void 0 ? void 0 : renderContent();
|
|
109
|
+
});
|
|
110
|
+
ctx.restore();
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
renderContent === null || renderContent === void 0 ? void 0 : renderContent();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function drawBoxBackground(ctx, viewElem, opts) {
|
|
57
117
|
var _a, _b;
|
|
58
118
|
const { pattern, viewScaleInfo, viewSizeInfo } = opts;
|
|
59
119
|
const transform = [];
|
|
60
|
-
let { borderRadius } = viewElem.detail;
|
|
61
|
-
const { borderWidth } = viewElem.detail;
|
|
62
|
-
if (typeof borderWidth !== 'number') {
|
|
63
|
-
borderRadius = 0;
|
|
64
|
-
}
|
|
65
120
|
if (viewElem.detail.background || pattern) {
|
|
66
121
|
const { x, y, w, h, radiusList } = calcViewBoxSize(viewElem, {
|
|
67
122
|
viewScaleInfo,
|
|
@@ -119,32 +174,30 @@ function drawBoxBackground(ctx, viewElem, opts) {
|
|
|
119
174
|
}
|
|
120
175
|
}
|
|
121
176
|
}
|
|
122
|
-
function drawBoxBorder(ctx, viewElem, opts) {
|
|
123
|
-
var _a, _b;
|
|
177
|
+
export function drawBoxBorder(ctx, viewElem, opts) {
|
|
124
178
|
if (viewElem.detail.borderWidth === 0) {
|
|
125
179
|
return;
|
|
126
180
|
}
|
|
127
181
|
if (!isColorStr(viewElem.detail.borderColor)) {
|
|
128
182
|
return;
|
|
129
183
|
}
|
|
130
|
-
if (((_a = viewElem === null || viewElem === void 0 ? void 0 : viewElem.detail) === null || _a === void 0 ? void 0 : _a.opacity) !== undefined && ((_b = viewElem === null || viewElem === void 0 ? void 0 : viewElem.detail) === null || _b === void 0 ? void 0 : _b.opacity) >= 0) {
|
|
131
|
-
ctx.globalAlpha = viewElem.detail.opacity;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
ctx.globalAlpha = 1;
|
|
135
|
-
}
|
|
136
184
|
const { viewScaleInfo } = opts;
|
|
137
185
|
const { scale } = viewScaleInfo;
|
|
138
186
|
let borderColor = defaultElemConfig.borderColor;
|
|
139
187
|
if (isColorStr(viewElem.detail.borderColor) === true) {
|
|
140
188
|
borderColor = viewElem.detail.borderColor;
|
|
141
189
|
}
|
|
142
|
-
const { borderWidth, borderRadius,
|
|
143
|
-
let
|
|
144
|
-
if (
|
|
145
|
-
|
|
190
|
+
const { borderDash, borderWidth, borderRadius, boxSizing = defaultElemConfig.boxSizing } = viewElem.detail;
|
|
191
|
+
let viewBorderDash = [];
|
|
192
|
+
if (Array.isArray(borderDash) && borderDash.length > 0) {
|
|
193
|
+
viewBorderDash = borderDash.map((num) => Math.ceil(num * scale));
|
|
194
|
+
}
|
|
195
|
+
if (viewBorderDash.length > 0) {
|
|
196
|
+
ctx.lineCap = 'butt';
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
ctx.lineCap = 'square';
|
|
146
200
|
}
|
|
147
|
-
bw = bw * scale;
|
|
148
201
|
let radiusList = [0, 0, 0, 0];
|
|
149
202
|
if (typeof borderRadius === 'number') {
|
|
150
203
|
const br = borderRadius * scale;
|
|
@@ -153,11 +206,12 @@ function drawBoxBorder(ctx, viewElem, opts) {
|
|
|
153
206
|
else if (Array.isArray(borderRadius) && (borderRadius === null || borderRadius === void 0 ? void 0 : borderRadius.length) === 4) {
|
|
154
207
|
radiusList = [borderRadius[0] * scale, borderRadius[1] * scale, borderRadius[2] * scale, borderRadius[3] * scale];
|
|
155
208
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
viewBorderDash = borderDash.map((num) => Math.ceil(num * scale));
|
|
209
|
+
let bw = 0;
|
|
210
|
+
if (typeof borderWidth === 'number') {
|
|
211
|
+
bw = borderWidth || 1;
|
|
160
212
|
}
|
|
213
|
+
bw = bw * scale;
|
|
214
|
+
ctx.strokeStyle = borderColor;
|
|
161
215
|
let borderTop = 0;
|
|
162
216
|
let borderRight = 0;
|
|
163
217
|
let borderBottom = 0;
|
|
@@ -242,12 +296,6 @@ function drawBoxBorder(ctx, viewElem, opts) {
|
|
|
242
296
|
w = viewElem.w;
|
|
243
297
|
h = viewElem.h;
|
|
244
298
|
}
|
|
245
|
-
if (viewBorderDash.length > 0) {
|
|
246
|
-
ctx.lineCap = 'butt';
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
ctx.lineCap = 'square';
|
|
250
|
-
}
|
|
251
299
|
w = Math.max(w, 1);
|
|
252
300
|
h = Math.max(h, 1);
|
|
253
301
|
radiusList = radiusList.map((r) => {
|
|
@@ -263,7 +311,6 @@ function drawBoxBorder(ctx, viewElem, opts) {
|
|
|
263
311
|
ctx.arcTo(x, y, x + w, y, radiusList[0]);
|
|
264
312
|
ctx.closePath();
|
|
265
313
|
ctx.stroke();
|
|
266
|
-
ctx.globalAlpha = 1;
|
|
267
314
|
}
|
|
268
315
|
ctx.setLineDash([]);
|
|
269
316
|
}
|
|
@@ -281,6 +328,12 @@ export function drawBoxShadow(ctx, viewElem, opts) {
|
|
|
281
328
|
ctx.restore();
|
|
282
329
|
}
|
|
283
330
|
else {
|
|
331
|
+
ctx.save();
|
|
332
|
+
ctx.shadowColor = 'transparent';
|
|
333
|
+
ctx.shadowOffsetX = 0;
|
|
334
|
+
ctx.shadowOffsetY = 0;
|
|
335
|
+
ctx.shadowBlur = 0;
|
|
284
336
|
renderContent();
|
|
337
|
+
ctx.restore();
|
|
285
338
|
}
|
|
286
339
|
}
|
package/dist/esm/draw/circle.js
CHANGED
|
@@ -1,49 +1,73 @@
|
|
|
1
|
-
import { rotateElement } from '@idraw/util';
|
|
1
|
+
import { rotateElement, calcViewElementSize } from '@idraw/util';
|
|
2
2
|
import { createColorStyle } from './color';
|
|
3
|
-
import { drawBoxShadow } from './box';
|
|
3
|
+
import { drawBoxShadow, getOpacity } from './box';
|
|
4
4
|
export function drawCircle(ctx, elem, opts) {
|
|
5
5
|
const { detail, angle } = elem;
|
|
6
|
-
const {
|
|
7
|
-
const {
|
|
8
|
-
|
|
6
|
+
const { viewScaleInfo, viewSizeInfo, parentOpacity } = opts;
|
|
7
|
+
const { background = '#000000', borderColor = '#000000', boxSizing, borderWidth = 0, borderDash } = detail;
|
|
8
|
+
let bw = 0;
|
|
9
|
+
if (typeof borderWidth === 'number' && borderWidth > 0) {
|
|
10
|
+
bw = borderWidth;
|
|
11
|
+
}
|
|
12
|
+
else if (Array.isArray(borderWidth) && typeof borderWidth[0] === 'number' && borderWidth[0] > 0) {
|
|
13
|
+
bw = borderWidth[0];
|
|
14
|
+
}
|
|
15
|
+
bw = bw * viewScaleInfo.scale;
|
|
16
|
+
const { x, y, w, h } = calcViewElementSize({ x: elem.x, y: elem.y, w: elem.w, h: elem.h }, { viewScaleInfo }) || elem;
|
|
9
17
|
const viewElem = Object.assign(Object.assign({}, elem), { x, y, w, h, angle });
|
|
10
18
|
rotateElement(ctx, { x, y, w, h, angle }, () => {
|
|
11
19
|
drawBoxShadow(ctx, viewElem, {
|
|
12
20
|
viewScaleInfo,
|
|
13
21
|
viewSizeInfo,
|
|
14
22
|
renderContent: () => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const b = h / 2;
|
|
23
|
+
let a = w / 2;
|
|
24
|
+
let b = h / 2;
|
|
18
25
|
const centerX = x + a;
|
|
19
26
|
const centerY = y + b;
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
const radiusA = a;
|
|
28
|
+
const radiusB = b;
|
|
29
|
+
if (bw > 0) {
|
|
30
|
+
if (boxSizing === 'content-box') {
|
|
31
|
+
}
|
|
32
|
+
else if (boxSizing === 'center-line') {
|
|
33
|
+
a = a - bw / 2;
|
|
34
|
+
b = b - bw / 2;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
a = a - bw;
|
|
38
|
+
b = b - bw;
|
|
39
|
+
}
|
|
22
40
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (typeof borderWidth === 'number' && borderWidth > 0) {
|
|
27
|
-
const ba = borderWidth / 2 + a;
|
|
28
|
-
const bb = borderWidth / 2 + b;
|
|
41
|
+
if (a >= 0 && b >= 0) {
|
|
42
|
+
const opacity = getOpacity(viewElem) * parentOpacity;
|
|
43
|
+
ctx.globalAlpha = opacity;
|
|
29
44
|
ctx.beginPath();
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
45
|
+
const fillStyle = createColorStyle(ctx, background, {
|
|
46
|
+
viewElementSize: { x, y, w, h },
|
|
47
|
+
viewScaleInfo,
|
|
48
|
+
opacity: ctx.globalAlpha
|
|
49
|
+
});
|
|
50
|
+
ctx.fillStyle = fillStyle;
|
|
51
|
+
ctx.circle(centerX, centerY, radiusA, radiusB, 0, 0, 2 * Math.PI);
|
|
33
52
|
ctx.closePath();
|
|
34
|
-
ctx.
|
|
53
|
+
ctx.fill();
|
|
54
|
+
ctx.globalAlpha = parentOpacity;
|
|
55
|
+
if (typeof bw === 'number' && bw > 0) {
|
|
56
|
+
const ba = bw / 2 + a;
|
|
57
|
+
const bb = bw / 2 + b;
|
|
58
|
+
ctx.beginPath();
|
|
59
|
+
if (borderDash) {
|
|
60
|
+
const lineDash = borderDash.map((n) => n * viewScaleInfo.scale);
|
|
61
|
+
ctx.setLineDash(lineDash);
|
|
62
|
+
}
|
|
63
|
+
ctx.strokeStyle = borderColor;
|
|
64
|
+
ctx.lineWidth = bw;
|
|
65
|
+
ctx.circle(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
|
|
66
|
+
ctx.closePath();
|
|
67
|
+
ctx.stroke();
|
|
68
|
+
ctx.setLineDash([]);
|
|
69
|
+
}
|
|
35
70
|
}
|
|
36
|
-
ctx.beginPath();
|
|
37
|
-
const fillStyle = createColorStyle(ctx, background, {
|
|
38
|
-
viewElementSize: { x, y, w, h },
|
|
39
|
-
viewScaleInfo,
|
|
40
|
-
opacity: ctx.globalAlpha
|
|
41
|
-
});
|
|
42
|
-
ctx.fillStyle = fillStyle;
|
|
43
|
-
ctx.circle(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
|
|
44
|
-
ctx.closePath();
|
|
45
|
-
ctx.fill();
|
|
46
|
-
ctx.globalAlpha = 1;
|
|
47
71
|
}
|
|
48
72
|
});
|
|
49
73
|
});
|
|
@@ -4,18 +4,21 @@ const defaultDetail = getDefaultElementDetailConfig();
|
|
|
4
4
|
export function drawElementList(ctx, data, opts) {
|
|
5
5
|
var _a;
|
|
6
6
|
const { elements = [] } = data;
|
|
7
|
+
const { parentOpacity } = opts;
|
|
7
8
|
for (let i = 0; i < elements.length; i++) {
|
|
8
9
|
const element = elements[i];
|
|
9
10
|
const elem = Object.assign(Object.assign({}, element), {
|
|
10
11
|
detail: Object.assign(Object.assign({}, defaultDetail), element === null || element === void 0 ? void 0 : element.detail)
|
|
11
12
|
});
|
|
12
13
|
if (opts.forceDrawAll !== true) {
|
|
13
|
-
if (!((_a = opts.calculator) === null || _a === void 0 ? void 0 : _a.
|
|
14
|
+
if (!((_a = opts.calculator) === null || _a === void 0 ? void 0 : _a.needRender(elem))) {
|
|
14
15
|
continue;
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
try {
|
|
18
|
-
drawElement(ctx, elem, opts)
|
|
19
|
+
drawElement(ctx, elem, Object.assign(Object.assign({}, opts), {
|
|
20
|
+
parentOpacity
|
|
21
|
+
}));
|
|
19
22
|
}
|
|
20
23
|
catch (err) {
|
|
21
24
|
console.error(err);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function drawGlobalBackground(ctx, global, opts) {
|
|
2
|
+
if (typeof (global === null || global === void 0 ? void 0 : global.background) === 'string') {
|
|
3
|
+
const { viewSizeInfo } = opts;
|
|
4
|
+
const { width, height } = viewSizeInfo;
|
|
5
|
+
ctx.globalAlpha = 1;
|
|
6
|
+
ctx.fillStyle = global.background;
|
|
7
|
+
ctx.fillRect(0, 0, width, height);
|
|
8
|
+
}
|
|
9
|
+
}
|