@idraw/renderer 0.2.0-alpha.26 → 0.3.0-alpha.2
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/index.cjs.js +76 -85
- package/dist/index.d.ts +1 -2
- package/dist/{index.es.js → index.esm.js} +76 -85
- package/dist/index.global.js +76 -85
- package/dist/index.global.min.js +1 -1
- package/esm/default.d.ts +1 -0
- package/esm/default.js +2 -0
- package/esm/esm.d.ts +2 -0
- package/esm/esm.js +3 -0
- package/esm/index.js +136 -0
- package/esm/names.d.ts +9 -0
- package/esm/names.js +9 -0
- package/package.json +8 -6
package/esm/index.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
var _a, _b, _c;
|
|
2
|
+
import { createUUID, deepClone, Context } from '@idraw/util';
|
|
3
|
+
import { drawContext } from './lib/draw';
|
|
4
|
+
import Loader from './lib/loader';
|
|
5
|
+
import { RendererEvent } from './lib/renderer-event';
|
|
6
|
+
import { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem } from './names';
|
|
7
|
+
const { requestAnimationFrame } = window;
|
|
8
|
+
var DrawStatus;
|
|
9
|
+
(function (DrawStatus) {
|
|
10
|
+
DrawStatus["NULL"] = "null";
|
|
11
|
+
DrawStatus["FREE"] = "free";
|
|
12
|
+
DrawStatus["DRAWING"] = "drawing";
|
|
13
|
+
DrawStatus["FREEZE"] = "freeze";
|
|
14
|
+
})(DrawStatus || (DrawStatus = {}));
|
|
15
|
+
export class Renderer extends RendererEvent {
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
super();
|
|
18
|
+
this[_a] = [];
|
|
19
|
+
this[_b] = null;
|
|
20
|
+
this[_c] = DrawStatus.NULL;
|
|
21
|
+
this[_opts] = opts;
|
|
22
|
+
this[_loader] = new Loader({
|
|
23
|
+
maxParallelNum: 6
|
|
24
|
+
});
|
|
25
|
+
this[_loader].on('load', (res) => {
|
|
26
|
+
this[_drawFrame]();
|
|
27
|
+
this.trigger('load', { element: res.element });
|
|
28
|
+
});
|
|
29
|
+
this[_loader].on('error', (res) => {
|
|
30
|
+
this.trigger('error', { element: res.element, error: res.error });
|
|
31
|
+
});
|
|
32
|
+
this[_loader].on('complete', () => {
|
|
33
|
+
this.trigger('loadComplete', { t: Date.now() });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
render(target, originData, opts) {
|
|
37
|
+
const { changeResourceUUIDs = [] } = opts || {};
|
|
38
|
+
this[_status] = DrawStatus.FREE;
|
|
39
|
+
const data = deepClone(originData);
|
|
40
|
+
if (Array.isArray(data.elements)) {
|
|
41
|
+
data.elements.forEach((elem) => {
|
|
42
|
+
if (!(typeof elem.uuid === 'string' && elem.uuid)) {
|
|
43
|
+
elem.uuid = createUUID();
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (!this[_ctx]) {
|
|
48
|
+
if (this[_opts] && Object.prototype.toString.call(target) === '[object HTMLCanvasElement]') {
|
|
49
|
+
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this[_opts];
|
|
50
|
+
const canvas = target;
|
|
51
|
+
canvas.width = width * devicePixelRatio;
|
|
52
|
+
canvas.height = height * devicePixelRatio;
|
|
53
|
+
const ctx2d = canvas.getContext('2d');
|
|
54
|
+
this[_ctx] = new Context(ctx2d, {
|
|
55
|
+
width,
|
|
56
|
+
height,
|
|
57
|
+
contextWidth: contextWidth || width,
|
|
58
|
+
contextHeight: contextHeight || height,
|
|
59
|
+
devicePixelRatio
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else if (target) {
|
|
63
|
+
this[_ctx] = target;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if ([DrawStatus.FREEZE].includes(this[_status])) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const _data = deepClone({ data, });
|
|
70
|
+
this[_queue].push(_data);
|
|
71
|
+
this[_drawFrame]();
|
|
72
|
+
this[_loader].load(data, changeResourceUUIDs || []);
|
|
73
|
+
}
|
|
74
|
+
getContext() {
|
|
75
|
+
return this[_ctx];
|
|
76
|
+
}
|
|
77
|
+
thaw() {
|
|
78
|
+
this[_status] = DrawStatus.FREE;
|
|
79
|
+
}
|
|
80
|
+
[(_a = _queue, _b = _ctx, _c = _status, _freeze)]() {
|
|
81
|
+
this[_status] = DrawStatus.FREEZE;
|
|
82
|
+
}
|
|
83
|
+
[_drawFrame]() {
|
|
84
|
+
if (this[_status] === DrawStatus.FREEZE) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
requestAnimationFrame(() => {
|
|
88
|
+
if (this[_status] === DrawStatus.FREEZE) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const ctx = this[_ctx];
|
|
92
|
+
let item = this[_queue][0];
|
|
93
|
+
let isLastFrame = false;
|
|
94
|
+
if (this[_queue].length > 1) {
|
|
95
|
+
item = this[_queue].shift();
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
isLastFrame = true;
|
|
99
|
+
}
|
|
100
|
+
if (this[_loader].isComplete() !== true) {
|
|
101
|
+
this[_drawFrame]();
|
|
102
|
+
if (item && ctx) {
|
|
103
|
+
drawContext(ctx, item.data, this[_loader]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (item && ctx) {
|
|
107
|
+
drawContext(ctx, item.data, this[_loader]);
|
|
108
|
+
this[_retainQueueOneItem]();
|
|
109
|
+
if (!isLastFrame) {
|
|
110
|
+
this[_drawFrame]();
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
this[_status] = DrawStatus.FREE;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this[_status] = DrawStatus.FREE;
|
|
118
|
+
}
|
|
119
|
+
this.trigger('drawFrame', { t: Date.now() });
|
|
120
|
+
if (this[_loader].isComplete() === true && this[_queue].length === 1 && this[_status] === DrawStatus.FREE) {
|
|
121
|
+
if (ctx && this[_queue][0] && this[_queue][0].data) {
|
|
122
|
+
drawContext(ctx, this[_queue][0].data, this[_loader]);
|
|
123
|
+
}
|
|
124
|
+
this.trigger('drawFrameComplete', { t: Date.now() });
|
|
125
|
+
this[_freeze]();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
[_retainQueueOneItem]() {
|
|
130
|
+
if (this[_queue].length <= 1) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const lastOne = deepClone(this[_queue][this[_queue].length - 1]);
|
|
134
|
+
this[_queue] = [lastOne];
|
|
135
|
+
}
|
|
136
|
+
}
|
package/esm/names.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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/esm/names.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idraw/renderer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-alpha.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
|
-
"module": "dist/index.
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
7
|
"unpkg": "dist/index.global.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"dist/*.ts",
|
|
14
|
-
"dist/*.js"
|
|
14
|
+
"dist/*.js",
|
|
15
|
+
"esm/*.ts",
|
|
16
|
+
"esm/*.js"
|
|
15
17
|
],
|
|
16
18
|
"repository": {
|
|
17
19
|
"type": "git",
|
|
@@ -24,13 +26,13 @@
|
|
|
24
26
|
"author": "chenshenhai",
|
|
25
27
|
"license": "MIT",
|
|
26
28
|
"devDependencies": {
|
|
27
|
-
"@idraw/types": "^0.
|
|
29
|
+
"@idraw/types": "^0.3.0-alpha.2"
|
|
28
30
|
},
|
|
29
31
|
"dependencies": {
|
|
30
|
-
"@idraw/util": "^0.
|
|
32
|
+
"@idraw/util": "^0.3.0-alpha.2"
|
|
31
33
|
},
|
|
32
34
|
"publishConfig": {
|
|
33
35
|
"access": "public"
|
|
34
36
|
},
|
|
35
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "36f2aa4ef4e834ef329b3e4385b21b849e7f460f"
|
|
36
38
|
}
|