@idraw/renderer 0.3.0-beta.3 → 0.3.0-beta.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/esm/index.d.ts +11 -12
- package/dist/esm/index.js +48 -50
- package/dist/esm/lib/calculate.d.ts +3 -3
- package/dist/esm/lib/calculate.js +5 -5
- package/dist/esm/lib/diff.d.ts +6 -6
- package/dist/esm/lib/diff.js +9 -7
- package/dist/esm/lib/draw/base.d.ts +5 -5
- package/dist/esm/lib/draw/circle.d.ts +2 -2
- package/dist/esm/lib/draw/circle.js +1 -1
- package/dist/esm/lib/draw/html.d.ts +2 -2
- package/dist/esm/lib/draw/image.d.ts +2 -2
- package/dist/esm/lib/draw/index.d.ts +2 -2
- package/dist/esm/lib/draw/rect.d.ts +2 -2
- package/dist/esm/lib/draw/svg.d.ts +2 -2
- package/dist/esm/lib/draw/text.d.ts +2 -2
- package/dist/esm/lib/loader-event.d.ts +5 -5
- package/dist/esm/lib/loader.d.ts +2 -2
- package/dist/esm/lib/loader.js +22 -14
- package/dist/esm/lib/parse.d.ts +2 -2
- package/dist/esm/lib/renderer-event.d.ts +8 -8
- package/dist/esm/lib/temp.d.ts +4 -4
- package/dist/esm/lib/temp.js +1 -1
- package/dist/esm/lib/transform.d.ts +4 -4
- package/dist/esm/lib/transform.js +1 -1
- package/dist/index.global.js +71 -82
- package/dist/index.global.min.js +1 -1
- package/package.json +4 -4
- package/dist/esm/names.d.ts +0 -9
- package/dist/esm/names.js +0 -9
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawData, IDrawContext } from '@idraw/types';
|
|
2
2
|
import { RendererEvent } from './lib/renderer-event';
|
|
3
|
-
import { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem } from './names';
|
|
4
3
|
type Options = {
|
|
5
4
|
width: number;
|
|
6
5
|
height: number;
|
|
@@ -9,19 +8,19 @@ type Options = {
|
|
|
9
8
|
devicePixelRatio: number;
|
|
10
9
|
};
|
|
11
10
|
export default class Renderer extends RendererEvent {
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
private
|
|
11
|
+
private _queue;
|
|
12
|
+
private _ctx;
|
|
13
|
+
private _status;
|
|
14
|
+
private _loader;
|
|
15
|
+
private _opts?;
|
|
17
16
|
constructor(opts?: Options);
|
|
18
|
-
render(target: HTMLCanvasElement |
|
|
17
|
+
render(target: HTMLCanvasElement | IDrawContext, originData: IDrawData, opts?: {
|
|
19
18
|
changeResourceUUIDs?: string[];
|
|
20
19
|
}): void;
|
|
21
|
-
getContext():
|
|
20
|
+
getContext(): IDrawContext | null;
|
|
22
21
|
thaw(): void;
|
|
23
|
-
private
|
|
24
|
-
private
|
|
25
|
-
private
|
|
22
|
+
private _freeze;
|
|
23
|
+
private _drawFrame;
|
|
24
|
+
private _retainQueueOneItem;
|
|
26
25
|
}
|
|
27
26
|
export {};
|
package/dist/esm/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
var _a, _b, _c;
|
|
2
1
|
import { createUUID, deepClone, Context } from '@idraw/util';
|
|
3
2
|
import { drawContext } from './lib/draw';
|
|
4
3
|
import Loader from './lib/loader';
|
|
5
4
|
import { RendererEvent } from './lib/renderer-event';
|
|
6
|
-
import { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem } from './names';
|
|
7
5
|
const { requestAnimationFrame } = window;
|
|
8
6
|
var DrawStatus;
|
|
9
7
|
(function (DrawStatus) {
|
|
@@ -15,27 +13,27 @@ var DrawStatus;
|
|
|
15
13
|
export default class Renderer extends RendererEvent {
|
|
16
14
|
constructor(opts) {
|
|
17
15
|
super();
|
|
18
|
-
this
|
|
19
|
-
this
|
|
20
|
-
this
|
|
21
|
-
this
|
|
22
|
-
this
|
|
16
|
+
this._queue = [];
|
|
17
|
+
this._ctx = null;
|
|
18
|
+
this._status = DrawStatus.NULL;
|
|
19
|
+
this._opts = opts;
|
|
20
|
+
this._loader = new Loader({
|
|
23
21
|
maxParallelNum: 6
|
|
24
22
|
});
|
|
25
|
-
this
|
|
26
|
-
this
|
|
23
|
+
this._loader.on('load', (res) => {
|
|
24
|
+
this._drawFrame();
|
|
27
25
|
this.trigger('load', { element: res.element });
|
|
28
26
|
});
|
|
29
|
-
this
|
|
27
|
+
this._loader.on('error', (res) => {
|
|
30
28
|
this.trigger('error', { element: res.element, error: res.error });
|
|
31
29
|
});
|
|
32
|
-
this
|
|
30
|
+
this._loader.on('complete', () => {
|
|
33
31
|
this.trigger('loadComplete', { t: Date.now() });
|
|
34
32
|
});
|
|
35
33
|
}
|
|
36
34
|
render(target, originData, opts) {
|
|
37
35
|
const { changeResourceUUIDs = [] } = opts || {};
|
|
38
|
-
this
|
|
36
|
+
this._status = DrawStatus.FREE;
|
|
39
37
|
const data = deepClone(originData);
|
|
40
38
|
if (Array.isArray(data.elements)) {
|
|
41
39
|
data.elements.forEach((elem) => {
|
|
@@ -44,15 +42,15 @@ export default class Renderer extends RendererEvent {
|
|
|
44
42
|
}
|
|
45
43
|
});
|
|
46
44
|
}
|
|
47
|
-
if (!this
|
|
48
|
-
if (this
|
|
45
|
+
if (!this._ctx) {
|
|
46
|
+
if (this._opts &&
|
|
49
47
|
Object.prototype.toString.call(target) === '[object HTMLCanvasElement]') {
|
|
50
|
-
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this
|
|
48
|
+
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this._opts;
|
|
51
49
|
const canvas = target;
|
|
52
50
|
canvas.width = width * devicePixelRatio;
|
|
53
51
|
canvas.height = height * devicePixelRatio;
|
|
54
52
|
const ctx2d = canvas.getContext('2d');
|
|
55
|
-
this
|
|
53
|
+
this._ctx = new Context(ctx2d, {
|
|
56
54
|
width,
|
|
57
55
|
height,
|
|
58
56
|
contextWidth: contextWidth || width,
|
|
@@ -61,79 +59,79 @@ export default class Renderer extends RendererEvent {
|
|
|
61
59
|
});
|
|
62
60
|
}
|
|
63
61
|
else if (target) {
|
|
64
|
-
this
|
|
62
|
+
this._ctx = target;
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
|
-
if ([DrawStatus.FREEZE].includes(this
|
|
65
|
+
if ([DrawStatus.FREEZE].includes(this._status)) {
|
|
68
66
|
return;
|
|
69
67
|
}
|
|
70
68
|
const _data = deepClone({ data });
|
|
71
|
-
this
|
|
72
|
-
this
|
|
73
|
-
this
|
|
69
|
+
this._queue.push(_data);
|
|
70
|
+
this._drawFrame();
|
|
71
|
+
this._loader.load(data, changeResourceUUIDs || []);
|
|
74
72
|
}
|
|
75
73
|
getContext() {
|
|
76
|
-
return this
|
|
74
|
+
return this._ctx;
|
|
77
75
|
}
|
|
78
76
|
thaw() {
|
|
79
|
-
this
|
|
77
|
+
this._status = DrawStatus.FREE;
|
|
80
78
|
}
|
|
81
|
-
|
|
82
|
-
this
|
|
79
|
+
_freeze() {
|
|
80
|
+
this._status = DrawStatus.FREEZE;
|
|
83
81
|
}
|
|
84
|
-
|
|
85
|
-
if (this
|
|
82
|
+
_drawFrame() {
|
|
83
|
+
if (this._status === DrawStatus.FREEZE) {
|
|
86
84
|
return;
|
|
87
85
|
}
|
|
88
86
|
requestAnimationFrame(() => {
|
|
89
|
-
if (this
|
|
87
|
+
if (this._status === DrawStatus.FREEZE) {
|
|
90
88
|
return;
|
|
91
89
|
}
|
|
92
|
-
const ctx = this
|
|
93
|
-
let item = this
|
|
90
|
+
const ctx = this._ctx;
|
|
91
|
+
let item = this._queue[0];
|
|
94
92
|
let isLastFrame = false;
|
|
95
|
-
if (this
|
|
96
|
-
item = this
|
|
93
|
+
if (this._queue.length > 1) {
|
|
94
|
+
item = this._queue.shift();
|
|
97
95
|
}
|
|
98
96
|
else {
|
|
99
97
|
isLastFrame = true;
|
|
100
98
|
}
|
|
101
|
-
if (this
|
|
102
|
-
this
|
|
99
|
+
if (this._loader.isComplete() !== true) {
|
|
100
|
+
this._drawFrame();
|
|
103
101
|
if (item && ctx) {
|
|
104
|
-
drawContext(ctx, item.data, this
|
|
102
|
+
drawContext(ctx, item.data, this._loader);
|
|
105
103
|
}
|
|
106
104
|
}
|
|
107
105
|
else if (item && ctx) {
|
|
108
|
-
drawContext(ctx, item.data, this
|
|
109
|
-
this
|
|
106
|
+
drawContext(ctx, item.data, this._loader);
|
|
107
|
+
this._retainQueueOneItem();
|
|
110
108
|
if (!isLastFrame) {
|
|
111
|
-
this
|
|
109
|
+
this._drawFrame();
|
|
112
110
|
}
|
|
113
111
|
else {
|
|
114
|
-
this
|
|
112
|
+
this._status = DrawStatus.FREE;
|
|
115
113
|
}
|
|
116
114
|
}
|
|
117
115
|
else {
|
|
118
|
-
this
|
|
116
|
+
this._status = DrawStatus.FREE;
|
|
119
117
|
}
|
|
120
118
|
this.trigger('drawFrame', { t: Date.now() });
|
|
121
|
-
if (this
|
|
122
|
-
this
|
|
123
|
-
this
|
|
124
|
-
if (ctx && this
|
|
125
|
-
drawContext(ctx, this
|
|
119
|
+
if (this._loader.isComplete() === true &&
|
|
120
|
+
this._queue.length === 1 &&
|
|
121
|
+
this._status === DrawStatus.FREE) {
|
|
122
|
+
if (ctx && this._queue[0] && this._queue[0].data) {
|
|
123
|
+
drawContext(ctx, this._queue[0].data, this._loader);
|
|
126
124
|
}
|
|
127
125
|
this.trigger('drawFrameComplete', { t: Date.now() });
|
|
128
|
-
this
|
|
126
|
+
this._freeze();
|
|
129
127
|
}
|
|
130
128
|
});
|
|
131
129
|
}
|
|
132
|
-
|
|
133
|
-
if (this
|
|
130
|
+
_retainQueueOneItem() {
|
|
131
|
+
if (this._queue.length <= 1) {
|
|
134
132
|
return;
|
|
135
133
|
}
|
|
136
|
-
const lastOne = deepClone(this
|
|
137
|
-
this
|
|
134
|
+
const lastOne = deepClone(this._queue[this._queue.length - 1]);
|
|
135
|
+
this._queue = [lastOne];
|
|
138
136
|
}
|
|
139
137
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DataElement, DataElemDesc, Point } from '@idraw/types';
|
|
2
2
|
export declare function parseRadianToAngle(radian: number): number;
|
|
3
3
|
export declare function parseAngleToRadian(angle: number): number;
|
|
4
|
-
export declare function calcElementCenter(elem:
|
|
5
|
-
export declare function calcRadian(center:
|
|
4
|
+
export declare function calcElementCenter(elem: DataElement<keyof DataElemDesc>): Point;
|
|
5
|
+
export declare function calcRadian(center: Point, start: Point, end: Point): number;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export function parseRadianToAngle(radian) {
|
|
2
|
-
return radian / Math.PI * 180;
|
|
2
|
+
return (radian / Math.PI) * 180;
|
|
3
3
|
}
|
|
4
4
|
export function parseAngleToRadian(angle) {
|
|
5
|
-
return angle / 180 * Math.PI;
|
|
5
|
+
return (angle / 180) * Math.PI;
|
|
6
6
|
}
|
|
7
7
|
export function calcElementCenter(elem) {
|
|
8
8
|
const p = {
|
|
9
9
|
x: elem.x + elem.w / 2,
|
|
10
|
-
y: elem.y + elem.h / 2
|
|
10
|
+
y: elem.y + elem.h / 2
|
|
11
11
|
};
|
|
12
12
|
return p;
|
|
13
13
|
}
|
|
@@ -15,10 +15,10 @@ export function calcRadian(center, start, end) {
|
|
|
15
15
|
const startAngle = calcLineAngle(center, start);
|
|
16
16
|
const endAngle = calcLineAngle(center, end);
|
|
17
17
|
if (endAngle !== null && startAngle !== null) {
|
|
18
|
-
if (startAngle > Math.PI * 3 / 2 && endAngle < Math.PI / 2) {
|
|
18
|
+
if (startAngle > (Math.PI * 3) / 2 && endAngle < Math.PI / 2) {
|
|
19
19
|
return endAngle + (Math.PI * 2 - startAngle);
|
|
20
20
|
}
|
|
21
|
-
else if (endAngle > Math.PI * 3 / 2 && startAngle < Math.PI / 2) {
|
|
21
|
+
else if (endAngle > (Math.PI * 3) / 2 && startAngle < Math.PI / 2) {
|
|
22
22
|
return startAngle + (Math.PI * 2 - endAngle);
|
|
23
23
|
}
|
|
24
24
|
else {
|
package/dist/esm/lib/diff.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function isChangeImageElementResource(before:
|
|
3
|
-
export declare function isChangeSVGElementResource(before:
|
|
4
|
-
export declare function isChangeHTMLElementResource(before:
|
|
5
|
-
export declare function diffElementResourceChange(before:
|
|
6
|
-
export declare function diffElementResourceChangeList(before:
|
|
1
|
+
import { DataElement, IDrawData, DataElemDesc } from '@idraw/types';
|
|
2
|
+
export declare function isChangeImageElementResource(before: DataElement<'image'>, after: DataElement<'image'>): boolean;
|
|
3
|
+
export declare function isChangeSVGElementResource(before: DataElement<'svg'>, after: DataElement<'svg'>): boolean;
|
|
4
|
+
export declare function isChangeHTMLElementResource(before: DataElement<'html'>, after: DataElement<'html'>): boolean;
|
|
5
|
+
export declare function diffElementResourceChange(before: DataElement<keyof DataElemDesc>, after: DataElement<keyof DataElemDesc>): string | null;
|
|
6
|
+
export declare function diffElementResourceChangeList(before: IDrawData, after: IDrawData): string[];
|
package/dist/esm/lib/diff.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
export function isChangeImageElementResource(before, after) {
|
|
2
2
|
var _a, _b;
|
|
3
|
-
return ((
|
|
3
|
+
return ((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.src) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.src);
|
|
4
4
|
}
|
|
5
5
|
export function isChangeSVGElementResource(before, after) {
|
|
6
6
|
var _a, _b;
|
|
7
|
-
return ((
|
|
7
|
+
return ((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.svg) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.svg);
|
|
8
8
|
}
|
|
9
9
|
export function isChangeHTMLElementResource(before, after) {
|
|
10
10
|
var _a, _b, _c, _d, _e, _f;
|
|
11
|
-
return (((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.html) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.html)
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
return (((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.html) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.html) ||
|
|
12
|
+
((_c = before === null || before === void 0 ? void 0 : before.desc) === null || _c === void 0 ? void 0 : _c.width) !== ((_d = after === null || after === void 0 ? void 0 : after.desc) === null || _d === void 0 ? void 0 : _d.width) ||
|
|
13
|
+
((_e = before === null || before === void 0 ? void 0 : before.desc) === null || _e === void 0 ? void 0 : _e.height) !== ((_f = after === null || after === void 0 ? void 0 : after.desc) === null || _f === void 0 ? void 0 : _f.height));
|
|
14
14
|
}
|
|
15
15
|
export function diffElementResourceChange(before, after) {
|
|
16
16
|
let result = null;
|
|
@@ -28,7 +28,8 @@ export function diffElementResourceChange(before, after) {
|
|
|
28
28
|
isChange = isChangeHTMLElementResource(before, after);
|
|
29
29
|
break;
|
|
30
30
|
}
|
|
31
|
-
default:
|
|
31
|
+
default:
|
|
32
|
+
break;
|
|
32
33
|
}
|
|
33
34
|
if (isChange === true) {
|
|
34
35
|
result = after.uuid;
|
|
@@ -59,7 +60,8 @@ export function diffElementResourceChangeList(before, after) {
|
|
|
59
60
|
isChange = isChangeHTMLElementResource(beforeMap[uuid], afterMap[uuid]);
|
|
60
61
|
break;
|
|
61
62
|
}
|
|
62
|
-
default:
|
|
63
|
+
default:
|
|
64
|
+
break;
|
|
63
65
|
}
|
|
64
66
|
if (isChange === true) {
|
|
65
67
|
uuids.push(uuid);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function clearContext(ctx:
|
|
3
|
-
export declare function drawBgColor(ctx:
|
|
4
|
-
export declare function drawBox(ctx:
|
|
5
|
-
export declare function drawBoxBorder(ctx:
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
|
+
export declare function clearContext(ctx: IDrawContext): void;
|
|
3
|
+
export declare function drawBgColor(ctx: IDrawContext, color: string): void;
|
|
4
|
+
export declare function drawBox(ctx: IDrawContext, elem: DataElement<'text' | 'rect'>, pattern: string | CanvasPattern | null): void;
|
|
5
|
+
export declare function drawBoxBorder(ctx: IDrawContext, elem: DataElement<'text' | 'rect'>): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function drawCircle(ctx:
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
|
+
export declare function drawCircle(ctx: IDrawContext, elem: DataElement<'circle'>): void;
|
|
@@ -4,7 +4,7 @@ export function drawCircle(ctx, elem) {
|
|
|
4
4
|
clearContext(ctx);
|
|
5
5
|
rotateElement(ctx, elem, (ctx) => {
|
|
6
6
|
const { x, y, w, h, desc } = elem;
|
|
7
|
-
const { bgColor = '#000000', borderColor = '#000000', borderWidth = 0
|
|
7
|
+
const { bgColor = '#000000', borderColor = '#000000', borderWidth = 0 } = desc;
|
|
8
8
|
const a = w / 2;
|
|
9
9
|
const b = h / 2;
|
|
10
10
|
const centerX = x + a;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
2
|
import Loader from '../loader';
|
|
3
|
-
export declare function drawHTML(ctx:
|
|
3
|
+
export declare function drawHTML(ctx: IDrawContext, elem: DataElement<'html'>, loader: Loader): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
2
|
import Loader from '../loader';
|
|
3
|
-
export declare function drawImage(ctx:
|
|
3
|
+
export declare function drawImage(ctx: IDrawContext, elem: DataElement<'image'>, loader: Loader): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawContext, IDrawData } from '@idraw/types';
|
|
2
2
|
import Loader from '../loader';
|
|
3
|
-
export declare function drawContext(ctx:
|
|
3
|
+
export declare function drawContext(ctx: IDrawContext, data: IDrawData, loader: Loader): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function drawRect(ctx:
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
|
+
export declare function drawRect(ctx: IDrawContext, elem: DataElement<'rect'>): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
2
|
import Loader from '../loader';
|
|
3
|
-
export declare function drawSVG(ctx:
|
|
3
|
+
export declare function drawSVG(ctx: IDrawContext, elem: DataElement<'svg'>, loader: Loader): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawContext, DataElement } from '@idraw/types';
|
|
2
2
|
import Loader from '../loader';
|
|
3
|
-
export declare function drawText(ctx:
|
|
3
|
+
export declare function drawText(ctx: IDrawContext, elem: DataElement<'text'>, loader: Loader): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DataElement, DataElemDesc } from '@idraw/types';
|
|
2
2
|
export type TypeLoadDataItem = {
|
|
3
3
|
uuid: string;
|
|
4
4
|
type: 'image' | 'svg' | 'html';
|
|
@@ -7,16 +7,16 @@ export type TypeLoadDataItem = {
|
|
|
7
7
|
elemW: number;
|
|
8
8
|
elemH: number;
|
|
9
9
|
source: string;
|
|
10
|
-
element:
|
|
10
|
+
element: DataElement<keyof DataElemDesc>;
|
|
11
11
|
error?: any;
|
|
12
12
|
};
|
|
13
13
|
export type TypeLoadData = {
|
|
14
14
|
[uuid: string]: TypeLoadDataItem;
|
|
15
15
|
};
|
|
16
16
|
export type TypeLoaderEventArgMap = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
complete: void;
|
|
18
|
+
load: TypeLoadData[string];
|
|
19
|
+
error: TypeLoadData[string];
|
|
20
20
|
};
|
|
21
21
|
export interface TypeLoaderEvent {
|
|
22
22
|
on<T extends keyof TypeLoaderEventArgMap>(key: T, callback: (p: TypeLoaderEventArgMap[T]) => void): void;
|
package/dist/esm/lib/loader.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDrawData } from '@idraw/types';
|
|
2
2
|
import { TypeLoaderEventArgMap } from './loader-event';
|
|
3
3
|
type Options = {
|
|
4
4
|
maxParallelNum: number;
|
|
@@ -12,7 +12,7 @@ export default class Loader {
|
|
|
12
12
|
private _status;
|
|
13
13
|
private _waitingLoadQueue;
|
|
14
14
|
constructor(opts: Options);
|
|
15
|
-
load(data:
|
|
15
|
+
load(data: IDrawData, changeResourceUUIDs: string[]): void;
|
|
16
16
|
on<T extends keyof TypeLoaderEventArgMap>(name: T, callback: (arg: TypeLoaderEventArgMap[T]) => void): void;
|
|
17
17
|
off<T extends keyof TypeLoaderEventArgMap>(name: T, callback: (arg: TypeLoaderEventArgMap[T]) => void): void;
|
|
18
18
|
isComplete(): boolean;
|
package/dist/esm/lib/loader.js
CHANGED
|
@@ -29,7 +29,8 @@ export default class Loader {
|
|
|
29
29
|
}
|
|
30
30
|
load(data, changeResourceUUIDs) {
|
|
31
31
|
const [uuidQueue, loadData] = this._resetLoadData(data, changeResourceUUIDs);
|
|
32
|
-
if (this._status === LoaderStatus.FREE ||
|
|
32
|
+
if (this._status === LoaderStatus.FREE ||
|
|
33
|
+
this._status === LoaderStatus.COMPLETE) {
|
|
33
34
|
this._currentUUIDQueue = uuidQueue;
|
|
34
35
|
this._currentLoadData = loadData;
|
|
35
36
|
this._loadTask();
|
|
@@ -37,7 +38,7 @@ export default class Loader {
|
|
|
37
38
|
else if (this._status === LoaderStatus.LOADING && uuidQueue.length > 0) {
|
|
38
39
|
this._waitingLoadQueue.push({
|
|
39
40
|
uuidQueue,
|
|
40
|
-
loadData
|
|
41
|
+
loadData
|
|
41
42
|
});
|
|
42
43
|
}
|
|
43
44
|
}
|
|
@@ -63,7 +64,7 @@ export default class Loader {
|
|
|
63
64
|
const storageLoadData = this._storageLoadData;
|
|
64
65
|
for (let i = data.elements.length - 1; i >= 0; i--) {
|
|
65
66
|
const elem = data.elements[i];
|
|
66
|
-
if (['image', 'svg', 'html'
|
|
67
|
+
if (['image', 'svg', 'html'].includes(elem.type)) {
|
|
67
68
|
if (!storageLoadData[elem.uuid]) {
|
|
68
69
|
loadData[elem.uuid] = this._createEmptyLoadItem(elem);
|
|
69
70
|
uuidQueue.push(elem.uuid);
|
|
@@ -105,7 +106,7 @@ export default class Loader {
|
|
|
105
106
|
source,
|
|
106
107
|
elemW,
|
|
107
108
|
elemH,
|
|
108
|
-
element: deepClone(elem)
|
|
109
|
+
element: deepClone(elem)
|
|
109
110
|
};
|
|
110
111
|
}
|
|
111
112
|
_loadTask() {
|
|
@@ -148,7 +149,8 @@ export default class Loader {
|
|
|
148
149
|
break;
|
|
149
150
|
}
|
|
150
151
|
loadUUIDList.push(uuid);
|
|
151
|
-
this._loadElementSource(this._currentLoadData[uuid])
|
|
152
|
+
this._loadElementSource(this._currentLoadData[uuid])
|
|
153
|
+
.then((image) => {
|
|
152
154
|
var _a, _b;
|
|
153
155
|
loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
|
|
154
156
|
const status = _loadAction();
|
|
@@ -160,9 +162,11 @@ export default class Loader {
|
|
|
160
162
|
source: this._currentLoadData[uuid].source,
|
|
161
163
|
elemW: this._currentLoadData[uuid].elemW,
|
|
162
164
|
elemH: this._currentLoadData[uuid].elemH,
|
|
163
|
-
element: this._currentLoadData[uuid].element
|
|
165
|
+
element: this._currentLoadData[uuid].element
|
|
164
166
|
};
|
|
165
|
-
if (loadUUIDList.length === 0 &&
|
|
167
|
+
if (loadUUIDList.length === 0 &&
|
|
168
|
+
uuids.length === 0 &&
|
|
169
|
+
status === true) {
|
|
166
170
|
this._status = LoaderStatus.FREE;
|
|
167
171
|
this._loadTask();
|
|
168
172
|
}
|
|
@@ -174,9 +178,10 @@ export default class Loader {
|
|
|
174
178
|
source: this._storageLoadData[uuid].source,
|
|
175
179
|
elemW: this._storageLoadData[uuid].elemW,
|
|
176
180
|
elemH: this._storageLoadData[uuid].elemH,
|
|
177
|
-
element: (_b = this._storageLoadData[uuid]) === null || _b === void 0 ? void 0 : _b.element
|
|
181
|
+
element: (_b = this._storageLoadData[uuid]) === null || _b === void 0 ? void 0 : _b.element
|
|
178
182
|
});
|
|
179
|
-
})
|
|
183
|
+
})
|
|
184
|
+
.catch((err) => {
|
|
180
185
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
181
186
|
console.warn(err);
|
|
182
187
|
loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
|
|
@@ -191,10 +196,12 @@ export default class Loader {
|
|
|
191
196
|
source: (_b = this._currentLoadData[uuid]) === null || _b === void 0 ? void 0 : _b.source,
|
|
192
197
|
elemW: (_c = this._currentLoadData[uuid]) === null || _c === void 0 ? void 0 : _c.elemW,
|
|
193
198
|
elemH: (_d = this._currentLoadData[uuid]) === null || _d === void 0 ? void 0 : _d.elemH,
|
|
194
|
-
element: (_e = this._currentLoadData[uuid]) === null || _e === void 0 ? void 0 : _e.element
|
|
199
|
+
element: (_e = this._currentLoadData[uuid]) === null || _e === void 0 ? void 0 : _e.element
|
|
195
200
|
};
|
|
196
201
|
}
|
|
197
|
-
if (loadUUIDList.length === 0 &&
|
|
202
|
+
if (loadUUIDList.length === 0 &&
|
|
203
|
+
uuids.length === 0 &&
|
|
204
|
+
status === true) {
|
|
198
205
|
this._status = LoaderStatus.FREE;
|
|
199
206
|
this._loadTask();
|
|
200
207
|
}
|
|
@@ -207,7 +214,7 @@ export default class Loader {
|
|
|
207
214
|
source: (_j = this._storageLoadData[uuid]) === null || _j === void 0 ? void 0 : _j.source,
|
|
208
215
|
elemW: (_k = this._storageLoadData[uuid]) === null || _k === void 0 ? void 0 : _k.elemW,
|
|
209
216
|
elemH: (_l = this._storageLoadData[uuid]) === null || _l === void 0 ? void 0 : _l.elemH,
|
|
210
|
-
element: (_m = this._storageLoadData[uuid]) === null || _m === void 0 ? void 0 : _m.element
|
|
217
|
+
element: (_m = this._storageLoadData[uuid]) === null || _m === void 0 ? void 0 : _m.element
|
|
211
218
|
});
|
|
212
219
|
}
|
|
213
220
|
});
|
|
@@ -228,11 +235,12 @@ export default class Loader {
|
|
|
228
235
|
}
|
|
229
236
|
else if (params && params.type === 'html') {
|
|
230
237
|
const image = yield loadHTML(params.source, {
|
|
231
|
-
width: params.elemW,
|
|
238
|
+
width: params.elemW,
|
|
239
|
+
height: params.elemH
|
|
232
240
|
});
|
|
233
241
|
return image;
|
|
234
242
|
}
|
|
235
|
-
throw Error(
|
|
243
|
+
throw Error("Element's source is not support!");
|
|
236
244
|
});
|
|
237
245
|
}
|
|
238
246
|
}
|
package/dist/esm/lib/parse.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function parseData(data: any):
|
|
1
|
+
import { IDrawData } from '@idraw/types';
|
|
2
|
+
export declare function parseData(data: any): IDrawData;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DataElement, DataElemDesc } from '@idraw/types';
|
|
2
2
|
export type TypeRendererEventArgMap = {
|
|
3
|
-
|
|
3
|
+
drawFrame: {
|
|
4
4
|
t: number;
|
|
5
5
|
};
|
|
6
|
-
|
|
6
|
+
drawFrameComplete: {
|
|
7
7
|
t: number;
|
|
8
8
|
};
|
|
9
|
-
|
|
10
|
-
element:
|
|
9
|
+
load: {
|
|
10
|
+
element: DataElement<keyof DataElemDesc>;
|
|
11
11
|
};
|
|
12
|
-
|
|
12
|
+
loadComplete: {
|
|
13
13
|
t: number;
|
|
14
14
|
};
|
|
15
|
-
|
|
16
|
-
element:
|
|
15
|
+
error: {
|
|
16
|
+
element: DataElement<keyof DataElemDesc>;
|
|
17
17
|
error: any;
|
|
18
18
|
};
|
|
19
19
|
};
|
package/dist/esm/lib/temp.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HelperWrapperControllerDirection, Point } from '@idraw/types';
|
|
2
2
|
import { Mode, CursorStatus } from './../constant/static';
|
|
3
3
|
type TempDataDesc = {
|
|
4
4
|
hasInited: boolean;
|
|
@@ -8,9 +8,9 @@ type TempDataDesc = {
|
|
|
8
8
|
selectedUUID: string | null;
|
|
9
9
|
selectedUUIDList: string[];
|
|
10
10
|
hoverUUID: string | null;
|
|
11
|
-
selectedControllerDirection:
|
|
12
|
-
hoverControllerDirection:
|
|
13
|
-
prevPoint:
|
|
11
|
+
selectedControllerDirection: HelperWrapperControllerDirection | null;
|
|
12
|
+
hoverControllerDirection: HelperWrapperControllerDirection | null;
|
|
13
|
+
prevPoint: Point | null;
|
|
14
14
|
};
|
|
15
15
|
export declare class TempData {
|
|
16
16
|
private _temp;
|
package/dist/esm/lib/temp.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
declare function rotateElement(ctx:
|
|
3
|
-
declare function rotateContext(ctx:
|
|
4
|
-
export { rotateContext, rotateElement
|
|
1
|
+
import { IDrawContext, Point, DataElement, DataElemDesc } from '@idraw/types';
|
|
2
|
+
declare function rotateElement(ctx: IDrawContext, elem: DataElement<keyof DataElemDesc>, callback: (ctx: IDrawContext) => void): void;
|
|
3
|
+
declare function rotateContext(ctx: IDrawContext, center: Point | undefined, radian: number, callback: (ctx: IDrawContext) => void): void;
|
|
4
|
+
export { rotateContext, rotateElement };
|
package/dist/index.global.js
CHANGED
|
@@ -88,8 +88,8 @@ var iDrawRenderer = function() {
|
|
|
88
88
|
const reader = new FileReader();
|
|
89
89
|
reader.readAsDataURL(blob);
|
|
90
90
|
reader.onload = function(event) {
|
|
91
|
-
var
|
|
92
|
-
const base64 = (
|
|
91
|
+
var _a;
|
|
92
|
+
const base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
93
93
|
resolve(base64);
|
|
94
94
|
};
|
|
95
95
|
reader.onerror = function(err) {
|
|
@@ -104,8 +104,8 @@ var iDrawRenderer = function() {
|
|
|
104
104
|
const reader = new FileReader();
|
|
105
105
|
reader.readAsDataURL(blob);
|
|
106
106
|
reader.onload = function(event) {
|
|
107
|
-
var
|
|
108
|
-
const base64 = (
|
|
107
|
+
var _a;
|
|
108
|
+
const base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
109
109
|
resolve(base64);
|
|
110
110
|
};
|
|
111
111
|
reader.onerror = function(err) {
|
|
@@ -744,7 +744,7 @@ var iDrawRenderer = function() {
|
|
|
744
744
|
});
|
|
745
745
|
}
|
|
746
746
|
function drawContext(ctx, data, loader) {
|
|
747
|
-
var
|
|
747
|
+
var _a;
|
|
748
748
|
clearContext(ctx);
|
|
749
749
|
const size = ctx.getSize();
|
|
750
750
|
ctx.clearRect(0, 0, size.contextWidth, size.contextHeight);
|
|
@@ -756,7 +756,7 @@ var iDrawRenderer = function() {
|
|
|
756
756
|
}
|
|
757
757
|
for (let i = 0; i < data.elements.length; i++) {
|
|
758
758
|
const elem = data.elements[i];
|
|
759
|
-
if (((
|
|
759
|
+
if (((_a = elem == null ? void 0 : elem.operation) == null ? void 0 : _a.invisible) === true) {
|
|
760
760
|
continue;
|
|
761
761
|
}
|
|
762
762
|
switch (elem.type) {
|
|
@@ -850,7 +850,10 @@ var iDrawRenderer = function() {
|
|
|
850
850
|
this._waitingLoadQueue = [];
|
|
851
851
|
}
|
|
852
852
|
load(data, changeResourceUUIDs) {
|
|
853
|
-
const [uuidQueue, loadData] = this._resetLoadData(
|
|
853
|
+
const [uuidQueue, loadData] = this._resetLoadData(
|
|
854
|
+
data,
|
|
855
|
+
changeResourceUUIDs
|
|
856
|
+
);
|
|
854
857
|
if (this._status === "free" || this._status === "complete") {
|
|
855
858
|
this._currentUUIDQueue = uuidQueue;
|
|
856
859
|
this._currentLoadData = loadData;
|
|
@@ -872,14 +875,14 @@ var iDrawRenderer = function() {
|
|
|
872
875
|
return this._status === "complete";
|
|
873
876
|
}
|
|
874
877
|
getContent(uuid) {
|
|
875
|
-
var
|
|
876
|
-
if (((
|
|
878
|
+
var _a;
|
|
879
|
+
if (((_a = this._storageLoadData[uuid]) == null ? void 0 : _a.status) === "loaded") {
|
|
877
880
|
return this._storageLoadData[uuid].content;
|
|
878
881
|
}
|
|
879
882
|
return null;
|
|
880
883
|
}
|
|
881
884
|
// getPattern(
|
|
882
|
-
// elem:
|
|
885
|
+
// elem: DataElement<keyof DataElemDesc>,
|
|
883
886
|
// opts?: {
|
|
884
887
|
// forceUpdate: boolean
|
|
885
888
|
// }
|
|
@@ -990,7 +993,7 @@ var iDrawRenderer = function() {
|
|
|
990
993
|
}
|
|
991
994
|
loadUUIDList.push(uuid);
|
|
992
995
|
this._loadElementSource(this._currentLoadData[uuid]).then((image) => {
|
|
993
|
-
var
|
|
996
|
+
var _a, _b;
|
|
994
997
|
loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
|
|
995
998
|
const status = _loadAction();
|
|
996
999
|
this._storageLoadData[uuid] = {
|
|
@@ -1008,29 +1011,29 @@ var iDrawRenderer = function() {
|
|
|
1008
1011
|
this._loadTask();
|
|
1009
1012
|
}
|
|
1010
1013
|
this._event.trigger("load", {
|
|
1011
|
-
uuid: (
|
|
1014
|
+
uuid: (_a = this._storageLoadData[uuid]) == null ? void 0 : _a.uuid,
|
|
1012
1015
|
type: this._storageLoadData[uuid].type,
|
|
1013
1016
|
status: this._storageLoadData[uuid].status,
|
|
1014
1017
|
content: this._storageLoadData[uuid].content,
|
|
1015
1018
|
source: this._storageLoadData[uuid].source,
|
|
1016
1019
|
elemW: this._storageLoadData[uuid].elemW,
|
|
1017
1020
|
elemH: this._storageLoadData[uuid].elemH,
|
|
1018
|
-
element: (
|
|
1021
|
+
element: (_b = this._storageLoadData[uuid]) == null ? void 0 : _b.element
|
|
1019
1022
|
});
|
|
1020
1023
|
}).catch((err) => {
|
|
1021
|
-
var
|
|
1024
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
1022
1025
|
console.warn(err);
|
|
1023
1026
|
loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
|
|
1024
1027
|
const status = _loadAction();
|
|
1025
1028
|
if (this._currentLoadData[uuid]) {
|
|
1026
1029
|
this._storageLoadData[uuid] = {
|
|
1027
1030
|
uuid,
|
|
1028
|
-
type: (
|
|
1031
|
+
type: (_a = this._currentLoadData[uuid]) == null ? void 0 : _a.type,
|
|
1029
1032
|
status: "fail",
|
|
1030
1033
|
content: null,
|
|
1031
1034
|
error: err,
|
|
1032
|
-
source: (
|
|
1033
|
-
elemW: (
|
|
1035
|
+
source: (_b = this._currentLoadData[uuid]) == null ? void 0 : _b.source,
|
|
1036
|
+
elemW: (_c = this._currentLoadData[uuid]) == null ? void 0 : _c.elemW,
|
|
1034
1037
|
elemH: (_d = this._currentLoadData[uuid]) == null ? void 0 : _d.elemH,
|
|
1035
1038
|
element: (_e = this._currentLoadData[uuid]) == null ? void 0 : _e.element
|
|
1036
1039
|
};
|
|
@@ -1062,18 +1065,13 @@ var iDrawRenderer = function() {
|
|
|
1062
1065
|
const image = await loadImage(params.source);
|
|
1063
1066
|
return image;
|
|
1064
1067
|
} else if (params && params.type === "svg") {
|
|
1065
|
-
const image = await loadSVG(
|
|
1066
|
-
params.source
|
|
1067
|
-
);
|
|
1068
|
+
const image = await loadSVG(params.source);
|
|
1068
1069
|
return image;
|
|
1069
1070
|
} else if (params && params.type === "html") {
|
|
1070
|
-
const image = await loadHTML(
|
|
1071
|
-
params.
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
height: params.elemH
|
|
1075
|
-
}
|
|
1076
|
-
);
|
|
1071
|
+
const image = await loadHTML(params.source, {
|
|
1072
|
+
width: params.elemW,
|
|
1073
|
+
height: params.elemH
|
|
1074
|
+
});
|
|
1077
1075
|
return image;
|
|
1078
1076
|
}
|
|
1079
1077
|
throw Error("Element's source is not support!");
|
|
@@ -1127,40 +1125,31 @@ var iDrawRenderer = function() {
|
|
|
1127
1125
|
return false;
|
|
1128
1126
|
}
|
|
1129
1127
|
}
|
|
1130
|
-
const _queue = Symbol("_queue");
|
|
1131
|
-
const _ctx = Symbol("_ctx");
|
|
1132
|
-
const _status = Symbol("_status");
|
|
1133
|
-
const _loader = Symbol("_loader");
|
|
1134
|
-
const _opts = Symbol("_opts");
|
|
1135
|
-
const _freeze = Symbol("_freeze");
|
|
1136
|
-
const _drawFrame = Symbol("_drawFrame");
|
|
1137
|
-
const _retainQueueOneItem = Symbol("_retainQueueOneItem");
|
|
1138
|
-
var _a, _b, _c;
|
|
1139
1128
|
const { requestAnimationFrame } = window;
|
|
1140
1129
|
class Renderer extends RendererEvent {
|
|
1141
1130
|
constructor(opts) {
|
|
1142
1131
|
super();
|
|
1143
|
-
this
|
|
1144
|
-
this
|
|
1145
|
-
this
|
|
1146
|
-
this
|
|
1147
|
-
this
|
|
1132
|
+
this._queue = [];
|
|
1133
|
+
this._ctx = null;
|
|
1134
|
+
this._status = "null";
|
|
1135
|
+
this._opts = opts;
|
|
1136
|
+
this._loader = new Loader({
|
|
1148
1137
|
maxParallelNum: 6
|
|
1149
1138
|
});
|
|
1150
|
-
this
|
|
1151
|
-
this
|
|
1139
|
+
this._loader.on("load", (res) => {
|
|
1140
|
+
this._drawFrame();
|
|
1152
1141
|
this.trigger("load", { element: res.element });
|
|
1153
1142
|
});
|
|
1154
|
-
this
|
|
1143
|
+
this._loader.on("error", (res) => {
|
|
1155
1144
|
this.trigger("error", { element: res.element, error: res.error });
|
|
1156
1145
|
});
|
|
1157
|
-
this
|
|
1146
|
+
this._loader.on("complete", () => {
|
|
1158
1147
|
this.trigger("loadComplete", { t: Date.now() });
|
|
1159
1148
|
});
|
|
1160
1149
|
}
|
|
1161
1150
|
render(target, originData, opts) {
|
|
1162
1151
|
const { changeResourceUUIDs = [] } = opts || {};
|
|
1163
|
-
this
|
|
1152
|
+
this._status = "free";
|
|
1164
1153
|
const data = deepClone(originData);
|
|
1165
1154
|
if (Array.isArray(data.elements)) {
|
|
1166
1155
|
data.elements.forEach((elem) => {
|
|
@@ -1169,14 +1158,14 @@ var iDrawRenderer = function() {
|
|
|
1169
1158
|
}
|
|
1170
1159
|
});
|
|
1171
1160
|
}
|
|
1172
|
-
if (!this
|
|
1173
|
-
if (this
|
|
1174
|
-
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this
|
|
1161
|
+
if (!this._ctx) {
|
|
1162
|
+
if (this._opts && Object.prototype.toString.call(target) === "[object HTMLCanvasElement]") {
|
|
1163
|
+
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this._opts;
|
|
1175
1164
|
const canvas = target;
|
|
1176
1165
|
canvas.width = width * devicePixelRatio;
|
|
1177
1166
|
canvas.height = height * devicePixelRatio;
|
|
1178
1167
|
const ctx2d = canvas.getContext("2d");
|
|
1179
|
-
this
|
|
1168
|
+
this._ctx = new Context(ctx2d, {
|
|
1180
1169
|
width,
|
|
1181
1170
|
height,
|
|
1182
1171
|
contextWidth: contextWidth || width,
|
|
@@ -1184,77 +1173,77 @@ var iDrawRenderer = function() {
|
|
|
1184
1173
|
devicePixelRatio
|
|
1185
1174
|
});
|
|
1186
1175
|
} else if (target) {
|
|
1187
|
-
this
|
|
1176
|
+
this._ctx = target;
|
|
1188
1177
|
}
|
|
1189
1178
|
}
|
|
1190
1179
|
if ([
|
|
1191
1180
|
"freeze"
|
|
1192
1181
|
/* FREEZE */
|
|
1193
|
-
].includes(this
|
|
1182
|
+
].includes(this._status)) {
|
|
1194
1183
|
return;
|
|
1195
1184
|
}
|
|
1196
1185
|
const _data = deepClone({ data });
|
|
1197
|
-
this
|
|
1198
|
-
this
|
|
1199
|
-
this
|
|
1186
|
+
this._queue.push(_data);
|
|
1187
|
+
this._drawFrame();
|
|
1188
|
+
this._loader.load(data, changeResourceUUIDs || []);
|
|
1200
1189
|
}
|
|
1201
1190
|
getContext() {
|
|
1202
|
-
return this
|
|
1191
|
+
return this._ctx;
|
|
1203
1192
|
}
|
|
1204
1193
|
thaw() {
|
|
1205
|
-
this
|
|
1194
|
+
this._status = "free";
|
|
1206
1195
|
}
|
|
1207
|
-
|
|
1208
|
-
this
|
|
1196
|
+
_freeze() {
|
|
1197
|
+
this._status = "freeze";
|
|
1209
1198
|
}
|
|
1210
|
-
|
|
1211
|
-
if (this
|
|
1199
|
+
_drawFrame() {
|
|
1200
|
+
if (this._status === "freeze") {
|
|
1212
1201
|
return;
|
|
1213
1202
|
}
|
|
1214
1203
|
requestAnimationFrame(() => {
|
|
1215
|
-
if (this
|
|
1204
|
+
if (this._status === "freeze") {
|
|
1216
1205
|
return;
|
|
1217
1206
|
}
|
|
1218
|
-
const ctx = this
|
|
1219
|
-
let item = this
|
|
1207
|
+
const ctx = this._ctx;
|
|
1208
|
+
let item = this._queue[0];
|
|
1220
1209
|
let isLastFrame = false;
|
|
1221
|
-
if (this
|
|
1222
|
-
item = this
|
|
1210
|
+
if (this._queue.length > 1) {
|
|
1211
|
+
item = this._queue.shift();
|
|
1223
1212
|
} else {
|
|
1224
1213
|
isLastFrame = true;
|
|
1225
1214
|
}
|
|
1226
|
-
if (this
|
|
1227
|
-
this
|
|
1215
|
+
if (this._loader.isComplete() !== true) {
|
|
1216
|
+
this._drawFrame();
|
|
1228
1217
|
if (item && ctx) {
|
|
1229
|
-
drawContext(ctx, item.data, this
|
|
1218
|
+
drawContext(ctx, item.data, this._loader);
|
|
1230
1219
|
}
|
|
1231
1220
|
} else if (item && ctx) {
|
|
1232
|
-
drawContext(ctx, item.data, this
|
|
1233
|
-
this
|
|
1221
|
+
drawContext(ctx, item.data, this._loader);
|
|
1222
|
+
this._retainQueueOneItem();
|
|
1234
1223
|
if (!isLastFrame) {
|
|
1235
|
-
this
|
|
1224
|
+
this._drawFrame();
|
|
1236
1225
|
} else {
|
|
1237
|
-
this
|
|
1226
|
+
this._status = "free";
|
|
1238
1227
|
}
|
|
1239
1228
|
} else {
|
|
1240
|
-
this
|
|
1229
|
+
this._status = "free";
|
|
1241
1230
|
}
|
|
1242
1231
|
this.trigger("drawFrame", { t: Date.now() });
|
|
1243
|
-
if (this
|
|
1244
|
-
if (ctx && this
|
|
1245
|
-
drawContext(ctx, this
|
|
1232
|
+
if (this._loader.isComplete() === true && this._queue.length === 1 && this._status === "free") {
|
|
1233
|
+
if (ctx && this._queue[0] && this._queue[0].data) {
|
|
1234
|
+
drawContext(ctx, this._queue[0].data, this._loader);
|
|
1246
1235
|
}
|
|
1247
1236
|
this.trigger("drawFrameComplete", { t: Date.now() });
|
|
1248
|
-
this
|
|
1237
|
+
this._freeze();
|
|
1249
1238
|
}
|
|
1250
1239
|
});
|
|
1251
1240
|
}
|
|
1252
|
-
|
|
1253
|
-
if (this
|
|
1241
|
+
_retainQueueOneItem() {
|
|
1242
|
+
if (this._queue.length <= 1) {
|
|
1254
1243
|
return;
|
|
1255
1244
|
}
|
|
1256
|
-
const lastOne = deepClone(this
|
|
1257
|
-
this
|
|
1245
|
+
const lastOne = deepClone(this._queue[this._queue.length - 1]);
|
|
1246
|
+
this._queue = [lastOne];
|
|
1258
1247
|
}
|
|
1259
1248
|
}
|
|
1260
1249
|
return Renderer;
|
package/dist/index.global.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var iDrawRenderer=function(){"use strict";function t(t){return"string"==typeof t&&/^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(t)}function e(t){return function t(e){const i=(s=e,Object.prototype.toString.call(s).replace(/[\]|\[]{1,1}/gi,"").split(" ")[1]);var s;if(["Null","Number","String","Boolean","Undefined"].indexOf(i)>=0)return e;if("Array"===i){const i=[];return e.forEach((e=>{i.push(t(e))})),i}if("Object"===i){const i={};return Object.keys(e).forEach((s=>{i[s]=t(e[s])})),i}}(t)}function i(t){return(Object.prototype.toString.call(t)||"").replace(/(\[object|\])/gi,"").trim()}const s={type(t,e){const s=i(t);return!0===e?s.toLocaleLowerCase():s},array:t=>"Array"===i(t),json:t=>"Object"===i(t),function:t=>"Function"===i(t),asyncFunction:t=>"AsyncFunction"===i(t),string:t=>"String"===i(t),number:t=>"Number"===i(t),undefined:t=>"Undefined"===i(t),null:t=>"Null"===i(t),promise:t=>"Promise"===i(t)};var o=globalThis&&globalThis.__awaiter||function(t,e,i,s){return new(i||(i=Promise))((function(o,r){function n(t){try{h(s.next(t))}catch(t){r(t)}}function a(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}h((s=s.apply(t,e||[])).next())}))};const{Image:r}=window;function n(t){return new Promise(((e,i)=>{const s=new r;s.crossOrigin="anonymous",s.onload=function(){e(s)},s.onabort=i,s.onerror=i,s.src=t}))}function a(t){return o(this,void 0,void 0,(function*(){const e=yield function(t){return new Promise(((e,i)=>{const s=new Blob([t],{type:"image/svg+xml;charset=utf-8"}),o=new FileReader;o.readAsDataURL(s),o.onload=function(t){var i;const s=null===(i=null==t?void 0:t.target)||void 0===i?void 0:i.result;e(s)},o.onerror=function(t){i(t)}}))}(t);return yield n(e)}))}function h(t,e){return o(this,void 0,void 0,(function*(){t=t.replace(/\&/gi,"&");const i=yield function(t,e){const{width:i,height:s}=e;return new Promise(((e,o)=>{const r=new Blob([`\n <svg xmlns="http://www.w3.org/2000/svg" width="${i||""}" height = "${s||""}">\n <foreignObject width="100%" height="100%">\n <div xmlns = "http://www.w3.org/1999/xhtml">\n ${t}\n </div>\n </foreignObject>\n </svg>\n `],{type:"image/svg+xml;charset=utf-8"}),n=new FileReader;n.readAsDataURL(r),n.onload=function(t){var i;const s=null===(i=null==t?void 0:t.target)||void 0===i?void 0:i.result;e(s)},n.onerror=function(t){o(t)}}))}(t,e);return yield n(i)}))}class l{constructor(t,e){this._opts=e,this._ctx=t,this._transform={scale:1,scrollX:0,scrollY:0}}getContext(){return this._ctx}resetSize(t){this._opts=Object.assign(Object.assign({},this._opts),t)}calcDeviceNum(t){return t*this._opts.devicePixelRatio}calcScreenNum(t){return t/this._opts.devicePixelRatio}getSize(){return{width:this._opts.width,height:this._opts.height,contextWidth:this._opts.contextWidth,contextHeight:this._opts.contextHeight,devicePixelRatio:this._opts.devicePixelRatio}}setTransform(t){this._transform=Object.assign(Object.assign({},this._transform),t)}getTransform(){return{scale:this._transform.scale,scrollX:this._transform.scrollX,scrollY:this._transform.scrollY}}setFillStyle(t){this._ctx.fillStyle=t}fill(t){return this._ctx.fill(t||"nonzero")}arc(t,e,i,s,o,r){return this._ctx.arc(this._doSize(t),this._doSize(e),this._doSize(i),s,o,r)}rect(t,e,i,s){return this._ctx.rect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}fillRect(t,e,i,s){return this._ctx.fillRect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}clearRect(t,e,i,s){return this._ctx.clearRect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}beginPath(){return this._ctx.beginPath()}closePath(){return this._ctx.closePath()}lineTo(t,e){return this._ctx.lineTo(this._doSize(t),this._doSize(e))}moveTo(t,e){return this._ctx.moveTo(this._doSize(t),this._doSize(e))}arcTo(t,e,i,s,o){return this._ctx.arcTo(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s),this._doSize(o))}setLineWidth(t){return this._ctx.lineWidth=this._doSize(t)}setLineDash(t){return this._ctx.setLineDash(t.map((t=>this._doSize(t))))}isPointInPath(t,e){return this._ctx.isPointInPath(this._doX(t),this._doY(e))}isPointInPathWithoutScroll(t,e){return this._ctx.isPointInPath(this._doSize(t),this._doSize(e))}setStrokeStyle(t){this._ctx.strokeStyle=t}stroke(){return this._ctx.stroke()}translate(t,e){return this._ctx.translate(this._doSize(t),this._doSize(e))}rotate(t){return this._ctx.rotate(t)}drawImage(...t){const e=t[0],i=t[1],s=t[2],o=t[3],r=t[4],n=t[t.length-4],a=t[t.length-3],h=t[t.length-2],l=t[t.length-1];return 9===t.length?this._ctx.drawImage(e,this._doSize(i),this._doSize(s),this._doSize(o),this._doSize(r),this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l)):this._ctx.drawImage(e,this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l))}createPattern(t,e){return this._ctx.createPattern(t,e)}measureText(t){return this._ctx.measureText(t)}setTextAlign(t){this._ctx.textAlign=t}fillText(t,e,i,s){return void 0!==s?this._ctx.fillText(t,this._doSize(e),this._doSize(i),this._doSize(s)):this._ctx.fillText(t,this._doSize(e),this._doSize(i))}strokeText(t,e,i,s){return void 0!==s?this._ctx.strokeText(t,this._doSize(e),this._doSize(i),this._doSize(s)):this._ctx.strokeText(t,this._doSize(e),this._doSize(i))}setFont(t){const e=[];"bold"===t.fontWeight&&e.push(`${t.fontWeight}`),e.push(`${this._doSize(t.fontSize||12)}px`),e.push(`${t.fontFamily||"sans-serif"}`),this._ctx.font=`${e.join(" ")}`}setTextBaseline(t){this._ctx.textBaseline=t}setGlobalAlpha(t){this._ctx.globalAlpha=t}save(){this._ctx.save()}restore(){this._ctx.restore()}scale(t,e){this._ctx.scale(t,e)}setShadowColor(t){this._ctx.shadowColor=t}setShadowOffsetX(t){this._ctx.shadowOffsetX=this._doSize(t)}setShadowOffsetY(t){this._ctx.shadowOffsetY=this._doSize(t)}setShadowBlur(t){this._ctx.shadowBlur=this._doSize(t)}ellipse(t,e,i,s,o,r,n,a){this._ctx.ellipse(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s),o,r,n,a)}_doSize(t){return this._opts.devicePixelRatio*t}_doX(t){const{scale:e,scrollX:i}=this._transform,s=(t-i)/e;return this._doSize(s)}_doY(t){const{scale:e,scrollY:i}=this._transform,s=(t-i)/e;return this._doSize(s)}}function c(t){return"number"==typeof t&&(t>0||t<=0)}function u(t){return"number"==typeof t&&t>=0}function d(t){return"string"==typeof t&&/^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${t}`)}function f(t){return"string"==typeof t&&/^(data:image\/)/.test(`${t}`)}const _={x:function(t){return c(t)},y:function(t){return c(t)},w:u,h:function(t){return"number"==typeof t&&t>=0},angle:function(t){return"number"==typeof t&&t>=-360&&t<=360},number:c,borderWidth:function(t){return u(t)},borderRadius:function(t){return c(t)&&t>=0},color:function(e){return t(e)},imageSrc:function(t){return f(t)||d(t)},imageURL:d,imageBase64:f,svg:function(t){return"string"==typeof t&&/^(<svg[\s]{1,}|<svg>)/i.test(`${t}`.trim())&&/<\/[\s]{0,}svg>$/i.test(`${t}`.trim())},html:function(t){let e=!1;if("string"==typeof t){let i=document.createElement("div");i.innerHTML=t,i.children.length>0&&(e=!0),i=null}return e},text:function(t){return"string"==typeof t},fontSize:function(t){return c(t)&&t>0},lineHeight:function(t){return c(t)&&t>0},textAlign:function(t){return["center","left","right"].includes(t)},fontFamily:function(t){return"string"==typeof t&&t.length>0},fontWeight:function(t){return["bold"].includes(t)},strokeWidth:function(t){return c(t)&&t>0}};function g(t,e,i){const s=function(t){return{x:t.x+t.w/2,y:t.y+t.h/2}}(e);return function(t,e,i,s){e&&(i>0||i<0)&&(t.translate(e.x,e.y),t.rotate(i),t.translate(-e.x,-e.y));s(t),e&&(i>0||i<0)&&(t.translate(e.x,e.y),t.rotate(-i),t.translate(-e.x,-e.y))}(t,s,(e.angle||0)/180*Math.PI||0,i)}function m(t){t.setFillStyle("#000000"),t.setStrokeStyle("#000000"),t.setLineDash([]),t.setGlobalAlpha(1),t.setShadowColor("#00000000"),t.setShadowOffsetX(0),t.setShadowOffsetY(0),t.setShadowBlur(0)}function x(e,i,o){m(e),function(e,i){m(e),g(e,i,(()=>{if(!(i.desc.borderWidth&&i.desc.borderWidth>0))return;const s=i.desc.borderWidth;let o="#000000";!0===t(i.desc.borderColor)&&(o=i.desc.borderColor);const r=i.x-s/2,n=i.y-s/2,a=i.w+s,h=i.h+s;let l=i.desc.borderRadius||0;l=Math.min(l,a/2,h/2),l<a/2&&l<h/2&&(l+=s/2);const{desc:c}=i;void 0!==c.shadowColor&&t(c.shadowColor)&&e.setShadowColor(c.shadowColor),void 0!==c.shadowOffsetX&&_.number(c.shadowOffsetX)&&e.setShadowOffsetX(c.shadowOffsetX),void 0!==c.shadowOffsetY&&_.number(c.shadowOffsetY)&&e.setShadowOffsetY(c.shadowOffsetY),void 0!==c.shadowBlur&&_.number(c.shadowBlur)&&e.setShadowBlur(c.shadowBlur),e.beginPath(),e.setLineWidth(s),e.setStrokeStyle(o),e.moveTo(r+l,n),e.arcTo(r+a,n,r+a,n+h,l),e.arcTo(r+a,n+h,r,n+h,l),e.arcTo(r,n+h,r,n,l),e.arcTo(r,n,r+a,n,l),e.closePath(),e.stroke()}))}(e,i),m(e),g(e,i,(()=>{const{x:t,y:r,w:n,h:a}=i;let h=i.desc.borderRadius||0;h=Math.min(h,n/2,a/2),(n<2*h||a<2*h)&&(h=0),e.beginPath(),e.moveTo(t+h,r),e.arcTo(t+n,r,t+n,r+a,h),e.arcTo(t+n,r+a,t,r+a,h),e.arcTo(t,r+a,t,r,h),e.arcTo(t,r,t+n,r,h),e.closePath(),("string"==typeof o||["CanvasPattern"].includes(s.type(o)))&&e.setFillStyle(o),e.fill()}))}function S(t,e){x(t,e,e.desc.bgColor)}function w(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function p(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function y(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function v(e,i,s){m(e),x(e,i,i.desc.bgColor||"transparent"),g(e,i,(()=>{const s={fontSize:12,fontFamily:"sans-serif",textAlign:"center",...i.desc};e.setFillStyle(i.desc.color),e.setTextBaseline("top"),e.setFont({fontWeight:s.fontWeight,fontSize:s.fontSize,fontFamily:s.fontFamily});const o=s.text.replace(/\r\n/gi,"\n"),r=s.lineHeight||s.fontSize,n=o.split("\n"),a=[];let h=0;n.forEach(((t,s)=>{let o="";if(t.length>0){for(let l=0;l<t.length&&(e.measureText(o+(t[l]||"")).width<e.calcDeviceNum(i.w)?o+=t[l]||"":(a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),o=t[l]||"",h++),!((h+1)*r>i.h));l++)if(t.length-1===l&&(h+1)*r<i.h){a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),s<n.length-1&&h++;break}}else a.push({text:"",width:0})}));let l=0;a.length*r<i.h&&("top"===i.desc.verticalAlign?l=0:"bottom"===i.desc.verticalAlign?l+=i.h-a.length*r:l+=(i.h-a.length*r)/2);{const o=i.y+l;void 0!==s.textShadowColor&&t(s.textShadowColor)&&e.setShadowColor(s.textShadowColor),void 0!==s.textShadowOffsetX&&_.number(s.textShadowOffsetX)&&e.setShadowOffsetX(s.textShadowOffsetX),void 0!==s.textShadowOffsetY&&_.number(s.textShadowOffsetY)&&e.setShadowOffsetY(s.textShadowOffsetY),void 0!==s.textShadowBlur&&_.number(s.textShadowBlur)&&e.setShadowBlur(s.textShadowBlur),a.forEach(((t,n)=>{let a=i.x;"center"===s.textAlign?a=i.x+(i.w-t.width)/2:"right"===s.textAlign&&(a=i.x+(i.w-t.width)),e.fillText(t.text,a,o+r*n)})),m(e)}if(t(s.strokeColor)&&void 0!==s.strokeWidth&&s.strokeWidth>0){const t=i.y+l;a.forEach(((o,n)=>{let a=i.x;"center"===s.textAlign?a=i.x+(i.w-o.width)/2:"right"===s.textAlign&&(a=i.x+(i.w-o.width)),void 0!==s.strokeColor&&e.setStrokeStyle(s.strokeColor),void 0!==s.strokeWidth&&s.strokeWidth>0&&e.setLineWidth(s.strokeWidth),e.strokeText(o.text,a,t+r*n)}))}}))}function b(t,e){m(t),g(t,e,(t=>{const{x:i,y:s,w:o,h:r,desc:n}=e,{bgColor:a="#000000",borderColor:h="#000000",borderWidth:l=0}=n,c=o/2,u=r/2,d=i+c,f=s+u;if(l&&l>0){const e=l/2+c,i=l/2+u;t.beginPath(),t.setStrokeStyle(h),t.setLineWidth(l),t.ellipse(d,f,e,i,0,0,2*Math.PI),t.closePath(),t.stroke()}t.beginPath(),t.setFillStyle(a),t.ellipse(d,f,c,u,0,0,2*Math.PI),t.closePath(),t.fill()}))}function z(e,i,s){var o;m(e);const r=e.getSize();if(e.clearRect(0,0,r.contextWidth,r.contextHeight),"string"==typeof i.bgColor&&t(i.bgColor)&&function(t,e){const i=t.getSize();t.setFillStyle(e),t.fillRect(0,0,i.contextWidth,i.contextHeight)}(e,i.bgColor),i.elements.length>0)for(let t=0;t<i.elements.length;t++){const r=i.elements[t];if(!0!==(null==(o=null==r?void 0:r.operation)?void 0:o.invisible))switch(r.type){case"rect":S(e,r);break;case"text":v(e,r);break;case"image":w(e,r,s);break;case"svg":p(e,r,s);break;case"html":y(e,r,s);break;case"circle":b(e,r)}}}class L{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);null==i||i.push(e),this._listeners.set(t,i||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);if(Array.isArray(i))for(let t=0;t<(null==i?void 0:i.length);t++)if(i[t]===e){i.splice(t,1);break}this._listeners.set(t,i||[])}}trigger(t,e){const i=this._listeners.get(t);return!!Array.isArray(i)&&(i.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}class D{constructor(t){this._currentLoadData={},this._currentUUIDQueue=[],this._storageLoadData={},this._status="free",this._waitingLoadQueue=[],this._opts=t,this._event=new L,this._waitingLoadQueue=[]}load(t,e){const[i,s]=this._resetLoadData(t,e);"free"===this._status||"complete"===this._status?(this._currentUUIDQueue=i,this._currentLoadData=s,this._loadTask()):"loading"===this._status&&i.length>0&&this._waitingLoadQueue.push({uuidQueue:i,loadData:s})}on(t,e){this._event.on(t,e)}off(t,e){this._event.off(t,e)}isComplete(){return"complete"===this._status}getContent(t){var e;return"loaded"===(null==(e=this._storageLoadData[t])?void 0:e.status)?this._storageLoadData[t].content:null}_resetLoadData(t,e){const i={},s=[],o=this._storageLoadData;for(let r=t.elements.length-1;r>=0;r--){const n=t.elements[r];["image","svg","html"].includes(n.type)&&(o[n.uuid]?e.includes(n.uuid)&&(i[n.uuid]=this._createEmptyLoadItem(n),s.push(n.uuid)):(i[n.uuid]=this._createEmptyLoadItem(n),s.push(n.uuid)))}return[s,i]}_createEmptyLoadItem(t){let i="";const s=t.type;let o=t.w,r=t.h;if("image"===t.type){i=t.desc.src||""}else if("svg"===t.type){i=t.desc.svg||""}else if("html"===t.type){const e=t;i=(e.desc.html||"").replace(/<script[\s\S]*?<\/script>/gi,""),o=e.desc.width||t.w,r=e.desc.height||t.h}return{uuid:t.uuid,type:s,status:"null",content:null,source:i,elemW:o,elemH:r,element:e(t)}}_loadTask(){if("loading"===this._status)return;if(this._status="loading",0===this._currentUUIDQueue.length){if(0===this._waitingLoadQueue.length)return this._status="complete",void this._event.trigger("complete",void 0);{const t=this._waitingLoadQueue.shift();if(t){const{uuidQueue:e,loadData:i}=t;this._currentLoadData=i,this._currentUUIDQueue=e}}}const{maxParallelNum:t}=this._opts,e=this._currentUUIDQueue.splice(0,t);e.forEach(((t,e)=>{}));const i=[],s=()=>{if(i.length>=t)return!1;if(0===e.length)return!0;for(let o=i.length;o<t;o++){const t=e.shift();if(void 0===t)break;i.push(t),this._loadElementSource(this._currentLoadData[t]).then((o=>{var r,n;i.splice(i.indexOf(t),1);const a=s();this._storageLoadData[t]={uuid:t,type:this._currentLoadData[t].type,status:"loaded",content:o,source:this._currentLoadData[t].source,elemW:this._currentLoadData[t].elemW,elemH:this._currentLoadData[t].elemH,element:this._currentLoadData[t].element},0===i.length&&0===e.length&&!0===a&&(this._status="free",this._loadTask()),this._event.trigger("load",{uuid:null==(r=this._storageLoadData[t])?void 0:r.uuid,type:this._storageLoadData[t].type,status:this._storageLoadData[t].status,content:this._storageLoadData[t].content,source:this._storageLoadData[t].source,elemW:this._storageLoadData[t].elemW,elemH:this._storageLoadData[t].elemH,element:null==(n=this._storageLoadData[t])?void 0:n.element})})).catch((o=>{var r,n,a,h,l,c,u,d,f,_,g,m;console.warn(o),i.splice(i.indexOf(t),1);const x=s();this._currentLoadData[t]&&(this._storageLoadData[t]={uuid:t,type:null==(r=this._currentLoadData[t])?void 0:r.type,status:"fail",content:null,error:o,source:null==(n=this._currentLoadData[t])?void 0:n.source,elemW:null==(a=this._currentLoadData[t])?void 0:a.elemW,elemH:null==(h=this._currentLoadData[t])?void 0:h.elemH,element:null==(l=this._currentLoadData[t])?void 0:l.element}),0===i.length&&0===e.length&&!0===x&&(this._status="free",this._loadTask()),this._currentLoadData[t]&&this._event.trigger("error",{uuid:t,type:null==(c=this._storageLoadData[t])?void 0:c.type,status:null==(u=this._storageLoadData[t])?void 0:u.status,content:null==(d=this._storageLoadData[t])?void 0:d.content,source:null==(f=this._storageLoadData[t])?void 0:f.source,elemW:null==(_=this._storageLoadData[t])?void 0:_.elemW,elemH:null==(g=this._storageLoadData[t])?void 0:g.elemH,element:null==(m=this._storageLoadData[t])?void 0:m.element})}))}return!1};s()}async _loadElementSource(t){if(t&&"image"===t.type){return await n(t.source)}if(t&&"svg"===t.type){return await a(t.source)}if(t&&"html"===t.type){return await h(t.source,{width:t.elemW,height:t.elemH})}throw Error("Element's source is not support!")}}class T{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);null==i||i.push(e),this._listeners.set(t,i||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);if(Array.isArray(i))for(let t=0;t<(null==i?void 0:i.length);t++)if(i[t]===e){i.splice(t,1);break}this._listeners.set(t,i||[])}}trigger(t,e){const i=this._listeners.get(t);return!!Array.isArray(i)&&(i.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}const P=Symbol("_queue"),O=Symbol("_ctx"),C=Symbol("_status"),W=Symbol("_loader"),k=Symbol("_opts"),A=Symbol("_freeze"),I=Symbol("_drawFrame"),R=Symbol("_retainQueueOneItem");var F,$,H;const{requestAnimationFrame:B}=window;class E extends T{constructor(t){super(),this[F]=[],this[$]=null,this[H]="null",this[k]=t,this[W]=new D({maxParallelNum:6}),this[W].on("load",(t=>{this[I](),this.trigger("load",{element:t.element})})),this[W].on("error",(t=>{this.trigger("error",{element:t.element,error:t.error})})),this[W].on("complete",(()=>{this.trigger("loadComplete",{t:Date.now()})}))}render(t,i,s){const{changeResourceUUIDs:o=[]}=s||{};this[C]="free";const r=e(i);if(Array.isArray(r.elements)&&r.elements.forEach((t=>{"string"==typeof t.uuid&&t.uuid||(t.uuid=function(){function t(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return`${t()}${t()}-${t()}-${t()}-${t()}-${t()}${t()}${t()}`}())})),!this[O])if(this[k]&&"[object HTMLCanvasElement]"===Object.prototype.toString.call(t)){const{width:e,height:i,contextWidth:s,contextHeight:o,devicePixelRatio:r}=this[k],n=t;n.width=e*r,n.height=i*r;const a=n.getContext("2d");this[O]=new l(a,{width:e,height:i,contextWidth:s||e,contextHeight:o||i,devicePixelRatio:r})}else t&&(this[O]=t);if(["freeze"].includes(this[C]))return;const n=e({data:r});this[P].push(n),this[I](),this[W].load(r,o||[])}getContext(){return this[O]}thaw(){this[C]="free"}[(F=P,$=O,H=C,A)](){this[C]="freeze"}[I](){"freeze"!==this[C]&&B((()=>{if("freeze"===this[C])return;const t=this[O];let e=this[P][0],i=!1;this[P].length>1?e=this[P].shift():i=!0,!0!==this[W].isComplete()?(this[I](),e&&t&&z(t,e.data,this[W])):e&&t?(z(t,e.data,this[W]),this[R](),i?this[C]="free":this[I]()):this[C]="free",this.trigger("drawFrame",{t:Date.now()}),!0===this[W].isComplete()&&1===this[P].length&&"free"===this[C]&&(t&&this[P][0]&&this[P][0].data&&z(t,this[P][0].data,this[W]),this.trigger("drawFrameComplete",{t:Date.now()}),this[A]())}))}[R](){if(this[P].length<=1)return;const t=e(this[P][this[P].length-1]);this[P]=[t]}}return E}();
|
|
1
|
+
var iDrawRenderer=function(){"use strict";function t(t){return"string"==typeof t&&/^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(t)}function e(t){return function t(e){const s=(i=e,Object.prototype.toString.call(i).replace(/[\]|\[]{1,1}/gi,"").split(" ")[1]);var i;if(["Null","Number","String","Boolean","Undefined"].indexOf(s)>=0)return e;if("Array"===s){const s=[];return e.forEach((e=>{s.push(t(e))})),s}if("Object"===s){const s={};return Object.keys(e).forEach((i=>{s[i]=t(e[i])})),s}}(t)}function s(t){return(Object.prototype.toString.call(t)||"").replace(/(\[object|\])/gi,"").trim()}const i={type(t,e){const i=s(t);return!0===e?i.toLocaleLowerCase():i},array:t=>"Array"===s(t),json:t=>"Object"===s(t),function:t=>"Function"===s(t),asyncFunction:t=>"AsyncFunction"===s(t),string:t=>"String"===s(t),number:t=>"Number"===s(t),undefined:t=>"Undefined"===s(t),null:t=>"Null"===s(t),promise:t=>"Promise"===s(t)};var o=globalThis&&globalThis.__awaiter||function(t,e,s,i){return new(s||(s=Promise))((function(o,r){function n(t){try{h(i.next(t))}catch(t){r(t)}}function a(t){try{h(i.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,a)}h((i=i.apply(t,e||[])).next())}))};const{Image:r}=window;function n(t){return new Promise(((e,s)=>{const i=new r;i.crossOrigin="anonymous",i.onload=function(){e(i)},i.onabort=s,i.onerror=s,i.src=t}))}function a(t){return o(this,void 0,void 0,(function*(){const e=yield function(t){return new Promise(((e,s)=>{const i=new Blob([t],{type:"image/svg+xml;charset=utf-8"}),o=new FileReader;o.readAsDataURL(i),o.onload=function(t){var s;const i=null===(s=null==t?void 0:t.target)||void 0===s?void 0:s.result;e(i)},o.onerror=function(t){s(t)}}))}(t);return yield n(e)}))}function h(t,e){return o(this,void 0,void 0,(function*(){t=t.replace(/\&/gi,"&");const s=yield function(t,e){const{width:s,height:i}=e;return new Promise(((e,o)=>{const r=new Blob([`\n <svg xmlns="http://www.w3.org/2000/svg" width="${s||""}" height = "${i||""}">\n <foreignObject width="100%" height="100%">\n <div xmlns = "http://www.w3.org/1999/xhtml">\n ${t}\n </div>\n </foreignObject>\n </svg>\n `],{type:"image/svg+xml;charset=utf-8"}),n=new FileReader;n.readAsDataURL(r),n.onload=function(t){var s;const i=null===(s=null==t?void 0:t.target)||void 0===s?void 0:s.result;e(i)},n.onerror=function(t){o(t)}}))}(t,e);return yield n(s)}))}class l{constructor(t,e){this._opts=e,this._ctx=t,this._transform={scale:1,scrollX:0,scrollY:0}}getContext(){return this._ctx}resetSize(t){this._opts=Object.assign(Object.assign({},this._opts),t)}calcDeviceNum(t){return t*this._opts.devicePixelRatio}calcScreenNum(t){return t/this._opts.devicePixelRatio}getSize(){return{width:this._opts.width,height:this._opts.height,contextWidth:this._opts.contextWidth,contextHeight:this._opts.contextHeight,devicePixelRatio:this._opts.devicePixelRatio}}setTransform(t){this._transform=Object.assign(Object.assign({},this._transform),t)}getTransform(){return{scale:this._transform.scale,scrollX:this._transform.scrollX,scrollY:this._transform.scrollY}}setFillStyle(t){this._ctx.fillStyle=t}fill(t){return this._ctx.fill(t||"nonzero")}arc(t,e,s,i,o,r){return this._ctx.arc(this._doSize(t),this._doSize(e),this._doSize(s),i,o,r)}rect(t,e,s,i){return this._ctx.rect(this._doSize(t),this._doSize(e),this._doSize(s),this._doSize(i))}fillRect(t,e,s,i){return this._ctx.fillRect(this._doSize(t),this._doSize(e),this._doSize(s),this._doSize(i))}clearRect(t,e,s,i){return this._ctx.clearRect(this._doSize(t),this._doSize(e),this._doSize(s),this._doSize(i))}beginPath(){return this._ctx.beginPath()}closePath(){return this._ctx.closePath()}lineTo(t,e){return this._ctx.lineTo(this._doSize(t),this._doSize(e))}moveTo(t,e){return this._ctx.moveTo(this._doSize(t),this._doSize(e))}arcTo(t,e,s,i,o){return this._ctx.arcTo(this._doSize(t),this._doSize(e),this._doSize(s),this._doSize(i),this._doSize(o))}setLineWidth(t){return this._ctx.lineWidth=this._doSize(t)}setLineDash(t){return this._ctx.setLineDash(t.map((t=>this._doSize(t))))}isPointInPath(t,e){return this._ctx.isPointInPath(this._doX(t),this._doY(e))}isPointInPathWithoutScroll(t,e){return this._ctx.isPointInPath(this._doSize(t),this._doSize(e))}setStrokeStyle(t){this._ctx.strokeStyle=t}stroke(){return this._ctx.stroke()}translate(t,e){return this._ctx.translate(this._doSize(t),this._doSize(e))}rotate(t){return this._ctx.rotate(t)}drawImage(...t){const e=t[0],s=t[1],i=t[2],o=t[3],r=t[4],n=t[t.length-4],a=t[t.length-3],h=t[t.length-2],l=t[t.length-1];return 9===t.length?this._ctx.drawImage(e,this._doSize(s),this._doSize(i),this._doSize(o),this._doSize(r),this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l)):this._ctx.drawImage(e,this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l))}createPattern(t,e){return this._ctx.createPattern(t,e)}measureText(t){return this._ctx.measureText(t)}setTextAlign(t){this._ctx.textAlign=t}fillText(t,e,s,i){return void 0!==i?this._ctx.fillText(t,this._doSize(e),this._doSize(s),this._doSize(i)):this._ctx.fillText(t,this._doSize(e),this._doSize(s))}strokeText(t,e,s,i){return void 0!==i?this._ctx.strokeText(t,this._doSize(e),this._doSize(s),this._doSize(i)):this._ctx.strokeText(t,this._doSize(e),this._doSize(s))}setFont(t){const e=[];"bold"===t.fontWeight&&e.push(`${t.fontWeight}`),e.push(`${this._doSize(t.fontSize||12)}px`),e.push(`${t.fontFamily||"sans-serif"}`),this._ctx.font=`${e.join(" ")}`}setTextBaseline(t){this._ctx.textBaseline=t}setGlobalAlpha(t){this._ctx.globalAlpha=t}save(){this._ctx.save()}restore(){this._ctx.restore()}scale(t,e){this._ctx.scale(t,e)}setShadowColor(t){this._ctx.shadowColor=t}setShadowOffsetX(t){this._ctx.shadowOffsetX=this._doSize(t)}setShadowOffsetY(t){this._ctx.shadowOffsetY=this._doSize(t)}setShadowBlur(t){this._ctx.shadowBlur=this._doSize(t)}ellipse(t,e,s,i,o,r,n,a){this._ctx.ellipse(this._doSize(t),this._doSize(e),this._doSize(s),this._doSize(i),o,r,n,a)}_doSize(t){return this._opts.devicePixelRatio*t}_doX(t){const{scale:e,scrollX:s}=this._transform,i=(t-s)/e;return this._doSize(i)}_doY(t){const{scale:e,scrollY:s}=this._transform,i=(t-s)/e;return this._doSize(i)}}function u(t){return"number"==typeof t&&(t>0||t<=0)}function c(t){return"number"==typeof t&&t>=0}function d(t){return"string"==typeof t&&/^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${t}`)}function _(t){return"string"==typeof t&&/^(data:image\/)/.test(`${t}`)}const f={x:function(t){return u(t)},y:function(t){return u(t)},w:c,h:function(t){return"number"==typeof t&&t>=0},angle:function(t){return"number"==typeof t&&t>=-360&&t<=360},number:u,borderWidth:function(t){return c(t)},borderRadius:function(t){return u(t)&&t>=0},color:function(e){return t(e)},imageSrc:function(t){return _(t)||d(t)},imageURL:d,imageBase64:_,svg:function(t){return"string"==typeof t&&/^(<svg[\s]{1,}|<svg>)/i.test(`${t}`.trim())&&/<\/[\s]{0,}svg>$/i.test(`${t}`.trim())},html:function(t){let e=!1;if("string"==typeof t){let s=document.createElement("div");s.innerHTML=t,s.children.length>0&&(e=!0),s=null}return e},text:function(t){return"string"==typeof t},fontSize:function(t){return u(t)&&t>0},lineHeight:function(t){return u(t)&&t>0},textAlign:function(t){return["center","left","right"].includes(t)},fontFamily:function(t){return"string"==typeof t&&t.length>0},fontWeight:function(t){return["bold"].includes(t)},strokeWidth:function(t){return u(t)&&t>0}};function g(t,e,s){const i=function(t){return{x:t.x+t.w/2,y:t.y+t.h/2}}(e);return function(t,e,s,i){e&&(s>0||s<0)&&(t.translate(e.x,e.y),t.rotate(s),t.translate(-e.x,-e.y));i(t),e&&(s>0||s<0)&&(t.translate(e.x,e.y),t.rotate(-s),t.translate(-e.x,-e.y))}(t,i,(e.angle||0)/180*Math.PI||0,s)}function m(t){t.setFillStyle("#000000"),t.setStrokeStyle("#000000"),t.setLineDash([]),t.setGlobalAlpha(1),t.setShadowColor("#00000000"),t.setShadowOffsetX(0),t.setShadowOffsetY(0),t.setShadowBlur(0)}function x(e,s,o){m(e),function(e,s){m(e),g(e,s,(()=>{if(!(s.desc.borderWidth&&s.desc.borderWidth>0))return;const i=s.desc.borderWidth;let o="#000000";!0===t(s.desc.borderColor)&&(o=s.desc.borderColor);const r=s.x-i/2,n=s.y-i/2,a=s.w+i,h=s.h+i;let l=s.desc.borderRadius||0;l=Math.min(l,a/2,h/2),l<a/2&&l<h/2&&(l+=i/2);const{desc:u}=s;void 0!==u.shadowColor&&t(u.shadowColor)&&e.setShadowColor(u.shadowColor),void 0!==u.shadowOffsetX&&f.number(u.shadowOffsetX)&&e.setShadowOffsetX(u.shadowOffsetX),void 0!==u.shadowOffsetY&&f.number(u.shadowOffsetY)&&e.setShadowOffsetY(u.shadowOffsetY),void 0!==u.shadowBlur&&f.number(u.shadowBlur)&&e.setShadowBlur(u.shadowBlur),e.beginPath(),e.setLineWidth(i),e.setStrokeStyle(o),e.moveTo(r+l,n),e.arcTo(r+a,n,r+a,n+h,l),e.arcTo(r+a,n+h,r,n+h,l),e.arcTo(r,n+h,r,n,l),e.arcTo(r,n,r+a,n,l),e.closePath(),e.stroke()}))}(e,s),m(e),g(e,s,(()=>{const{x:t,y:r,w:n,h:a}=s;let h=s.desc.borderRadius||0;h=Math.min(h,n/2,a/2),(n<2*h||a<2*h)&&(h=0),e.beginPath(),e.moveTo(t+h,r),e.arcTo(t+n,r,t+n,r+a,h),e.arcTo(t+n,r+a,t,r+a,h),e.arcTo(t,r+a,t,r,h),e.arcTo(t,r,t+n,r,h),e.closePath(),("string"==typeof o||["CanvasPattern"].includes(i.type(o)))&&e.setFillStyle(o),e.fill()}))}function w(t,e){x(t,e,e.desc.bgColor)}function S(t,e,s){const i=s.getContent(e.uuid);g(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function p(t,e,s){const i=s.getContent(e.uuid);g(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function y(t,e,s){const i=s.getContent(e.uuid);g(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function v(e,s,i){m(e),x(e,s,s.desc.bgColor||"transparent"),g(e,s,(()=>{const i={fontSize:12,fontFamily:"sans-serif",textAlign:"center",...s.desc};e.setFillStyle(s.desc.color),e.setTextBaseline("top"),e.setFont({fontWeight:i.fontWeight,fontSize:i.fontSize,fontFamily:i.fontFamily});const o=i.text.replace(/\r\n/gi,"\n"),r=i.lineHeight||i.fontSize,n=o.split("\n"),a=[];let h=0;n.forEach(((t,i)=>{let o="";if(t.length>0){for(let l=0;l<t.length&&(e.measureText(o+(t[l]||"")).width<e.calcDeviceNum(s.w)?o+=t[l]||"":(a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),o=t[l]||"",h++),!((h+1)*r>s.h));l++)if(t.length-1===l&&(h+1)*r<s.h){a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),i<n.length-1&&h++;break}}else a.push({text:"",width:0})}));let l=0;a.length*r<s.h&&("top"===s.desc.verticalAlign?l=0:"bottom"===s.desc.verticalAlign?l+=s.h-a.length*r:l+=(s.h-a.length*r)/2);{const o=s.y+l;void 0!==i.textShadowColor&&t(i.textShadowColor)&&e.setShadowColor(i.textShadowColor),void 0!==i.textShadowOffsetX&&f.number(i.textShadowOffsetX)&&e.setShadowOffsetX(i.textShadowOffsetX),void 0!==i.textShadowOffsetY&&f.number(i.textShadowOffsetY)&&e.setShadowOffsetY(i.textShadowOffsetY),void 0!==i.textShadowBlur&&f.number(i.textShadowBlur)&&e.setShadowBlur(i.textShadowBlur),a.forEach(((t,n)=>{let a=s.x;"center"===i.textAlign?a=s.x+(s.w-t.width)/2:"right"===i.textAlign&&(a=s.x+(s.w-t.width)),e.fillText(t.text,a,o+r*n)})),m(e)}if(t(i.strokeColor)&&void 0!==i.strokeWidth&&i.strokeWidth>0){const t=s.y+l;a.forEach(((o,n)=>{let a=s.x;"center"===i.textAlign?a=s.x+(s.w-o.width)/2:"right"===i.textAlign&&(a=s.x+(s.w-o.width)),void 0!==i.strokeColor&&e.setStrokeStyle(i.strokeColor),void 0!==i.strokeWidth&&i.strokeWidth>0&&e.setLineWidth(i.strokeWidth),e.strokeText(o.text,a,t+r*n)}))}}))}function z(t,e){m(t),g(t,e,(t=>{const{x:s,y:i,w:o,h:r,desc:n}=e,{bgColor:a="#000000",borderColor:h="#000000",borderWidth:l=0}=n,u=o/2,c=r/2,d=s+u,_=i+c;if(l&&l>0){const e=l/2+u,s=l/2+c;t.beginPath(),t.setStrokeStyle(h),t.setLineWidth(l),t.ellipse(d,_,e,s,0,0,2*Math.PI),t.closePath(),t.stroke()}t.beginPath(),t.setFillStyle(a),t.ellipse(d,_,u,c,0,0,2*Math.PI),t.closePath(),t.fill()}))}function b(e,s,i){var o;m(e);const r=e.getSize();if(e.clearRect(0,0,r.contextWidth,r.contextHeight),"string"==typeof s.bgColor&&t(s.bgColor)&&function(t,e){const s=t.getSize();t.setFillStyle(e),t.fillRect(0,0,s.contextWidth,s.contextHeight)}(e,s.bgColor),s.elements.length>0)for(let t=0;t<s.elements.length;t++){const r=s.elements[t];if(!0!==(null==(o=null==r?void 0:r.operation)?void 0:o.invisible))switch(r.type){case"rect":w(e,r);break;case"text":v(e,r);break;case"image":S(e,r,i);break;case"svg":p(e,r,i);break;case"html":y(e,r,i);break;case"circle":z(e,r)}}}class L{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const s=this._listeners.get(t);null==s||s.push(e),this._listeners.set(t,s||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const s=this._listeners.get(t);if(Array.isArray(s))for(let t=0;t<(null==s?void 0:s.length);t++)if(s[t]===e){s.splice(t,1);break}this._listeners.set(t,s||[])}}trigger(t,e){const s=this._listeners.get(t);return!!Array.isArray(s)&&(s.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}class D{constructor(t){this._currentLoadData={},this._currentUUIDQueue=[],this._storageLoadData={},this._status="free",this._waitingLoadQueue=[],this._opts=t,this._event=new L,this._waitingLoadQueue=[]}load(t,e){const[s,i]=this._resetLoadData(t,e);"free"===this._status||"complete"===this._status?(this._currentUUIDQueue=s,this._currentLoadData=i,this._loadTask()):"loading"===this._status&&s.length>0&&this._waitingLoadQueue.push({uuidQueue:s,loadData:i})}on(t,e){this._event.on(t,e)}off(t,e){this._event.off(t,e)}isComplete(){return"complete"===this._status}getContent(t){var e;return"loaded"===(null==(e=this._storageLoadData[t])?void 0:e.status)?this._storageLoadData[t].content:null}_resetLoadData(t,e){const s={},i=[],o=this._storageLoadData;for(let r=t.elements.length-1;r>=0;r--){const n=t.elements[r];["image","svg","html"].includes(n.type)&&(o[n.uuid]?e.includes(n.uuid)&&(s[n.uuid]=this._createEmptyLoadItem(n),i.push(n.uuid)):(s[n.uuid]=this._createEmptyLoadItem(n),i.push(n.uuid)))}return[i,s]}_createEmptyLoadItem(t){let s="";const i=t.type;let o=t.w,r=t.h;if("image"===t.type){s=t.desc.src||""}else if("svg"===t.type){s=t.desc.svg||""}else if("html"===t.type){const e=t;s=(e.desc.html||"").replace(/<script[\s\S]*?<\/script>/gi,""),o=e.desc.width||t.w,r=e.desc.height||t.h}return{uuid:t.uuid,type:i,status:"null",content:null,source:s,elemW:o,elemH:r,element:e(t)}}_loadTask(){if("loading"===this._status)return;if(this._status="loading",0===this._currentUUIDQueue.length){if(0===this._waitingLoadQueue.length)return this._status="complete",void this._event.trigger("complete",void 0);{const t=this._waitingLoadQueue.shift();if(t){const{uuidQueue:e,loadData:s}=t;this._currentLoadData=s,this._currentUUIDQueue=e}}}const{maxParallelNum:t}=this._opts,e=this._currentUUIDQueue.splice(0,t);e.forEach(((t,e)=>{}));const s=[],i=()=>{if(s.length>=t)return!1;if(0===e.length)return!0;for(let o=s.length;o<t;o++){const t=e.shift();if(void 0===t)break;s.push(t),this._loadElementSource(this._currentLoadData[t]).then((o=>{var r,n;s.splice(s.indexOf(t),1);const a=i();this._storageLoadData[t]={uuid:t,type:this._currentLoadData[t].type,status:"loaded",content:o,source:this._currentLoadData[t].source,elemW:this._currentLoadData[t].elemW,elemH:this._currentLoadData[t].elemH,element:this._currentLoadData[t].element},0===s.length&&0===e.length&&!0===a&&(this._status="free",this._loadTask()),this._event.trigger("load",{uuid:null==(r=this._storageLoadData[t])?void 0:r.uuid,type:this._storageLoadData[t].type,status:this._storageLoadData[t].status,content:this._storageLoadData[t].content,source:this._storageLoadData[t].source,elemW:this._storageLoadData[t].elemW,elemH:this._storageLoadData[t].elemH,element:null==(n=this._storageLoadData[t])?void 0:n.element})})).catch((o=>{var r,n,a,h,l,u,c,d,_,f,g,m;console.warn(o),s.splice(s.indexOf(t),1);const x=i();this._currentLoadData[t]&&(this._storageLoadData[t]={uuid:t,type:null==(r=this._currentLoadData[t])?void 0:r.type,status:"fail",content:null,error:o,source:null==(n=this._currentLoadData[t])?void 0:n.source,elemW:null==(a=this._currentLoadData[t])?void 0:a.elemW,elemH:null==(h=this._currentLoadData[t])?void 0:h.elemH,element:null==(l=this._currentLoadData[t])?void 0:l.element}),0===s.length&&0===e.length&&!0===x&&(this._status="free",this._loadTask()),this._currentLoadData[t]&&this._event.trigger("error",{uuid:t,type:null==(u=this._storageLoadData[t])?void 0:u.type,status:null==(c=this._storageLoadData[t])?void 0:c.status,content:null==(d=this._storageLoadData[t])?void 0:d.content,source:null==(_=this._storageLoadData[t])?void 0:_.source,elemW:null==(f=this._storageLoadData[t])?void 0:f.elemW,elemH:null==(g=this._storageLoadData[t])?void 0:g.elemH,element:null==(m=this._storageLoadData[t])?void 0:m.element})}))}return!1};i()}async _loadElementSource(t){if(t&&"image"===t.type){return await n(t.source)}if(t&&"svg"===t.type){return await a(t.source)}if(t&&"html"===t.type){return await h(t.source,{width:t.elemW,height:t.elemH})}throw Error("Element's source is not support!")}}class T{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const s=this._listeners.get(t);null==s||s.push(e),this._listeners.set(t,s||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const s=this._listeners.get(t);if(Array.isArray(s))for(let t=0;t<(null==s?void 0:s.length);t++)if(s[t]===e){s.splice(t,1);break}this._listeners.set(t,s||[])}}trigger(t,e){const s=this._listeners.get(t);return!!Array.isArray(s)&&(s.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}const{requestAnimationFrame:O}=window;return class extends T{constructor(t){super(),this._queue=[],this._ctx=null,this._status="null",this._opts=t,this._loader=new D({maxParallelNum:6}),this._loader.on("load",(t=>{this._drawFrame(),this.trigger("load",{element:t.element})})),this._loader.on("error",(t=>{this.trigger("error",{element:t.element,error:t.error})})),this._loader.on("complete",(()=>{this.trigger("loadComplete",{t:Date.now()})}))}render(t,s,i){const{changeResourceUUIDs:o=[]}=i||{};this._status="free";const r=e(s);if(Array.isArray(r.elements)&&r.elements.forEach((t=>{"string"==typeof t.uuid&&t.uuid||(t.uuid=function(){function t(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return`${t()}${t()}-${t()}-${t()}-${t()}-${t()}${t()}${t()}`}())})),!this._ctx)if(this._opts&&"[object HTMLCanvasElement]"===Object.prototype.toString.call(t)){const{width:e,height:s,contextWidth:i,contextHeight:o,devicePixelRatio:r}=this._opts,n=t;n.width=e*r,n.height=s*r;const a=n.getContext("2d");this._ctx=new l(a,{width:e,height:s,contextWidth:i||e,contextHeight:o||s,devicePixelRatio:r})}else t&&(this._ctx=t);if(["freeze"].includes(this._status))return;const n=e({data:r});this._queue.push(n),this._drawFrame(),this._loader.load(r,o||[])}getContext(){return this._ctx}thaw(){this._status="free"}_freeze(){this._status="freeze"}_drawFrame(){"freeze"!==this._status&&O((()=>{if("freeze"===this._status)return;const t=this._ctx;let e=this._queue[0],s=!1;this._queue.length>1?e=this._queue.shift():s=!0,!0!==this._loader.isComplete()?(this._drawFrame(),e&&t&&b(t,e.data,this._loader)):e&&t?(b(t,e.data,this._loader),this._retainQueueOneItem(),s?this._status="free":this._drawFrame()):this._status="free",this.trigger("drawFrame",{t:Date.now()}),!0===this._loader.isComplete()&&1===this._queue.length&&"free"===this._status&&(t&&this._queue[0]&&this._queue[0].data&&b(t,this._queue[0].data,this._loader),this.trigger("drawFrameComplete",{t:Date.now()}),this._freeze())}))}_retainQueueOneItem(){if(this._queue.length<=1)return;const t=e(this._queue[this._queue.length-1]);this._queue=[t]}}}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idraw/renderer",
|
|
3
|
-
"version": "0.3.0-beta.
|
|
3
|
+
"version": "0.3.0-beta.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/esm/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"author": "chenshenhai",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@idraw/types": "^0.3.0-beta.
|
|
24
|
+
"@idraw/types": "^0.3.0-beta.7"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@idraw/util": "^0.3.0-beta.
|
|
27
|
+
"@idraw/util": "^0.3.0-beta.7"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "2be8fc67cf7ffb642e839d52a9caf9c62b41178d"
|
|
33
33
|
}
|
package/dist/esm/names.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
declare const _queue: unique symbol;
|
|
2
|
-
declare const _ctx: unique symbol;
|
|
3
|
-
declare const _status: unique symbol;
|
|
4
|
-
declare const _loader: unique symbol;
|
|
5
|
-
declare const _opts: unique symbol;
|
|
6
|
-
declare const _freeze: unique symbol;
|
|
7
|
-
declare const _drawFrame: unique symbol;
|
|
8
|
-
declare const _retainQueueOneItem: unique symbol;
|
|
9
|
-
export { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem };
|
package/dist/esm/names.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
const _queue = Symbol('_queue');
|
|
2
|
-
const _ctx = Symbol('_ctx');
|
|
3
|
-
const _status = Symbol('_status');
|
|
4
|
-
const _loader = Symbol('_loader');
|
|
5
|
-
const _opts = Symbol('_opts');
|
|
6
|
-
const _freeze = Symbol('_freeze');
|
|
7
|
-
const _drawFrame = Symbol('_drawFrame');
|
|
8
|
-
const _retainQueueOneItem = Symbol('_retainQueueOneItem');
|
|
9
|
-
export { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem };
|