@idraw/util 0.2.0-alpha.16 → 0.2.0-alpha.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -71,7 +71,7 @@ function createUUID() {
71
71
 
72
72
  function deepClone(target) {
73
73
  function _clone(t) {
74
- var type = is(t);
74
+ var type = is$1(t);
75
75
  if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
76
76
  return t;
77
77
  }
@@ -93,7 +93,7 @@ function deepClone(target) {
93
93
  }
94
94
  return _clone(target);
95
95
  }
96
- function is(data) {
96
+ function is$1(data) {
97
97
  return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
98
98
  }
99
99
 
@@ -136,6 +136,16 @@ var istype = {
136
136
  },
137
137
  };
138
138
 
139
+ var __assign = function() {
140
+ __assign = Object.assign || function __assign(t) {
141
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
142
+ s = arguments[i];
143
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
144
+ }
145
+ return t;
146
+ };
147
+ return __assign.apply(this, arguments);
148
+ };
139
149
  function __awaiter(thisArg, _arguments, P, generator) {
140
150
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
141
151
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -252,7 +262,420 @@ function loadHTML(html, opts) {
252
262
  });
253
263
  }
254
264
 
265
+ var Context = (function () {
266
+ function Context(ctx, opts) {
267
+ this._opts = opts;
268
+ this._ctx = ctx;
269
+ this._transform = {
270
+ scale: 1,
271
+ scrollX: 0,
272
+ scrollY: 0,
273
+ };
274
+ }
275
+ Context.prototype.getContext = function () {
276
+ return this._ctx;
277
+ };
278
+ Context.prototype.resetSize = function (opts) {
279
+ this._opts = __assign(__assign({}, this._opts), opts);
280
+ };
281
+ Context.prototype.calcDeviceNum = function (num) {
282
+ return num * this._opts.devicePixelRatio;
283
+ };
284
+ Context.prototype.calcScreenNum = function (num) {
285
+ return num / this._opts.devicePixelRatio;
286
+ };
287
+ Context.prototype.getSize = function () {
288
+ return {
289
+ width: this._opts.width,
290
+ height: this._opts.height,
291
+ contextWidth: this._opts.contextWidth,
292
+ contextHeight: this._opts.contextHeight,
293
+ devicePixelRatio: this._opts.devicePixelRatio,
294
+ };
295
+ };
296
+ Context.prototype.setTransform = function (config) {
297
+ this._transform = __assign(__assign({}, this._transform), config);
298
+ };
299
+ Context.prototype.getTransform = function () {
300
+ return {
301
+ scale: this._transform.scale,
302
+ scrollX: this._transform.scrollX,
303
+ scrollY: this._transform.scrollY,
304
+ };
305
+ };
306
+ Context.prototype.setFillStyle = function (color) {
307
+ this._ctx.fillStyle = color;
308
+ };
309
+ Context.prototype.fill = function (fillRule) {
310
+ return this._ctx.fill(fillRule || 'nonzero');
311
+ };
312
+ Context.prototype.arc = function (x, y, radius, startAngle, endAngle, anticlockwise) {
313
+ return this._ctx.arc(this._doSize(x), this._doSize(y), this._doSize(radius), startAngle, endAngle, anticlockwise);
314
+ };
315
+ Context.prototype.rect = function (x, y, w, h) {
316
+ return this._ctx.rect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
317
+ };
318
+ Context.prototype.fillRect = function (x, y, w, h) {
319
+ return this._ctx.fillRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
320
+ };
321
+ Context.prototype.clearRect = function (x, y, w, h) {
322
+ return this._ctx.clearRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
323
+ };
324
+ Context.prototype.beginPath = function () {
325
+ return this._ctx.beginPath();
326
+ };
327
+ Context.prototype.closePath = function () {
328
+ return this._ctx.closePath();
329
+ };
330
+ Context.prototype.lineTo = function (x, y) {
331
+ return this._ctx.lineTo(this._doSize(x), this._doSize(y));
332
+ };
333
+ Context.prototype.moveTo = function (x, y) {
334
+ return this._ctx.moveTo(this._doSize(x), this._doSize(y));
335
+ };
336
+ Context.prototype.arcTo = function (x1, y1, x2, y2, radius) {
337
+ return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
338
+ };
339
+ Context.prototype.setLineWidth = function (w) {
340
+ return this._ctx.lineWidth = this._doSize(w);
341
+ };
342
+ Context.prototype.setLineDash = function (nums) {
343
+ var _this = this;
344
+ return this._ctx.setLineDash(nums.map(function (n) { return _this._doSize(n); }));
345
+ };
346
+ Context.prototype.isPointInPath = function (x, y) {
347
+ return this._ctx.isPointInPath(this._doX(x), this._doY(y));
348
+ };
349
+ Context.prototype.isPointInPathWithoutScroll = function (x, y) {
350
+ return this._ctx.isPointInPath(this._doSize(x), this._doSize(y));
351
+ };
352
+ Context.prototype.setStrokeStyle = function (color) {
353
+ this._ctx.strokeStyle = color;
354
+ };
355
+ Context.prototype.stroke = function () {
356
+ return this._ctx.stroke();
357
+ };
358
+ Context.prototype.translate = function (x, y) {
359
+ return this._ctx.translate(this._doSize(x), this._doSize(y));
360
+ };
361
+ Context.prototype.rotate = function (angle) {
362
+ return this._ctx.rotate(angle);
363
+ };
364
+ Context.prototype.drawImage = function () {
365
+ var args = [];
366
+ for (var _i = 0; _i < arguments.length; _i++) {
367
+ args[_i] = arguments[_i];
368
+ }
369
+ var image = args[0];
370
+ var sx = args[1];
371
+ var sy = args[2];
372
+ var sw = args[3];
373
+ var sh = args[4];
374
+ var dx = args[args.length - 4];
375
+ var dy = args[args.length - 3];
376
+ var dw = args[args.length - 2];
377
+ var dh = args[args.length - 1];
378
+ if (args.length === 9) {
379
+ 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));
380
+ }
381
+ else {
382
+ return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
383
+ }
384
+ };
385
+ Context.prototype.createPattern = function (image, repetition) {
386
+ return this._ctx.createPattern(image, repetition);
387
+ };
388
+ Context.prototype.measureText = function (text) {
389
+ return this._ctx.measureText(text);
390
+ };
391
+ Context.prototype.setTextAlign = function (align) {
392
+ this._ctx.textAlign = align;
393
+ };
394
+ Context.prototype.fillText = function (text, x, y, maxWidth) {
395
+ if (maxWidth !== undefined) {
396
+ return this._ctx.fillText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
397
+ }
398
+ else {
399
+ return this._ctx.fillText(text, this._doSize(x), this._doSize(y));
400
+ }
401
+ };
402
+ Context.prototype.strokeText = function (text, x, y, maxWidth) {
403
+ if (maxWidth !== undefined) {
404
+ return this._ctx.strokeText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
405
+ }
406
+ else {
407
+ return this._ctx.strokeText(text, this._doSize(x), this._doSize(y));
408
+ }
409
+ };
410
+ Context.prototype.setFont = function (opts) {
411
+ var strList = [];
412
+ if (opts.fontWeight === 'bold') {
413
+ strList.push("" + opts.fontWeight);
414
+ }
415
+ strList.push(this._doSize(opts.fontSize || 12) + "px");
416
+ strList.push("" + (opts.fontFamily || 'sans-serif'));
417
+ this._ctx.font = "" + strList.join(' ');
418
+ };
419
+ Context.prototype.setTextBaseline = function (baseline) {
420
+ this._ctx.textBaseline = baseline;
421
+ };
422
+ Context.prototype.setGlobalAlpha = function (alpha) {
423
+ this._ctx.globalAlpha = alpha;
424
+ };
425
+ Context.prototype.save = function () {
426
+ this._ctx.save();
427
+ };
428
+ Context.prototype.restore = function () {
429
+ this._ctx.restore();
430
+ };
431
+ Context.prototype.scale = function (ratioX, ratioY) {
432
+ this._ctx.scale(ratioX, ratioY);
433
+ };
434
+ Context.prototype.setShadowColor = function (color) {
435
+ this._ctx.shadowColor = color;
436
+ };
437
+ Context.prototype.setShadowOffsetX = function (offsetX) {
438
+ this._ctx.shadowOffsetX = this._doSize(offsetX);
439
+ };
440
+ Context.prototype.setShadowOffsetY = function (offsetY) {
441
+ this._ctx.shadowOffsetY = this._doSize(offsetY);
442
+ };
443
+ Context.prototype.setShadowBlur = function (blur) {
444
+ this._ctx.shadowBlur = this._doSize(blur);
445
+ };
446
+ Context.prototype.ellipse = function (x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
447
+ this._ctx.ellipse(this._doSize(x), this._doSize(y), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
448
+ };
449
+ Context.prototype._doSize = function (num) {
450
+ return this._opts.devicePixelRatio * num;
451
+ };
452
+ Context.prototype._doX = function (x) {
453
+ var _a = this._transform, scale = _a.scale, scrollX = _a.scrollX;
454
+ var _x = (x - scrollX) / scale;
455
+ return this._doSize(_x);
456
+ };
457
+ Context.prototype._doY = function (y) {
458
+ var _a = this._transform, scale = _a.scale, scrollY = _a.scrollY;
459
+ var _y = (y - scrollY) / scale;
460
+ return this._doSize(_y);
461
+ };
462
+ return Context;
463
+ }());
464
+
465
+ function number(value) {
466
+ return (typeof value === 'number' && (value > 0 || value <= 0));
467
+ }
468
+ function x(value) {
469
+ return number(value);
470
+ }
471
+ function y(value) {
472
+ return number(value);
473
+ }
474
+ function w(value) {
475
+ return (typeof value === 'number' && value >= 0);
476
+ }
477
+ function h(value) {
478
+ return (typeof value === 'number' && value >= 0);
479
+ }
480
+ function angle(value) {
481
+ return (typeof value === 'number' && value >= -360 && value <= 360);
482
+ }
483
+ function borderWidth(value) {
484
+ return w(value);
485
+ }
486
+ function borderRadius(value) {
487
+ return number(value) && value >= 0;
488
+ }
489
+ function color(value) {
490
+ return isColorStr(value);
491
+ }
492
+ function imageURL(value) {
493
+ return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test("" + value));
494
+ }
495
+ function imageBase64(value) {
496
+ return (typeof value === 'string' && /^(data:image\/)/.test("" + value));
497
+ }
498
+ function imageSrc(value) {
499
+ return (imageBase64(value) || imageURL(value));
500
+ }
501
+ function svg(value) {
502
+ return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test(("" + value).trim()) && /<\/[\s]{0,}svg>$/i.test(("" + value).trim()));
503
+ }
504
+ function html(value) {
505
+ var result = false;
506
+ if (typeof value === 'string') {
507
+ var div = document.createElement('div');
508
+ div.innerHTML = value;
509
+ if (div.children.length > 0) {
510
+ result = true;
511
+ }
512
+ div = null;
513
+ }
514
+ return result;
515
+ }
516
+ function text(value) {
517
+ return typeof value === 'string';
518
+ }
519
+ function fontSize(value) {
520
+ return number(value) && value > 0;
521
+ }
522
+ function lineHeight(value) {
523
+ return number(value) && value > 0;
524
+ }
525
+ function strokeWidth(value) {
526
+ return number(value) && value > 0;
527
+ }
528
+ function textAlign(value) {
529
+ return ['center', 'left', 'right'].includes(value);
530
+ }
531
+ function fontFamily(value) {
532
+ return typeof value === 'string' && value.length > 0;
533
+ }
534
+ function fontWeight(value) {
535
+ return ['bold'].includes(value);
536
+ }
537
+ var is = {
538
+ x: x,
539
+ y: y,
540
+ w: w,
541
+ h: h,
542
+ angle: angle,
543
+ number: number,
544
+ borderWidth: borderWidth,
545
+ borderRadius: borderRadius,
546
+ color: color,
547
+ imageSrc: imageSrc,
548
+ imageURL: imageURL,
549
+ imageBase64: imageBase64,
550
+ svg: svg,
551
+ html: html,
552
+ text: text,
553
+ fontSize: fontSize,
554
+ lineHeight: lineHeight,
555
+ textAlign: textAlign,
556
+ fontFamily: fontFamily,
557
+ fontWeight: fontWeight,
558
+ strokeWidth: strokeWidth,
559
+ };
560
+
561
+ function attrs(attrs) {
562
+ var x = attrs.x, y = attrs.y, w = attrs.w, h = attrs.h, angle = attrs.angle;
563
+ if (!(is.x(x) && is.y(y) && is.w(w) && is.h(h) && is.angle(angle))) {
564
+ return false;
565
+ }
566
+ if (!(angle >= -360 && angle <= 360)) {
567
+ return false;
568
+ }
569
+ return true;
570
+ }
571
+ function box(desc) {
572
+ if (desc === void 0) { desc = {}; }
573
+ var borderColor = desc.borderColor, borderRadius = desc.borderRadius, borderWidth = desc.borderWidth;
574
+ if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
575
+ return false;
576
+ }
577
+ if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
578
+ return false;
579
+ }
580
+ if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
581
+ return false;
582
+ }
583
+ return true;
584
+ }
585
+ function rectDesc(desc) {
586
+ var bgColor = desc.bgColor;
587
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
588
+ return false;
589
+ }
590
+ if (!box(desc)) {
591
+ return false;
592
+ }
593
+ return true;
594
+ }
595
+ function circleDesc(desc) {
596
+ var bgColor = desc.bgColor, borderColor = desc.borderColor, borderWidth = desc.borderWidth;
597
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
598
+ return false;
599
+ }
600
+ if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
601
+ return false;
602
+ }
603
+ if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
604
+ return false;
605
+ }
606
+ return true;
607
+ }
608
+ function imageDesc(desc) {
609
+ var src = desc.src;
610
+ if (!is.imageSrc(src)) {
611
+ return false;
612
+ }
613
+ return true;
614
+ }
615
+ function svgDesc(desc) {
616
+ var svg = desc.svg;
617
+ if (!is.svg(svg)) {
618
+ return false;
619
+ }
620
+ return true;
621
+ }
622
+ function htmlDesc(desc) {
623
+ var html = desc.html;
624
+ if (!is.html(html)) {
625
+ return false;
626
+ }
627
+ return true;
628
+ }
629
+ function textDesc(desc) {
630
+ 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;
631
+ if (!is.text(text)) {
632
+ return false;
633
+ }
634
+ if (!is.color(color)) {
635
+ return false;
636
+ }
637
+ if (!is.fontSize(fontSize)) {
638
+ return false;
639
+ }
640
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
641
+ return false;
642
+ }
643
+ if (desc.hasOwnProperty('fontWeight') && !is.fontWeight(fontWeight)) {
644
+ return false;
645
+ }
646
+ if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)) {
647
+ return false;
648
+ }
649
+ if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)) {
650
+ return false;
651
+ }
652
+ if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)) {
653
+ return false;
654
+ }
655
+ if (desc.hasOwnProperty('strokeWidth') && !is.strokeWidth(strokeWidth)) {
656
+ return false;
657
+ }
658
+ if (desc.hasOwnProperty('strokeColor') && !is.color(strokeColor)) {
659
+ return false;
660
+ }
661
+ if (!box(desc)) {
662
+ return false;
663
+ }
664
+ return true;
665
+ }
666
+ var check = {
667
+ attrs: attrs,
668
+ textDesc: textDesc,
669
+ rectDesc: rectDesc,
670
+ circleDesc: circleDesc,
671
+ imageDesc: imageDesc,
672
+ svgDesc: svgDesc,
673
+ htmlDesc: htmlDesc,
674
+ };
675
+
255
676
  var index = {
677
+ is: is,
678
+ check: check,
256
679
  time: {
257
680
  delay: delay,
258
681
  compose: compose,
@@ -277,7 +700,8 @@ var index = {
277
700
  istype: istype,
278
701
  data: {
279
702
  deepClone: deepClone,
280
- }
703
+ },
704
+ Context: Context,
281
705
  };
282
706
 
283
707
  export { index as default };