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