@idraw/util 0.2.0-alpha.9 → 0.3.0-0.3.0-beta.6.0

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.
@@ -1,288 +1,714 @@
1
- var iDrawUtil = (function () {
2
- 'use strict';
3
-
4
- function compose(middleware) {
5
- return function (context, next) {
6
- return dispatch(0);
7
- function dispatch(i) {
8
- var fn = middleware[i];
9
- if (i === middleware.length && next) {
10
- fn = next;
11
- }
12
- if (!fn)
13
- return Promise.resolve();
14
- try {
15
- return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
16
- }
17
- catch (err) {
18
- return Promise.reject(err);
19
- }
20
- }
21
- };
22
- }
23
- function delay(time) {
24
- return new Promise(function (resolve) {
25
- setTimeout(function () {
26
- resolve();
27
- }, time);
28
- });
29
- }
30
- function throttle(fn, timeout) {
31
- var timer = -1;
32
- return function () {
33
- var args = [];
34
- for (var _i = 0; _i < arguments.length; _i++) {
35
- args[_i] = arguments[_i];
36
- }
37
- if (timer > 0) {
38
- return;
39
- }
40
- timer = setTimeout(function () {
41
- fn.apply(void 0, args);
42
- timer = -1;
43
- }, timeout);
44
- };
45
- }
46
-
47
- function downloadImageFromCanvas(canvas, opts) {
48
- var filename = opts.filename, _a = opts.type, type = _a === void 0 ? 'image/jpeg' : _a;
49
- var stream = canvas.toDataURL(type);
50
- var downloadLink = document.createElement('a');
51
- downloadLink.href = stream;
52
- downloadLink.download = filename;
53
- var downloadClickEvent = document.createEvent('MouseEvents');
54
- downloadClickEvent.initEvent('click', true, false);
55
- downloadLink.dispatchEvent(downloadClickEvent);
56
- }
57
-
58
- function toColorHexNum(color) {
59
- return parseInt(color.replace(/^\#/, '0x'));
60
- }
61
- function toColorHexStr(color) {
62
- return '#' + color.toString(16);
63
- }
64
- function isColorStr(color) {
65
- return typeof color === 'string' && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color);
66
- }
67
-
68
- function createUUID() {
69
- function str4() {
70
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
71
- }
72
- return "" + str4() + str4() + "-" + str4() + "-" + str4() + "-" + str4() + "-" + str4() + str4() + str4();
73
- }
74
-
75
- function deepClone(target) {
76
- function _clone(t) {
77
- var type = is(t);
78
- if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
79
- return t;
80
- }
81
- else if (type === 'Array') {
82
- var arr_1 = [];
83
- t.forEach(function (item) {
84
- arr_1.push(_clone(item));
85
- });
86
- return arr_1;
87
- }
88
- else if (type === 'Object') {
89
- var obj_1 = {};
90
- var keys = Object.keys(t);
91
- keys.forEach(function (key) {
92
- obj_1[key] = _clone(t[key]);
93
- });
94
- return obj_1;
95
- }
96
- }
97
- return _clone(target);
98
- }
99
- function is(data) {
100
- return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
101
- }
102
-
103
- function parsePrototype(data) {
104
- var typeStr = Object.prototype.toString.call(data) || '';
105
- var result = typeStr.replace(/(\[object|\])/ig, '').trim();
106
- return result;
107
- }
108
- var istype = {
109
- type: function (data, lowerCase) {
110
- var result = parsePrototype(data);
111
- return lowerCase === true ? result.toLocaleLowerCase() : result;
112
- },
113
- array: function (data) {
114
- return parsePrototype(data) === 'Array';
115
- },
116
- json: function (data) {
117
- return parsePrototype(data) === 'Object';
118
- },
119
- function: function (data) {
120
- return parsePrototype(data) === 'Function';
121
- },
122
- asyncFunction: function (data) {
123
- return parsePrototype(data) === 'AsyncFunction';
124
- },
125
- string: function (data) {
126
- return parsePrototype(data) === 'String';
127
- },
128
- number: function (data) {
129
- return parsePrototype(data) === 'Number';
130
- },
131
- undefined: function (data) {
132
- return parsePrototype(data) === 'Undefined';
133
- },
134
- null: function (data) {
135
- return parsePrototype(data) === 'Null';
136
- },
137
- promise: function (data) {
138
- return parsePrototype(data) === 'Promise';
139
- },
1
+ var iDrawUtil = function(exports) {
2
+ "use strict";
3
+ function compose(middleware) {
4
+ return function(context, next) {
5
+ return dispatch(0);
6
+ function dispatch(i) {
7
+ let fn = middleware[i];
8
+ if (i === middleware.length && next) {
9
+ fn = next;
10
+ }
11
+ if (!fn)
12
+ return Promise.resolve();
13
+ try {
14
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
15
+ } catch (err) {
16
+ return Promise.reject(err);
17
+ }
18
+ }
19
+ };
20
+ }
21
+ function delay(time) {
22
+ return new Promise((resolve) => {
23
+ setTimeout(() => {
24
+ resolve();
25
+ }, time);
26
+ });
27
+ }
28
+ function throttle(fn, timeout) {
29
+ let timer = -1;
30
+ return function(...args) {
31
+ if (timer > 0) {
32
+ return;
33
+ }
34
+ timer = setTimeout(() => {
35
+ fn(...args);
36
+ timer = -1;
37
+ }, timeout);
38
+ };
39
+ }
40
+ function downloadImageFromCanvas(canvas, opts) {
41
+ const { filename, type = "image/jpeg" } = opts;
42
+ const stream = canvas.toDataURL(type);
43
+ const downloadLink = document.createElement("a");
44
+ downloadLink.href = stream;
45
+ downloadLink.download = filename;
46
+ const downloadClickEvent = document.createEvent("MouseEvents");
47
+ downloadClickEvent.initEvent("click", true, false);
48
+ downloadLink.dispatchEvent(downloadClickEvent);
49
+ }
50
+ function toColorHexNum(color2) {
51
+ return parseInt(color2.replace(/^\#/, "0x"));
52
+ }
53
+ function toColorHexStr(color2) {
54
+ return "#" + color2.toString(16);
55
+ }
56
+ function isColorStr(color2) {
57
+ return typeof color2 === "string" && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color2);
58
+ }
59
+ function createUUID() {
60
+ function str4() {
61
+ return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
62
+ }
63
+ return `${str4()}${str4()}-${str4()}-${str4()}-${str4()}-${str4()}${str4()}${str4()}`;
64
+ }
65
+ function deepClone(target) {
66
+ function _clone(t) {
67
+ const type = is$1(t);
68
+ if (["Null", "Number", "String", "Boolean", "Undefined"].indexOf(type) >= 0) {
69
+ return t;
70
+ } else if (type === "Array") {
71
+ const arr = [];
72
+ t.forEach((item) => {
73
+ arr.push(_clone(item));
74
+ });
75
+ return arr;
76
+ } else if (type === "Object") {
77
+ const obj = {};
78
+ const keys = Object.keys(t);
79
+ keys.forEach((key) => {
80
+ obj[key] = _clone(t[key]);
81
+ });
82
+ return obj;
83
+ }
84
+ }
85
+ return _clone(target);
86
+ }
87
+ function is$1(data) {
88
+ return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, "").split(" ")[1];
89
+ }
90
+ function parsePrototype(data) {
91
+ const typeStr = Object.prototype.toString.call(data) || "";
92
+ const result = typeStr.replace(/(\[object|\])/ig, "").trim();
93
+ return result;
94
+ }
95
+ const istype = {
96
+ type(data, lowerCase) {
97
+ const result = parsePrototype(data);
98
+ return lowerCase === true ? result.toLocaleLowerCase() : result;
99
+ },
100
+ array(data) {
101
+ return parsePrototype(data) === "Array";
102
+ },
103
+ json(data) {
104
+ return parsePrototype(data) === "Object";
105
+ },
106
+ function(data) {
107
+ return parsePrototype(data) === "Function";
108
+ },
109
+ asyncFunction(data) {
110
+ return parsePrototype(data) === "AsyncFunction";
111
+ },
112
+ string(data) {
113
+ return parsePrototype(data) === "String";
114
+ },
115
+ number(data) {
116
+ return parsePrototype(data) === "Number";
117
+ },
118
+ undefined(data) {
119
+ return parsePrototype(data) === "Undefined";
120
+ },
121
+ null(data) {
122
+ return parsePrototype(data) === "Null";
123
+ },
124
+ promise(data) {
125
+ return parsePrototype(data) === "Promise";
126
+ }
127
+ // nodeList (data: any): boolean {
128
+ // return parsePrototype(data) === 'NodeList';
129
+ // },
130
+ // imageData(data: any): boolean {
131
+ // return parsePrototype(data) === 'ImageData';
132
+ // }
140
133
  };
141
-
142
- function __awaiter(thisArg, _arguments, P, generator) {
143
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
144
- return new (P || (P = Promise))(function (resolve, reject) {
145
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
146
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
147
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
148
- step((generator = generator.apply(thisArg, _arguments || [])).next());
149
- });
150
- }
151
- function __generator(thisArg, body) {
152
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
153
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
154
- function verb(n) { return function (v) { return step([n, v]); }; }
155
- function step(op) {
156
- if (f) throw new TypeError("Generator is already executing.");
157
- while (_) try {
158
- 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;
159
- if (y = 0, t) op = [op[0] & 2, t.value];
160
- switch (op[0]) {
161
- case 0: case 1: t = op; break;
162
- case 4: _.label++; return { value: op[1], done: false };
163
- case 5: _.label++; y = op[1]; op = [0]; continue;
164
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
165
- default:
166
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
167
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
168
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
169
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
170
- if (t[2]) _.ops.pop();
171
- _.trys.pop(); continue;
172
- }
173
- op = body.call(thisArg, _);
174
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
175
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
134
+ function parseHTMLToDataURL(html2, opts) {
135
+ const { width, height } = opts;
136
+ return new Promise((resolve, reject) => {
137
+ const _svg = `
138
+ <svg xmlns="http://www.w3.org/2000/svg" width="${width || ""}" height = "${height || ""}">
139
+ <foreignObject width="100%" height="100%">
140
+ <div xmlns = "http://www.w3.org/1999/xhtml">
141
+ ${html2}
142
+ </div>
143
+ </foreignObject>
144
+ </svg>
145
+ `;
146
+ const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
147
+ const reader = new FileReader();
148
+ reader.readAsDataURL(blob);
149
+ reader.onload = function(event) {
150
+ var _a;
151
+ const base64 = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result;
152
+ resolve(base64);
153
+ };
154
+ reader.onerror = function(err) {
155
+ reject(err);
156
+ };
157
+ });
158
+ }
159
+ function parseSVGToDataURL(svg2) {
160
+ return new Promise((resolve, reject) => {
161
+ const _svg = svg2;
162
+ const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
163
+ const reader = new FileReader();
164
+ reader.readAsDataURL(blob);
165
+ reader.onload = function(event) {
166
+ var _a;
167
+ const base64 = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result;
168
+ resolve(base64);
169
+ };
170
+ reader.onerror = function(err) {
171
+ reject(err);
172
+ };
173
+ });
174
+ }
175
+ const { Image } = window;
176
+ function loadImage(src) {
177
+ return new Promise((resolve, reject) => {
178
+ const img = new Image();
179
+ img.crossOrigin = "anonymous";
180
+ img.onload = function() {
181
+ resolve(img);
182
+ };
183
+ img.onabort = reject;
184
+ img.onerror = reject;
185
+ img.src = src;
186
+ });
187
+ }
188
+ async function loadSVG(svg2) {
189
+ const dataURL = await parseSVGToDataURL(svg2);
190
+ const image = await loadImage(dataURL);
191
+ return image;
192
+ }
193
+ function filterAmpersand(str) {
194
+ return str.replace(/\&/ig, "&amp;");
195
+ }
196
+ async function loadHTML(html2, opts) {
197
+ html2 = filterAmpersand(html2);
198
+ const dataURL = await parseHTMLToDataURL(html2, opts);
199
+ const image = await loadImage(dataURL);
200
+ return image;
201
+ }
202
+ class Context {
203
+ // private _scale: number;
204
+ // private _scrollX: number;
205
+ // private _scrollY: number;
206
+ constructor(ctx, opts) {
207
+ this._opts = opts;
208
+ this._ctx = ctx;
209
+ this._transform = {
210
+ scale: 1,
211
+ scrollX: 0,
212
+ scrollY: 0
213
+ };
214
+ }
215
+ getContext() {
216
+ return this._ctx;
217
+ }
218
+ resetSize(opts) {
219
+ this._opts = { ...this._opts, ...opts };
220
+ }
221
+ calcDeviceNum(num) {
222
+ return num * this._opts.devicePixelRatio;
223
+ }
224
+ calcScreenNum(num) {
225
+ return num / this._opts.devicePixelRatio;
226
+ }
227
+ getSize() {
228
+ return {
229
+ width: this._opts.width,
230
+ height: this._opts.height,
231
+ contextWidth: this._opts.contextWidth,
232
+ contextHeight: this._opts.contextHeight,
233
+ devicePixelRatio: this._opts.devicePixelRatio
234
+ };
235
+ }
236
+ setTransform(config) {
237
+ this._transform = { ...this._transform, ...config };
238
+ }
239
+ getTransform() {
240
+ return {
241
+ scale: this._transform.scale,
242
+ scrollX: this._transform.scrollX,
243
+ scrollY: this._transform.scrollY
244
+ };
245
+ }
246
+ setFillStyle(color2) {
247
+ this._ctx.fillStyle = color2;
248
+ }
249
+ fill(fillRule) {
250
+ return this._ctx.fill(fillRule || "nonzero");
251
+ }
252
+ arc(x2, y2, radius, startAngle, endAngle, anticlockwise) {
253
+ return this._ctx.arc(
254
+ this._doSize(x2),
255
+ this._doSize(y2),
256
+ this._doSize(radius),
257
+ startAngle,
258
+ endAngle,
259
+ anticlockwise
260
+ );
261
+ }
262
+ rect(x2, y2, w2, h2) {
263
+ return this._ctx.rect(
264
+ this._doSize(x2),
265
+ this._doSize(y2),
266
+ this._doSize(w2),
267
+ this._doSize(h2)
268
+ );
269
+ }
270
+ fillRect(x2, y2, w2, h2) {
271
+ return this._ctx.fillRect(
272
+ this._doSize(x2),
273
+ this._doSize(y2),
274
+ this._doSize(w2),
275
+ this._doSize(h2)
276
+ );
277
+ }
278
+ clearRect(x2, y2, w2, h2) {
279
+ return this._ctx.clearRect(
280
+ this._doSize(x2),
281
+ this._doSize(y2),
282
+ this._doSize(w2),
283
+ this._doSize(h2)
284
+ );
285
+ }
286
+ beginPath() {
287
+ return this._ctx.beginPath();
288
+ }
289
+ closePath() {
290
+ return this._ctx.closePath();
291
+ }
292
+ lineTo(x2, y2) {
293
+ return this._ctx.lineTo(this._doSize(x2), this._doSize(y2));
294
+ }
295
+ moveTo(x2, y2) {
296
+ return this._ctx.moveTo(this._doSize(x2), this._doSize(y2));
297
+ }
298
+ arcTo(x1, y1, x2, y2, radius) {
299
+ return this._ctx.arcTo(
300
+ this._doSize(x1),
301
+ this._doSize(y1),
302
+ this._doSize(x2),
303
+ this._doSize(y2),
304
+ this._doSize(radius)
305
+ );
306
+ }
307
+ setLineWidth(w2) {
308
+ return this._ctx.lineWidth = this._doSize(w2);
309
+ }
310
+ setLineDash(nums) {
311
+ return this._ctx.setLineDash(nums.map((n) => this._doSize(n)));
312
+ }
313
+ isPointInPath(x2, y2) {
314
+ return this._ctx.isPointInPath(this._doX(x2), this._doY(y2));
315
+ }
316
+ isPointInPathWithoutScroll(x2, y2) {
317
+ return this._ctx.isPointInPath(this._doSize(x2), this._doSize(y2));
318
+ }
319
+ setStrokeStyle(color2) {
320
+ this._ctx.strokeStyle = color2;
321
+ }
322
+ stroke() {
323
+ return this._ctx.stroke();
324
+ }
325
+ translate(x2, y2) {
326
+ return this._ctx.translate(this._doSize(x2), this._doSize(y2));
327
+ }
328
+ rotate(angle2) {
329
+ return this._ctx.rotate(angle2);
330
+ }
331
+ drawImage(...args) {
332
+ const image = args[0];
333
+ const sx = args[1];
334
+ const sy = args[2];
335
+ const sw = args[3];
336
+ const sh = args[4];
337
+ const dx = args[args.length - 4];
338
+ const dy = args[args.length - 3];
339
+ const dw = args[args.length - 2];
340
+ const dh = args[args.length - 1];
341
+ if (args.length === 9) {
342
+ return this._ctx.drawImage(
343
+ image,
344
+ this._doSize(sx),
345
+ this._doSize(sy),
346
+ this._doSize(sw),
347
+ this._doSize(sh),
348
+ this._doSize(dx),
349
+ this._doSize(dy),
350
+ this._doSize(dw),
351
+ this._doSize(dh)
352
+ );
353
+ } else {
354
+ return this._ctx.drawImage(
355
+ image,
356
+ this._doSize(dx),
357
+ this._doSize(dy),
358
+ this._doSize(dw),
359
+ this._doSize(dh)
360
+ );
361
+ }
362
+ }
363
+ createPattern(image, repetition) {
364
+ return this._ctx.createPattern(image, repetition);
365
+ }
366
+ measureText(text2) {
367
+ return this._ctx.measureText(text2);
368
+ }
369
+ setTextAlign(align) {
370
+ this._ctx.textAlign = align;
371
+ }
372
+ fillText(text2, x2, y2, maxWidth) {
373
+ if (maxWidth !== void 0) {
374
+ return this._ctx.fillText(
375
+ text2,
376
+ this._doSize(x2),
377
+ this._doSize(y2),
378
+ this._doSize(maxWidth)
379
+ );
380
+ } else {
381
+ return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2));
176
382
  }
383
+ }
384
+ strokeText(text2, x2, y2, maxWidth) {
385
+ if (maxWidth !== void 0) {
386
+ return this._ctx.strokeText(
387
+ text2,
388
+ this._doSize(x2),
389
+ this._doSize(y2),
390
+ this._doSize(maxWidth)
391
+ );
392
+ } else {
393
+ return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2));
394
+ }
395
+ }
396
+ setFont(opts) {
397
+ const strList = [];
398
+ if (opts.fontWeight === "bold") {
399
+ strList.push(`${opts.fontWeight}`);
400
+ }
401
+ strList.push(`${this._doSize(opts.fontSize || 12)}px`);
402
+ strList.push(`${opts.fontFamily || "sans-serif"}`);
403
+ this._ctx.font = `${strList.join(" ")}`;
404
+ }
405
+ setTextBaseline(baseline) {
406
+ this._ctx.textBaseline = baseline;
407
+ }
408
+ setGlobalAlpha(alpha) {
409
+ this._ctx.globalAlpha = alpha;
410
+ }
411
+ save() {
412
+ this._ctx.save();
413
+ }
414
+ restore() {
415
+ this._ctx.restore();
416
+ }
417
+ scale(ratioX, ratioY) {
418
+ this._ctx.scale(ratioX, ratioY);
419
+ }
420
+ setShadowColor(color2) {
421
+ this._ctx.shadowColor = color2;
422
+ }
423
+ setShadowOffsetX(offsetX) {
424
+ this._ctx.shadowOffsetX = this._doSize(offsetX);
425
+ }
426
+ setShadowOffsetY(offsetY) {
427
+ this._ctx.shadowOffsetY = this._doSize(offsetY);
428
+ }
429
+ setShadowBlur(blur) {
430
+ this._ctx.shadowBlur = this._doSize(blur);
431
+ }
432
+ ellipse(x2, y2, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
433
+ this._ctx.ellipse(
434
+ this._doSize(x2),
435
+ this._doSize(y2),
436
+ this._doSize(radiusX),
437
+ this._doSize(radiusY),
438
+ rotation,
439
+ startAngle,
440
+ endAngle,
441
+ counterclockwise
442
+ );
443
+ }
444
+ _doSize(num) {
445
+ return this._opts.devicePixelRatio * num;
446
+ }
447
+ _doX(x2) {
448
+ const { scale, scrollX } = this._transform;
449
+ const _x = (x2 - scrollX) / scale;
450
+ return this._doSize(_x);
451
+ }
452
+ _doY(y2) {
453
+ const { scale, scrollY } = this._transform;
454
+ const _y = (y2 - scrollY) / scale;
455
+ return this._doSize(_y);
456
+ }
457
+ }
458
+ function number(value) {
459
+ return typeof value === "number" && (value > 0 || value <= 0);
460
+ }
461
+ function x(value) {
462
+ return number(value);
463
+ }
464
+ function y(value) {
465
+ return number(value);
466
+ }
467
+ function w(value) {
468
+ return typeof value === "number" && value >= 0;
469
+ }
470
+ function h(value) {
471
+ return typeof value === "number" && value >= 0;
177
472
  }
178
-
179
- function parseHTMLToDataURL(html, opts) {
180
- var width = opts.width, height = opts.height;
181
- return new Promise(function (resolve, reject) {
182
- var _svg = "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + (width || '') + "\" height = \"" + (height || '') + "\">\n <foreignObject width=\"100%\" height=\"100%\">\n <div xmlns = \"http://www.w3.org/1999/xhtml\">\n " + html + "\n </div>\n </foreignObject>\n </svg>\n ";
183
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
184
- var reader = new FileReader();
185
- reader.readAsDataURL(blob);
186
- reader.onload = function (event) {
187
- var _a;
188
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
189
- resolve(base64);
190
- };
191
- reader.onerror = function (err) {
192
- reject(err);
193
- };
194
- });
195
- }
196
- function parseSVGToDataURL(svg) {
197
- return new Promise(function (resolve, reject) {
198
- var _svg = svg;
199
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
200
- var reader = new FileReader();
201
- reader.readAsDataURL(blob);
202
- reader.onload = function (event) {
203
- var _a;
204
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
205
- resolve(base64);
206
- };
207
- reader.onerror = function (err) {
208
- reject(err);
209
- };
210
- });
211
- }
212
-
213
- var Image = window.Image;
214
- function loadImage(src) {
215
- return new Promise(function (resolve, reject) {
216
- var img = new Image;
217
- img.onload = function () {
218
- resolve(img);
219
- };
220
- img.onabort = reject;
221
- img.onerror = reject;
222
- img.src = src;
223
- });
224
- }
225
- function loadSVG(svg) {
226
- return __awaiter(this, void 0, void 0, function () {
227
- var dataURL, image;
228
- return __generator(this, function (_a) {
229
- switch (_a.label) {
230
- case 0: return [4, parseSVGToDataURL(svg)];
231
- case 1:
232
- dataURL = _a.sent();
233
- return [4, loadImage(dataURL)];
234
- case 2:
235
- image = _a.sent();
236
- return [2, image];
237
- }
238
- });
239
- });
240
- }
241
- function loadHTML(html, opts) {
242
- return __awaiter(this, void 0, void 0, function () {
243
- var dataURL, image;
244
- return __generator(this, function (_a) {
245
- switch (_a.label) {
246
- case 0: return [4, parseHTMLToDataURL(html, opts)];
247
- case 1:
248
- dataURL = _a.sent();
249
- return [4, loadImage(dataURL)];
250
- case 2:
251
- image = _a.sent();
252
- return [2, image];
253
- }
254
- });
255
- });
256
- }
257
-
258
- var index = {
259
- time: {
260
- delay: delay,
261
- compose: compose,
262
- throttle: throttle,
263
- },
264
- loader: {
265
- loadImage: loadImage,
266
- loadSVG: loadSVG,
267
- loadHTML: loadHTML,
268
- },
269
- file: {
270
- downloadImageFromCanvas: downloadImageFromCanvas,
271
- },
272
- color: {
273
- toColorHexStr: toColorHexStr,
274
- toColorHexNum: toColorHexNum,
275
- isColorStr: isColorStr,
276
- },
277
- uuid: {
278
- createUUID: createUUID
279
- },
280
- istype: istype,
281
- data: {
282
- deepClone: deepClone,
283
- }
473
+ function angle(value) {
474
+ return typeof value === "number" && value >= -360 && value <= 360;
475
+ }
476
+ function borderWidth(value) {
477
+ return w(value);
478
+ }
479
+ function borderRadius(value) {
480
+ return number(value) && value >= 0;
481
+ }
482
+ function color(value) {
483
+ return isColorStr(value);
484
+ }
485
+ function imageURL(value) {
486
+ return typeof value === "string" && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`);
487
+ }
488
+ function imageBase64(value) {
489
+ return typeof value === "string" && /^(data:image\/)/.test(`${value}`);
490
+ }
491
+ function imageSrc(value) {
492
+ return imageBase64(value) || imageURL(value);
493
+ }
494
+ function svg(value) {
495
+ return typeof value === "string" && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim());
496
+ }
497
+ function html(value) {
498
+ let result = false;
499
+ if (typeof value === "string") {
500
+ let div = document.createElement("div");
501
+ div.innerHTML = value;
502
+ if (div.children.length > 0) {
503
+ result = true;
504
+ }
505
+ div = null;
506
+ }
507
+ return result;
508
+ }
509
+ function text(value) {
510
+ return typeof value === "string";
511
+ }
512
+ function fontSize(value) {
513
+ return number(value) && value > 0;
514
+ }
515
+ function lineHeight(value) {
516
+ return number(value) && value > 0;
517
+ }
518
+ function strokeWidth(value) {
519
+ return number(value) && value > 0;
520
+ }
521
+ function textAlign(value) {
522
+ return ["center", "left", "right"].includes(value);
523
+ }
524
+ function fontFamily(value) {
525
+ return typeof value === "string" && value.length > 0;
526
+ }
527
+ function fontWeight(value) {
528
+ return ["bold"].includes(value);
529
+ }
530
+ const is = {
531
+ x,
532
+ y,
533
+ w,
534
+ h,
535
+ angle,
536
+ number,
537
+ borderWidth,
538
+ borderRadius,
539
+ color,
540
+ imageSrc,
541
+ imageURL,
542
+ imageBase64,
543
+ svg,
544
+ html,
545
+ text,
546
+ fontSize,
547
+ lineHeight,
548
+ textAlign,
549
+ fontFamily,
550
+ fontWeight,
551
+ strokeWidth
552
+ };
553
+ function attrs(attrs2) {
554
+ const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = attrs2;
555
+ if (!(is.x(x2) && is.y(y2) && is.w(w2) && is.h(h2) && is.angle(angle2))) {
556
+ return false;
557
+ }
558
+ if (!(angle2 >= -360 && angle2 <= 360)) {
559
+ return false;
560
+ }
561
+ return true;
562
+ }
563
+ function box(desc = {}) {
564
+ const { borderColor, borderRadius: borderRadius2, borderWidth: borderWidth2 } = desc;
565
+ if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
566
+ return false;
567
+ }
568
+ if (desc.hasOwnProperty("borderRadius") && !is.number(borderRadius2)) {
569
+ return false;
570
+ }
571
+ if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
572
+ return false;
573
+ }
574
+ return true;
575
+ }
576
+ function rectDesc(desc) {
577
+ const { bgColor } = desc;
578
+ if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
579
+ return false;
580
+ }
581
+ if (!box(desc)) {
582
+ return false;
583
+ }
584
+ return true;
585
+ }
586
+ function circleDesc(desc) {
587
+ const { bgColor, borderColor, borderWidth: borderWidth2 } = desc;
588
+ if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
589
+ return false;
590
+ }
591
+ if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
592
+ return false;
593
+ }
594
+ if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
595
+ return false;
596
+ }
597
+ return true;
598
+ }
599
+ function imageDesc(desc) {
600
+ const { src } = desc;
601
+ if (!is.imageSrc(src)) {
602
+ return false;
603
+ }
604
+ return true;
605
+ }
606
+ function svgDesc(desc) {
607
+ const { svg: svg2 } = desc;
608
+ if (!is.svg(svg2)) {
609
+ return false;
610
+ }
611
+ return true;
612
+ }
613
+ function htmlDesc(desc) {
614
+ const { html: html2 } = desc;
615
+ if (!is.html(html2)) {
616
+ return false;
617
+ }
618
+ return true;
619
+ }
620
+ function textDesc(desc) {
621
+ const {
622
+ text: text2,
623
+ color: color2,
624
+ fontSize: fontSize2,
625
+ lineHeight: lineHeight2,
626
+ fontFamily: fontFamily2,
627
+ textAlign: textAlign2,
628
+ fontWeight: fontWeight2,
629
+ bgColor,
630
+ strokeWidth: strokeWidth2,
631
+ strokeColor
632
+ } = desc;
633
+ if (!is.text(text2)) {
634
+ return false;
635
+ }
636
+ if (!is.color(color2)) {
637
+ return false;
638
+ }
639
+ if (!is.fontSize(fontSize2)) {
640
+ return false;
641
+ }
642
+ if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
643
+ return false;
644
+ }
645
+ if (desc.hasOwnProperty("fontWeight") && !is.fontWeight(fontWeight2)) {
646
+ return false;
647
+ }
648
+ if (desc.hasOwnProperty("lineHeight") && !is.lineHeight(lineHeight2)) {
649
+ return false;
650
+ }
651
+ if (desc.hasOwnProperty("fontFamily") && !is.fontFamily(fontFamily2)) {
652
+ return false;
653
+ }
654
+ if (desc.hasOwnProperty("textAlign") && !is.textAlign(textAlign2)) {
655
+ return false;
656
+ }
657
+ if (desc.hasOwnProperty("strokeWidth") && !is.strokeWidth(strokeWidth2)) {
658
+ return false;
659
+ }
660
+ if (desc.hasOwnProperty("strokeColor") && !is.color(strokeColor)) {
661
+ return false;
662
+ }
663
+ if (!box(desc)) {
664
+ return false;
665
+ }
666
+ return true;
667
+ }
668
+ const check = {
669
+ attrs,
670
+ textDesc,
671
+ rectDesc,
672
+ circleDesc,
673
+ imageDesc,
674
+ svgDesc,
675
+ htmlDesc
676
+ };
677
+ const index = {
678
+ is,
679
+ check,
680
+ delay,
681
+ compose,
682
+ throttle,
683
+ loadImage,
684
+ loadSVG,
685
+ loadHTML,
686
+ downloadImageFromCanvas,
687
+ toColorHexStr,
688
+ toColorHexNum,
689
+ isColorStr,
690
+ createUUID,
691
+ istype,
692
+ deepClone,
693
+ Context
284
694
  };
285
-
286
- return index;
287
-
288
- })();
695
+ exports.Context = Context;
696
+ exports.check = check;
697
+ exports.compose = compose;
698
+ exports.createUUID = createUUID;
699
+ exports.deepClone = deepClone;
700
+ exports.default = index;
701
+ exports.delay = delay;
702
+ exports.downloadImageFromCanvas = downloadImageFromCanvas;
703
+ exports.is = is;
704
+ exports.isColorStr = isColorStr;
705
+ exports.istype = istype;
706
+ exports.loadHTML = loadHTML;
707
+ exports.loadImage = loadImage;
708
+ exports.loadSVG = loadSVG;
709
+ exports.throttle = throttle;
710
+ exports.toColorHexNum = toColorHexNum;
711
+ exports.toColorHexStr = toColorHexStr;
712
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
713
+ return exports;
714
+ }({});