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