watermark-js-plus 0.9.11 → 0.9.13
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.cjs.js +244 -237
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.esm.js +244 -237
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.iife.js +244 -237
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.umd.js +244 -237
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/types/src/core/canvas.d.ts +42 -0
- package/dist/types/src/core/watermark.d.ts +1 -16
- package/package.json +10 -10
package/dist/index.umd.js
CHANGED
|
@@ -448,12 +448,13 @@
|
|
|
448
448
|
}
|
|
449
449
|
});
|
|
450
450
|
|
|
451
|
-
// import { WatermarkOptions } from '../types'
|
|
452
451
|
var WatermarkCanvas = /** @class */ (function () {
|
|
453
|
-
function WatermarkCanvas() {
|
|
452
|
+
function WatermarkCanvas(args, options) {
|
|
453
|
+
this.props = args;
|
|
454
|
+
this.options = options;
|
|
455
|
+
this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
|
|
456
|
+
this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
|
|
454
457
|
}
|
|
455
|
-
// constructor (args: Partial<WatermarkOptions> = {}) {
|
|
456
|
-
// }
|
|
457
458
|
/**
|
|
458
459
|
* Create an HD canvas.
|
|
459
460
|
* @param width - width of canvas
|
|
@@ -470,224 +471,25 @@
|
|
|
470
471
|
(_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
471
472
|
return canvas;
|
|
472
473
|
};
|
|
473
|
-
return WatermarkCanvas;
|
|
474
|
-
}());
|
|
475
|
-
|
|
476
|
-
var GridLayout = /** @class */ (function () {
|
|
477
|
-
function GridLayout(args, partialCanvas) {
|
|
478
|
-
var _a, _b, _c, _d;
|
|
479
|
-
this.options = args;
|
|
480
|
-
this.partialWidth = this.options.width;
|
|
481
|
-
this.partialHeight = this.options.height;
|
|
482
|
-
this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
|
|
483
|
-
this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
|
|
484
|
-
this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
|
|
485
|
-
this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
|
|
486
|
-
this.partialCanvas = partialCanvas;
|
|
487
|
-
}
|
|
488
|
-
GridLayout.prototype.draw = function () {
|
|
489
|
-
var _a, _b;
|
|
490
|
-
var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
|
|
491
|
-
var layoutContext = layoutCanvas.getContext('2d');
|
|
492
|
-
for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
493
|
-
for (var colIndex = 0; colIndex < this.cols; colIndex++) {
|
|
494
|
-
if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage(this.partialCanvas, this.partialWidth * colIndex + this.gap[0] * colIndex, this.partialHeight * rowIndex + this.gap[1] * rowIndex, this.partialWidth, this.partialHeight);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
return layoutCanvas;
|
|
501
|
-
};
|
|
502
|
-
return GridLayout;
|
|
503
|
-
}());
|
|
504
|
-
|
|
505
|
-
var renderLayout = function (options, partialCanvas) {
|
|
506
|
-
switch (options.layout) {
|
|
507
|
-
case 'grid':
|
|
508
|
-
return new GridLayout(options, partialCanvas).draw();
|
|
509
|
-
default:
|
|
510
|
-
return partialCanvas;
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
var generateBackgroundSize = function (options) {
|
|
514
|
-
var _a, _b, _c;
|
|
515
|
-
switch (options.layout) {
|
|
516
|
-
case 'grid':
|
|
517
|
-
{
|
|
518
|
-
var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
|
|
519
|
-
var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
|
|
520
|
-
var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
|
|
521
|
-
return [
|
|
522
|
-
options.width * cols + gap[0] * cols,
|
|
523
|
-
options.height * rows + gap[1] * rows
|
|
524
|
-
];
|
|
525
|
-
}
|
|
526
|
-
default:
|
|
527
|
-
return [
|
|
528
|
-
options.width,
|
|
529
|
-
options.height
|
|
530
|
-
];
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* Watermark class
|
|
536
|
-
*/
|
|
537
|
-
var Watermark = /** @class */ (function () {
|
|
538
|
-
/**
|
|
539
|
-
* Watermark constructor
|
|
540
|
-
* @param args - watermark args
|
|
541
|
-
*/
|
|
542
|
-
function Watermark(args) {
|
|
543
|
-
if (args === void 0) { args = {}; }
|
|
544
|
-
this.parentElement = document.body;
|
|
545
|
-
this.props = args;
|
|
546
|
-
this.options = Object.assign({}, initialOptions, args);
|
|
547
|
-
this.changeParentElement(this.options.parent);
|
|
548
|
-
this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
|
|
549
|
-
this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
|
|
550
|
-
bootstrap();
|
|
551
|
-
}
|
|
552
|
-
Watermark.prototype.changeOptions = function (args, mode) {
|
|
553
|
-
if (args === void 0) { args = {}; }
|
|
554
|
-
if (mode === void 0) { mode = 'overwrite'; }
|
|
555
|
-
this.initConfigData(args, mode);
|
|
556
|
-
};
|
|
557
474
|
/**
|
|
558
|
-
*
|
|
475
|
+
* Clean the canvas
|
|
476
|
+
* @param canvas
|
|
559
477
|
*/
|
|
560
|
-
|
|
561
|
-
var
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
return __generator(this, function (_e) {
|
|
565
|
-
switch (_e.label) {
|
|
566
|
-
case 0:
|
|
567
|
-
if (!this.validateUnique()) {
|
|
568
|
-
return [2 /*return*/];
|
|
569
|
-
}
|
|
570
|
-
if (!this.validateContent()) {
|
|
571
|
-
return [2 /*return*/];
|
|
572
|
-
}
|
|
573
|
-
firstDraw = isUndefined(this.watermarkDom);
|
|
574
|
-
return [4 /*yield*/, this.draw()];
|
|
575
|
-
case 1:
|
|
576
|
-
_e.sent();
|
|
577
|
-
this.layoutCanvas = renderLayout(this.options, this.canvas);
|
|
578
|
-
image = convertImage(this.layoutCanvas);
|
|
579
|
-
this.clearCanvas();
|
|
580
|
-
this.watermarkDom = document.createElement('div');
|
|
581
|
-
watermarkInnerDom = document.createElement('div');
|
|
582
|
-
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
583
|
-
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
584
|
-
parentElementType = this.checkParentElementType();
|
|
585
|
-
this.watermarkDom.style.cssText = "\n z-index: ".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
|
|
586
|
-
backgroundSize = generateBackgroundSize(this.options);
|
|
587
|
-
watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, "!important;\n ");
|
|
588
|
-
this.watermarkDom.append(watermarkInnerDom);
|
|
589
|
-
this.parentElement.appendChild(this.watermarkDom);
|
|
590
|
-
if (this.options.mutationObserve) {
|
|
591
|
-
try {
|
|
592
|
-
this.bindMutationObserve();
|
|
593
|
-
}
|
|
594
|
-
catch (e) {
|
|
595
|
-
(_b = (_a = this.options).onObserveError) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
firstDraw && ((_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c));
|
|
599
|
-
return [2 /*return*/];
|
|
600
|
-
}
|
|
601
|
-
});
|
|
602
|
-
});
|
|
603
|
-
};
|
|
604
|
-
/**
|
|
605
|
-
* Delete this watermark.
|
|
606
|
-
*/
|
|
607
|
-
Watermark.prototype.destroy = function () {
|
|
608
|
-
this.remove();
|
|
609
|
-
this.watermarkDom = undefined;
|
|
610
|
-
};
|
|
611
|
-
Watermark.prototype.check = function () {
|
|
612
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
613
|
-
return __generator(this, function (_a) {
|
|
614
|
-
switch (_a.label) {
|
|
615
|
-
case 0:
|
|
616
|
-
if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
|
|
617
|
-
this.remove();
|
|
618
|
-
return [4 /*yield*/, this.create()];
|
|
619
|
-
case 1:
|
|
620
|
-
_a.sent();
|
|
621
|
-
_a.label = 2;
|
|
622
|
-
case 2: return [2 /*return*/];
|
|
623
|
-
}
|
|
624
|
-
});
|
|
625
|
-
});
|
|
626
|
-
};
|
|
627
|
-
Watermark.prototype.remove = function () {
|
|
628
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
629
|
-
(_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
630
|
-
(_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
631
|
-
(_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
|
|
632
|
-
if (!isUndefined(this.checkWatermarkElementRequestID)) {
|
|
633
|
-
cancelAnimationFrame(this.checkWatermarkElementRequestID);
|
|
634
|
-
}
|
|
635
|
-
(_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
|
|
636
|
-
(_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
|
|
637
|
-
};
|
|
638
|
-
Watermark.prototype.initConfigData = function (args, mode) {
|
|
639
|
-
var _this = this;
|
|
640
|
-
if (mode === void 0) { mode = 'overwrite'; }
|
|
641
|
-
this.props = args;
|
|
642
|
-
if (mode === 'overwrite') {
|
|
643
|
-
this.options = Object.assign({}, initialOptions, args);
|
|
644
|
-
}
|
|
645
|
-
else {
|
|
646
|
-
Object.keys(args).forEach(function (key) {
|
|
647
|
-
_this.options[key] = args[key];
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
this.changeParentElement(this.options.parent);
|
|
651
|
-
this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
|
|
652
|
-
this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
|
|
653
|
-
// document.body?.appendChild(this.canvas)
|
|
654
|
-
};
|
|
655
|
-
Watermark.prototype.changeParentElement = function (parent) {
|
|
656
|
-
if (typeof parent === 'string') {
|
|
657
|
-
var parentElement = document.querySelector(parent);
|
|
658
|
-
parentElement && (this.parentElement = parentElement);
|
|
659
|
-
}
|
|
660
|
-
else {
|
|
661
|
-
this.parentElement = parent;
|
|
478
|
+
WatermarkCanvas.clearCanvas = function (canvas) {
|
|
479
|
+
var ctx = canvas.getContext('2d');
|
|
480
|
+
if (ctx === null) {
|
|
481
|
+
throw new Error('get context error');
|
|
662
482
|
}
|
|
483
|
+
ctx.restore();
|
|
484
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
663
485
|
};
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
if (this.options.unique) {
|
|
667
|
-
this.parentElement.childNodes.forEach(function (node) {
|
|
668
|
-
if (!result) {
|
|
669
|
-
return;
|
|
670
|
-
}
|
|
671
|
-
if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
|
|
672
|
-
result = false;
|
|
673
|
-
// throw new Error('duplicate watermark error')
|
|
674
|
-
}
|
|
675
|
-
});
|
|
676
|
-
}
|
|
677
|
-
return result;
|
|
486
|
+
WatermarkCanvas.prototype.getCanvas = function () {
|
|
487
|
+
return this.canvas;
|
|
678
488
|
};
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
case 'image':
|
|
682
|
-
return Object.hasOwnProperty.call(this.options, 'image');
|
|
683
|
-
case 'multi-line-text':
|
|
684
|
-
case 'rich-text':
|
|
685
|
-
case 'text':
|
|
686
|
-
return this.options.content.length > 0;
|
|
687
|
-
}
|
|
688
|
-
return false;
|
|
489
|
+
WatermarkCanvas.prototype.clear = function () {
|
|
490
|
+
WatermarkCanvas.clearCanvas(this.canvas);
|
|
689
491
|
};
|
|
690
|
-
|
|
492
|
+
WatermarkCanvas.prototype.draw = function () {
|
|
691
493
|
var _this = this;
|
|
692
494
|
var ctx = this.canvas.getContext('2d');
|
|
693
495
|
if (ctx === null) {
|
|
@@ -728,7 +530,7 @@
|
|
|
728
530
|
}
|
|
729
531
|
});
|
|
730
532
|
};
|
|
731
|
-
|
|
533
|
+
WatermarkCanvas.prototype.setStyle = function (ctx) {
|
|
732
534
|
var _a;
|
|
733
535
|
var propName = 'fillStyle';
|
|
734
536
|
if (this.options.textType === 'stroke') {
|
|
@@ -765,7 +567,7 @@
|
|
|
765
567
|
this.options.extraDrawFunc(ctx);
|
|
766
568
|
}
|
|
767
569
|
};
|
|
768
|
-
|
|
570
|
+
WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
|
|
769
571
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
770
572
|
var gradient = ctx.createLinearGradient(getValue((_c = (_b = (_a = this.options.advancedStyle) === null || _a === void 0 ? void 0 : _a.params) === null || _b === void 0 ? void 0 : _b.linear) === null || _c === void 0 ? void 0 : _c.x0, this.recommendOptions.advancedStyleParams.linear.x0), getValue((_f = (_e = (_d = this.options.advancedStyle) === null || _d === void 0 ? void 0 : _d.params) === null || _e === void 0 ? void 0 : _e.linear) === null || _f === void 0 ? void 0 : _f.y0, 0), getValue((_j = (_h = (_g = this.options.advancedStyle) === null || _g === void 0 ? void 0 : _g.params) === null || _h === void 0 ? void 0 : _h.linear) === null || _j === void 0 ? void 0 : _j.x1, this.recommendOptions.advancedStyleParams.linear.x1), getValue((_m = (_l = (_k = this.options.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.linear) === null || _m === void 0 ? void 0 : _m.y1, 0));
|
|
771
573
|
(_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
|
|
@@ -773,7 +575,7 @@
|
|
|
773
575
|
});
|
|
774
576
|
return gradient;
|
|
775
577
|
};
|
|
776
|
-
|
|
578
|
+
WatermarkCanvas.prototype.createConicGradient = function (ctx) {
|
|
777
579
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
778
580
|
var gradient = ctx.createConicGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.conic) === null || _d === void 0 ? void 0 : _d.startAngle, 0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.conic) === null || _h === void 0 ? void 0 : _h.x, this.recommendOptions.advancedStyleParams.conic.x), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.conic) === null || _m === void 0 ? void 0 : _m.y, this.recommendOptions.advancedStyleParams.conic.y));
|
|
779
581
|
(_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
|
|
@@ -781,7 +583,7 @@
|
|
|
781
583
|
});
|
|
782
584
|
return gradient;
|
|
783
585
|
};
|
|
784
|
-
|
|
586
|
+
WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
|
|
785
587
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
|
|
786
588
|
var gradient = ctx.createRadialGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.radial) === null || _d === void 0 ? void 0 : _d.x0, this.recommendOptions.advancedStyleParams.radial.x0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.radial) === null || _h === void 0 ? void 0 : _h.y0, this.recommendOptions.advancedStyleParams.radial.y0), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.radial) === null || _m === void 0 ? void 0 : _m.r0, this.recommendOptions.advancedStyleParams.radial.r0), getValue((_r = (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.params) === null || _q === void 0 ? void 0 : _q.radial) === null || _r === void 0 ? void 0 : _r.x1, this.recommendOptions.advancedStyleParams.radial.x1), getValue((_v = (_u = (_t = (_s = this.options) === null || _s === void 0 ? void 0 : _s.advancedStyle) === null || _t === void 0 ? void 0 : _t.params) === null || _u === void 0 ? void 0 : _u.radial) === null || _v === void 0 ? void 0 : _v.y1, this.recommendOptions.advancedStyleParams.radial.y1), getValue((_z = (_y = (_x = (_w = this.options) === null || _w === void 0 ? void 0 : _w.advancedStyle) === null || _x === void 0 ? void 0 : _x.params) === null || _y === void 0 ? void 0 : _y.radial) === null || _z === void 0 ? void 0 : _z.r1, this.recommendOptions.advancedStyleParams.radial.r1));
|
|
787
589
|
(_2 = (_1 = (_0 = this.options) === null || _0 === void 0 ? void 0 : _0.advancedStyle) === null || _1 === void 0 ? void 0 : _1.colorStops) === null || _2 === void 0 ? void 0 : _2.forEach(function (item) {
|
|
@@ -789,18 +591,18 @@
|
|
|
789
591
|
});
|
|
790
592
|
return gradient;
|
|
791
593
|
};
|
|
792
|
-
|
|
594
|
+
WatermarkCanvas.prototype.createPattern = function (ctx) {
|
|
793
595
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
794
596
|
return ctx.createPattern((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.pattern) === null || _d === void 0 ? void 0 : _d.image, ((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.pattern) === null || _h === void 0 ? void 0 : _h.repetition) || '');
|
|
795
597
|
};
|
|
796
|
-
|
|
598
|
+
WatermarkCanvas.prototype.setText = function (ctx, params) {
|
|
797
599
|
var methodName = 'fillText';
|
|
798
600
|
if (this.options.textType === 'stroke') {
|
|
799
601
|
methodName = 'strokeText';
|
|
800
602
|
}
|
|
801
603
|
ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
|
|
802
604
|
};
|
|
803
|
-
|
|
605
|
+
WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
|
|
804
606
|
this.setText(ctx, {
|
|
805
607
|
text: this.options.content,
|
|
806
608
|
x: 0,
|
|
@@ -809,7 +611,7 @@
|
|
|
809
611
|
});
|
|
810
612
|
resolve(ctx.canvas);
|
|
811
613
|
};
|
|
812
|
-
|
|
614
|
+
WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
|
|
813
615
|
var _this = this;
|
|
814
616
|
loadImage(this.options.image).then(function (image) {
|
|
815
617
|
var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
|
|
@@ -818,7 +620,7 @@
|
|
|
818
620
|
resolve(ctx.canvas);
|
|
819
621
|
});
|
|
820
622
|
};
|
|
821
|
-
|
|
623
|
+
WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
|
|
822
624
|
var _this = this;
|
|
823
625
|
// image.width = this.options.width
|
|
824
626
|
// image.height = this.options.height
|
|
@@ -843,7 +645,7 @@
|
|
|
843
645
|
});
|
|
844
646
|
resolve(ctx.canvas);
|
|
845
647
|
};
|
|
846
|
-
|
|
648
|
+
WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
|
|
847
649
|
var _this = this;
|
|
848
650
|
var obj = createCustomContentSVG(ctx, this.options);
|
|
849
651
|
loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
|
|
@@ -852,7 +654,7 @@
|
|
|
852
654
|
resolve(ctx.canvas);
|
|
853
655
|
});
|
|
854
656
|
};
|
|
855
|
-
|
|
657
|
+
WatermarkCanvas.prototype.getImageRect = function (image) {
|
|
856
658
|
var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
|
|
857
659
|
switch (true) {
|
|
858
660
|
case rect.width !== 0 && rect.height === 0:
|
|
@@ -868,7 +670,7 @@
|
|
|
868
670
|
}
|
|
869
671
|
return rect;
|
|
870
672
|
};
|
|
871
|
-
|
|
673
|
+
WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
|
|
872
674
|
var _a, _b;
|
|
873
675
|
var result = {
|
|
874
676
|
x: -imageWidth / 2,
|
|
@@ -912,6 +714,220 @@
|
|
|
912
714
|
!isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
|
|
913
715
|
return result;
|
|
914
716
|
};
|
|
717
|
+
return WatermarkCanvas;
|
|
718
|
+
}());
|
|
719
|
+
|
|
720
|
+
var GridLayout = /** @class */ (function () {
|
|
721
|
+
function GridLayout(args, partialCanvas) {
|
|
722
|
+
var _a, _b, _c, _d;
|
|
723
|
+
this.options = args;
|
|
724
|
+
this.partialWidth = this.options.width;
|
|
725
|
+
this.partialHeight = this.options.height;
|
|
726
|
+
this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
|
|
727
|
+
this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
|
|
728
|
+
this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
|
|
729
|
+
this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
|
|
730
|
+
this.partialCanvas = partialCanvas;
|
|
731
|
+
}
|
|
732
|
+
GridLayout.prototype.draw = function () {
|
|
733
|
+
var _a, _b;
|
|
734
|
+
var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
|
|
735
|
+
var layoutContext = layoutCanvas.getContext('2d');
|
|
736
|
+
for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
737
|
+
for (var colIndex = 0; colIndex < this.cols; colIndex++) {
|
|
738
|
+
if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage(this.partialCanvas, this.partialWidth * colIndex + this.gap[0] * colIndex, this.partialHeight * rowIndex + this.gap[1] * rowIndex, this.partialWidth, this.partialHeight);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
return layoutCanvas;
|
|
745
|
+
};
|
|
746
|
+
return GridLayout;
|
|
747
|
+
}());
|
|
748
|
+
|
|
749
|
+
var renderLayout = function (options, partialCanvas) {
|
|
750
|
+
switch (options.layout) {
|
|
751
|
+
case 'grid':
|
|
752
|
+
return new GridLayout(options, partialCanvas).draw();
|
|
753
|
+
default:
|
|
754
|
+
return partialCanvas;
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
var generateBackgroundSize = function (options) {
|
|
758
|
+
var _a, _b, _c;
|
|
759
|
+
switch (options.layout) {
|
|
760
|
+
case 'grid':
|
|
761
|
+
{
|
|
762
|
+
var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
|
|
763
|
+
var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
|
|
764
|
+
var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
|
|
765
|
+
return [
|
|
766
|
+
options.width * cols + gap[0] * cols,
|
|
767
|
+
options.height * rows + gap[1] * rows
|
|
768
|
+
];
|
|
769
|
+
}
|
|
770
|
+
default:
|
|
771
|
+
return [
|
|
772
|
+
options.width,
|
|
773
|
+
options.height
|
|
774
|
+
];
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Watermark class
|
|
780
|
+
*/
|
|
781
|
+
var Watermark = /** @class */ (function () {
|
|
782
|
+
/**
|
|
783
|
+
* Watermark constructor
|
|
784
|
+
* @param args - watermark args
|
|
785
|
+
*/
|
|
786
|
+
function Watermark(args) {
|
|
787
|
+
if (args === void 0) { args = {}; }
|
|
788
|
+
this.parentElement = document.body;
|
|
789
|
+
this.props = args;
|
|
790
|
+
this.options = Object.assign({}, initialOptions, args);
|
|
791
|
+
this.changeParentElement(this.options.parent);
|
|
792
|
+
this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
|
|
793
|
+
bootstrap();
|
|
794
|
+
}
|
|
795
|
+
Watermark.prototype.changeOptions = function (args, mode) {
|
|
796
|
+
if (args === void 0) { args = {}; }
|
|
797
|
+
if (mode === void 0) { mode = 'overwrite'; }
|
|
798
|
+
this.initConfigData(args, mode);
|
|
799
|
+
};
|
|
800
|
+
/**
|
|
801
|
+
* Creating a watermark.
|
|
802
|
+
*/
|
|
803
|
+
Watermark.prototype.create = function () {
|
|
804
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
805
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
806
|
+
var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
|
|
807
|
+
return __generator(this, function (_h) {
|
|
808
|
+
switch (_h.label) {
|
|
809
|
+
case 0:
|
|
810
|
+
if (!this.validateUnique()) {
|
|
811
|
+
return [2 /*return*/];
|
|
812
|
+
}
|
|
813
|
+
if (!this.validateContent()) {
|
|
814
|
+
return [2 /*return*/];
|
|
815
|
+
}
|
|
816
|
+
firstDraw = isUndefined(this.watermarkDom);
|
|
817
|
+
return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
|
|
818
|
+
case 1:
|
|
819
|
+
_h.sent();
|
|
820
|
+
this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
|
|
821
|
+
image = convertImage(this.layoutCanvas);
|
|
822
|
+
(_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
|
|
823
|
+
this.watermarkDom = document.createElement('div');
|
|
824
|
+
watermarkInnerDom = document.createElement('div');
|
|
825
|
+
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
826
|
+
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
827
|
+
parentElementType = this.checkParentElementType();
|
|
828
|
+
this.watermarkDom.style.cssText = "\n z-index: ".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
|
|
829
|
+
backgroundSize = generateBackgroundSize(this.options);
|
|
830
|
+
watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, "!important;\n ");
|
|
831
|
+
this.watermarkDom.append(watermarkInnerDom);
|
|
832
|
+
this.parentElement.appendChild(this.watermarkDom);
|
|
833
|
+
if (this.options.mutationObserve) {
|
|
834
|
+
try {
|
|
835
|
+
this.bindMutationObserve();
|
|
836
|
+
}
|
|
837
|
+
catch (e) {
|
|
838
|
+
(_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
|
|
842
|
+
return [2 /*return*/];
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
});
|
|
846
|
+
};
|
|
847
|
+
/**
|
|
848
|
+
* Delete this watermark.
|
|
849
|
+
*/
|
|
850
|
+
Watermark.prototype.destroy = function () {
|
|
851
|
+
this.remove();
|
|
852
|
+
this.watermarkDom = undefined;
|
|
853
|
+
};
|
|
854
|
+
Watermark.prototype.check = function () {
|
|
855
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
856
|
+
return __generator(this, function (_a) {
|
|
857
|
+
switch (_a.label) {
|
|
858
|
+
case 0:
|
|
859
|
+
if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
|
|
860
|
+
this.remove();
|
|
861
|
+
return [4 /*yield*/, this.create()];
|
|
862
|
+
case 1:
|
|
863
|
+
_a.sent();
|
|
864
|
+
_a.label = 2;
|
|
865
|
+
case 2: return [2 /*return*/];
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
});
|
|
869
|
+
};
|
|
870
|
+
Watermark.prototype.remove = function () {
|
|
871
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
872
|
+
(_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
873
|
+
(_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
874
|
+
(_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
|
|
875
|
+
if (!isUndefined(this.checkWatermarkElementRequestID)) {
|
|
876
|
+
cancelAnimationFrame(this.checkWatermarkElementRequestID);
|
|
877
|
+
}
|
|
878
|
+
(_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
|
|
879
|
+
(_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
|
|
880
|
+
};
|
|
881
|
+
Watermark.prototype.initConfigData = function (args, mode) {
|
|
882
|
+
var _this = this;
|
|
883
|
+
if (mode === void 0) { mode = 'overwrite'; }
|
|
884
|
+
this.props = args;
|
|
885
|
+
if (mode === 'overwrite') {
|
|
886
|
+
this.options = Object.assign({}, initialOptions, args);
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
Object.keys(args).forEach(function (key) {
|
|
890
|
+
_this.options[key] = args[key];
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
this.changeParentElement(this.options.parent);
|
|
894
|
+
this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
|
|
895
|
+
};
|
|
896
|
+
Watermark.prototype.changeParentElement = function (parent) {
|
|
897
|
+
if (typeof parent === 'string') {
|
|
898
|
+
var parentElement = document.querySelector(parent);
|
|
899
|
+
parentElement && (this.parentElement = parentElement);
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
this.parentElement = parent;
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
Watermark.prototype.validateUnique = function () {
|
|
906
|
+
var result = true;
|
|
907
|
+
if (this.options.unique) {
|
|
908
|
+
this.parentElement.childNodes.forEach(function (node) {
|
|
909
|
+
if (!result) {
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
|
|
913
|
+
result = false;
|
|
914
|
+
// throw new Error('duplicate watermark error')
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
return result;
|
|
919
|
+
};
|
|
920
|
+
Watermark.prototype.validateContent = function () {
|
|
921
|
+
switch (this.options.contentType) {
|
|
922
|
+
case 'image':
|
|
923
|
+
return Object.hasOwnProperty.call(this.options, 'image');
|
|
924
|
+
case 'multi-line-text':
|
|
925
|
+
case 'rich-text':
|
|
926
|
+
case 'text':
|
|
927
|
+
return this.options.content.length > 0;
|
|
928
|
+
}
|
|
929
|
+
return false;
|
|
930
|
+
};
|
|
915
931
|
Watermark.prototype.checkParentElementType = function () {
|
|
916
932
|
if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
|
|
917
933
|
return 'root';
|
|
@@ -995,15 +1011,6 @@
|
|
|
995
1011
|
characterData: true // 节点内容或节点文本的变动。
|
|
996
1012
|
});
|
|
997
1013
|
};
|
|
998
|
-
Watermark.prototype.clearCanvas = function () {
|
|
999
|
-
var ctx = this.canvas.getContext('2d');
|
|
1000
|
-
if (ctx === null) {
|
|
1001
|
-
throw new Error('get context error');
|
|
1002
|
-
}
|
|
1003
|
-
ctx.restore();
|
|
1004
|
-
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
1005
|
-
// this.canvas.width = this.options.w
|
|
1006
|
-
};
|
|
1007
1014
|
return Watermark;
|
|
1008
1015
|
}());
|
|
1009
1016
|
|