@idraw/renderer 0.3.0-alpha.4 → 0.3.0-alpha.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.
Files changed (56) hide show
  1. package/dist/esm/constant/element.d.ts +2 -0
  2. package/dist/esm/constant/element.js +10 -0
  3. package/dist/esm/constant/static.d.ts +11 -0
  4. package/dist/esm/constant/static.js +13 -0
  5. package/dist/esm/index.d.ts +27 -0
  6. package/{esm → dist/esm}/index.js +7 -4
  7. package/dist/esm/lib/calculate.d.ts +5 -0
  8. package/dist/esm/lib/calculate.js +64 -0
  9. package/dist/esm/lib/diff.d.ts +6 -0
  10. package/dist/esm/lib/diff.js +80 -0
  11. package/dist/esm/lib/draw/base.d.ts +5 -0
  12. package/dist/esm/lib/draw/base.js +89 -0
  13. package/dist/esm/lib/draw/circle.d.ts +2 -0
  14. package/dist/esm/lib/draw/circle.js +28 -0
  15. package/dist/esm/lib/draw/html.d.ts +3 -0
  16. package/dist/esm/lib/draw/html.js +9 -0
  17. package/dist/esm/lib/draw/image.d.ts +3 -0
  18. package/dist/esm/lib/draw/image.js +9 -0
  19. package/dist/esm/lib/draw/index.d.ts +3 -0
  20. package/dist/esm/lib/draw/index.js +55 -0
  21. package/dist/esm/lib/draw/rect.d.ts +2 -0
  22. package/dist/esm/lib/draw/rect.js +4 -0
  23. package/dist/esm/lib/draw/svg.d.ts +3 -0
  24. package/dist/esm/lib/draw/svg.js +9 -0
  25. package/dist/esm/lib/draw/text.d.ts +3 -0
  26. package/dist/esm/lib/draw/text.js +116 -0
  27. package/dist/esm/lib/index.d.ts +6 -0
  28. package/dist/esm/lib/index.js +6 -0
  29. package/dist/esm/lib/loader-event.d.ts +33 -0
  30. package/dist/esm/lib/loader-event.js +50 -0
  31. package/dist/esm/lib/loader.d.ts +25 -0
  32. package/dist/esm/lib/loader.js +238 -0
  33. package/dist/esm/lib/parse.d.ts +2 -0
  34. package/dist/esm/lib/parse.js +32 -0
  35. package/dist/esm/lib/renderer-event.d.ts +32 -0
  36. package/dist/esm/lib/renderer-event.js +50 -0
  37. package/dist/esm/lib/temp.d.ts +22 -0
  38. package/dist/esm/lib/temp.js +29 -0
  39. package/dist/esm/lib/transform.d.ts +4 -0
  40. package/dist/esm/lib/transform.js +20 -0
  41. package/dist/esm/lib/value.d.ts +2 -0
  42. package/dist/esm/lib/value.js +7 -0
  43. package/{esm → dist/esm}/names.d.ts +0 -0
  44. package/{esm → dist/esm}/names.js +0 -0
  45. package/dist/esm/util/filter.d.ts +1 -0
  46. package/dist/esm/util/filter.js +3 -0
  47. package/dist/index.global.js +1205 -1539
  48. package/dist/index.global.min.js +1 -1
  49. package/package.json +9 -14
  50. package/dist/index.cjs.js +0 -1588
  51. package/dist/index.d.ts +0 -81
  52. package/dist/index.esm.js +0 -1586
  53. package/esm/default.d.ts +0 -1
  54. package/esm/default.js +0 -2
  55. package/esm/esm.d.ts +0 -2
  56. package/esm/esm.js +0 -3
@@ -1,1591 +1,1257 @@
1
- var iDrawRenderer = (function () {
2
- 'use strict';
3
-
4
- var extendStatics = function(d, b) {
5
- extendStatics = Object.setPrototypeOf ||
6
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
8
- return extendStatics(d, b);
9
- };
10
- function __extends(d, b) {
11
- if (typeof b !== "function" && b !== null)
12
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
13
- extendStatics(d, b);
14
- function __() { this.constructor = d; }
15
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16
- }
17
- var __assign$1 = function() {
18
- __assign$1 = Object.assign || function __assign(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
22
- }
23
- return t;
24
- };
25
- return __assign$1.apply(this, arguments);
26
- };
27
- function __awaiter$1(thisArg, _arguments, P, generator) {
28
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
- return new (P || (P = Promise))(function (resolve, reject) {
30
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1
+ var iDrawRenderer = function() {
2
+ "use strict";
3
+ function isColorStr(color2) {
4
+ return typeof color2 === "string" && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color2);
5
+ }
6
+ function createUUID() {
7
+ function str4() {
8
+ return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
9
+ }
10
+ return `${str4()}${str4()}-${str4()}-${str4()}-${str4()}-${str4()}${str4()}${str4()}`;
11
+ }
12
+ function deepClone(target) {
13
+ function _clone(t) {
14
+ const type = is$1(t);
15
+ if (["Null", "Number", "String", "Boolean", "Undefined"].indexOf(type) >= 0) {
16
+ return t;
17
+ } else if (type === "Array") {
18
+ const arr = [];
19
+ t.forEach((item) => {
20
+ arr.push(_clone(item));
21
+ });
22
+ return arr;
23
+ } else if (type === "Object") {
24
+ const obj = {};
25
+ const keys = Object.keys(t);
26
+ keys.forEach((key) => {
27
+ obj[key] = _clone(t[key]);
34
28
  });
29
+ return obj;
30
+ }
35
31
  }
36
- function __generator$1(thisArg, body) {
37
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
38
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
- function verb(n) { return function (v) { return step([n, v]); }; }
40
- function step(op) {
41
- if (f) throw new TypeError("Generator is already executing.");
42
- while (_) try {
43
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
- if (y = 0, t) op = [op[0] & 2, t.value];
45
- switch (op[0]) {
46
- case 0: case 1: t = op; break;
47
- case 4: _.label++; return { value: op[1], done: false };
48
- case 5: _.label++; y = op[1]; op = [0]; continue;
49
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
- default:
51
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
- if (t[2]) _.ops.pop();
56
- _.trys.pop(); continue;
57
- }
58
- op = body.call(thisArg, _);
59
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
32
+ return _clone(target);
33
+ }
34
+ function is$1(data) {
35
+ return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, "").split(" ")[1];
36
+ }
37
+ function parsePrototype(data) {
38
+ const typeStr = Object.prototype.toString.call(data) || "";
39
+ const result = typeStr.replace(/(\[object|\])/ig, "").trim();
40
+ return result;
41
+ }
42
+ const istype = {
43
+ type(data, lowerCase) {
44
+ const result = parsePrototype(data);
45
+ return lowerCase === true ? result.toLocaleLowerCase() : result;
46
+ },
47
+ array(data) {
48
+ return parsePrototype(data) === "Array";
49
+ },
50
+ json(data) {
51
+ return parsePrototype(data) === "Object";
52
+ },
53
+ function(data) {
54
+ return parsePrototype(data) === "Function";
55
+ },
56
+ asyncFunction(data) {
57
+ return parsePrototype(data) === "AsyncFunction";
58
+ },
59
+ string(data) {
60
+ return parsePrototype(data) === "String";
61
+ },
62
+ number(data) {
63
+ return parsePrototype(data) === "Number";
64
+ },
65
+ undefined(data) {
66
+ return parsePrototype(data) === "Undefined";
67
+ },
68
+ null(data) {
69
+ return parsePrototype(data) === "Null";
70
+ },
71
+ promise(data) {
72
+ return parsePrototype(data) === "Promise";
73
+ }
74
+ };
75
+ function parseHTMLToDataURL(html2, opts) {
76
+ const { width, height } = opts;
77
+ return new Promise((resolve, reject) => {
78
+ const _svg = `
79
+ <svg xmlns="http://www.w3.org/2000/svg" width="${width || ""}" height = "${height || ""}">
80
+ <foreignObject width="100%" height="100%">
81
+ <div xmlns = "http://www.w3.org/1999/xhtml">
82
+ ${html2}
83
+ </div>
84
+ </foreignObject>
85
+ </svg>
86
+ `;
87
+ const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
88
+ const reader = new FileReader();
89
+ reader.readAsDataURL(blob);
90
+ reader.onload = function(event) {
91
+ var _a2;
92
+ const base64 = (_a2 = event === null || event === void 0 ? void 0 : event.target) === null || _a2 === void 0 ? void 0 : _a2.result;
93
+ resolve(base64);
94
+ };
95
+ reader.onerror = function(err) {
96
+ reject(err);
97
+ };
98
+ });
99
+ }
100
+ function parseSVGToDataURL(svg2) {
101
+ return new Promise((resolve, reject) => {
102
+ const _svg = svg2;
103
+ const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
104
+ const reader = new FileReader();
105
+ reader.readAsDataURL(blob);
106
+ reader.onload = function(event) {
107
+ var _a2;
108
+ const base64 = (_a2 = event === null || event === void 0 ? void 0 : event.target) === null || _a2 === void 0 ? void 0 : _a2.result;
109
+ resolve(base64);
110
+ };
111
+ reader.onerror = function(err) {
112
+ reject(err);
113
+ };
114
+ });
115
+ }
116
+ var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
117
+ function adopt(value) {
118
+ return value instanceof P ? value : new P(function(resolve) {
119
+ resolve(value);
120
+ });
121
+ }
122
+ return new (P || (P = Promise))(function(resolve, reject) {
123
+ function fulfilled(value) {
124
+ try {
125
+ step(generator.next(value));
126
+ } catch (e) {
127
+ reject(e);
128
+ }
129
+ }
130
+ function rejected(value) {
131
+ try {
132
+ step(generator["throw"](value));
133
+ } catch (e) {
134
+ reject(e);
61
135
  }
136
+ }
137
+ function step(result) {
138
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
139
+ }
140
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
141
+ });
142
+ };
143
+ const { Image } = window;
144
+ function loadImage(src) {
145
+ return new Promise((resolve, reject) => {
146
+ const img = new Image();
147
+ img.crossOrigin = "anonymous";
148
+ img.onload = function() {
149
+ resolve(img);
150
+ };
151
+ img.onabort = reject;
152
+ img.onerror = reject;
153
+ img.src = src;
154
+ });
155
+ }
156
+ function loadSVG(svg2) {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ const dataURL = yield parseSVGToDataURL(svg2);
159
+ const image = yield loadImage(dataURL);
160
+ return image;
161
+ });
162
+ }
163
+ function filterAmpersand(str) {
164
+ return str.replace(/\&/ig, "&amp;");
165
+ }
166
+ function loadHTML(html2, opts) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ html2 = filterAmpersand(html2);
169
+ const dataURL = yield parseHTMLToDataURL(html2, opts);
170
+ const image = yield loadImage(dataURL);
171
+ return image;
172
+ });
173
+ }
174
+ class Context {
175
+ constructor(ctx, opts) {
176
+ this._opts = opts;
177
+ this._ctx = ctx;
178
+ this._transform = {
179
+ scale: 1,
180
+ scrollX: 0,
181
+ scrollY: 0
182
+ };
62
183
  }
63
-
64
- function compose(middleware) {
65
- return function (context, next) {
66
- return dispatch(0);
67
- function dispatch(i) {
68
- var fn = middleware[i];
69
- if (i === middleware.length && next) {
70
- fn = next;
71
- }
72
- if (!fn)
73
- return Promise.resolve();
74
- try {
75
- return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
76
- }
77
- catch (err) {
78
- return Promise.reject(err);
79
- }
80
- }
81
- };
184
+ getContext() {
185
+ return this._ctx;
82
186
  }
83
- function delay(time) {
84
- return new Promise(function (resolve) {
85
- setTimeout(function () {
86
- resolve();
87
- }, time);
88
- });
187
+ resetSize(opts) {
188
+ this._opts = Object.assign(Object.assign({}, this._opts), opts);
89
189
  }
90
- function throttle(fn, timeout) {
91
- var timer = -1;
92
- return function () {
93
- var args = [];
94
- for (var _i = 0; _i < arguments.length; _i++) {
95
- args[_i] = arguments[_i];
96
- }
97
- if (timer > 0) {
98
- return;
99
- }
100
- timer = setTimeout(function () {
101
- fn.apply(void 0, args);
102
- timer = -1;
103
- }, timeout);
104
- };
105
- }
106
- function downloadImageFromCanvas(canvas, opts) {
107
- var filename = opts.filename, _a = opts.type, type = _a === void 0 ? 'image/jpeg' : _a;
108
- var stream = canvas.toDataURL(type);
109
- var downloadLink = document.createElement('a');
110
- downloadLink.href = stream;
111
- downloadLink.download = filename;
112
- var downloadClickEvent = document.createEvent('MouseEvents');
113
- downloadClickEvent.initEvent('click', true, false);
114
- downloadLink.dispatchEvent(downloadClickEvent);
115
- }
116
- function toColorHexNum(color) {
117
- return parseInt(color.replace(/^\#/, '0x'));
118
- }
119
- function toColorHexStr(color) {
120
- return '#' + color.toString(16);
121
- }
122
- function isColorStr(color) {
123
- return typeof color === 'string' && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color);
124
- }
125
- function createUUID() {
126
- function str4() {
127
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
128
- }
129
- return "".concat(str4()).concat(str4(), "-").concat(str4(), "-").concat(str4(), "-").concat(str4(), "-").concat(str4()).concat(str4()).concat(str4());
190
+ calcDeviceNum(num) {
191
+ return num * this._opts.devicePixelRatio;
130
192
  }
131
- function deepClone(target) {
132
- function _clone(t) {
133
- var type = is$1(t);
134
- if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
135
- return t;
136
- }
137
- else if (type === 'Array') {
138
- var arr_1 = [];
139
- t.forEach(function (item) {
140
- arr_1.push(_clone(item));
141
- });
142
- return arr_1;
143
- }
144
- else if (type === 'Object') {
145
- var obj_1 = {};
146
- var keys = Object.keys(t);
147
- keys.forEach(function (key) {
148
- obj_1[key] = _clone(t[key]);
149
- });
150
- return obj_1;
151
- }
152
- }
153
- return _clone(target);
193
+ calcScreenNum(num) {
194
+ return num / this._opts.devicePixelRatio;
154
195
  }
155
- function is$1(data) {
156
- return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
196
+ getSize() {
197
+ return {
198
+ width: this._opts.width,
199
+ height: this._opts.height,
200
+ contextWidth: this._opts.contextWidth,
201
+ contextHeight: this._opts.contextHeight,
202
+ devicePixelRatio: this._opts.devicePixelRatio
203
+ };
157
204
  }
158
- function parsePrototype(data) {
159
- var typeStr = Object.prototype.toString.call(data) || '';
160
- var result = typeStr.replace(/(\[object|\])/ig, '').trim();
161
- return result;
205
+ setTransform(config) {
206
+ this._transform = Object.assign(Object.assign({}, this._transform), config);
162
207
  }
163
- var istype = {
164
- type: function (data, lowerCase) {
165
- var result = parsePrototype(data);
166
- return lowerCase === true ? result.toLocaleLowerCase() : result;
167
- },
168
- array: function (data) {
169
- return parsePrototype(data) === 'Array';
170
- },
171
- json: function (data) {
172
- return parsePrototype(data) === 'Object';
173
- },
174
- function: function (data) {
175
- return parsePrototype(data) === 'Function';
176
- },
177
- asyncFunction: function (data) {
178
- return parsePrototype(data) === 'AsyncFunction';
179
- },
180
- string: function (data) {
181
- return parsePrototype(data) === 'String';
182
- },
183
- number: function (data) {
184
- return parsePrototype(data) === 'Number';
185
- },
186
- undefined: function (data) {
187
- return parsePrototype(data) === 'Undefined';
188
- },
189
- null: function (data) {
190
- return parsePrototype(data) === 'Null';
191
- },
192
- promise: function (data) {
193
- return parsePrototype(data) === 'Promise';
194
- },
195
- };
196
- var __assign = function() {
197
- __assign = Object.assign || function __assign(t) {
198
- for (var s, i = 1, n = arguments.length; i < n; i++) {
199
- s = arguments[i];
200
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
201
- }
202
- return t;
203
- };
204
- return __assign.apply(this, arguments);
205
- };
206
- function __awaiter(thisArg, _arguments, P, generator) {
207
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
208
- return new (P || (P = Promise))(function (resolve, reject) {
209
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
210
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
211
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
212
- step((generator = generator.apply(thisArg, _arguments || [])).next());
213
- });
208
+ getTransform() {
209
+ return {
210
+ scale: this._transform.scale,
211
+ scrollX: this._transform.scrollX,
212
+ scrollY: this._transform.scrollY
213
+ };
214
214
  }
215
- function __generator(thisArg, body) {
216
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
217
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
218
- function verb(n) { return function (v) { return step([n, v]); }; }
219
- function step(op) {
220
- if (f) throw new TypeError("Generator is already executing.");
221
- while (_) try {
222
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
223
- if (y = 0, t) op = [op[0] & 2, t.value];
224
- switch (op[0]) {
225
- case 0: case 1: t = op; break;
226
- case 4: _.label++; return { value: op[1], done: false };
227
- case 5: _.label++; y = op[1]; op = [0]; continue;
228
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
229
- default:
230
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
231
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
232
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
233
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
234
- if (t[2]) _.ops.pop();
235
- _.trys.pop(); continue;
236
- }
237
- op = body.call(thisArg, _);
238
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
239
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
240
- }
215
+ setFillStyle(color2) {
216
+ this._ctx.fillStyle = color2;
241
217
  }
242
- function parseHTMLToDataURL(html, opts) {
243
- var width = opts.width, height = opts.height;
244
- return new Promise(function (resolve, reject) {
245
- var _svg = "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"".concat(width || '', "\" height = \"").concat(height || '', "\">\n <foreignObject width=\"100%\" height=\"100%\">\n <div xmlns = \"http://www.w3.org/1999/xhtml\">\n ").concat(html, "\n </div>\n </foreignObject>\n </svg>\n ");
246
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
247
- var reader = new FileReader();
248
- reader.readAsDataURL(blob);
249
- reader.onload = function (event) {
250
- var _a;
251
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
252
- resolve(base64);
253
- };
254
- reader.onerror = function (err) {
255
- reject(err);
256
- };
257
- });
218
+ fill(fillRule) {
219
+ return this._ctx.fill(fillRule || "nonzero");
258
220
  }
259
- function parseSVGToDataURL(svg) {
260
- return new Promise(function (resolve, reject) {
261
- var _svg = svg;
262
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
263
- var reader = new FileReader();
264
- reader.readAsDataURL(blob);
265
- reader.onload = function (event) {
266
- var _a;
267
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
268
- resolve(base64);
269
- };
270
- reader.onerror = function (err) {
271
- reject(err);
272
- };
273
- });
221
+ arc(x2, y2, radius, startAngle, endAngle, anticlockwise) {
222
+ return this._ctx.arc(this._doSize(x2), this._doSize(y2), this._doSize(radius), startAngle, endAngle, anticlockwise);
274
223
  }
275
- var Image = window.Image;
276
- function loadImage(src) {
277
- return new Promise(function (resolve, reject) {
278
- var img = new Image;
279
- img.crossOrigin = 'anonymous';
280
- img.onload = function () {
281
- resolve(img);
282
- };
283
- img.onabort = reject;
284
- img.onerror = reject;
285
- img.src = src;
286
- });
224
+ rect(x2, y2, w2, h2) {
225
+ return this._ctx.rect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
287
226
  }
288
- function loadSVG(svg) {
289
- return __awaiter(this, void 0, void 0, function () {
290
- var dataURL, image;
291
- return __generator(this, function (_a) {
292
- switch (_a.label) {
293
- case 0: return [4, parseSVGToDataURL(svg)];
294
- case 1:
295
- dataURL = _a.sent();
296
- return [4, loadImage(dataURL)];
297
- case 2:
298
- image = _a.sent();
299
- return [2, image];
300
- }
301
- });
302
- });
227
+ fillRect(x2, y2, w2, h2) {
228
+ return this._ctx.fillRect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
303
229
  }
304
- function filterAmpersand(str) {
305
- return str.replace(/\&/ig, '&amp;');
306
- }
307
- function loadHTML(html, opts) {
308
- return __awaiter(this, void 0, void 0, function () {
309
- var dataURL, image;
310
- return __generator(this, function (_a) {
311
- switch (_a.label) {
312
- case 0:
313
- html = filterAmpersand(html);
314
- return [4, parseHTMLToDataURL(html, opts)];
315
- case 1:
316
- dataURL = _a.sent();
317
- return [4, loadImage(dataURL)];
318
- case 2:
319
- image = _a.sent();
320
- return [2, image];
321
- }
322
- });
323
- });
230
+ clearRect(x2, y2, w2, h2) {
231
+ return this._ctx.clearRect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
324
232
  }
325
- var Context = (function () {
326
- function Context(ctx, opts) {
327
- this._opts = opts;
328
- this._ctx = ctx;
329
- this._transform = {
330
- scale: 1,
331
- scrollX: 0,
332
- scrollY: 0,
333
- };
334
- }
335
- Context.prototype.getContext = function () {
336
- return this._ctx;
337
- };
338
- Context.prototype.resetSize = function (opts) {
339
- this._opts = __assign(__assign({}, this._opts), opts);
340
- };
341
- Context.prototype.calcDeviceNum = function (num) {
342
- return num * this._opts.devicePixelRatio;
343
- };
344
- Context.prototype.calcScreenNum = function (num) {
345
- return num / this._opts.devicePixelRatio;
346
- };
347
- Context.prototype.getSize = function () {
348
- return {
349
- width: this._opts.width,
350
- height: this._opts.height,
351
- contextWidth: this._opts.contextWidth,
352
- contextHeight: this._opts.contextHeight,
353
- devicePixelRatio: this._opts.devicePixelRatio,
354
- };
355
- };
356
- Context.prototype.setTransform = function (config) {
357
- this._transform = __assign(__assign({}, this._transform), config);
358
- };
359
- Context.prototype.getTransform = function () {
360
- return {
361
- scale: this._transform.scale,
362
- scrollX: this._transform.scrollX,
363
- scrollY: this._transform.scrollY,
364
- };
365
- };
366
- Context.prototype.setFillStyle = function (color) {
367
- this._ctx.fillStyle = color;
368
- };
369
- Context.prototype.fill = function (fillRule) {
370
- return this._ctx.fill(fillRule || 'nonzero');
371
- };
372
- Context.prototype.arc = function (x, y, radius, startAngle, endAngle, anticlockwise) {
373
- return this._ctx.arc(this._doSize(x), this._doSize(y), this._doSize(radius), startAngle, endAngle, anticlockwise);
374
- };
375
- Context.prototype.rect = function (x, y, w, h) {
376
- return this._ctx.rect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
377
- };
378
- Context.prototype.fillRect = function (x, y, w, h) {
379
- return this._ctx.fillRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
380
- };
381
- Context.prototype.clearRect = function (x, y, w, h) {
382
- return this._ctx.clearRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
383
- };
384
- Context.prototype.beginPath = function () {
385
- return this._ctx.beginPath();
386
- };
387
- Context.prototype.closePath = function () {
388
- return this._ctx.closePath();
389
- };
390
- Context.prototype.lineTo = function (x, y) {
391
- return this._ctx.lineTo(this._doSize(x), this._doSize(y));
392
- };
393
- Context.prototype.moveTo = function (x, y) {
394
- return this._ctx.moveTo(this._doSize(x), this._doSize(y));
395
- };
396
- Context.prototype.arcTo = function (x1, y1, x2, y2, radius) {
397
- return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
398
- };
399
- Context.prototype.setLineWidth = function (w) {
400
- return this._ctx.lineWidth = this._doSize(w);
401
- };
402
- Context.prototype.setLineDash = function (nums) {
403
- var _this = this;
404
- return this._ctx.setLineDash(nums.map(function (n) { return _this._doSize(n); }));
405
- };
406
- Context.prototype.isPointInPath = function (x, y) {
407
- return this._ctx.isPointInPath(this._doX(x), this._doY(y));
408
- };
409
- Context.prototype.isPointInPathWithoutScroll = function (x, y) {
410
- return this._ctx.isPointInPath(this._doSize(x), this._doSize(y));
411
- };
412
- Context.prototype.setStrokeStyle = function (color) {
413
- this._ctx.strokeStyle = color;
414
- };
415
- Context.prototype.stroke = function () {
416
- return this._ctx.stroke();
417
- };
418
- Context.prototype.translate = function (x, y) {
419
- return this._ctx.translate(this._doSize(x), this._doSize(y));
420
- };
421
- Context.prototype.rotate = function (angle) {
422
- return this._ctx.rotate(angle);
423
- };
424
- Context.prototype.drawImage = function () {
425
- var args = [];
426
- for (var _i = 0; _i < arguments.length; _i++) {
427
- args[_i] = arguments[_i];
428
- }
429
- var image = args[0];
430
- var sx = args[1];
431
- var sy = args[2];
432
- var sw = args[3];
433
- var sh = args[4];
434
- var dx = args[args.length - 4];
435
- var dy = args[args.length - 3];
436
- var dw = args[args.length - 2];
437
- var dh = args[args.length - 1];
438
- if (args.length === 9) {
439
- return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
440
- }
441
- else {
442
- return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
443
- }
444
- };
445
- Context.prototype.createPattern = function (image, repetition) {
446
- return this._ctx.createPattern(image, repetition);
447
- };
448
- Context.prototype.measureText = function (text) {
449
- return this._ctx.measureText(text);
450
- };
451
- Context.prototype.setTextAlign = function (align) {
452
- this._ctx.textAlign = align;
453
- };
454
- Context.prototype.fillText = function (text, x, y, maxWidth) {
455
- if (maxWidth !== undefined) {
456
- return this._ctx.fillText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
457
- }
458
- else {
459
- return this._ctx.fillText(text, this._doSize(x), this._doSize(y));
460
- }
461
- };
462
- Context.prototype.strokeText = function (text, x, y, maxWidth) {
463
- if (maxWidth !== undefined) {
464
- return this._ctx.strokeText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
465
- }
466
- else {
467
- return this._ctx.strokeText(text, this._doSize(x), this._doSize(y));
233
+ beginPath() {
234
+ return this._ctx.beginPath();
235
+ }
236
+ closePath() {
237
+ return this._ctx.closePath();
238
+ }
239
+ lineTo(x2, y2) {
240
+ return this._ctx.lineTo(this._doSize(x2), this._doSize(y2));
241
+ }
242
+ moveTo(x2, y2) {
243
+ return this._ctx.moveTo(this._doSize(x2), this._doSize(y2));
244
+ }
245
+ arcTo(x1, y1, x2, y2, radius) {
246
+ return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
247
+ }
248
+ setLineWidth(w2) {
249
+ return this._ctx.lineWidth = this._doSize(w2);
250
+ }
251
+ setLineDash(nums) {
252
+ return this._ctx.setLineDash(nums.map((n) => this._doSize(n)));
253
+ }
254
+ isPointInPath(x2, y2) {
255
+ return this._ctx.isPointInPath(this._doX(x2), this._doY(y2));
256
+ }
257
+ isPointInPathWithoutScroll(x2, y2) {
258
+ return this._ctx.isPointInPath(this._doSize(x2), this._doSize(y2));
259
+ }
260
+ setStrokeStyle(color2) {
261
+ this._ctx.strokeStyle = color2;
262
+ }
263
+ stroke() {
264
+ return this._ctx.stroke();
265
+ }
266
+ translate(x2, y2) {
267
+ return this._ctx.translate(this._doSize(x2), this._doSize(y2));
268
+ }
269
+ rotate(angle2) {
270
+ return this._ctx.rotate(angle2);
271
+ }
272
+ drawImage(...args) {
273
+ const image = args[0];
274
+ const sx = args[1];
275
+ const sy = args[2];
276
+ const sw = args[3];
277
+ const sh = args[4];
278
+ const dx = args[args.length - 4];
279
+ const dy = args[args.length - 3];
280
+ const dw = args[args.length - 2];
281
+ const dh = args[args.length - 1];
282
+ if (args.length === 9) {
283
+ return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
284
+ } else {
285
+ return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
286
+ }
287
+ }
288
+ createPattern(image, repetition) {
289
+ return this._ctx.createPattern(image, repetition);
290
+ }
291
+ measureText(text2) {
292
+ return this._ctx.measureText(text2);
293
+ }
294
+ setTextAlign(align) {
295
+ this._ctx.textAlign = align;
296
+ }
297
+ fillText(text2, x2, y2, maxWidth) {
298
+ if (maxWidth !== void 0) {
299
+ return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
300
+ } else {
301
+ return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2));
302
+ }
303
+ }
304
+ strokeText(text2, x2, y2, maxWidth) {
305
+ if (maxWidth !== void 0) {
306
+ return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
307
+ } else {
308
+ return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2));
309
+ }
310
+ }
311
+ setFont(opts) {
312
+ const strList = [];
313
+ if (opts.fontWeight === "bold") {
314
+ strList.push(`${opts.fontWeight}`);
315
+ }
316
+ strList.push(`${this._doSize(opts.fontSize || 12)}px`);
317
+ strList.push(`${opts.fontFamily || "sans-serif"}`);
318
+ this._ctx.font = `${strList.join(" ")}`;
319
+ }
320
+ setTextBaseline(baseline) {
321
+ this._ctx.textBaseline = baseline;
322
+ }
323
+ setGlobalAlpha(alpha) {
324
+ this._ctx.globalAlpha = alpha;
325
+ }
326
+ save() {
327
+ this._ctx.save();
328
+ }
329
+ restore() {
330
+ this._ctx.restore();
331
+ }
332
+ scale(ratioX, ratioY) {
333
+ this._ctx.scale(ratioX, ratioY);
334
+ }
335
+ setShadowColor(color2) {
336
+ this._ctx.shadowColor = color2;
337
+ }
338
+ setShadowOffsetX(offsetX) {
339
+ this._ctx.shadowOffsetX = this._doSize(offsetX);
340
+ }
341
+ setShadowOffsetY(offsetY) {
342
+ this._ctx.shadowOffsetY = this._doSize(offsetY);
343
+ }
344
+ setShadowBlur(blur) {
345
+ this._ctx.shadowBlur = this._doSize(blur);
346
+ }
347
+ ellipse(x2, y2, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
348
+ this._ctx.ellipse(this._doSize(x2), this._doSize(y2), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
349
+ }
350
+ _doSize(num) {
351
+ return this._opts.devicePixelRatio * num;
352
+ }
353
+ _doX(x2) {
354
+ const { scale, scrollX } = this._transform;
355
+ const _x = (x2 - scrollX) / scale;
356
+ return this._doSize(_x);
357
+ }
358
+ _doY(y2) {
359
+ const { scale, scrollY } = this._transform;
360
+ const _y = (y2 - scrollY) / scale;
361
+ return this._doSize(_y);
362
+ }
363
+ }
364
+ function number(value) {
365
+ return typeof value === "number" && (value > 0 || value <= 0);
366
+ }
367
+ function x(value) {
368
+ return number(value);
369
+ }
370
+ function y(value) {
371
+ return number(value);
372
+ }
373
+ function w(value) {
374
+ return typeof value === "number" && value >= 0;
375
+ }
376
+ function h(value) {
377
+ return typeof value === "number" && value >= 0;
378
+ }
379
+ function angle(value) {
380
+ return typeof value === "number" && value >= -360 && value <= 360;
381
+ }
382
+ function borderWidth(value) {
383
+ return w(value);
384
+ }
385
+ function borderRadius(value) {
386
+ return number(value) && value >= 0;
387
+ }
388
+ function color(value) {
389
+ return isColorStr(value);
390
+ }
391
+ function imageURL(value) {
392
+ return typeof value === "string" && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`);
393
+ }
394
+ function imageBase64(value) {
395
+ return typeof value === "string" && /^(data:image\/)/.test(`${value}`);
396
+ }
397
+ function imageSrc(value) {
398
+ return imageBase64(value) || imageURL(value);
399
+ }
400
+ function svg(value) {
401
+ return typeof value === "string" && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim());
402
+ }
403
+ function html(value) {
404
+ let result = false;
405
+ if (typeof value === "string") {
406
+ let div = document.createElement("div");
407
+ div.innerHTML = value;
408
+ if (div.children.length > 0) {
409
+ result = true;
410
+ }
411
+ div = null;
412
+ }
413
+ return result;
414
+ }
415
+ function text(value) {
416
+ return typeof value === "string";
417
+ }
418
+ function fontSize(value) {
419
+ return number(value) && value > 0;
420
+ }
421
+ function lineHeight(value) {
422
+ return number(value) && value > 0;
423
+ }
424
+ function strokeWidth(value) {
425
+ return number(value) && value > 0;
426
+ }
427
+ function textAlign(value) {
428
+ return ["center", "left", "right"].includes(value);
429
+ }
430
+ function fontFamily(value) {
431
+ return typeof value === "string" && value.length > 0;
432
+ }
433
+ function fontWeight(value) {
434
+ return ["bold"].includes(value);
435
+ }
436
+ const is = {
437
+ x,
438
+ y,
439
+ w,
440
+ h,
441
+ angle,
442
+ number,
443
+ borderWidth,
444
+ borderRadius,
445
+ color,
446
+ imageSrc,
447
+ imageURL,
448
+ imageBase64,
449
+ svg,
450
+ html,
451
+ text,
452
+ fontSize,
453
+ lineHeight,
454
+ textAlign,
455
+ fontFamily,
456
+ fontWeight,
457
+ strokeWidth
458
+ };
459
+ function parseAngleToRadian(angle2) {
460
+ return angle2 / 180 * Math.PI;
461
+ }
462
+ function calcElementCenter(elem) {
463
+ const p = {
464
+ x: elem.x + elem.w / 2,
465
+ y: elem.y + elem.h / 2
466
+ };
467
+ return p;
468
+ }
469
+ function rotateElement(ctx, elem, callback) {
470
+ const center = calcElementCenter(elem);
471
+ const radian = parseAngleToRadian(elem.angle || 0);
472
+ return rotateContext(ctx, center, radian || 0, callback);
473
+ }
474
+ function rotateContext(ctx, center, radian, callback) {
475
+ if (center && (radian > 0 || radian < 0)) {
476
+ ctx.translate(center.x, center.y);
477
+ ctx.rotate(radian);
478
+ ctx.translate(-center.x, -center.y);
479
+ }
480
+ callback(ctx);
481
+ if (center && (radian > 0 || radian < 0)) {
482
+ ctx.translate(center.x, center.y);
483
+ ctx.rotate(-radian);
484
+ ctx.translate(-center.x, -center.y);
485
+ }
486
+ }
487
+ function clearContext(ctx) {
488
+ ctx.setFillStyle("#000000");
489
+ ctx.setStrokeStyle("#000000");
490
+ ctx.setLineDash([]);
491
+ ctx.setGlobalAlpha(1);
492
+ ctx.setShadowColor("#00000000");
493
+ ctx.setShadowOffsetX(0);
494
+ ctx.setShadowOffsetY(0);
495
+ ctx.setShadowBlur(0);
496
+ }
497
+ function drawBgColor(ctx, color2) {
498
+ const size = ctx.getSize();
499
+ ctx.setFillStyle(color2);
500
+ ctx.fillRect(0, 0, size.contextWidth, size.contextHeight);
501
+ }
502
+ function drawBox(ctx, elem, pattern) {
503
+ clearContext(ctx);
504
+ drawBoxBorder(ctx, elem);
505
+ clearContext(ctx);
506
+ rotateElement(ctx, elem, () => {
507
+ const { x: x2, y: y2, w: w2, h: h2 } = elem;
508
+ let r = elem.desc.borderRadius || 0;
509
+ r = Math.min(r, w2 / 2, h2 / 2);
510
+ if (w2 < r * 2 || h2 < r * 2) {
511
+ r = 0;
512
+ }
513
+ ctx.beginPath();
514
+ ctx.moveTo(x2 + r, y2);
515
+ ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, r);
516
+ ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, r);
517
+ ctx.arcTo(x2, y2 + h2, x2, y2, r);
518
+ ctx.arcTo(x2, y2, x2 + w2, y2, r);
519
+ ctx.closePath();
520
+ if (typeof pattern === "string") {
521
+ ctx.setFillStyle(pattern);
522
+ } else if (["CanvasPattern"].includes(istype.type(pattern))) {
523
+ ctx.setFillStyle(pattern);
524
+ }
525
+ ctx.fill();
526
+ });
527
+ }
528
+ function drawBoxBorder(ctx, elem) {
529
+ clearContext(ctx);
530
+ rotateElement(ctx, elem, () => {
531
+ if (!(elem.desc.borderWidth && elem.desc.borderWidth > 0)) {
532
+ return;
533
+ }
534
+ const bw = elem.desc.borderWidth;
535
+ let borderColor = "#000000";
536
+ if (isColorStr(elem.desc.borderColor) === true) {
537
+ borderColor = elem.desc.borderColor;
538
+ }
539
+ const x2 = elem.x - bw / 2;
540
+ const y2 = elem.y - bw / 2;
541
+ const w2 = elem.w + bw;
542
+ const h2 = elem.h + bw;
543
+ let r = elem.desc.borderRadius || 0;
544
+ r = Math.min(r, w2 / 2, h2 / 2);
545
+ if (r < w2 / 2 && r < h2 / 2) {
546
+ r = r + bw / 2;
547
+ }
548
+ const { desc } = elem;
549
+ if (desc.shadowColor !== void 0 && isColorStr(desc.shadowColor)) {
550
+ ctx.setShadowColor(desc.shadowColor);
551
+ }
552
+ if (desc.shadowOffsetX !== void 0 && is.number(desc.shadowOffsetX)) {
553
+ ctx.setShadowOffsetX(desc.shadowOffsetX);
554
+ }
555
+ if (desc.shadowOffsetY !== void 0 && is.number(desc.shadowOffsetY)) {
556
+ ctx.setShadowOffsetY(desc.shadowOffsetY);
557
+ }
558
+ if (desc.shadowBlur !== void 0 && is.number(desc.shadowBlur)) {
559
+ ctx.setShadowBlur(desc.shadowBlur);
560
+ }
561
+ ctx.beginPath();
562
+ ctx.setLineWidth(bw);
563
+ ctx.setStrokeStyle(borderColor);
564
+ ctx.moveTo(x2 + r, y2);
565
+ ctx.arcTo(x2 + w2, y2, x2 + w2, y2 + h2, r);
566
+ ctx.arcTo(x2 + w2, y2 + h2, x2, y2 + h2, r);
567
+ ctx.arcTo(x2, y2 + h2, x2, y2, r);
568
+ ctx.arcTo(x2, y2, x2 + w2, y2, r);
569
+ ctx.closePath();
570
+ ctx.stroke();
571
+ });
572
+ }
573
+ function drawRect(ctx, elem) {
574
+ drawBox(ctx, elem, elem.desc.bgColor);
575
+ }
576
+ function drawImage(ctx, elem, loader) {
577
+ const content = loader.getContent(elem.uuid);
578
+ rotateElement(ctx, elem, () => {
579
+ if (content) {
580
+ ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
581
+ }
582
+ });
583
+ }
584
+ function drawSVG(ctx, elem, loader) {
585
+ const content = loader.getContent(elem.uuid);
586
+ rotateElement(ctx, elem, () => {
587
+ if (content) {
588
+ ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
589
+ }
590
+ });
591
+ }
592
+ function drawHTML(ctx, elem, loader) {
593
+ const content = loader.getContent(elem.uuid);
594
+ rotateElement(ctx, elem, () => {
595
+ if (content) {
596
+ ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
597
+ }
598
+ });
599
+ }
600
+ function drawText(ctx, elem, loader) {
601
+ clearContext(ctx);
602
+ drawBox(ctx, elem, elem.desc.bgColor || "transparent");
603
+ rotateElement(ctx, elem, () => {
604
+ const desc = {
605
+ ...{
606
+ fontSize: 12,
607
+ fontFamily: "sans-serif",
608
+ textAlign: "center"
609
+ },
610
+ ...elem.desc
611
+ };
612
+ ctx.setFillStyle(elem.desc.color);
613
+ ctx.setTextBaseline("top");
614
+ ctx.setFont({
615
+ fontWeight: desc.fontWeight,
616
+ fontSize: desc.fontSize,
617
+ fontFamily: desc.fontFamily
618
+ });
619
+ const descText = desc.text.replace(/\r\n/ig, "\n");
620
+ const fontHeight = desc.lineHeight || desc.fontSize;
621
+ const descTextList = descText.split("\n");
622
+ const lines = [];
623
+ let lineNum = 0;
624
+ descTextList.forEach((tempText, idx) => {
625
+ let lineText = "";
626
+ if (tempText.length > 0) {
627
+ for (let i = 0; i < tempText.length; i++) {
628
+ if (ctx.measureText(lineText + (tempText[i] || "")).width < ctx.calcDeviceNum(elem.w)) {
629
+ lineText += tempText[i] || "";
630
+ } else {
631
+ lines.push({
632
+ text: lineText,
633
+ width: ctx.calcScreenNum(ctx.measureText(lineText).width)
634
+ });
635
+ lineText = tempText[i] || "";
636
+ lineNum++;
468
637
  }
469
- };
470
- Context.prototype.setFont = function (opts) {
471
- var strList = [];
472
- if (opts.fontWeight === 'bold') {
473
- strList.push("".concat(opts.fontWeight));
638
+ if ((lineNum + 1) * fontHeight > elem.h) {
639
+ break;
474
640
  }
475
- strList.push("".concat(this._doSize(opts.fontSize || 12), "px"));
476
- strList.push("".concat(opts.fontFamily || 'sans-serif'));
477
- this._ctx.font = "".concat(strList.join(' '));
478
- };
479
- Context.prototype.setTextBaseline = function (baseline) {
480
- this._ctx.textBaseline = baseline;
481
- };
482
- Context.prototype.setGlobalAlpha = function (alpha) {
483
- this._ctx.globalAlpha = alpha;
484
- };
485
- Context.prototype.save = function () {
486
- this._ctx.save();
487
- };
488
- Context.prototype.restore = function () {
489
- this._ctx.restore();
490
- };
491
- Context.prototype.scale = function (ratioX, ratioY) {
492
- this._ctx.scale(ratioX, ratioY);
493
- };
494
- Context.prototype.setShadowColor = function (color) {
495
- this._ctx.shadowColor = color;
496
- };
497
- Context.prototype.setShadowOffsetX = function (offsetX) {
498
- this._ctx.shadowOffsetX = this._doSize(offsetX);
499
- };
500
- Context.prototype.setShadowOffsetY = function (offsetY) {
501
- this._ctx.shadowOffsetY = this._doSize(offsetY);
502
- };
503
- Context.prototype.setShadowBlur = function (blur) {
504
- this._ctx.shadowBlur = this._doSize(blur);
505
- };
506
- Context.prototype.ellipse = function (x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
507
- this._ctx.ellipse(this._doSize(x), this._doSize(y), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
508
- };
509
- Context.prototype._doSize = function (num) {
510
- return this._opts.devicePixelRatio * num;
511
- };
512
- Context.prototype._doX = function (x) {
513
- var _a = this._transform, scale = _a.scale, scrollX = _a.scrollX;
514
- var _x = (x - scrollX) / scale;
515
- return this._doSize(_x);
516
- };
517
- Context.prototype._doY = function (y) {
518
- var _a = this._transform, scale = _a.scale, scrollY = _a.scrollY;
519
- var _y = (y - scrollY) / scale;
520
- return this._doSize(_y);
521
- };
522
- return Context;
523
- }());
524
- function number(value) {
525
- return (typeof value === 'number' && (value > 0 || value <= 0));
526
- }
527
- function x(value) {
528
- return number(value);
529
- }
530
- function y(value) {
531
- return number(value);
532
- }
533
- function w(value) {
534
- return (typeof value === 'number' && value >= 0);
535
- }
536
- function h(value) {
537
- return (typeof value === 'number' && value >= 0);
538
- }
539
- function angle(value) {
540
- return (typeof value === 'number' && value >= -360 && value <= 360);
541
- }
542
- function borderWidth(value) {
543
- return w(value);
544
- }
545
- function borderRadius(value) {
546
- return number(value) && value >= 0;
547
- }
548
- function color(value) {
549
- return isColorStr(value);
550
- }
551
- function imageURL(value) {
552
- return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test("".concat(value)));
553
- }
554
- function imageBase64(value) {
555
- return (typeof value === 'string' && /^(data:image\/)/.test("".concat(value)));
556
- }
557
- function imageSrc(value) {
558
- return (imageBase64(value) || imageURL(value));
559
- }
560
- function svg(value) {
561
- return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test("".concat(value).trim()) && /<\/[\s]{0,}svg>$/i.test("".concat(value).trim()));
562
- }
563
- function html(value) {
564
- var result = false;
565
- if (typeof value === 'string') {
566
- var div = document.createElement('div');
567
- div.innerHTML = value;
568
- if (div.children.length > 0) {
569
- result = true;
641
+ if (tempText.length - 1 === i) {
642
+ if ((lineNum + 1) * fontHeight < elem.h) {
643
+ lines.push({
644
+ text: lineText,
645
+ width: ctx.calcScreenNum(ctx.measureText(lineText).width)
646
+ });
647
+ if (idx < descTextList.length - 1) {
648
+ lineNum++;
649
+ }
650
+ break;
651
+ }
570
652
  }
571
- div = null;
653
+ }
654
+ } else {
655
+ lines.push({
656
+ text: "",
657
+ width: 0
658
+ });
572
659
  }
573
- return result;
574
- }
575
- function text(value) {
576
- return typeof value === 'string';
577
- }
578
- function fontSize(value) {
579
- return number(value) && value > 0;
580
- }
581
- function lineHeight(value) {
582
- return number(value) && value > 0;
583
- }
584
- function strokeWidth(value) {
585
- return number(value) && value > 0;
586
- }
587
- function textAlign(value) {
588
- return ['center', 'left', 'right'].includes(value);
589
- }
590
- function fontFamily(value) {
591
- return typeof value === 'string' && value.length > 0;
592
- }
593
- function fontWeight(value) {
594
- return ['bold'].includes(value);
595
- }
596
- var is = {
597
- x: x,
598
- y: y,
599
- w: w,
600
- h: h,
601
- angle: angle,
602
- number: number,
603
- borderWidth: borderWidth,
604
- borderRadius: borderRadius,
605
- color: color,
606
- imageSrc: imageSrc,
607
- imageURL: imageURL,
608
- imageBase64: imageBase64,
609
- svg: svg,
610
- html: html,
611
- text: text,
612
- fontSize: fontSize,
613
- lineHeight: lineHeight,
614
- textAlign: textAlign,
615
- fontFamily: fontFamily,
616
- fontWeight: fontWeight,
617
- strokeWidth: strokeWidth,
618
- };
619
- function attrs(attrs) {
620
- var x = attrs.x, y = attrs.y, w = attrs.w, h = attrs.h, angle = attrs.angle;
621
- if (!(is.x(x) && is.y(y) && is.w(w) && is.h(h) && is.angle(angle))) {
622
- return false;
660
+ });
661
+ {
662
+ let _y = elem.y;
663
+ if (lines.length * fontHeight < elem.h) {
664
+ _y += (elem.h - lines.length * fontHeight) / 2;
623
665
  }
624
- if (!(angle >= -360 && angle <= 360)) {
625
- return false;
666
+ if (desc.textShadowColor !== void 0 && isColorStr(desc.textShadowColor)) {
667
+ ctx.setShadowColor(desc.textShadowColor);
626
668
  }
627
- return true;
628
- }
629
- function box(desc) {
630
- if (desc === void 0) { desc = {}; }
631
- var borderColor = desc.borderColor, borderRadius = desc.borderRadius, borderWidth = desc.borderWidth;
632
- if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
633
- return false;
669
+ if (desc.textShadowOffsetX !== void 0 && is.number(desc.textShadowOffsetX)) {
670
+ ctx.setShadowOffsetX(desc.textShadowOffsetX);
634
671
  }
635
- if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
636
- return false;
672
+ if (desc.textShadowOffsetY !== void 0 && is.number(desc.textShadowOffsetY)) {
673
+ ctx.setShadowOffsetY(desc.textShadowOffsetY);
637
674
  }
638
- if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
639
- return false;
675
+ if (desc.textShadowBlur !== void 0 && is.number(desc.textShadowBlur)) {
676
+ ctx.setShadowBlur(desc.textShadowBlur);
640
677
  }
641
- return true;
678
+ lines.forEach((line, i) => {
679
+ let _x = elem.x;
680
+ if (desc.textAlign === "center") {
681
+ _x = elem.x + (elem.w - line.width) / 2;
682
+ } else if (desc.textAlign === "right") {
683
+ _x = elem.x + (elem.w - line.width);
684
+ }
685
+ ctx.fillText(line.text, _x, _y + fontHeight * i);
686
+ });
687
+ clearContext(ctx);
688
+ }
689
+ if (isColorStr(desc.strokeColor) && desc.strokeWidth !== void 0 && desc.strokeWidth > 0) {
690
+ let _y = elem.y;
691
+ if (lines.length * fontHeight < elem.h) {
692
+ _y += (elem.h - lines.length * fontHeight) / 2;
693
+ }
694
+ lines.forEach((line, i) => {
695
+ let _x = elem.x;
696
+ if (desc.textAlign === "center") {
697
+ _x = elem.x + (elem.w - line.width) / 2;
698
+ } else if (desc.textAlign === "right") {
699
+ _x = elem.x + (elem.w - line.width);
700
+ }
701
+ if (desc.strokeColor !== void 0) {
702
+ ctx.setStrokeStyle(desc.strokeColor);
703
+ }
704
+ if (desc.strokeWidth !== void 0 && desc.strokeWidth > 0) {
705
+ ctx.setLineWidth(desc.strokeWidth);
706
+ }
707
+ ctx.strokeText(line.text, _x, _y + fontHeight * i);
708
+ });
709
+ }
710
+ });
711
+ }
712
+ function drawCircle(ctx, elem) {
713
+ clearContext(ctx);
714
+ rotateElement(ctx, elem, (ctx2) => {
715
+ const { x: x2, y: y2, w: w2, h: h2, desc } = elem;
716
+ const {
717
+ bgColor = "#000000",
718
+ borderColor = "#000000",
719
+ borderWidth: borderWidth2 = 0
720
+ } = desc;
721
+ const a = w2 / 2;
722
+ const b = h2 / 2;
723
+ const centerX = x2 + a;
724
+ const centerY = y2 + b;
725
+ if (borderWidth2 && borderWidth2 > 0) {
726
+ const ba = borderWidth2 / 2 + a;
727
+ const bb = borderWidth2 / 2 + b;
728
+ ctx2.beginPath();
729
+ ctx2.setStrokeStyle(borderColor);
730
+ ctx2.setLineWidth(borderWidth2);
731
+ ctx2.ellipse(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
732
+ ctx2.closePath();
733
+ ctx2.stroke();
734
+ }
735
+ ctx2.beginPath();
736
+ ctx2.setFillStyle(bgColor);
737
+ ctx2.ellipse(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
738
+ ctx2.closePath();
739
+ ctx2.fill();
740
+ });
741
+ }
742
+ function drawContext(ctx, data, loader) {
743
+ var _a2;
744
+ clearContext(ctx);
745
+ const size = ctx.getSize();
746
+ ctx.clearRect(0, 0, size.contextWidth, size.contextHeight);
747
+ if (typeof data.bgColor === "string" && isColorStr(data.bgColor)) {
748
+ drawBgColor(ctx, data.bgColor);
749
+ }
750
+ if (!(data.elements.length > 0)) {
751
+ return;
642
752
  }
643
- function rectDesc(desc) {
644
- var bgColor = desc.bgColor;
645
- if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
646
- return false;
753
+ for (let i = 0; i < data.elements.length; i++) {
754
+ const elem = data.elements[i];
755
+ if (((_a2 = elem == null ? void 0 : elem.operation) == null ? void 0 : _a2.invisible) === true) {
756
+ continue;
757
+ }
758
+ switch (elem.type) {
759
+ case "rect": {
760
+ drawRect(ctx, elem);
761
+ break;
647
762
  }
648
- if (!box(desc)) {
649
- return false;
763
+ case "text": {
764
+ drawText(ctx, elem);
765
+ break;
650
766
  }
651
- return true;
652
- }
653
- function circleDesc(desc) {
654
- var bgColor = desc.bgColor, borderColor = desc.borderColor, borderWidth = desc.borderWidth;
655
- if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
656
- return false;
767
+ case "image": {
768
+ drawImage(ctx, elem, loader);
769
+ break;
657
770
  }
658
- if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
659
- return false;
771
+ case "svg": {
772
+ drawSVG(ctx, elem, loader);
773
+ break;
660
774
  }
661
- if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
662
- return false;
775
+ case "html": {
776
+ drawHTML(ctx, elem, loader);
777
+ break;
663
778
  }
664
- return true;
779
+ case "circle": {
780
+ drawCircle(ctx, elem);
781
+ break;
782
+ }
783
+ }
784
+ }
785
+ }
786
+ class LoaderEvent {
787
+ constructor() {
788
+ this._listeners = /* @__PURE__ */ new Map();
665
789
  }
666
- function imageDesc(desc) {
667
- var src = desc.src;
668
- if (!is.imageSrc(src)) {
669
- return false;
790
+ on(eventKey, callback) {
791
+ if (this._listeners.has(eventKey)) {
792
+ const callbacks = this._listeners.get(eventKey);
793
+ callbacks == null ? void 0 : callbacks.push(callback);
794
+ this._listeners.set(eventKey, callbacks || []);
795
+ } else {
796
+ this._listeners.set(eventKey, [callback]);
797
+ }
798
+ }
799
+ off(eventKey, callback) {
800
+ if (this._listeners.has(eventKey)) {
801
+ const callbacks = this._listeners.get(eventKey);
802
+ if (Array.isArray(callbacks)) {
803
+ for (let i = 0; i < (callbacks == null ? void 0 : callbacks.length); i++) {
804
+ if (callbacks[i] === callback) {
805
+ callbacks.splice(i, 1);
806
+ break;
807
+ }
808
+ }
670
809
  }
810
+ this._listeners.set(eventKey, callbacks || []);
811
+ }
812
+ }
813
+ trigger(eventKey, arg) {
814
+ const callbacks = this._listeners.get(eventKey);
815
+ if (Array.isArray(callbacks)) {
816
+ callbacks.forEach((cb) => {
817
+ cb(arg);
818
+ });
671
819
  return true;
820
+ } else {
821
+ return false;
822
+ }
672
823
  }
673
- function svgDesc(desc) {
674
- var svg = desc.svg;
675
- if (!is.svg(svg)) {
676
- return false;
824
+ has(name) {
825
+ if (this._listeners.has(name)) {
826
+ const list = this._listeners.get(name);
827
+ if (Array.isArray(list) && list.length > 0) {
828
+ return true;
677
829
  }
678
- return true;
830
+ }
831
+ return false;
832
+ }
833
+ }
834
+ function filterScript(html2) {
835
+ return html2.replace(/<script[\s\S]*?<\/script>/ig, "");
836
+ }
837
+ class Loader {
838
+ constructor(opts) {
839
+ this._currentLoadData = {};
840
+ this._currentUUIDQueue = [];
841
+ this._storageLoadData = {};
842
+ this._status = "free";
843
+ this._waitingLoadQueue = [];
844
+ this._opts = opts;
845
+ this._event = new LoaderEvent();
846
+ this._waitingLoadQueue = [];
847
+ }
848
+ load(data, changeResourceUUIDs) {
849
+ const [uuidQueue, loadData] = this._resetLoadData(data, changeResourceUUIDs);
850
+ if (this._status === "free" || this._status === "complete") {
851
+ this._currentUUIDQueue = uuidQueue;
852
+ this._currentLoadData = loadData;
853
+ this._loadTask();
854
+ } else if (this._status === "loading" && uuidQueue.length > 0) {
855
+ this._waitingLoadQueue.push({
856
+ uuidQueue,
857
+ loadData
858
+ });
859
+ }
860
+ }
861
+ on(name, callback) {
862
+ this._event.on(name, callback);
863
+ }
864
+ off(name, callback) {
865
+ this._event.off(name, callback);
866
+ }
867
+ isComplete() {
868
+ return this._status === "complete";
679
869
  }
680
- function htmlDesc(desc) {
681
- var html = desc.html;
682
- if (!is.html(html)) {
683
- return false;
870
+ getContent(uuid) {
871
+ var _a2;
872
+ if (((_a2 = this._storageLoadData[uuid]) == null ? void 0 : _a2.status) === "loaded") {
873
+ return this._storageLoadData[uuid].content;
874
+ }
875
+ return null;
876
+ }
877
+ // getPattern(
878
+ // elem: TypeElement<keyof TypeElemDesc>,
879
+ // opts?: {
880
+ // forceUpdate: boolean
881
+ // }
882
+ // ): null | CanvasPattern {
883
+ // if (this._patternMap[elem.uuid] ) {
884
+ // if (!(opts && opts.forceUpdate === true)) {
885
+ // return this._patternMap[elem.uuid];
886
+ // }
887
+ // }
888
+ // const item = this._currentLoadData[elem.uuid];
889
+ // if (item?.status === 'loaded') {
890
+ // const board = this._opts.board;
891
+ // const tempCanvas = board.createCanvas();
892
+ // const tempCtx = board.createContext(tempCanvas);
893
+ // const image = this.getContent(elem.uuid);
894
+ // tempCtx.drawImage(image, elem.x, elem.y, elem.w, elem.h);
895
+ // const canvas = board.createCanvas();
896
+ // const ctx = board.createContext(canvas);
897
+ // const pattern = ctx.createPattern(tempCanvas, 'no-repeat');
898
+ // if (pattern) this._patternMap[elem.uuid] = pattern;
899
+ // return pattern;
900
+ // }
901
+ // return null;
902
+ // }
903
+ _resetLoadData(data, changeResourceUUIDs) {
904
+ const loadData = {};
905
+ const uuidQueue = [];
906
+ const storageLoadData = this._storageLoadData;
907
+ for (let i = data.elements.length - 1; i >= 0; i--) {
908
+ const elem = data.elements[i];
909
+ if (["image", "svg", "html"].includes(elem.type)) {
910
+ if (!storageLoadData[elem.uuid]) {
911
+ loadData[elem.uuid] = this._createEmptyLoadItem(elem);
912
+ uuidQueue.push(elem.uuid);
913
+ } else {
914
+ if (changeResourceUUIDs.includes(elem.uuid)) {
915
+ loadData[elem.uuid] = this._createEmptyLoadItem(elem);
916
+ uuidQueue.push(elem.uuid);
917
+ }
918
+ }
684
919
  }
685
- return true;
920
+ }
921
+ return [uuidQueue, loadData];
686
922
  }
687
- function textDesc(desc) {
688
- var text = desc.text, color = desc.color, fontSize = desc.fontSize, lineHeight = desc.lineHeight, fontFamily = desc.fontFamily, textAlign = desc.textAlign, fontWeight = desc.fontWeight, bgColor = desc.bgColor, strokeWidth = desc.strokeWidth, strokeColor = desc.strokeColor;
689
- if (!is.text(text)) {
690
- return false;
923
+ _createEmptyLoadItem(elem) {
924
+ let source = "";
925
+ const type = elem.type;
926
+ let elemW = elem.w;
927
+ let elemH = elem.h;
928
+ if (elem.type === "image") {
929
+ const _elem = elem;
930
+ source = _elem.desc.src || "";
931
+ } else if (elem.type === "svg") {
932
+ const _elem = elem;
933
+ source = _elem.desc.svg || "";
934
+ } else if (elem.type === "html") {
935
+ const _elem = elem;
936
+ source = filterScript(_elem.desc.html || "");
937
+ elemW = _elem.desc.width || elem.w;
938
+ elemH = _elem.desc.height || elem.h;
939
+ }
940
+ return {
941
+ uuid: elem.uuid,
942
+ type,
943
+ status: "null",
944
+ content: null,
945
+ source,
946
+ elemW,
947
+ elemH,
948
+ element: deepClone(elem)
949
+ };
950
+ }
951
+ _loadTask() {
952
+ if (this._status === "loading") {
953
+ return;
954
+ }
955
+ this._status = "loading";
956
+ if (this._currentUUIDQueue.length === 0) {
957
+ if (this._waitingLoadQueue.length === 0) {
958
+ this._status = "complete";
959
+ this._event.trigger("complete", void 0);
960
+ return;
961
+ } else {
962
+ const waitingItem = this._waitingLoadQueue.shift();
963
+ if (waitingItem) {
964
+ const { uuidQueue, loadData } = waitingItem;
965
+ this._currentLoadData = loadData;
966
+ this._currentUUIDQueue = uuidQueue;
967
+ }
691
968
  }
692
- if (!is.color(color)) {
693
- return false;
969
+ }
970
+ const { maxParallelNum } = this._opts;
971
+ const uuids = this._currentUUIDQueue.splice(0, maxParallelNum);
972
+ uuids.forEach((url, i) => {
973
+ });
974
+ const loadUUIDList = [];
975
+ const _loadAction = () => {
976
+ if (loadUUIDList.length >= maxParallelNum) {
977
+ return false;
694
978
  }
695
- if (!is.fontSize(fontSize)) {
696
- return false;
979
+ if (uuids.length === 0) {
980
+ return true;
697
981
  }
698
- if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
699
- return false;
982
+ for (let i = loadUUIDList.length; i < maxParallelNum; i++) {
983
+ const uuid = uuids.shift();
984
+ if (uuid === void 0) {
985
+ break;
986
+ }
987
+ loadUUIDList.push(uuid);
988
+ this._loadElementSource(this._currentLoadData[uuid]).then((image) => {
989
+ var _a2, _b2;
990
+ loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
991
+ const status = _loadAction();
992
+ this._storageLoadData[uuid] = {
993
+ uuid,
994
+ type: this._currentLoadData[uuid].type,
995
+ status: "loaded",
996
+ content: image,
997
+ source: this._currentLoadData[uuid].source,
998
+ elemW: this._currentLoadData[uuid].elemW,
999
+ elemH: this._currentLoadData[uuid].elemH,
1000
+ element: this._currentLoadData[uuid].element
1001
+ };
1002
+ if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1003
+ this._status = "free";
1004
+ this._loadTask();
1005
+ }
1006
+ this._event.trigger("load", {
1007
+ uuid: (_a2 = this._storageLoadData[uuid]) == null ? void 0 : _a2.uuid,
1008
+ type: this._storageLoadData[uuid].type,
1009
+ status: this._storageLoadData[uuid].status,
1010
+ content: this._storageLoadData[uuid].content,
1011
+ source: this._storageLoadData[uuid].source,
1012
+ elemW: this._storageLoadData[uuid].elemW,
1013
+ elemH: this._storageLoadData[uuid].elemH,
1014
+ element: (_b2 = this._storageLoadData[uuid]) == null ? void 0 : _b2.element
1015
+ });
1016
+ }).catch((err) => {
1017
+ var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1018
+ console.warn(err);
1019
+ loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
1020
+ const status = _loadAction();
1021
+ if (this._currentLoadData[uuid]) {
1022
+ this._storageLoadData[uuid] = {
1023
+ uuid,
1024
+ type: (_a2 = this._currentLoadData[uuid]) == null ? void 0 : _a2.type,
1025
+ status: "fail",
1026
+ content: null,
1027
+ error: err,
1028
+ source: (_b2 = this._currentLoadData[uuid]) == null ? void 0 : _b2.source,
1029
+ elemW: (_c2 = this._currentLoadData[uuid]) == null ? void 0 : _c2.elemW,
1030
+ elemH: (_d = this._currentLoadData[uuid]) == null ? void 0 : _d.elemH,
1031
+ element: (_e = this._currentLoadData[uuid]) == null ? void 0 : _e.element
1032
+ };
1033
+ }
1034
+ if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1035
+ this._status = "free";
1036
+ this._loadTask();
1037
+ }
1038
+ if (this._currentLoadData[uuid]) {
1039
+ this._event.trigger("error", {
1040
+ uuid,
1041
+ type: (_f = this._storageLoadData[uuid]) == null ? void 0 : _f.type,
1042
+ status: (_g = this._storageLoadData[uuid]) == null ? void 0 : _g.status,
1043
+ content: (_h = this._storageLoadData[uuid]) == null ? void 0 : _h.content,
1044
+ source: (_i = this._storageLoadData[uuid]) == null ? void 0 : _i.source,
1045
+ elemW: (_j = this._storageLoadData[uuid]) == null ? void 0 : _j.elemW,
1046
+ elemH: (_k = this._storageLoadData[uuid]) == null ? void 0 : _k.elemH,
1047
+ element: (_l = this._storageLoadData[uuid]) == null ? void 0 : _l.element
1048
+ });
1049
+ }
1050
+ });
700
1051
  }
701
- if (desc.hasOwnProperty('fontWeight') && !is.fontWeight(fontWeight)) {
702
- return false;
1052
+ return false;
1053
+ };
1054
+ _loadAction();
1055
+ }
1056
+ async _loadElementSource(params) {
1057
+ if (params && params.type === "image") {
1058
+ const image = await loadImage(params.source);
1059
+ return image;
1060
+ } else if (params && params.type === "svg") {
1061
+ const image = await loadSVG(
1062
+ params.source
1063
+ );
1064
+ return image;
1065
+ } else if (params && params.type === "html") {
1066
+ const image = await loadHTML(
1067
+ params.source,
1068
+ {
1069
+ width: params.elemW,
1070
+ height: params.elemH
1071
+ }
1072
+ );
1073
+ return image;
1074
+ }
1075
+ throw Error("Element's source is not support!");
1076
+ }
1077
+ }
1078
+ class RendererEvent {
1079
+ constructor() {
1080
+ this._listeners = /* @__PURE__ */ new Map();
1081
+ }
1082
+ on(eventKey, callback) {
1083
+ if (this._listeners.has(eventKey)) {
1084
+ const callbacks = this._listeners.get(eventKey);
1085
+ callbacks == null ? void 0 : callbacks.push(callback);
1086
+ this._listeners.set(eventKey, callbacks || []);
1087
+ } else {
1088
+ this._listeners.set(eventKey, [callback]);
1089
+ }
1090
+ }
1091
+ off(eventKey, callback) {
1092
+ if (this._listeners.has(eventKey)) {
1093
+ const callbacks = this._listeners.get(eventKey);
1094
+ if (Array.isArray(callbacks)) {
1095
+ for (let i = 0; i < (callbacks == null ? void 0 : callbacks.length); i++) {
1096
+ if (callbacks[i] === callback) {
1097
+ callbacks.splice(i, 1);
1098
+ break;
1099
+ }
1100
+ }
703
1101
  }
704
- if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)) {
705
- return false;
1102
+ this._listeners.set(eventKey, callbacks || []);
1103
+ }
1104
+ }
1105
+ trigger(eventKey, arg) {
1106
+ const callbacks = this._listeners.get(eventKey);
1107
+ if (Array.isArray(callbacks)) {
1108
+ callbacks.forEach((cb) => {
1109
+ cb(arg);
1110
+ });
1111
+ return true;
1112
+ } else {
1113
+ return false;
1114
+ }
1115
+ }
1116
+ has(name) {
1117
+ if (this._listeners.has(name)) {
1118
+ const list = this._listeners.get(name);
1119
+ if (Array.isArray(list) && list.length > 0) {
1120
+ return true;
706
1121
  }
707
- if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)) {
708
- return false;
1122
+ }
1123
+ return false;
1124
+ }
1125
+ }
1126
+ const _queue = Symbol("_queue");
1127
+ const _ctx = Symbol("_ctx");
1128
+ const _status = Symbol("_status");
1129
+ const _loader = Symbol("_loader");
1130
+ const _opts = Symbol("_opts");
1131
+ const _freeze = Symbol("_freeze");
1132
+ const _drawFrame = Symbol("_drawFrame");
1133
+ const _retainQueueOneItem = Symbol("_retainQueueOneItem");
1134
+ var _a, _b, _c;
1135
+ const { requestAnimationFrame } = window;
1136
+ class Renderer extends RendererEvent {
1137
+ constructor(opts) {
1138
+ super();
1139
+ this[_a] = [];
1140
+ this[_b] = null;
1141
+ this[_c] = "null";
1142
+ this[_opts] = opts;
1143
+ this[_loader] = new Loader({
1144
+ maxParallelNum: 6
1145
+ });
1146
+ this[_loader].on("load", (res) => {
1147
+ this[_drawFrame]();
1148
+ this.trigger("load", { element: res.element });
1149
+ });
1150
+ this[_loader].on("error", (res) => {
1151
+ this.trigger("error", { element: res.element, error: res.error });
1152
+ });
1153
+ this[_loader].on("complete", () => {
1154
+ this.trigger("loadComplete", { t: Date.now() });
1155
+ });
1156
+ }
1157
+ render(target, originData, opts) {
1158
+ const { changeResourceUUIDs = [] } = opts || {};
1159
+ this[_status] = "free";
1160
+ const data = deepClone(originData);
1161
+ if (Array.isArray(data.elements)) {
1162
+ data.elements.forEach((elem) => {
1163
+ if (!(typeof elem.uuid === "string" && elem.uuid)) {
1164
+ elem.uuid = createUUID();
1165
+ }
1166
+ });
1167
+ }
1168
+ if (!this[_ctx]) {
1169
+ if (this[_opts] && Object.prototype.toString.call(target) === "[object HTMLCanvasElement]") {
1170
+ const { width, height, contextWidth, contextHeight, devicePixelRatio } = this[_opts];
1171
+ const canvas = target;
1172
+ canvas.width = width * devicePixelRatio;
1173
+ canvas.height = height * devicePixelRatio;
1174
+ const ctx2d = canvas.getContext("2d");
1175
+ this[_ctx] = new Context(ctx2d, {
1176
+ width,
1177
+ height,
1178
+ contextWidth: contextWidth || width,
1179
+ contextHeight: contextHeight || height,
1180
+ devicePixelRatio
1181
+ });
1182
+ } else if (target) {
1183
+ this[_ctx] = target;
709
1184
  }
710
- if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)) {
711
- return false;
1185
+ }
1186
+ if ([
1187
+ "freeze"
1188
+ /* FREEZE */
1189
+ ].includes(this[_status])) {
1190
+ return;
1191
+ }
1192
+ const _data = deepClone({ data });
1193
+ this[_queue].push(_data);
1194
+ this[_drawFrame]();
1195
+ this[_loader].load(data, changeResourceUUIDs || []);
1196
+ }
1197
+ getContext() {
1198
+ return this[_ctx];
1199
+ }
1200
+ thaw() {
1201
+ this[_status] = "free";
1202
+ }
1203
+ [(_a = _queue, _b = _ctx, _c = _status, _freeze)]() {
1204
+ this[_status] = "freeze";
1205
+ }
1206
+ [_drawFrame]() {
1207
+ if (this[_status] === "freeze") {
1208
+ return;
1209
+ }
1210
+ requestAnimationFrame(() => {
1211
+ if (this[_status] === "freeze") {
1212
+ return;
712
1213
  }
713
- if (desc.hasOwnProperty('strokeWidth') && !is.strokeWidth(strokeWidth)) {
714
- return false;
1214
+ const ctx = this[_ctx];
1215
+ let item = this[_queue][0];
1216
+ let isLastFrame = false;
1217
+ if (this[_queue].length > 1) {
1218
+ item = this[_queue].shift();
1219
+ } else {
1220
+ isLastFrame = true;
715
1221
  }
716
- if (desc.hasOwnProperty('strokeColor') && !is.color(strokeColor)) {
717
- return false;
1222
+ if (this[_loader].isComplete() !== true) {
1223
+ this[_drawFrame]();
1224
+ if (item && ctx) {
1225
+ drawContext(ctx, item.data, this[_loader]);
1226
+ }
1227
+ } else if (item && ctx) {
1228
+ drawContext(ctx, item.data, this[_loader]);
1229
+ this[_retainQueueOneItem]();
1230
+ if (!isLastFrame) {
1231
+ this[_drawFrame]();
1232
+ } else {
1233
+ this[_status] = "free";
1234
+ }
1235
+ } else {
1236
+ this[_status] = "free";
718
1237
  }
719
- if (!box(desc)) {
720
- return false;
1238
+ this.trigger("drawFrame", { t: Date.now() });
1239
+ if (this[_loader].isComplete() === true && this[_queue].length === 1 && this[_status] === "free") {
1240
+ if (ctx && this[_queue][0] && this[_queue][0].data) {
1241
+ drawContext(ctx, this[_queue][0].data, this[_loader]);
1242
+ }
1243
+ this.trigger("drawFrameComplete", { t: Date.now() });
1244
+ this[_freeze]();
721
1245
  }
722
- return true;
1246
+ });
723
1247
  }
724
- var check = {
725
- attrs: attrs,
726
- textDesc: textDesc,
727
- rectDesc: rectDesc,
728
- circleDesc: circleDesc,
729
- imageDesc: imageDesc,
730
- svgDesc: svgDesc,
731
- htmlDesc: htmlDesc,
732
- };
733
- Object.freeze({
734
- __proto__: null,
735
- is: is,
736
- check: check,
737
- delay: delay,
738
- compose: compose,
739
- throttle: throttle,
740
- loadImage: loadImage,
741
- loadSVG: loadSVG,
742
- loadHTML: loadHTML,
743
- downloadImageFromCanvas: downloadImageFromCanvas,
744
- toColorHexStr: toColorHexStr,
745
- toColorHexNum: toColorHexNum,
746
- isColorStr: isColorStr,
747
- createUUID: createUUID,
748
- istype: istype,
749
- deepClone: deepClone,
750
- Context: Context
751
- });
752
-
753
- function parseAngleToRadian(angle) {
754
- return angle / 180 * Math.PI;
755
- }
756
- function calcElementCenter(elem) {
757
- var p = {
758
- x: elem.x + elem.w / 2,
759
- y: elem.y + elem.h / 2,
760
- };
761
- return p;
762
- }
763
-
764
- function rotateElement(ctx, elem, callback) {
765
- var center = calcElementCenter(elem);
766
- var radian = parseAngleToRadian(elem.angle || 0);
767
- return rotateContext(ctx, center, radian || 0, callback);
768
- }
769
- function rotateContext(ctx, center, radian, callback) {
770
- if (center && (radian > 0 || radian < 0)) {
771
- ctx.translate(center.x, center.y);
772
- ctx.rotate(radian);
773
- ctx.translate(-center.x, -center.y);
774
- }
775
- callback(ctx);
776
- if (center && (radian > 0 || radian < 0)) {
777
- ctx.translate(center.x, center.y);
778
- ctx.rotate(-radian);
779
- ctx.translate(-center.x, -center.y);
780
- }
781
- }
782
-
783
- function clearContext(ctx) {
784
- ctx.setFillStyle('#000000');
785
- ctx.setStrokeStyle('#000000');
786
- ctx.setLineDash([]);
787
- ctx.setGlobalAlpha(1);
788
- ctx.setShadowColor('#00000000');
789
- ctx.setShadowOffsetX(0);
790
- ctx.setShadowOffsetY(0);
791
- ctx.setShadowBlur(0);
792
- }
793
- function drawBgColor(ctx, color) {
794
- var size = ctx.getSize();
795
- ctx.setFillStyle(color);
796
- ctx.fillRect(0, 0, size.contextWidth, size.contextHeight);
797
- }
798
- function drawBox(ctx, elem, pattern) {
799
- clearContext(ctx);
800
- drawBoxBorder(ctx, elem);
801
- clearContext(ctx);
802
- rotateElement(ctx, elem, function () {
803
- var x = elem.x, y = elem.y, w = elem.w, h = elem.h;
804
- var r = elem.desc.borderRadius || 0;
805
- r = Math.min(r, w / 2, h / 2);
806
- if (w < r * 2 || h < r * 2) {
807
- r = 0;
808
- }
809
- ctx.beginPath();
810
- ctx.moveTo(x + r, y);
811
- ctx.arcTo(x + w, y, x + w, y + h, r);
812
- ctx.arcTo(x + w, y + h, x, y + h, r);
813
- ctx.arcTo(x, y + h, x, y, r);
814
- ctx.arcTo(x, y, x + w, y, r);
815
- ctx.closePath();
816
- if (typeof pattern === 'string') {
817
- ctx.setFillStyle(pattern);
818
- }
819
- else if (['CanvasPattern'].includes(istype.type(pattern))) {
820
- ctx.setFillStyle(pattern);
821
- }
822
- ctx.fill();
823
- });
824
- }
825
- function drawBoxBorder(ctx, elem) {
826
- clearContext(ctx);
827
- rotateElement(ctx, elem, function () {
828
- if (!(elem.desc.borderWidth && elem.desc.borderWidth > 0)) {
829
- return;
830
- }
831
- var bw = elem.desc.borderWidth;
832
- var borderColor = '#000000';
833
- if (isColorStr(elem.desc.borderColor) === true) {
834
- borderColor = elem.desc.borderColor;
835
- }
836
- var x = elem.x - bw / 2;
837
- var y = elem.y - bw / 2;
838
- var w = elem.w + bw;
839
- var h = elem.h + bw;
840
- var r = elem.desc.borderRadius || 0;
841
- r = Math.min(r, w / 2, h / 2);
842
- if (r < w / 2 && r < h / 2) {
843
- r = r + bw / 2;
844
- }
845
- var desc = elem.desc;
846
- if (desc.shadowColor !== undefined && isColorStr(desc.shadowColor)) {
847
- ctx.setShadowColor(desc.shadowColor);
848
- }
849
- if (desc.shadowOffsetX !== undefined && is.number(desc.shadowOffsetX)) {
850
- ctx.setShadowOffsetX(desc.shadowOffsetX);
851
- }
852
- if (desc.shadowOffsetY !== undefined && is.number(desc.shadowOffsetY)) {
853
- ctx.setShadowOffsetY(desc.shadowOffsetY);
854
- }
855
- if (desc.shadowBlur !== undefined && is.number(desc.shadowBlur)) {
856
- ctx.setShadowBlur(desc.shadowBlur);
857
- }
858
- ctx.beginPath();
859
- ctx.setLineWidth(bw);
860
- ctx.setStrokeStyle(borderColor);
861
- ctx.moveTo(x + r, y);
862
- ctx.arcTo(x + w, y, x + w, y + h, r);
863
- ctx.arcTo(x + w, y + h, x, y + h, r);
864
- ctx.arcTo(x, y + h, x, y, r);
865
- ctx.arcTo(x, y, x + w, y, r);
866
- ctx.closePath();
867
- ctx.stroke();
868
- });
869
- }
870
-
871
- function drawRect(ctx, elem) {
872
- drawBox(ctx, elem, elem.desc.bgColor);
873
- }
874
-
875
- function drawImage(ctx, elem, loader) {
876
- var content = loader.getContent(elem.uuid);
877
- rotateElement(ctx, elem, function () {
878
- if (content) {
879
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
880
- }
881
- });
882
- }
883
-
884
- function drawSVG(ctx, elem, loader) {
885
- var content = loader.getContent(elem.uuid);
886
- rotateElement(ctx, elem, function () {
887
- if (content) {
888
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
889
- }
890
- });
891
- }
892
-
893
- function drawHTML(ctx, elem, loader) {
894
- var content = loader.getContent(elem.uuid);
895
- rotateElement(ctx, elem, function () {
896
- if (content) {
897
- ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
898
- }
899
- });
900
- }
901
-
902
- function drawText(ctx, elem, loader) {
903
- clearContext(ctx);
904
- drawBox(ctx, elem, elem.desc.bgColor || 'transparent');
905
- rotateElement(ctx, elem, function () {
906
- var desc = __assign$1({
907
- fontSize: 12,
908
- fontFamily: 'sans-serif',
909
- textAlign: 'center',
910
- }, elem.desc);
911
- ctx.setFillStyle(elem.desc.color);
912
- ctx.setTextBaseline('top');
913
- ctx.setFont({
914
- fontWeight: desc.fontWeight,
915
- fontSize: desc.fontSize,
916
- fontFamily: desc.fontFamily
917
- });
918
- var descText = desc.text.replace(/\r\n/ig, '\n');
919
- var fontHeight = desc.lineHeight || desc.fontSize;
920
- var descTextList = descText.split('\n');
921
- var lines = [];
922
- var lineNum = 0;
923
- descTextList.forEach(function (tempText, idx) {
924
- var lineText = '';
925
- if (tempText.length > 0) {
926
- for (var i = 0; i < tempText.length; i++) {
927
- if (ctx.measureText(lineText + (tempText[i] || '')).width < ctx.calcDeviceNum(elem.w)) {
928
- lineText += (tempText[i] || '');
929
- }
930
- else {
931
- lines.push({
932
- text: lineText,
933
- width: ctx.calcScreenNum(ctx.measureText(lineText).width),
934
- });
935
- lineText = (tempText[i] || '');
936
- lineNum++;
937
- }
938
- if ((lineNum + 1) * fontHeight > elem.h) {
939
- break;
940
- }
941
- if (tempText.length - 1 === i) {
942
- if ((lineNum + 1) * fontHeight < elem.h) {
943
- lines.push({
944
- text: lineText,
945
- width: ctx.calcScreenNum(ctx.measureText(lineText).width),
946
- });
947
- if (idx < descTextList.length - 1) {
948
- lineNum++;
949
- }
950
- break;
951
- }
952
- }
953
- }
954
- }
955
- else {
956
- lines.push({
957
- text: '',
958
- width: 0,
959
- });
960
- }
961
- });
962
- {
963
- var _y_1 = elem.y;
964
- if (lines.length * fontHeight < elem.h) {
965
- _y_1 += ((elem.h - lines.length * fontHeight) / 2);
966
- }
967
- if (desc.textShadowColor !== undefined && isColorStr(desc.textShadowColor)) {
968
- ctx.setShadowColor(desc.textShadowColor);
969
- }
970
- if (desc.textShadowOffsetX !== undefined && is.number(desc.textShadowOffsetX)) {
971
- ctx.setShadowOffsetX(desc.textShadowOffsetX);
972
- }
973
- if (desc.textShadowOffsetY !== undefined && is.number(desc.textShadowOffsetY)) {
974
- ctx.setShadowOffsetY(desc.textShadowOffsetY);
975
- }
976
- if (desc.textShadowBlur !== undefined && is.number(desc.textShadowBlur)) {
977
- ctx.setShadowBlur(desc.textShadowBlur);
978
- }
979
- lines.forEach(function (line, i) {
980
- var _x = elem.x;
981
- if (desc.textAlign === 'center') {
982
- _x = elem.x + (elem.w - line.width) / 2;
983
- }
984
- else if (desc.textAlign === 'right') {
985
- _x = elem.x + (elem.w - line.width);
986
- }
987
- ctx.fillText(line.text, _x, _y_1 + fontHeight * i);
988
- });
989
- clearContext(ctx);
990
- }
991
- if (isColorStr(desc.strokeColor) && desc.strokeWidth !== undefined && desc.strokeWidth > 0) {
992
- var _y_2 = elem.y;
993
- if (lines.length * fontHeight < elem.h) {
994
- _y_2 += ((elem.h - lines.length * fontHeight) / 2);
995
- }
996
- lines.forEach(function (line, i) {
997
- var _x = elem.x;
998
- if (desc.textAlign === 'center') {
999
- _x = elem.x + (elem.w - line.width) / 2;
1000
- }
1001
- else if (desc.textAlign === 'right') {
1002
- _x = elem.x + (elem.w - line.width);
1003
- }
1004
- if (desc.strokeColor !== undefined) {
1005
- ctx.setStrokeStyle(desc.strokeColor);
1006
- }
1007
- if (desc.strokeWidth !== undefined && desc.strokeWidth > 0) {
1008
- ctx.setLineWidth(desc.strokeWidth);
1009
- }
1010
- ctx.strokeText(line.text, _x, _y_2 + fontHeight * i);
1011
- });
1012
- }
1013
- });
1014
- }
1015
-
1016
- function drawCircle(ctx, elem) {
1017
- clearContext(ctx);
1018
- rotateElement(ctx, elem, function (ctx) {
1019
- var x = elem.x, y = elem.y, w = elem.w, h = elem.h, desc = elem.desc;
1020
- var _a = desc.bgColor, bgColor = _a === void 0 ? '#000000' : _a, _b = desc.borderColor, borderColor = _b === void 0 ? '#000000' : _b, _c = desc.borderWidth, borderWidth = _c === void 0 ? 0 : _c;
1021
- var a = w / 2;
1022
- var b = h / 2;
1023
- var centerX = x + a;
1024
- var centerY = y + b;
1025
- if (borderWidth && borderWidth > 0) {
1026
- var ba = borderWidth / 2 + a;
1027
- var bb = borderWidth / 2 + b;
1028
- ctx.beginPath();
1029
- ctx.setStrokeStyle(borderColor);
1030
- ctx.setLineWidth(borderWidth);
1031
- ctx.ellipse(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
1032
- ctx.closePath();
1033
- ctx.stroke();
1034
- }
1035
- ctx.beginPath();
1036
- ctx.setFillStyle(bgColor);
1037
- ctx.ellipse(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
1038
- ctx.closePath();
1039
- ctx.fill();
1040
- });
1041
- }
1042
-
1043
- function drawContext(ctx, data, loader) {
1044
- var _a;
1045
- clearContext(ctx);
1046
- var size = ctx.getSize();
1047
- ctx.clearRect(0, 0, size.contextWidth, size.contextHeight);
1048
- if (typeof data.bgColor === 'string' && isColorStr(data.bgColor)) {
1049
- drawBgColor(ctx, data.bgColor);
1050
- }
1051
- if (!(data.elements.length > 0)) {
1052
- return;
1053
- }
1054
- for (var i = 0; i < data.elements.length; i++) {
1055
- var elem = data.elements[i];
1056
- if (((_a = elem === null || elem === void 0 ? void 0 : elem.operation) === null || _a === void 0 ? void 0 : _a.invisible) === true) {
1057
- continue;
1058
- }
1059
- switch (elem.type) {
1060
- case 'rect': {
1061
- drawRect(ctx, elem);
1062
- break;
1063
- }
1064
- case 'text': {
1065
- drawText(ctx, elem);
1066
- break;
1067
- }
1068
- case 'image': {
1069
- drawImage(ctx, elem, loader);
1070
- break;
1071
- }
1072
- case 'svg': {
1073
- drawSVG(ctx, elem, loader);
1074
- break;
1075
- }
1076
- case 'html': {
1077
- drawHTML(ctx, elem, loader);
1078
- break;
1079
- }
1080
- case 'circle': {
1081
- drawCircle(ctx, elem);
1082
- break;
1083
- }
1084
- }
1085
- }
1086
- }
1087
-
1088
- var LoaderEvent = (function () {
1089
- function LoaderEvent() {
1090
- this._listeners = new Map();
1091
- }
1092
- LoaderEvent.prototype.on = function (eventKey, callback) {
1093
- if (this._listeners.has(eventKey)) {
1094
- var callbacks = this._listeners.get(eventKey);
1095
- callbacks === null || callbacks === void 0 ? void 0 : callbacks.push(callback);
1096
- this._listeners.set(eventKey, callbacks || []);
1097
- }
1098
- else {
1099
- this._listeners.set(eventKey, [callback]);
1100
- }
1101
- };
1102
- LoaderEvent.prototype.off = function (eventKey, callback) {
1103
- if (this._listeners.has(eventKey)) {
1104
- var callbacks = this._listeners.get(eventKey);
1105
- if (Array.isArray(callbacks)) {
1106
- for (var i = 0; i < (callbacks === null || callbacks === void 0 ? void 0 : callbacks.length); i++) {
1107
- if (callbacks[i] === callback) {
1108
- callbacks.splice(i, 1);
1109
- break;
1110
- }
1111
- }
1112
- }
1113
- this._listeners.set(eventKey, callbacks || []);
1114
- }
1115
- };
1116
- LoaderEvent.prototype.trigger = function (eventKey, arg) {
1117
- var callbacks = this._listeners.get(eventKey);
1118
- if (Array.isArray(callbacks)) {
1119
- callbacks.forEach(function (cb) {
1120
- cb(arg);
1121
- });
1122
- return true;
1123
- }
1124
- else {
1125
- return false;
1126
- }
1127
- };
1128
- LoaderEvent.prototype.has = function (name) {
1129
- if (this._listeners.has(name)) {
1130
- var list = this._listeners.get(name);
1131
- if (Array.isArray(list) && list.length > 0) {
1132
- return true;
1133
- }
1134
- }
1135
- return false;
1136
- };
1137
- return LoaderEvent;
1138
- }());
1139
-
1140
- function filterScript(html) {
1141
- return html.replace(/<script[\s\S]*?<\/script>/ig, '');
1142
- }
1143
-
1144
- var LoaderStatus;
1145
- (function (LoaderStatus) {
1146
- LoaderStatus["FREE"] = "free";
1147
- LoaderStatus["LOADING"] = "loading";
1148
- LoaderStatus["COMPLETE"] = "complete";
1149
- })(LoaderStatus || (LoaderStatus = {}));
1150
- var Loader = (function () {
1151
- function Loader(opts) {
1152
- this._currentLoadData = {};
1153
- this._currentUUIDQueue = [];
1154
- this._storageLoadData = {};
1155
- this._status = LoaderStatus.FREE;
1156
- this._waitingLoadQueue = [];
1157
- this._opts = opts;
1158
- this._event = new LoaderEvent();
1159
- this._waitingLoadQueue = [];
1160
- }
1161
- Loader.prototype.load = function (data, changeResourceUUIDs) {
1162
- var _a = this._resetLoadData(data, changeResourceUUIDs), uuidQueue = _a[0], loadData = _a[1];
1163
- if (this._status === LoaderStatus.FREE || this._status === LoaderStatus.COMPLETE) {
1164
- this._currentUUIDQueue = uuidQueue;
1165
- this._currentLoadData = loadData;
1166
- this._loadTask();
1167
- }
1168
- else if (this._status === LoaderStatus.LOADING && uuidQueue.length > 0) {
1169
- this._waitingLoadQueue.push({
1170
- uuidQueue: uuidQueue,
1171
- loadData: loadData,
1172
- });
1173
- }
1174
- };
1175
- Loader.prototype.on = function (name, callback) {
1176
- this._event.on(name, callback);
1177
- };
1178
- Loader.prototype.off = function (name, callback) {
1179
- this._event.off(name, callback);
1180
- };
1181
- Loader.prototype.isComplete = function () {
1182
- return this._status === LoaderStatus.COMPLETE;
1183
- };
1184
- Loader.prototype.getContent = function (uuid) {
1185
- var _a;
1186
- if (((_a = this._storageLoadData[uuid]) === null || _a === void 0 ? void 0 : _a.status) === 'loaded') {
1187
- return this._storageLoadData[uuid].content;
1188
- }
1189
- return null;
1190
- };
1191
- Loader.prototype._resetLoadData = function (data, changeResourceUUIDs) {
1192
- var loadData = {};
1193
- var uuidQueue = [];
1194
- var storageLoadData = this._storageLoadData;
1195
- for (var i = data.elements.length - 1; i >= 0; i--) {
1196
- var elem = data.elements[i];
1197
- if (['image', 'svg', 'html',].includes(elem.type)) {
1198
- if (!storageLoadData[elem.uuid]) {
1199
- loadData[elem.uuid] = this._createEmptyLoadItem(elem);
1200
- uuidQueue.push(elem.uuid);
1201
- }
1202
- else {
1203
- if (changeResourceUUIDs.includes(elem.uuid)) {
1204
- loadData[elem.uuid] = this._createEmptyLoadItem(elem);
1205
- uuidQueue.push(elem.uuid);
1206
- }
1207
- }
1208
- }
1209
- }
1210
- return [uuidQueue, loadData];
1211
- };
1212
- Loader.prototype._createEmptyLoadItem = function (elem) {
1213
- var source = '';
1214
- var type = elem.type;
1215
- var elemW = elem.w;
1216
- var elemH = elem.h;
1217
- if (elem.type === 'image') {
1218
- var _elem = elem;
1219
- source = _elem.desc.src || '';
1220
- }
1221
- else if (elem.type === 'svg') {
1222
- var _elem = elem;
1223
- source = _elem.desc.svg || '';
1224
- }
1225
- else if (elem.type === 'html') {
1226
- var _elem = elem;
1227
- source = filterScript(_elem.desc.html || '');
1228
- elemW = _elem.desc.width || elem.w;
1229
- elemH = _elem.desc.height || elem.h;
1230
- }
1231
- return {
1232
- uuid: elem.uuid,
1233
- type: type,
1234
- status: 'null',
1235
- content: null,
1236
- source: source,
1237
- elemW: elemW,
1238
- elemH: elemH,
1239
- element: deepClone(elem),
1240
- };
1241
- };
1242
- Loader.prototype._loadTask = function () {
1243
- var _this = this;
1244
- if (this._status === LoaderStatus.LOADING) {
1245
- return;
1246
- }
1247
- this._status = LoaderStatus.LOADING;
1248
- if (this._currentUUIDQueue.length === 0) {
1249
- if (this._waitingLoadQueue.length === 0) {
1250
- this._status = LoaderStatus.COMPLETE;
1251
- this._event.trigger('complete', undefined);
1252
- return;
1253
- }
1254
- else {
1255
- var waitingItem = this._waitingLoadQueue.shift();
1256
- if (waitingItem) {
1257
- var uuidQueue = waitingItem.uuidQueue, loadData = waitingItem.loadData;
1258
- this._currentLoadData = loadData;
1259
- this._currentUUIDQueue = uuidQueue;
1260
- }
1261
- }
1262
- }
1263
- var maxParallelNum = this._opts.maxParallelNum;
1264
- var uuids = this._currentUUIDQueue.splice(0, maxParallelNum);
1265
- uuids.forEach(function (url, i) {
1266
- });
1267
- var loadUUIDList = [];
1268
- var _loadAction = function () {
1269
- if (loadUUIDList.length >= maxParallelNum) {
1270
- return false;
1271
- }
1272
- if (uuids.length === 0) {
1273
- return true;
1274
- }
1275
- var _loop_1 = function (i) {
1276
- var uuid = uuids.shift();
1277
- if (uuid === undefined) {
1278
- return "break";
1279
- }
1280
- loadUUIDList.push(uuid);
1281
- _this._loadElementSource(_this._currentLoadData[uuid]).then(function (image) {
1282
- var _a, _b;
1283
- loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
1284
- var status = _loadAction();
1285
- _this._storageLoadData[uuid] = {
1286
- uuid: uuid,
1287
- type: _this._currentLoadData[uuid].type,
1288
- status: 'loaded',
1289
- content: image,
1290
- source: _this._currentLoadData[uuid].source,
1291
- elemW: _this._currentLoadData[uuid].elemW,
1292
- elemH: _this._currentLoadData[uuid].elemH,
1293
- element: _this._currentLoadData[uuid].element,
1294
- };
1295
- if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1296
- _this._status = LoaderStatus.FREE;
1297
- _this._loadTask();
1298
- }
1299
- _this._event.trigger('load', {
1300
- uuid: (_a = _this._storageLoadData[uuid]) === null || _a === void 0 ? void 0 : _a.uuid,
1301
- type: _this._storageLoadData[uuid].type,
1302
- status: _this._storageLoadData[uuid].status,
1303
- content: _this._storageLoadData[uuid].content,
1304
- source: _this._storageLoadData[uuid].source,
1305
- elemW: _this._storageLoadData[uuid].elemW,
1306
- elemH: _this._storageLoadData[uuid].elemH,
1307
- element: (_b = _this._storageLoadData[uuid]) === null || _b === void 0 ? void 0 : _b.element,
1308
- });
1309
- }).catch(function (err) {
1310
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
1311
- console.warn(err);
1312
- loadUUIDList.splice(loadUUIDList.indexOf(uuid), 1);
1313
- var status = _loadAction();
1314
- if (_this._currentLoadData[uuid]) {
1315
- _this._storageLoadData[uuid] = {
1316
- uuid: uuid,
1317
- type: (_a = _this._currentLoadData[uuid]) === null || _a === void 0 ? void 0 : _a.type,
1318
- status: 'fail',
1319
- content: null,
1320
- error: err,
1321
- source: (_b = _this._currentLoadData[uuid]) === null || _b === void 0 ? void 0 : _b.source,
1322
- elemW: (_c = _this._currentLoadData[uuid]) === null || _c === void 0 ? void 0 : _c.elemW,
1323
- elemH: (_d = _this._currentLoadData[uuid]) === null || _d === void 0 ? void 0 : _d.elemH,
1324
- element: (_e = _this._currentLoadData[uuid]) === null || _e === void 0 ? void 0 : _e.element,
1325
- };
1326
- }
1327
- if (loadUUIDList.length === 0 && uuids.length === 0 && status === true) {
1328
- _this._status = LoaderStatus.FREE;
1329
- _this._loadTask();
1330
- }
1331
- if (_this._currentLoadData[uuid]) {
1332
- _this._event.trigger('error', {
1333
- uuid: uuid,
1334
- type: (_f = _this._storageLoadData[uuid]) === null || _f === void 0 ? void 0 : _f.type,
1335
- status: (_g = _this._storageLoadData[uuid]) === null || _g === void 0 ? void 0 : _g.status,
1336
- content: (_h = _this._storageLoadData[uuid]) === null || _h === void 0 ? void 0 : _h.content,
1337
- source: (_j = _this._storageLoadData[uuid]) === null || _j === void 0 ? void 0 : _j.source,
1338
- elemW: (_k = _this._storageLoadData[uuid]) === null || _k === void 0 ? void 0 : _k.elemW,
1339
- elemH: (_l = _this._storageLoadData[uuid]) === null || _l === void 0 ? void 0 : _l.elemH,
1340
- element: (_m = _this._storageLoadData[uuid]) === null || _m === void 0 ? void 0 : _m.element,
1341
- });
1342
- }
1343
- });
1344
- };
1345
- for (var i = loadUUIDList.length; i < maxParallelNum; i++) {
1346
- var state_1 = _loop_1();
1347
- if (state_1 === "break")
1348
- break;
1349
- }
1350
- return false;
1351
- };
1352
- _loadAction();
1353
- };
1354
- Loader.prototype._loadElementSource = function (params) {
1355
- return __awaiter$1(this, void 0, void 0, function () {
1356
- var image, image, image;
1357
- return __generator$1(this, function (_a) {
1358
- switch (_a.label) {
1359
- case 0:
1360
- if (!(params && params.type === 'image')) return [3, 2];
1361
- return [4, loadImage(params.source)];
1362
- case 1:
1363
- image = _a.sent();
1364
- return [2, image];
1365
- case 2:
1366
- if (!(params && params.type === 'svg')) return [3, 4];
1367
- return [4, loadSVG(params.source)];
1368
- case 3:
1369
- image = _a.sent();
1370
- return [2, image];
1371
- case 4:
1372
- if (!(params && params.type === 'html')) return [3, 6];
1373
- return [4, loadHTML(params.source, {
1374
- width: params.elemW, height: params.elemH
1375
- })];
1376
- case 5:
1377
- image = _a.sent();
1378
- return [2, image];
1379
- case 6: throw Error('Element\'s source is not support!');
1380
- }
1381
- });
1382
- });
1383
- };
1384
- return Loader;
1385
- }());
1386
-
1387
- var RendererEvent = (function () {
1388
- function RendererEvent() {
1389
- this._listeners = new Map();
1390
- }
1391
- RendererEvent.prototype.on = function (eventKey, callback) {
1392
- if (this._listeners.has(eventKey)) {
1393
- var callbacks = this._listeners.get(eventKey);
1394
- callbacks === null || callbacks === void 0 ? void 0 : callbacks.push(callback);
1395
- this._listeners.set(eventKey, callbacks || []);
1396
- }
1397
- else {
1398
- this._listeners.set(eventKey, [callback]);
1399
- }
1400
- };
1401
- RendererEvent.prototype.off = function (eventKey, callback) {
1402
- if (this._listeners.has(eventKey)) {
1403
- var callbacks = this._listeners.get(eventKey);
1404
- if (Array.isArray(callbacks)) {
1405
- for (var i = 0; i < (callbacks === null || callbacks === void 0 ? void 0 : callbacks.length); i++) {
1406
- if (callbacks[i] === callback) {
1407
- callbacks.splice(i, 1);
1408
- break;
1409
- }
1410
- }
1411
- }
1412
- this._listeners.set(eventKey, callbacks || []);
1413
- }
1414
- };
1415
- RendererEvent.prototype.trigger = function (eventKey, arg) {
1416
- var callbacks = this._listeners.get(eventKey);
1417
- if (Array.isArray(callbacks)) {
1418
- callbacks.forEach(function (cb) {
1419
- cb(arg);
1420
- });
1421
- return true;
1422
- }
1423
- else {
1424
- return false;
1425
- }
1426
- };
1427
- RendererEvent.prototype.has = function (name) {
1428
- if (this._listeners.has(name)) {
1429
- var list = this._listeners.get(name);
1430
- if (Array.isArray(list) && list.length > 0) {
1431
- return true;
1432
- }
1433
- }
1434
- return false;
1435
- };
1436
- return RendererEvent;
1437
- }());
1438
-
1439
- var _queue = Symbol('_queue');
1440
- var _ctx = Symbol('_ctx');
1441
- var _status = Symbol('_status');
1442
- var _loader = Symbol('_loader');
1443
- var _opts = Symbol('_opts');
1444
- var _freeze = Symbol('_freeze');
1445
- var _drawFrame = Symbol('_drawFrame');
1446
- var _retainQueueOneItem = Symbol('_retainQueueOneItem');
1447
-
1448
- var _a, _b, _c;
1449
- var requestAnimationFrame = window.requestAnimationFrame;
1450
- var DrawStatus;
1451
- (function (DrawStatus) {
1452
- DrawStatus["NULL"] = "null";
1453
- DrawStatus["FREE"] = "free";
1454
- DrawStatus["DRAWING"] = "drawing";
1455
- DrawStatus["FREEZE"] = "freeze";
1456
- })(DrawStatus || (DrawStatus = {}));
1457
- var Renderer = (function (_super) {
1458
- __extends(Renderer, _super);
1459
- function Renderer(opts) {
1460
- var _this = _super.call(this) || this;
1461
- _this[_a] = [];
1462
- _this[_b] = null;
1463
- _this[_c] = DrawStatus.NULL;
1464
- _this[_opts] = opts;
1465
- _this[_loader] = new Loader({
1466
- maxParallelNum: 6
1467
- });
1468
- _this[_loader].on('load', function (res) {
1469
- _this[_drawFrame]();
1470
- _this.trigger('load', { element: res.element });
1471
- });
1472
- _this[_loader].on('error', function (res) {
1473
- _this.trigger('error', { element: res.element, error: res.error });
1474
- });
1475
- _this[_loader].on('complete', function () {
1476
- _this.trigger('loadComplete', { t: Date.now() });
1477
- });
1478
- return _this;
1479
- }
1480
- Renderer.prototype.render = function (target, originData, opts) {
1481
- var _d = (opts || {}).changeResourceUUIDs, changeResourceUUIDs = _d === void 0 ? [] : _d;
1482
- this[_status] = DrawStatus.FREE;
1483
- var data = deepClone(originData);
1484
- if (Array.isArray(data.elements)) {
1485
- data.elements.forEach(function (elem) {
1486
- if (!(typeof elem.uuid === 'string' && elem.uuid)) {
1487
- elem.uuid = createUUID();
1488
- }
1489
- });
1490
- }
1491
- if (!this[_ctx]) {
1492
- if (this[_opts] && Object.prototype.toString.call(target) === '[object HTMLCanvasElement]') {
1493
- var _e = this[_opts], width = _e.width, height = _e.height, contextWidth = _e.contextWidth, contextHeight = _e.contextHeight, devicePixelRatio_1 = _e.devicePixelRatio;
1494
- var canvas = target;
1495
- canvas.width = width * devicePixelRatio_1;
1496
- canvas.height = height * devicePixelRatio_1;
1497
- var ctx2d = canvas.getContext('2d');
1498
- this[_ctx] = new Context(ctx2d, {
1499
- width: width,
1500
- height: height,
1501
- contextWidth: contextWidth || width,
1502
- contextHeight: contextHeight || height,
1503
- devicePixelRatio: devicePixelRatio_1
1504
- });
1505
- }
1506
- else if (target) {
1507
- this[_ctx] = target;
1508
- }
1509
- }
1510
- if ([DrawStatus.FREEZE].includes(this[_status])) {
1511
- return;
1512
- }
1513
- var _data = deepClone({ data: data, });
1514
- this[_queue].push(_data);
1515
- this[_drawFrame]();
1516
- this[_loader].load(data, changeResourceUUIDs || []);
1517
- };
1518
- Renderer.prototype.getContext = function () {
1519
- return this[_ctx];
1520
- };
1521
- Renderer.prototype.thaw = function () {
1522
- this[_status] = DrawStatus.FREE;
1523
- };
1524
- Renderer.prototype[(_a = _queue, _b = _ctx, _c = _status, _freeze)] = function () {
1525
- this[_status] = DrawStatus.FREEZE;
1526
- };
1527
- Renderer.prototype[_drawFrame] = function () {
1528
- var _this = this;
1529
- if (this[_status] === DrawStatus.FREEZE) {
1530
- return;
1531
- }
1532
- requestAnimationFrame(function () {
1533
- if (_this[_status] === DrawStatus.FREEZE) {
1534
- return;
1535
- }
1536
- var ctx = _this[_ctx];
1537
- var item = _this[_queue][0];
1538
- var isLastFrame = false;
1539
- if (_this[_queue].length > 1) {
1540
- item = _this[_queue].shift();
1541
- }
1542
- else {
1543
- isLastFrame = true;
1544
- }
1545
- if (_this[_loader].isComplete() !== true) {
1546
- _this[_drawFrame]();
1547
- if (item && ctx) {
1548
- drawContext(ctx, item.data, _this[_loader]);
1549
- }
1550
- }
1551
- else if (item && ctx) {
1552
- drawContext(ctx, item.data, _this[_loader]);
1553
- _this[_retainQueueOneItem]();
1554
- if (!isLastFrame) {
1555
- _this[_drawFrame]();
1556
- }
1557
- else {
1558
- _this[_status] = DrawStatus.FREE;
1559
- }
1560
- }
1561
- else {
1562
- _this[_status] = DrawStatus.FREE;
1563
- }
1564
- _this.trigger('drawFrame', { t: Date.now() });
1565
- if (_this[_loader].isComplete() === true && _this[_queue].length === 1 && _this[_status] === DrawStatus.FREE) {
1566
- if (ctx && _this[_queue][0] && _this[_queue][0].data) {
1567
- drawContext(ctx, _this[_queue][0].data, _this[_loader]);
1568
- }
1569
- _this.trigger('drawFrameComplete', { t: Date.now() });
1570
- _this[_freeze]();
1571
- }
1572
- });
1573
- };
1574
- Renderer.prototype[_retainQueueOneItem] = function () {
1575
- if (this[_queue].length <= 1) {
1576
- return;
1577
- }
1578
- var lastOne = deepClone(this[_queue][this[_queue].length - 1]);
1579
- this[_queue] = [lastOne];
1580
- };
1581
- return Renderer;
1582
- }(RendererEvent));
1583
-
1584
- var default_1 = /*#__PURE__*/Object.freeze({
1585
- __proto__: null,
1586
- Renderer: Renderer
1587
- });
1588
-
1589
- return default_1;
1590
-
1591
- })();
1248
+ [_retainQueueOneItem]() {
1249
+ if (this[_queue].length <= 1) {
1250
+ return;
1251
+ }
1252
+ const lastOne = deepClone(this[_queue][this[_queue].length - 1]);
1253
+ this[_queue] = [lastOne];
1254
+ }
1255
+ }
1256
+ return Renderer;
1257
+ }();