watermark-js-plus 0.8.23 → 0.9.2

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.
@@ -0,0 +1,941 @@
1
+ 'use strict';
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise */
18
+
19
+ var extendStatics = function(d, b) {
20
+ extendStatics = Object.setPrototypeOf ||
21
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
23
+ return extendStatics(d, b);
24
+ };
25
+
26
+ function __extends(d, b) {
27
+ if (typeof b !== "function" && b !== null)
28
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
29
+ extendStatics(d, b);
30
+ function __() { this.constructor = d; }
31
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
32
+ }
33
+
34
+ function __awaiter(thisArg, _arguments, P, generator) {
35
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
36
+ return new (P || (P = Promise))(function (resolve, reject) {
37
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
38
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
39
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
40
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
41
+ });
42
+ }
43
+
44
+ function __generator(thisArg, body) {
45
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
46
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
+ function verb(n) { return function (v) { return step([n, v]); }; }
48
+ function step(op) {
49
+ if (f) throw new TypeError("Generator is already executing.");
50
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
+ 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;
52
+ if (y = 0, t) op = [op[0] & 2, t.value];
53
+ switch (op[0]) {
54
+ case 0: case 1: t = op; break;
55
+ case 4: _.label++; return { value: op[1], done: false };
56
+ case 5: _.label++; y = op[1]; op = [0]; continue;
57
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
+ default:
59
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
+ if (t[2]) _.ops.pop();
64
+ _.trys.pop(); continue;
65
+ }
66
+ op = body.call(thisArg, _);
67
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
+ }
70
+ }
71
+
72
+ var convertImage = function (canvas) {
73
+ return canvas.toDataURL('image/png', 1);
74
+ };
75
+ var isFunction = function (value) {
76
+ return typeof value === 'function';
77
+ };
78
+ var isUndefined = function (value) {
79
+ return value === undefined;
80
+ };
81
+ var createSVGElement = function (tagName, attrs, namespaceURI) {
82
+ if (attrs === void 0) { attrs = {}; }
83
+ if (namespaceURI === void 0) { namespaceURI = 'http://www.w3.org/2000/svg'; }
84
+ var element = document.createElementNS(namespaceURI, tagName);
85
+ for (var attr in attrs) {
86
+ element.setAttribute(attr, attrs[attr]);
87
+ }
88
+ return element;
89
+ };
90
+ var getMultiLineData = function (ctx, text, maxWidth) {
91
+ var result = [];
92
+ var str = '';
93
+ for (var i = 0, len = text.length; i < len; i++) {
94
+ str += text.charAt(i);
95
+ if (ctx.measureText(str).width > maxWidth) {
96
+ result.push(str.substring(0, str.length - 1));
97
+ str = '';
98
+ i--;
99
+ }
100
+ }
101
+ result.push(str);
102
+ return result;
103
+ };
104
+ var createCustomContentSVG = function (ctx, options) {
105
+ var svgElement = createSVGElement('svg', {
106
+ xmlns: 'http://www.w3.org/2000/svg'
107
+ });
108
+ var bodyElement = document.createElement('div');
109
+ bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
110
+ bodyElement.style.cssText = "\n text-align: center;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 100%;\n height: 100%;\n font: ".concat(ctx.font, ";\n color: ").concat(options.fontColor, ";\n");
111
+ bodyElement.innerHTML = "<div class=\"rich-text-content\">".concat(options.content, "</div>");
112
+ document.body.appendChild(bodyElement);
113
+ var _a = bodyElement.querySelector('.rich-text-content'), offsetHeight = _a.offsetHeight, offsetWidth = _a.offsetWidth;
114
+ document.body.removeChild(bodyElement);
115
+ var width = options.richTextWidth || offsetWidth || options.width;
116
+ var height = options.richTextHeight || offsetHeight || options.height;
117
+ var foreignObjectElement = createSVGElement('foreignObject', {
118
+ width: width.toString(),
119
+ height: height.toString()
120
+ });
121
+ foreignObjectElement.appendChild(bodyElement);
122
+ svgElement.appendChild(foreignObjectElement);
123
+ return {
124
+ element: svgElement,
125
+ width: width,
126
+ height: height
127
+ };
128
+ };
129
+ var convertSVGToImage = function (svg) {
130
+ var richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
131
+ return "data:image/svg+xml;charset=utf-8,".concat(richContent);
132
+ };
133
+ var getValue = function (v1, v2) {
134
+ if (isUndefined(v1)) {
135
+ return v2;
136
+ }
137
+ else {
138
+ return v1;
139
+ }
140
+ };
141
+ var loadImage = function (url, width, height) {
142
+ if (width === void 0) { width = undefined; }
143
+ if (height === void 0) { height = undefined; }
144
+ var image = new Image();
145
+ image.setAttribute('crossOrigin', 'Anonymous');
146
+ !isUndefined(width) && (image.width = width);
147
+ !isUndefined(height) && (image.height = height);
148
+ image.src = url;
149
+ return new Promise(function (resolve) {
150
+ image.onload = function () {
151
+ resolve(image);
152
+ };
153
+ });
154
+ };
155
+
156
+ var initialOptions = {
157
+ width: 300,
158
+ height: 300,
159
+ rotate: 45,
160
+ auxiliaryLine: false,
161
+ translatePlacement: 'middle',
162
+ contentType: 'text',
163
+ content: 'hello watermark-js-plus',
164
+ textType: 'fill',
165
+ imageWidth: 0,
166
+ imageHeight: 0,
167
+ lineHeight: 30,
168
+ zIndex: 2147483647,
169
+ backgroundPosition: '0 0, 0 0',
170
+ backgroundRepeat: 'repeat',
171
+ fontSize: '20px',
172
+ fontFamily: 'sans-serif',
173
+ fontStyle: '',
174
+ fontVariant: '',
175
+ fontColor: '#000',
176
+ fontWeight: 'normal',
177
+ filter: 'none',
178
+ globalAlpha: 0.5,
179
+ mode: 'default',
180
+ mutationObserve: true,
181
+ unique: true,
182
+ parent: 'body',
183
+ onSuccess: function () { },
184
+ onBeforeDestroy: function () { },
185
+ onDestroyed: function () { },
186
+ onObserveError: function () { }
187
+ };
188
+ var generateRecommendOptions = function (canvas, options, args) {
189
+ var ctx = canvas.getContext('2d');
190
+ if (ctx === null) {
191
+ throw new Error('get context error');
192
+ }
193
+ ctx.font = "".concat(options.fontStyle, " ").concat(options.fontVariant, " ").concat(options.fontWeight, " ").concat(options.fontSize, " ").concat(options.fontFamily);
194
+ ctx.filter = options.filter;
195
+ if (options === null || options === void 0 ? void 0 : options.rotate) {
196
+ options.rotate = (360 - options.rotate % 360) * (Math.PI / 180);
197
+ }
198
+ if (isUndefined(args.textRowMaxWidth)) {
199
+ options.textRowMaxWidth = options.width;
200
+ }
201
+ var result = {
202
+ image: {
203
+ rect: {
204
+ width: options.imageWidth,
205
+ height: options.imageHeight
206
+ },
207
+ position: {
208
+ x: 0,
209
+ y: 0
210
+ }
211
+ },
212
+ textLine: {
213
+ data: [],
214
+ yOffsetValue: 0
215
+ },
216
+ advancedStyleParams: {
217
+ linear: {
218
+ x0: 0,
219
+ x1: 0
220
+ },
221
+ radial: {
222
+ x0: 0,
223
+ y0: 0,
224
+ r0: 0,
225
+ x1: 0,
226
+ y1: 0,
227
+ r1: 0
228
+ },
229
+ conic: {
230
+ x: 0,
231
+ y: 0,
232
+ startAngle: 0
233
+ },
234
+ pattern: {}
235
+ }
236
+ };
237
+ switch (options.contentType) {
238
+ case 'text':
239
+ result.textLine.data = [options.content];
240
+ break;
241
+ // case 'image':
242
+ // break
243
+ case 'multi-line-text':
244
+ result.textLine.data = getMultiLineData(ctx, options.content, options.textRowMaxWidth);
245
+ break;
246
+ // case 'rich-text':
247
+ // break
248
+ }
249
+ var translateX = options.width / 2;
250
+ var translateY = options.height / 2;
251
+ var textBaseline = 'middle';
252
+ var textAlign = 'center';
253
+ if (!isUndefined(args === null || args === void 0 ? void 0 : args.translateX) && !isUndefined(args === null || args === void 0 ? void 0 : args.translateY)) {
254
+ translateX = args === null || args === void 0 ? void 0 : args.translateX;
255
+ translateY = args === null || args === void 0 ? void 0 : args.translateY;
256
+ textBaseline = 'top';
257
+ textAlign = 'left';
258
+ }
259
+ else {
260
+ // default middle
261
+ // translateX = options.width / 2
262
+ // translateY = options.height / 2
263
+ // TextBaselineType = 'middle'
264
+ // textAlign = 'center'
265
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
266
+ result.advancedStyleParams.linear.x1 = options.width / 2;
267
+ // result.advancedStyleParams.radial.x0 = 0
268
+ // result.advancedStyleParams.radial.y0 = 0
269
+ result.advancedStyleParams.radial.r0 = 0;
270
+ // result.advancedStyleParams.radial.x1 = 0
271
+ // result.advancedStyleParams.radial.y1 = 0
272
+ result.advancedStyleParams.radial.r1 = options.width / 2;
273
+ // result.advancedStyleParams.conic.x = 0
274
+ // result.advancedStyleParams.conic.y = 0
275
+ }
276
+ switch (args.translatePlacement) {
277
+ case 'top':
278
+ translateX = options.width / 2;
279
+ translateY = 0;
280
+ textBaseline = 'top';
281
+ // textAlign = 'center'
282
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
283
+ result.advancedStyleParams.linear.x1 = options.width / 2;
284
+ // result.advancedStyleParams.radial.x0 = 0
285
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
286
+ // result.advancedStyleParams.radial.r0 = 0
287
+ // result.advancedStyleParams.radial.x1 = 0
288
+ // result.advancedStyleParams.radial.y1 = 0
289
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
290
+ // result.advancedStyleParams.radial.r1 = options.width / 2
291
+ // result.advancedStyleParams.conic.x = 0
292
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
293
+ break;
294
+ case 'top-start':
295
+ translateX = 0;
296
+ translateY = 0;
297
+ textBaseline = 'top';
298
+ textAlign = 'start';
299
+ result.advancedStyleParams.linear.x0 = 0;
300
+ result.advancedStyleParams.linear.x1 = options.width;
301
+ result.advancedStyleParams.radial.x0 = options.width / 2;
302
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
303
+ // result.advancedStyleParams.radial.r0 = 0
304
+ result.advancedStyleParams.radial.x1 = options.width / 2;
305
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
306
+ // result.advancedStyleParams.radial.r1 = options.width / 2
307
+ result.advancedStyleParams.conic.x = options.width / 2;
308
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
309
+ break;
310
+ case 'top-end':
311
+ translateX = options.width;
312
+ translateY = 0;
313
+ textBaseline = 'top';
314
+ textAlign = 'end';
315
+ result.advancedStyleParams.linear.x0 = 0;
316
+ result.advancedStyleParams.linear.x1 = -options.width;
317
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
318
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
319
+ // result.advancedStyleParams.radial.r0 = 0
320
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
321
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
322
+ // result.advancedStyleParams.radial.r1 = options.width / 2
323
+ result.advancedStyleParams.conic.x = -options.width / 2;
324
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
325
+ break;
326
+ case 'bottom':
327
+ translateX = options.width / 2;
328
+ translateY = options.height;
329
+ textBaseline = 'bottom';
330
+ // textAlign = 'center'
331
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
332
+ result.advancedStyleParams.linear.x1 = options.width / 2;
333
+ // result.advancedStyleParams.radial.x0 = 0
334
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
335
+ // result.advancedStyleParams.radial.r0 = 0
336
+ // result.advancedStyleParams.radial.x1 = 0
337
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
338
+ // result.advancedStyleParams.radial.r1 = options.width / 2
339
+ result.advancedStyleParams.conic.x = 0;
340
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
341
+ break;
342
+ case 'bottom-start':
343
+ translateX = 0;
344
+ translateY = options.height;
345
+ textBaseline = 'bottom';
346
+ textAlign = 'start';
347
+ result.advancedStyleParams.linear.x0 = 0;
348
+ result.advancedStyleParams.linear.x1 = options.width;
349
+ result.advancedStyleParams.radial.x0 = options.width / 2;
350
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
351
+ // result.advancedStyleParams.radial.r0 = 0
352
+ result.advancedStyleParams.radial.x1 = options.width / 2;
353
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
354
+ // result.advancedStyleParams.radial.r1 = options.width / 2
355
+ result.advancedStyleParams.conic.x = options.width / 2;
356
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
357
+ break;
358
+ case 'bottom-end':
359
+ translateX = options.width;
360
+ translateY = options.height;
361
+ textBaseline = 'bottom';
362
+ textAlign = 'end';
363
+ result.advancedStyleParams.linear.x0 = 0;
364
+ result.advancedStyleParams.linear.x1 = -options.width;
365
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
366
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
367
+ // result.advancedStyleParams.radial.r0 = 0
368
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
369
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
370
+ // result.advancedStyleParams.radial.r1 = options.width / 2
371
+ result.advancedStyleParams.conic.x = -options.width / 2;
372
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
373
+ break;
374
+ case 'left':
375
+ translateX = 0;
376
+ translateY = options.height / 2;
377
+ // TextBaselineType = 'middle'
378
+ textAlign = 'start';
379
+ result.advancedStyleParams.linear.x0 = 0;
380
+ result.advancedStyleParams.linear.x1 = options.width;
381
+ result.advancedStyleParams.radial.x0 = options.width / 2;
382
+ // result.advancedStyleParams.radial.y0 = 0
383
+ // result.advancedStyleParams.radial.r0 = 0
384
+ result.advancedStyleParams.radial.x1 = options.width / 2;
385
+ // result.advancedStyleParams.radial.y1 = 0
386
+ // result.advancedStyleParams.radial.r1 = options.width / 2
387
+ result.advancedStyleParams.conic.x = options.width / 2;
388
+ result.advancedStyleParams.conic.y = 0;
389
+ break;
390
+ case 'right':
391
+ translateX = options.width;
392
+ translateY = options.height / 2;
393
+ // TextBaselineType = 'middle'
394
+ textAlign = 'end';
395
+ result.advancedStyleParams.linear.x0 = 0;
396
+ result.advancedStyleParams.linear.x1 = -options.width;
397
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
398
+ // result.advancedStyleParams.radial.y0 = 0
399
+ // result.advancedStyleParams.radial.r0 = 0
400
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
401
+ // result.advancedStyleParams.radial.y1 = 0
402
+ // result.advancedStyleParams.radial.r1 = options.width / 2
403
+ result.advancedStyleParams.conic.x = -options.width / 2;
404
+ result.advancedStyleParams.conic.y = 0;
405
+ break;
406
+ }
407
+ options.translateX = translateX;
408
+ options.translateY = translateY;
409
+ isUndefined(args === null || args === void 0 ? void 0 : args.textBaseline) && (options.textBaseline = textBaseline);
410
+ isUndefined(args === null || args === void 0 ? void 0 : args.textAlign) && (options.textAlign = textAlign);
411
+ if (options.contentType === 'multi-line-text') {
412
+ switch (options.textBaseline) {
413
+ case 'middle':
414
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight / 2;
415
+ break;
416
+ case 'bottom':
417
+ case 'alphabetic':
418
+ case 'ideographic':
419
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight;
420
+ break;
421
+ case 'top':
422
+ case 'hanging':
423
+ result.textLine.yOffsetValue = 0;
424
+ break;
425
+ }
426
+ }
427
+ return result;
428
+ };
429
+
430
+ var bootstrap = (function () {
431
+ if (typeof window !== 'undefined') {
432
+ Object.defineProperty(window, 'MutationObserver', {
433
+ writable: false,
434
+ configurable: false
435
+ });
436
+ Object.defineProperty(window, 'requestAnimationFrame', {
437
+ writable: false,
438
+ configurable: false
439
+ });
440
+ }
441
+ });
442
+
443
+ /**
444
+ * Watermark class
445
+ */
446
+ var Watermark = /** @class */ (function () {
447
+ /**
448
+ * Watermark constructor
449
+ * @param args - watermark args
450
+ */
451
+ function Watermark(args) {
452
+ if (args === void 0) { args = {}; }
453
+ this.parentElement = document.body;
454
+ this.props = args;
455
+ this.options = Object.assign({}, initialOptions, args);
456
+ this.changeParentElement(this.options.parent);
457
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
458
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
459
+ bootstrap();
460
+ }
461
+ /**
462
+ * Create an HD canvas.
463
+ * @param width - width of canvas
464
+ * @param height - height of canvas
465
+ */
466
+ Watermark.createCanvas = function (width, height) {
467
+ var _a;
468
+ var ratio = window.devicePixelRatio || 1;
469
+ var canvas = document.createElement('canvas');
470
+ canvas.width = width * ratio; // actual rendered pixel
471
+ canvas.height = height * ratio; // actual rendered pixel
472
+ canvas.style.width = "".concat(width, "px"); // control display size
473
+ canvas.style.height = "".concat(height, "px"); // control display size
474
+ (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
475
+ return canvas;
476
+ };
477
+ Watermark.prototype.changeOptions = function (args, mode) {
478
+ if (args === void 0) { args = {}; }
479
+ if (mode === void 0) { mode = 'overwrite'; }
480
+ this.initConfigData(args, mode);
481
+ };
482
+ /**
483
+ * Creating a watermark.
484
+ */
485
+ Watermark.prototype.create = function () {
486
+ var _a, _b, _c, _d;
487
+ return __awaiter(this, void 0, void 0, function () {
488
+ var firstDraw, image, watermarkInnerDom, parentElementType;
489
+ return __generator(this, function (_e) {
490
+ switch (_e.label) {
491
+ case 0:
492
+ if (!this.validateUnique()) {
493
+ return [2 /*return*/];
494
+ }
495
+ if (!this.validateContent()) {
496
+ return [2 /*return*/];
497
+ }
498
+ firstDraw = isUndefined(this.watermarkDom);
499
+ return [4 /*yield*/, this.draw()];
500
+ case 1:
501
+ _e.sent();
502
+ image = convertImage(this.canvas);
503
+ this.clearCanvas();
504
+ this.watermarkDom = document.createElement('div');
505
+ watermarkInnerDom = document.createElement('div');
506
+ this.watermarkDom.__WATERMARK__ = 'watermark';
507
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
508
+ parentElementType = this.checkParentElementType();
509
+ 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 ");
510
+ 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(this.options.width, "px ").concat(this.options.height, "px!important;background-position:").concat(this.options.backgroundPosition, "!important;\n ");
511
+ this.watermarkDom.append(watermarkInnerDom);
512
+ this.parentElement.appendChild(this.watermarkDom);
513
+ if (this.options.mutationObserve) {
514
+ try {
515
+ this.bindMutationObserve();
516
+ }
517
+ catch (e) {
518
+ (_b = (_a = this.options).onObserveError) === null || _b === void 0 ? void 0 : _b.call(_a);
519
+ }
520
+ }
521
+ firstDraw && ((_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c));
522
+ return [2 /*return*/];
523
+ }
524
+ });
525
+ });
526
+ };
527
+ /**
528
+ * Delete this watermark.
529
+ */
530
+ Watermark.prototype.destroy = function () {
531
+ var _a, _b, _c, _d, _e, _f, _g;
532
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
533
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
534
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
535
+ if (!isUndefined(this.checkWatermarkElementRequestID)) {
536
+ cancelAnimationFrame(this.checkWatermarkElementRequestID);
537
+ }
538
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
539
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
540
+ };
541
+ Watermark.prototype.check = function () {
542
+ if (!document.body.contains(this.watermarkDom)) {
543
+ this.destroy();
544
+ this.create();
545
+ }
546
+ };
547
+ Watermark.prototype.initConfigData = function (args, mode) {
548
+ var _this = this;
549
+ if (mode === void 0) { mode = 'overwrite'; }
550
+ this.props = args;
551
+ if (mode === 'overwrite') {
552
+ this.options = Object.assign({}, initialOptions, args);
553
+ }
554
+ else {
555
+ Object.keys(args).forEach(function (key) {
556
+ _this.options[key] = args[key];
557
+ });
558
+ }
559
+ this.changeParentElement(this.options.parent);
560
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
561
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
562
+ // document.body?.appendChild(this.canvas)
563
+ };
564
+ Watermark.prototype.changeParentElement = function (parent) {
565
+ if (typeof parent === 'string') {
566
+ var parentElement = document.querySelector(parent);
567
+ parentElement && (this.parentElement = parentElement);
568
+ }
569
+ else {
570
+ this.parentElement = parent;
571
+ }
572
+ };
573
+ Watermark.prototype.validateUnique = function () {
574
+ var result = true;
575
+ if (this.options.unique) {
576
+ this.parentElement.childNodes.forEach(function (node) {
577
+ if (!result) {
578
+ return;
579
+ }
580
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
581
+ result = false;
582
+ // throw new Error('duplicate watermark error')
583
+ }
584
+ });
585
+ }
586
+ return result;
587
+ };
588
+ Watermark.prototype.validateContent = function () {
589
+ switch (this.options.contentType) {
590
+ case 'image':
591
+ return Object.hasOwnProperty.call(this.options, 'image');
592
+ case 'multi-line-text':
593
+ case 'rich-text':
594
+ case 'text':
595
+ return this.options.content.length > 0;
596
+ }
597
+ return false;
598
+ };
599
+ Watermark.prototype.draw = function () {
600
+ var _this = this;
601
+ var ctx = this.canvas.getContext('2d');
602
+ if (ctx === null) {
603
+ throw new Error('get context error');
604
+ }
605
+ if (this.options.auxiliaryLine) {
606
+ ctx.beginPath();
607
+ ctx.rect(1, 1, this.options.width, this.options.height);
608
+ ctx.lineWidth = 1;
609
+ ctx.strokeStyle = '#000';
610
+ ctx.stroke();
611
+ ctx.closePath();
612
+ ctx.beginPath();
613
+ ctx.rect(this.options.translateX, this.options.translateY, 1, 1);
614
+ ctx.lineWidth = 1;
615
+ ctx.strokeStyle = '#f00';
616
+ ctx.stroke();
617
+ ctx.closePath();
618
+ }
619
+ this.setStyle(ctx);
620
+ ctx.save();
621
+ ctx.translate(this.options.translateX, this.options.translateY);
622
+ ctx.rotate(this.options.rotate);
623
+ return new Promise(function (resolve) {
624
+ switch (_this.options.contentType) {
625
+ case 'text':
626
+ _this.drawText(ctx, resolve);
627
+ break;
628
+ case 'image':
629
+ _this.drawImage(ctx, resolve);
630
+ break;
631
+ case 'multi-line-text':
632
+ _this.drawMultiLineText(ctx, resolve);
633
+ break;
634
+ case 'rich-text':
635
+ _this.drawRichText(ctx, resolve);
636
+ break;
637
+ }
638
+ });
639
+ };
640
+ Watermark.prototype.setStyle = function (ctx) {
641
+ var _a;
642
+ var propName = 'fillStyle';
643
+ if (this.options.textType === 'stroke') {
644
+ propName = 'strokeStyle';
645
+ }
646
+ var style = this.options.fontColor;
647
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) {
648
+ switch (this.options.advancedStyle.type) {
649
+ case 'linear':
650
+ style = this.createLinearGradient(ctx);
651
+ break;
652
+ case 'radial':
653
+ style = this.createRadialGradient(ctx);
654
+ break;
655
+ case 'conic':
656
+ style = this.createConicGradient(ctx);
657
+ break;
658
+ case 'pattern':
659
+ style = this.createPattern(ctx);
660
+ break;
661
+ }
662
+ }
663
+ ctx[propName] && style && (ctx[propName] = style);
664
+ this.options.textAlign && (ctx.textAlign = this.options.textAlign);
665
+ this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
666
+ ctx.globalAlpha = this.options.globalAlpha;
667
+ if (this.options.shadowStyle) {
668
+ ctx.shadowBlur = getValue(this.options.shadowStyle.shadowBlur, 0);
669
+ ctx.shadowColor = getValue(this.options.shadowStyle.shadowColor, '#00000000');
670
+ ctx.shadowOffsetX = getValue(this.options.shadowStyle.shadowOffsetX, 0);
671
+ ctx.shadowOffsetY = getValue(this.options.shadowStyle.shadowOffsetY, 0);
672
+ }
673
+ if (isFunction(this.options.extraDrawFunc)) {
674
+ this.options.extraDrawFunc(ctx);
675
+ }
676
+ };
677
+ Watermark.prototype.createLinearGradient = function (ctx) {
678
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
679
+ 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));
680
+ (_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) {
681
+ gradient.addColorStop(item.offset, item.color);
682
+ });
683
+ return gradient;
684
+ };
685
+ Watermark.prototype.createConicGradient = function (ctx) {
686
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
687
+ 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));
688
+ (_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) {
689
+ gradient.addColorStop(item.offset, item.color);
690
+ });
691
+ return gradient;
692
+ };
693
+ Watermark.prototype.createRadialGradient = function (ctx) {
694
+ 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;
695
+ 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));
696
+ (_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) {
697
+ gradient.addColorStop(item.offset, item.color);
698
+ });
699
+ return gradient;
700
+ };
701
+ Watermark.prototype.createPattern = function (ctx) {
702
+ var _a, _b, _c, _d, _e, _f, _g, _h;
703
+ 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) || '');
704
+ };
705
+ Watermark.prototype.setText = function (ctx, params) {
706
+ var methodName = 'fillText';
707
+ if (this.options.textType === 'stroke') {
708
+ methodName = 'strokeText';
709
+ }
710
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
711
+ };
712
+ Watermark.prototype.drawText = function (ctx, resolve) {
713
+ this.setText(ctx, {
714
+ text: this.options.content,
715
+ x: 0,
716
+ y: 0,
717
+ maxWidth: this.options.textRowMaxWidth || this.options.width
718
+ });
719
+ resolve(ctx.canvas);
720
+ };
721
+ Watermark.prototype.drawImage = function (ctx, resolve) {
722
+ var _this = this;
723
+ loadImage(this.options.image).then(function (image) {
724
+ var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
725
+ var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
726
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
727
+ resolve(ctx.canvas);
728
+ });
729
+ };
730
+ Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
731
+ var _this = this;
732
+ // image.width = this.options.width
733
+ // image.height = this.options.height
734
+ // const element = createCustomContentSvg(context, this.options)
735
+ // image.src = convertSVGToImage(element)
736
+ // image.onload = () => {
737
+ // context.translate(this.options.width / 2, this.options.height / 2)
738
+ // context.rotate(this.options.rotate)
739
+ // context.drawImage(
740
+ // image,
741
+ // -this.options.width / 2,
742
+ // -this.options.height / 2,
743
+ // context.canvas.width,
744
+ // context.canvas.height
745
+ // )
746
+ // resolve(canvas)
747
+ // }
748
+ var lines = this.recommendOptions.textLine.data;
749
+ var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
750
+ lines.forEach(function (text, index) {
751
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
752
+ });
753
+ resolve(ctx.canvas);
754
+ };
755
+ Watermark.prototype.drawRichText = function (ctx, resolve) {
756
+ var _this = this;
757
+ var obj = createCustomContentSVG(ctx, this.options);
758
+ loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
759
+ var imagePosition = _this.getDrawImagePosition(image.width, image.height);
760
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
761
+ resolve(ctx.canvas);
762
+ });
763
+ };
764
+ Watermark.prototype.getImageRect = function (image) {
765
+ var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
766
+ switch (true) {
767
+ case rect.width !== 0 && rect.height === 0:
768
+ rect.height = rect.width * image.height / image.width;
769
+ break;
770
+ case rect.width === 0 && rect.height !== 0:
771
+ rect.width = rect.height * image.width / image.height;
772
+ break;
773
+ case rect.width === 0 && rect.height === 0:
774
+ rect.width = image.width;
775
+ rect.height = image.height;
776
+ break;
777
+ }
778
+ return rect;
779
+ };
780
+ Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
781
+ var _a, _b;
782
+ var result = {
783
+ x: -imageWidth / 2,
784
+ y: -imageHeight / 2
785
+ };
786
+ switch (this.options.translatePlacement) {
787
+ case 'top':
788
+ result.x = -imageWidth / 2;
789
+ result.y = 0;
790
+ break;
791
+ case 'top-start':
792
+ result.x = 0;
793
+ result.y = 0;
794
+ break;
795
+ case 'top-end':
796
+ result.x = -imageWidth;
797
+ result.y = 0;
798
+ break;
799
+ case 'bottom':
800
+ result.x = -imageWidth / 2;
801
+ result.y = -imageHeight;
802
+ break;
803
+ case 'bottom-start':
804
+ result.x = 0;
805
+ result.y = -imageHeight;
806
+ break;
807
+ case 'bottom-end':
808
+ result.x = -imageWidth;
809
+ result.y = -imageHeight;
810
+ break;
811
+ case 'left':
812
+ result.x = 0;
813
+ result.y = -imageHeight / 2;
814
+ break;
815
+ case 'right':
816
+ result.x = -imageWidth;
817
+ result.y = -imageHeight / 2;
818
+ break;
819
+ }
820
+ !isUndefined((_a = this.props) === null || _a === void 0 ? void 0 : _a.translateX) && (result.x = 0);
821
+ !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
822
+ return result;
823
+ };
824
+ Watermark.prototype.checkParentElementType = function () {
825
+ if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
826
+ return 'root';
827
+ }
828
+ return 'custom';
829
+ };
830
+ Watermark.prototype.checkWatermarkElement = function () {
831
+ if (!document.body.contains(this.watermarkDom)) {
832
+ this.destroy();
833
+ this.create();
834
+ }
835
+ this.checkWatermarkElementRequestID = requestAnimationFrame(this.checkWatermarkElement.bind(this));
836
+ };
837
+ Watermark.prototype.bindMutationObserve = function () {
838
+ var _this = this;
839
+ if (!this.watermarkDom) {
840
+ return;
841
+ }
842
+ this.checkWatermarkElementRequestID = requestAnimationFrame(this.checkWatermarkElement.bind(this));
843
+ this.observer = new MutationObserver(function (mutationsList) {
844
+ if (mutationsList.length > 0) {
845
+ _this.destroy();
846
+ _this.create();
847
+ }
848
+ });
849
+ this.observer.observe(this.watermarkDom, {
850
+ attributes: true,
851
+ childList: true,
852
+ subtree: true,
853
+ characterData: true // 节点内容或节点文本的变动。
854
+ });
855
+ this.parentObserve = new MutationObserver(function (mutationsList) {
856
+ mutationsList.forEach(function (item) {
857
+ var _a;
858
+ if ((item === null || item === void 0 ? void 0 : item.target) === _this.watermarkDom ||
859
+ ((_a = item === null || item === void 0 ? void 0 : item.removedNodes) === null || _a === void 0 ? void 0 : _a[0]) === _this.watermarkDom ||
860
+ (item.type === 'childList' && item.target === _this.parentElement && item.target.lastChild !== _this.watermarkDom)) {
861
+ _this.destroy();
862
+ _this.create();
863
+ }
864
+ });
865
+ });
866
+ this.parentObserve.observe(this.parentElement, {
867
+ attributes: true,
868
+ childList: true,
869
+ subtree: true,
870
+ characterData: true // 节点内容或节点文本的变动。
871
+ });
872
+ };
873
+ Watermark.prototype.clearCanvas = function () {
874
+ var ctx = this.canvas.getContext('2d');
875
+ if (ctx === null) {
876
+ throw new Error('get context error');
877
+ }
878
+ ctx.restore();
879
+ ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
880
+ // this.canvas.width = this.options.w
881
+ };
882
+ return Watermark;
883
+ }());
884
+
885
+ /**
886
+ * BlindWatermark class
887
+ */
888
+ var BlindWatermark = /** @class */ (function (_super) {
889
+ __extends(BlindWatermark, _super);
890
+ /**
891
+ * BlindWatermark constructor
892
+ * @param props - blind watermark options
893
+ */
894
+ function BlindWatermark(props) {
895
+ if (props === void 0) { props = {}; }
896
+ props.globalAlpha = 0.005;
897
+ props.mode = 'blind';
898
+ return _super.call(this, props) || this;
899
+ }
900
+ /**
901
+ * Decode blind watermark.
902
+ * @param props - decode options
903
+ */
904
+ BlindWatermark.decode = function (props) {
905
+ var options = Object.assign({
906
+ url: '',
907
+ fillColor: '#000',
908
+ compositeOperation: 'color-burn',
909
+ mode: 'canvas'
910
+ }, props);
911
+ if (!options.url) {
912
+ return;
913
+ }
914
+ if (options.mode === 'canvas') {
915
+ var img_1 = new Image();
916
+ img_1.src = options.url;
917
+ img_1.onload = function () {
918
+ var _a;
919
+ var width = img_1.width, height = img_1.height;
920
+ var canvas = Watermark.createCanvas(width, height);
921
+ var ctx = canvas.getContext('2d');
922
+ if (ctx === null) {
923
+ throw new Error('get context error');
924
+ }
925
+ ctx.drawImage(img_1, 0, 0, width, height);
926
+ ctx.globalCompositeOperation = options.compositeOperation;
927
+ ctx.fillStyle = options.fillColor;
928
+ ctx.fillRect(0, 0, width, height);
929
+ var resultImage = convertImage(canvas);
930
+ if (options.onSuccess && isFunction(options.onSuccess)) {
931
+ (_a = options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options, resultImage);
932
+ }
933
+ };
934
+ }
935
+ };
936
+ return BlindWatermark;
937
+ }(Watermark));
938
+
939
+ exports.BlindWatermark = BlindWatermark;
940
+ exports.Watermark = Watermark;
941
+ //# sourceMappingURL=index.cjs.js.map