watermark-js-plus 1.4.21 → 1.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1290 +1,1290 @@
1
1
  /*!
2
- * watermark-js-plus v1.4.21
3
- * (c) 2022-2023 Michael Sun
2
+ * watermark-js-plus v1.4.23
3
+ * (c) 2022-2024 Michael Sun
4
4
  * Released under the MIT License.
5
5
  */
6
6
  var WatermarkPlus = (function (exports) {
7
- 'use strict';
7
+ 'use strict';
8
8
 
9
- /******************************************************************************
10
- Copyright (c) Microsoft Corporation.
9
+ function styleInject(css, ref) {
10
+ if ( ref === void 0 ) ref = {};
11
+ var insertAt = ref.insertAt;
12
+
13
+ if (typeof document === 'undefined') { return; }
14
+
15
+ var head = document.head || document.getElementsByTagName('head')[0];
16
+ var style = document.createElement('style');
17
+ style.type = 'text/css';
18
+
19
+ if (insertAt === 'top') {
20
+ if (head.firstChild) {
21
+ head.insertBefore(style, head.firstChild);
22
+ } else {
23
+ head.appendChild(style);
24
+ }
25
+ } else {
26
+ head.appendChild(style);
27
+ }
28
+
29
+ if (style.styleSheet) {
30
+ style.styleSheet.cssText = css;
31
+ } else {
32
+ style.appendChild(document.createTextNode(css));
33
+ }
34
+ }
35
+
36
+ var css_248z = "@keyframes watermark{0%{background-position:0 0}25%{background-position:100% 100%}50%{background-position:0 0}75%{background-position:100% -100%}to{background-position:0 0}}";
37
+ styleInject(css_248z);
38
+
39
+ /******************************************************************************
40
+ Copyright (c) Microsoft Corporation.
11
41
 
12
- Permission to use, copy, modify, and/or distribute this software for any
13
- purpose with or without fee is hereby granted.
42
+ Permission to use, copy, modify, and/or distribute this software for any
43
+ purpose with or without fee is hereby granted.
14
44
 
15
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21
- PERFORMANCE OF THIS SOFTWARE.
22
- ***************************************************************************** */
23
- /* global Reflect, Promise */
45
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
46
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
47
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
48
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
49
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51
+ PERFORMANCE OF THIS SOFTWARE.
52
+ ***************************************************************************** */
53
+ /* global Reflect, Promise */
24
54
 
25
- var extendStatics = function(d, b) {
26
- extendStatics = Object.setPrototypeOf ||
27
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
28
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
29
- return extendStatics(d, b);
30
- };
55
+ var extendStatics = function(d, b) {
56
+ extendStatics = Object.setPrototypeOf ||
57
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
58
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
59
+ return extendStatics(d, b);
60
+ };
31
61
 
32
- function __extends(d, b) {
33
- if (typeof b !== "function" && b !== null)
34
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
35
- extendStatics(d, b);
36
- function __() { this.constructor = d; }
37
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
38
- }
62
+ function __extends(d, b) {
63
+ if (typeof b !== "function" && b !== null)
64
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
65
+ extendStatics(d, b);
66
+ function __() { this.constructor = d; }
67
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
68
+ }
39
69
 
40
- var __assign = function() {
41
- __assign = Object.assign || function __assign(t) {
42
- for (var s, i = 1, n = arguments.length; i < n; i++) {
43
- s = arguments[i];
44
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
45
- }
46
- return t;
47
- };
48
- return __assign.apply(this, arguments);
49
- };
70
+ var __assign = function() {
71
+ __assign = Object.assign || function __assign(t) {
72
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
73
+ s = arguments[i];
74
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
75
+ }
76
+ return t;
77
+ };
78
+ return __assign.apply(this, arguments);
79
+ };
50
80
 
51
- function __awaiter(thisArg, _arguments, P, generator) {
52
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
53
- return new (P || (P = Promise))(function (resolve, reject) {
54
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
55
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
56
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
57
- step((generator = generator.apply(thisArg, _arguments || [])).next());
58
- });
59
- }
81
+ function __awaiter(thisArg, _arguments, P, generator) {
82
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
83
+ return new (P || (P = Promise))(function (resolve, reject) {
84
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
85
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
86
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
87
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
88
+ });
89
+ }
60
90
 
61
- function __generator(thisArg, body) {
62
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
63
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
64
- function verb(n) { return function (v) { return step([n, v]); }; }
65
- function step(op) {
66
- if (f) throw new TypeError("Generator is already executing.");
67
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
68
- 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;
69
- if (y = 0, t) op = [op[0] & 2, t.value];
70
- switch (op[0]) {
71
- case 0: case 1: t = op; break;
72
- case 4: _.label++; return { value: op[1], done: false };
73
- case 5: _.label++; y = op[1]; op = [0]; continue;
74
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
75
- default:
76
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
77
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
78
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
79
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
80
- if (t[2]) _.ops.pop();
81
- _.trys.pop(); continue;
82
- }
83
- op = body.call(thisArg, _);
84
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
85
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
86
- }
87
- }
88
-
89
- var convertImage = function (canvas) {
90
- return canvas.toDataURL('image/png', 1);
91
- };
92
- var isFunction = function (value) {
93
- return typeof value === 'function';
94
- };
95
- var isUndefined = function (value) {
96
- return value === undefined;
97
- };
98
- var isString = function (value) {
99
- return typeof value === 'string';
100
- };
101
- var createSVGElement = function (tagName, attrs, namespaceURI) {
102
- if (attrs === void 0) { attrs = {}; }
103
- if (namespaceURI === void 0) { namespaceURI = 'http://www.w3.org/2000/svg'; }
104
- var element = document.createElementNS(namespaceURI, tagName);
105
- for (var attr in attrs) {
106
- element.setAttribute(attr, attrs[attr]);
107
- }
108
- return element;
109
- };
110
- var getMultiLineData = function (ctx, text, maxWidth) {
111
- var result = [];
112
- var str = '';
113
- for (var i = 0, len = text.length; i < len; i++) {
114
- str += text.charAt(i);
115
- if (ctx.measureText(str).width > maxWidth) {
116
- result.push(str.substring(0, str.length - 1));
117
- str = '';
118
- i--;
119
- }
120
- }
121
- result.push(str);
122
- return result;
123
- };
124
- var createCustomContentSVG = function (ctx, options) { return __awaiter(void 0, void 0, void 0, function () {
125
- var svgElement, bodyElement, _a, offsetHeight, offsetWidth, width, height, foreignObjectElement;
126
- return __generator(this, function (_b) {
127
- switch (_b.label) {
128
- case 0:
129
- svgElement = createSVGElement('svg', {
130
- xmlns: 'http://www.w3.org/2000/svg'
131
- });
132
- bodyElement = document.createElement('div');
133
- bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
134
- 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");
135
- bodyElement.innerHTML = "<div class='rich-text-content'>".concat(options.content, "</div>");
136
- document.body.appendChild(bodyElement);
137
- // convert all images to base64
138
- return [4 /*yield*/, convertImgToBase64(bodyElement)];
139
- case 1:
140
- // convert all images to base64
141
- _b.sent();
142
- _a = bodyElement.querySelector('.rich-text-content'), offsetHeight = _a.offsetHeight, offsetWidth = _a.offsetWidth;
143
- document.body.removeChild(bodyElement);
144
- width = options.richTextWidth || offsetWidth || options.width;
145
- height = options.richTextHeight || offsetHeight || options.height;
146
- svgElement.setAttribute('width', width.toString());
147
- svgElement.setAttribute('height', height.toString());
148
- foreignObjectElement = createSVGElement('foreignObject', {
149
- width: width.toString(),
150
- height: height.toString()
151
- });
152
- foreignObjectElement.appendChild(bodyElement);
153
- svgElement.appendChild(foreignObjectElement);
154
- return [2 /*return*/, {
155
- element: svgElement,
156
- width: width,
157
- height: height
158
- }];
159
- }
160
- });
161
- }); };
162
- function convertImgToBase64(bodyElement) {
163
- return __awaiter(this, void 0, void 0, function () {
164
- var imgElements, _loop_1, _i, _a, img;
165
- return __generator(this, function (_b) {
166
- switch (_b.label) {
167
- case 0:
168
- imgElements = bodyElement.querySelectorAll('img');
169
- _loop_1 = function (img) {
170
- var src, response, blob_1, imgData, error_1;
171
- return __generator(this, function (_c) {
172
- switch (_c.label) {
173
- case 0:
174
- src = img.getAttribute('src');
175
- if (!src) return [3 /*break*/, 6];
176
- _c.label = 1;
177
- case 1:
178
- _c.trys.push([1, 5, , 6]);
179
- return [4 /*yield*/, fetch(src)];
180
- case 2:
181
- response = _c.sent();
182
- return [4 /*yield*/, response.blob()];
183
- case 3:
184
- blob_1 = _c.sent();
185
- return [4 /*yield*/, new Promise(function (resolve, reject) {
186
- var reader = new FileReader();
187
- reader.onloadend = function () { return resolve(reader.result); };
188
- reader.onerror = reject;
189
- reader.readAsDataURL(blob_1);
190
- })];
191
- case 4:
192
- imgData = _c.sent();
193
- if (isString(imgData)) {
194
- img.setAttribute('src', imgData);
195
- }
196
- return [3 /*break*/, 6];
197
- case 5:
198
- error_1 = _c.sent();
199
- console.error("Error converting ".concat(src, " to base64:"), error_1);
200
- return [3 /*break*/, 6];
201
- case 6: return [2 /*return*/];
202
- }
203
- });
204
- };
205
- _i = 0, _a = Array.from(imgElements);
206
- _b.label = 1;
207
- case 1:
208
- if (!(_i < _a.length)) return [3 /*break*/, 4];
209
- img = _a[_i];
210
- return [5 /*yield**/, _loop_1(img)];
211
- case 2:
212
- _b.sent();
213
- _b.label = 3;
214
- case 3:
215
- _i++;
216
- return [3 /*break*/, 1];
217
- case 4: return [2 /*return*/];
218
- }
219
- });
220
- });
221
- }
222
- var convertSVGToImage = function (svg) {
223
- var richContent = svg.outerHTML.replace(/<img(.*?)>/g, '<img$1/>').replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
224
- return "data:image/svg+xml;charset=utf-8,".concat(richContent);
225
- };
226
- var getValue = function (v1, v2) {
227
- if (isUndefined(v1)) {
228
- return v2;
229
- }
230
- else {
231
- return v1;
232
- }
233
- };
234
- var loadImage = function (url, width, height) {
235
- if (width === void 0) { width = undefined; }
236
- if (height === void 0) { height = undefined; }
237
- var image = new Image();
238
- image.setAttribute('crossOrigin', 'Anonymous');
239
- !isUndefined(width) && (image.width = width);
240
- !isUndefined(height) && (image.height = height);
241
- image.src = url;
242
- return new Promise(function (resolve) {
243
- image.onload = function () {
244
- resolve(image);
245
- };
246
- });
247
- };
248
- var generateMatrix = function (rows, columns, value) {
249
- return Array.from({ length: rows }, function () { return new Array(columns).fill(value); });
250
- };
251
-
252
- var initialOptions = {
253
- width: 300,
254
- height: 300,
255
- rotate: 45,
256
- layout: 'default',
257
- auxiliaryLine: false,
258
- translatePlacement: 'middle',
259
- contentType: 'text',
260
- content: 'hello watermark-js-plus',
261
- textType: 'fill',
262
- imageWidth: 0,
263
- imageHeight: 0,
264
- lineHeight: 30,
265
- zIndex: 2147483647,
266
- backgroundPosition: '0 0',
267
- backgroundRepeat: 'repeat',
268
- fontSize: '20px',
269
- fontFamily: 'sans-serif',
270
- fontStyle: '',
271
- fontVariant: '',
272
- fontColor: '#000',
273
- fontWeight: 'normal',
274
- filter: 'none',
275
- letterSpacing: '0px',
276
- globalAlpha: 0.5,
277
- mode: 'default',
278
- mutationObserve: true,
279
- movable: false,
280
- parent: 'body',
281
- onSuccess: function () { },
282
- onBeforeDestroy: function () { },
283
- onDestroyed: function () { },
284
- onObserveError: function () { }
285
- };
286
- var generateRecommendOptions = function (canvas, options, args) {
287
- var ctx = canvas.getContext('2d');
288
- if (ctx === null) {
289
- throw new Error('get context error');
290
- }
291
- ctx.font = "".concat(options.fontStyle, " ").concat(options.fontVariant, " ").concat(options.fontWeight, " ").concat(options.fontSize, " ").concat(options.fontFamily);
292
- ctx.filter = options.filter;
293
- // @ts-ignore
294
- ctx.letterSpacing = options.letterSpacing;
295
- if (options === null || options === void 0 ? void 0 : options.rotate) {
296
- options.rotate = (360 - options.rotate % 360) * (Math.PI / 180);
297
- }
298
- if (isUndefined(args.textRowMaxWidth)) {
299
- options.textRowMaxWidth = options.width;
300
- }
301
- var result = {
302
- image: {
303
- rect: {
304
- width: options.imageWidth,
305
- height: options.imageHeight
306
- },
307
- position: {
308
- x: 0,
309
- y: 0
310
- }
311
- },
312
- textLine: {
313
- data: [],
314
- yOffsetValue: 0
315
- },
316
- advancedStyleParams: {
317
- linear: {
318
- x0: 0,
319
- x1: 0
320
- },
321
- radial: {
322
- x0: 0,
323
- y0: 0,
324
- r0: 0,
325
- x1: 0,
326
- y1: 0,
327
- r1: 0
328
- },
329
- conic: {
330
- x: 0,
331
- y: 0,
332
- startAngle: 0
333
- },
334
- pattern: {}
335
- }
336
- };
337
- switch (options.contentType) {
338
- case 'text':
339
- result.textLine.data = [options.content];
340
- break;
341
- case 'multi-line-text':
342
- result.textLine.data = getMultiLineData(ctx, options.content, options.textRowMaxWidth);
343
- break;
344
- // case 'image':
345
- // break
346
- // case 'rich-text':
347
- // break
348
- }
349
- var translateX = options.width / 2;
350
- var translateY = options.height / 2;
351
- var textBaseline = 'middle';
352
- var textAlign = 'center';
353
- if (!isUndefined(args === null || args === void 0 ? void 0 : args.translateX) && !isUndefined(args === null || args === void 0 ? void 0 : args.translateY)) {
354
- translateX = args === null || args === void 0 ? void 0 : args.translateX;
355
- translateY = args === null || args === void 0 ? void 0 : args.translateY;
356
- textBaseline = 'top';
357
- textAlign = 'left';
358
- }
359
- else {
360
- // default middle
361
- // translateX = options.width / 2
362
- // translateY = options.height / 2
363
- // TextBaselineType = 'middle'
364
- // textAlign = 'center'
365
- result.advancedStyleParams.linear.x0 = -options.width / 2;
366
- result.advancedStyleParams.linear.x1 = options.width / 2;
367
- // result.advancedStyleParams.radial.x0 = 0
368
- // result.advancedStyleParams.radial.y0 = 0
369
- result.advancedStyleParams.radial.r0 = 0;
370
- // result.advancedStyleParams.radial.x1 = 0
371
- // result.advancedStyleParams.radial.y1 = 0
372
- result.advancedStyleParams.radial.r1 = options.width / 2;
373
- // result.advancedStyleParams.conic.x = 0
374
- // result.advancedStyleParams.conic.y = 0
375
- }
376
- switch (args.translatePlacement) {
377
- case 'top':
378
- translateX = options.width / 2;
379
- translateY = 0;
380
- textBaseline = 'top';
381
- // textAlign = 'center'
382
- result.advancedStyleParams.linear.x0 = -options.width / 2;
383
- result.advancedStyleParams.linear.x1 = options.width / 2;
384
- // result.advancedStyleParams.radial.x0 = 0
385
- result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
386
- // result.advancedStyleParams.radial.r0 = 0
387
- // result.advancedStyleParams.radial.x1 = 0
388
- // result.advancedStyleParams.radial.y1 = 0
389
- result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
390
- // result.advancedStyleParams.radial.r1 = options.width / 2
391
- // result.advancedStyleParams.conic.x = 0
392
- result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
393
- break;
394
- case 'top-start':
395
- translateX = 0;
396
- translateY = 0;
397
- textBaseline = 'top';
398
- textAlign = 'start';
399
- result.advancedStyleParams.linear.x0 = 0;
400
- result.advancedStyleParams.linear.x1 = options.width;
401
- result.advancedStyleParams.radial.x0 = options.width / 2;
402
- result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
403
- // result.advancedStyleParams.radial.r0 = 0
404
- result.advancedStyleParams.radial.x1 = options.width / 2;
405
- result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
406
- // result.advancedStyleParams.radial.r1 = options.width / 2
407
- result.advancedStyleParams.conic.x = options.width / 2;
408
- result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
409
- break;
410
- case 'top-end':
411
- translateX = options.width;
412
- translateY = 0;
413
- textBaseline = 'top';
414
- textAlign = 'end';
415
- result.advancedStyleParams.linear.x0 = 0;
416
- result.advancedStyleParams.linear.x1 = -options.width;
417
- result.advancedStyleParams.radial.x0 = -options.width / 2;
418
- result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
419
- // result.advancedStyleParams.radial.r0 = 0
420
- result.advancedStyleParams.radial.x1 = -options.width / 2;
421
- result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
422
- // result.advancedStyleParams.radial.r1 = options.width / 2
423
- result.advancedStyleParams.conic.x = -options.width / 2;
424
- result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
425
- break;
426
- case 'bottom':
427
- translateX = options.width / 2;
428
- translateY = options.height;
429
- textBaseline = 'bottom';
430
- // textAlign = 'center'
431
- result.advancedStyleParams.linear.x0 = -options.width / 2;
432
- result.advancedStyleParams.linear.x1 = options.width / 2;
433
- // result.advancedStyleParams.radial.x0 = 0
434
- result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
435
- // result.advancedStyleParams.radial.r0 = 0
436
- // result.advancedStyleParams.radial.x1 = 0
437
- result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
438
- // result.advancedStyleParams.radial.r1 = options.width / 2
439
- result.advancedStyleParams.conic.x = 0;
440
- result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
441
- break;
442
- case 'bottom-start':
443
- translateX = 0;
444
- translateY = options.height;
445
- textBaseline = 'bottom';
446
- textAlign = 'start';
447
- result.advancedStyleParams.linear.x0 = 0;
448
- result.advancedStyleParams.linear.x1 = options.width;
449
- result.advancedStyleParams.radial.x0 = options.width / 2;
450
- result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
451
- // result.advancedStyleParams.radial.r0 = 0
452
- result.advancedStyleParams.radial.x1 = options.width / 2;
453
- result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
454
- // result.advancedStyleParams.radial.r1 = options.width / 2
455
- result.advancedStyleParams.conic.x = options.width / 2;
456
- result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
457
- break;
458
- case 'bottom-end':
459
- translateX = options.width;
460
- translateY = options.height;
461
- textBaseline = 'bottom';
462
- textAlign = 'end';
463
- result.advancedStyleParams.linear.x0 = 0;
464
- result.advancedStyleParams.linear.x1 = -options.width;
465
- result.advancedStyleParams.radial.x0 = -options.width / 2;
466
- result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
467
- // result.advancedStyleParams.radial.r0 = 0
468
- result.advancedStyleParams.radial.x1 = -options.width / 2;
469
- result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
470
- // result.advancedStyleParams.radial.r1 = options.width / 2
471
- result.advancedStyleParams.conic.x = -options.width / 2;
472
- result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
473
- break;
474
- case 'left':
475
- translateX = 0;
476
- translateY = options.height / 2;
477
- // TextBaselineType = 'middle'
478
- textAlign = 'start';
479
- result.advancedStyleParams.linear.x0 = 0;
480
- result.advancedStyleParams.linear.x1 = options.width;
481
- result.advancedStyleParams.radial.x0 = options.width / 2;
482
- // result.advancedStyleParams.radial.y0 = 0
483
- // result.advancedStyleParams.radial.r0 = 0
484
- result.advancedStyleParams.radial.x1 = options.width / 2;
485
- // result.advancedStyleParams.radial.y1 = 0
486
- // result.advancedStyleParams.radial.r1 = options.width / 2
487
- result.advancedStyleParams.conic.x = options.width / 2;
488
- result.advancedStyleParams.conic.y = 0;
489
- break;
490
- case 'right':
491
- translateX = options.width;
492
- translateY = options.height / 2;
493
- // TextBaselineType = 'middle'
494
- textAlign = 'end';
495
- result.advancedStyleParams.linear.x0 = 0;
496
- result.advancedStyleParams.linear.x1 = -options.width;
497
- result.advancedStyleParams.radial.x0 = -options.width / 2;
498
- // result.advancedStyleParams.radial.y0 = 0
499
- // result.advancedStyleParams.radial.r0 = 0
500
- result.advancedStyleParams.radial.x1 = -options.width / 2;
501
- // result.advancedStyleParams.radial.y1 = 0
502
- // result.advancedStyleParams.radial.r1 = options.width / 2
503
- result.advancedStyleParams.conic.x = -options.width / 2;
504
- result.advancedStyleParams.conic.y = 0;
505
- break;
506
- }
507
- options.translateX = translateX;
508
- options.translateY = translateY;
509
- isUndefined(args === null || args === void 0 ? void 0 : args.textBaseline) && (options.textBaseline = textBaseline);
510
- isUndefined(args === null || args === void 0 ? void 0 : args.textAlign) && (options.textAlign = textAlign);
511
- if (['text', 'multi-line-text'].includes(options.contentType)) {
512
- switch (options.textBaseline) {
513
- case 'middle':
514
- result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight / 2;
515
- break;
516
- case 'bottom':
517
- case 'alphabetic':
518
- case 'ideographic':
519
- result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight + (options.lineHeight - parseInt(options.fontSize)) / 2;
520
- break;
521
- case 'top':
522
- case 'hanging':
523
- result.textLine.yOffsetValue = -options.lineHeight / 2 + parseInt(options.fontSize) / 2;
524
- break;
525
- }
526
- }
527
- return result;
528
- };
529
-
530
- var bootstrap = (function () {
531
- if (typeof window !== 'undefined') {
532
- Object.defineProperty(window, 'MutationObserver', {
533
- writable: false,
534
- configurable: false
535
- });
536
- Object.defineProperty(window, 'requestAnimationFrame', {
537
- writable: false,
538
- configurable: false
539
- });
540
- }
541
- });
91
+ function __generator(thisArg, body) {
92
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
93
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
94
+ function verb(n) { return function (v) { return step([n, v]); }; }
95
+ function step(op) {
96
+ if (f) throw new TypeError("Generator is already executing.");
97
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
98
+ 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;
99
+ if (y = 0, t) op = [op[0] & 2, t.value];
100
+ switch (op[0]) {
101
+ case 0: case 1: t = op; break;
102
+ case 4: _.label++; return { value: op[1], done: false };
103
+ case 5: _.label++; y = op[1]; op = [0]; continue;
104
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
105
+ default:
106
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
107
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
108
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
109
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
110
+ if (t[2]) _.ops.pop();
111
+ _.trys.pop(); continue;
112
+ }
113
+ op = body.call(thisArg, _);
114
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
115
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
116
+ }
117
+ }
542
118
 
543
- var WatermarkCanvas = /** @class */ (function () {
544
- function WatermarkCanvas(args, options) {
545
- this.props = args;
546
- this.options = options;
547
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
548
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
549
- }
550
- /**
551
- * Create an HD canvas.
552
- * @param width - width of canvas
553
- * @param height - height of canvas
554
- */
555
- WatermarkCanvas.createCanvas = function (width, height) {
556
- var _a;
557
- var ratio = window.devicePixelRatio || 1;
558
- var canvas = document.createElement('canvas');
559
- canvas.width = width * ratio; // actual rendered pixel
560
- canvas.height = height * ratio; // actual rendered pixel
561
- canvas.style.width = "".concat(width, "px"); // control display size
562
- canvas.style.height = "".concat(height, "px"); // control display size
563
- (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
564
- return canvas;
565
- };
566
- /**
567
- * Clean the canvas
568
- * @param canvas
569
- */
570
- WatermarkCanvas.clearCanvas = function (canvas) {
571
- var ctx = canvas.getContext('2d');
572
- if (ctx === null) {
573
- throw new Error('get context error');
574
- }
575
- ctx.restore();
576
- ctx.clearRect(0, 0, canvas.width, canvas.height);
577
- };
578
- WatermarkCanvas.prototype.getCanvas = function () {
579
- return this.canvas;
580
- };
581
- WatermarkCanvas.prototype.clear = function () {
582
- WatermarkCanvas.clearCanvas(this.canvas);
583
- };
584
- WatermarkCanvas.prototype.draw = function () {
585
- var _this = this;
586
- var ctx = this.canvas.getContext('2d');
587
- if (ctx === null) {
588
- throw new Error('get context error');
589
- }
590
- if (this.options.auxiliaryLine) {
591
- ctx.beginPath();
592
- ctx.rect(0, 0, this.options.width, this.options.height);
593
- ctx.lineWidth = 1;
594
- ctx.strokeStyle = '#000';
595
- ctx.stroke();
596
- ctx.closePath();
597
- ctx.beginPath();
598
- ctx.rect(this.options.translateX, this.options.translateY, 1, 1);
599
- ctx.lineWidth = 1;
600
- ctx.strokeStyle = '#f00';
601
- ctx.stroke();
602
- ctx.closePath();
603
- }
604
- this.setStyle(ctx);
605
- ctx.save();
606
- ctx.translate(this.options.translateX, this.options.translateY);
607
- ctx.rotate(this.options.rotate);
608
- return new Promise(function (resolve) {
609
- switch (_this.options.contentType) {
610
- case 'text':
611
- _this.drawText(ctx, resolve);
612
- break;
613
- case 'image':
614
- _this.drawImage(ctx, resolve);
615
- break;
616
- case 'multi-line-text':
617
- _this.drawMultiLineText(ctx, resolve);
618
- break;
619
- case 'rich-text':
620
- _this.drawRichText(ctx, resolve);
621
- break;
622
- }
623
- });
624
- };
625
- WatermarkCanvas.prototype.setStyle = function (ctx) {
626
- var _a;
627
- var propName = 'fillStyle';
628
- if (this.options.textType === 'stroke') {
629
- propName = 'strokeStyle';
630
- }
631
- var style = this.options.fontColor;
632
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) {
633
- switch (this.options.advancedStyle.type) {
634
- case 'linear':
635
- style = this.createLinearGradient(ctx);
636
- break;
637
- case 'radial':
638
- style = this.createRadialGradient(ctx);
639
- break;
640
- case 'conic':
641
- style = this.createConicGradient(ctx);
642
- break;
643
- case 'pattern':
644
- style = this.createPattern(ctx);
645
- break;
646
- }
647
- }
648
- ctx[propName] && style && (ctx[propName] = style);
649
- this.options.textAlign && (ctx.textAlign = this.options.textAlign);
650
- this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
651
- ctx.globalAlpha = this.options.globalAlpha;
652
- if (this.options.shadowStyle) {
653
- ctx.shadowBlur = getValue(this.options.shadowStyle.shadowBlur, 0);
654
- ctx.shadowColor = getValue(this.options.shadowStyle.shadowColor, '#00000000');
655
- ctx.shadowOffsetX = getValue(this.options.shadowStyle.shadowOffsetX, 0);
656
- ctx.shadowOffsetY = getValue(this.options.shadowStyle.shadowOffsetY, 0);
657
- }
658
- if (isFunction(this.options.extraDrawFunc)) {
659
- this.options.extraDrawFunc(ctx);
660
- }
661
- };
662
- WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
663
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
664
- 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));
665
- (_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) {
666
- gradient.addColorStop(item.offset, item.color);
667
- });
668
- return gradient;
669
- };
670
- WatermarkCanvas.prototype.createConicGradient = function (ctx) {
671
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
672
- 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));
673
- (_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) {
674
- gradient.addColorStop(item.offset, item.color);
675
- });
676
- return gradient;
677
- };
678
- WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
679
- 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;
680
- 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));
681
- (_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) {
682
- gradient.addColorStop(item.offset, item.color);
683
- });
684
- return gradient;
685
- };
686
- WatermarkCanvas.prototype.createPattern = function (ctx) {
687
- var _a, _b, _c, _d, _e, _f, _g, _h;
688
- 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) || '');
689
- };
690
- WatermarkCanvas.prototype.setText = function (ctx, params) {
691
- var methodName = 'fillText';
692
- if (this.options.textType === 'stroke') {
693
- methodName = 'strokeText';
694
- }
695
- ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
696
- };
697
- WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
698
- this.setText(ctx, {
699
- text: this.options.content,
700
- x: 0,
701
- y: 0 - this.recommendOptions.textLine.yOffsetValue,
702
- maxWidth: this.options.textRowMaxWidth || this.options.width
703
- });
704
- resolve(ctx.canvas);
705
- };
706
- WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
707
- var _this = this;
708
- loadImage(this.options.image).then(function (image) {
709
- var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
710
- var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
711
- ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
712
- resolve(ctx.canvas);
713
- });
714
- };
715
- WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
716
- var _this = this;
717
- // image.width = this.options.width
718
- // image.height = this.options.height
719
- // const element = createCustomContentSvg(context, this.options)
720
- // image.src = convertSVGToImage(element)
721
- // image.onload = () => {
722
- // context.translate(this.options.width / 2, this.options.height / 2)
723
- // context.rotate(this.options.rotate)
724
- // context.drawImage(
725
- // image,
726
- // -this.options.width / 2,
727
- // -this.options.height / 2,
728
- // context.canvas.width,
729
- // context.canvas.height
730
- // )
731
- // resolve(canvas)
732
- // }
733
- var lines = this.recommendOptions.textLine.data;
734
- var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
735
- lines.forEach(function (text, index) {
736
- _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
737
- });
738
- resolve(ctx.canvas);
739
- };
740
- WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
741
- return __awaiter(this, void 0, void 0, function () {
742
- var obj;
743
- var _this = this;
744
- return __generator(this, function (_a) {
745
- switch (_a.label) {
746
- case 0: return [4 /*yield*/, createCustomContentSVG(ctx, this.options)];
747
- case 1:
748
- obj = _a.sent();
749
- loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
750
- var imagePosition = _this.getDrawImagePosition(image.width, image.height);
751
- ctx.drawImage(image, imagePosition.x, imagePosition.y, image.width, image.height);
752
- resolve(ctx.canvas);
753
- });
754
- return [2 /*return*/];
755
- }
756
- });
757
- });
758
- };
759
- WatermarkCanvas.prototype.getImageRect = function (image) {
760
- var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
761
- switch (true) {
762
- case rect.width !== 0 && rect.height === 0:
763
- rect.height = rect.width * image.height / image.width;
764
- break;
765
- case rect.width === 0 && rect.height !== 0:
766
- rect.width = rect.height * image.width / image.height;
767
- break;
768
- case rect.width === 0 && rect.height === 0:
769
- rect.width = image.width;
770
- rect.height = image.height;
771
- break;
772
- }
773
- return rect;
774
- };
775
- WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
776
- var _a, _b;
777
- var result = {
778
- x: -imageWidth / 2,
779
- y: -imageHeight / 2
780
- };
781
- switch (this.options.translatePlacement) {
782
- case 'top':
783
- result.x = -imageWidth / 2;
784
- result.y = 0;
785
- break;
786
- case 'top-start':
787
- result.x = 0;
788
- result.y = 0;
789
- break;
790
- case 'top-end':
791
- result.x = -imageWidth;
792
- result.y = 0;
793
- break;
794
- case 'bottom':
795
- result.x = -imageWidth / 2;
796
- result.y = -imageHeight;
797
- break;
798
- case 'bottom-start':
799
- result.x = 0;
800
- result.y = -imageHeight;
801
- break;
802
- case 'bottom-end':
803
- result.x = -imageWidth;
804
- result.y = -imageHeight;
805
- break;
806
- case 'left':
807
- result.x = 0;
808
- result.y = -imageHeight / 2;
809
- break;
810
- case 'right':
811
- result.x = -imageWidth;
812
- result.y = -imageHeight / 2;
813
- break;
814
- }
815
- !isUndefined((_a = this.props) === null || _a === void 0 ? void 0 : _a.translateX) && (result.x = 0);
816
- !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
817
- return result;
818
- };
819
- return WatermarkCanvas;
820
- }());
821
-
822
- var GridLayout = /** @class */ (function () {
823
- function GridLayout(args, partialCanvas) {
824
- var _a, _b, _c, _d;
825
- this.options = args;
826
- this.partialWidth = this.options.width;
827
- this.partialHeight = this.options.height;
828
- this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
829
- this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
830
- this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
831
- this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
832
- this.partialCanvas = partialCanvas;
833
- }
834
- GridLayout.prototype.draw = function () {
835
- var _a, _b, _c, _d, _e, _f, _g, _h;
836
- var layoutCanvas = WatermarkCanvas.createCanvas(((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.width) || (this.partialWidth * this.cols + this.gap[0] * this.cols), ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.height) || (this.partialHeight * this.rows + this.gap[1] * this.rows));
837
- var layoutContext = layoutCanvas.getContext('2d');
838
- if ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.backgroundImage) {
839
- layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.backgroundImage, 0, 0, (_e = this.options.gridLayoutOptions) === null || _e === void 0 ? void 0 : _e.width, (_f = this.options.gridLayoutOptions) === null || _f === void 0 ? void 0 : _f.height);
840
- }
841
- for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
842
- for (var colIndex = 0; colIndex < this.cols; colIndex++) {
843
- if (!((_h = (_g = this.matrix) === null || _g === void 0 ? void 0 : _g[rowIndex]) === null || _h === void 0 ? void 0 : _h[colIndex])) {
844
- continue;
845
- }
846
- 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);
847
- }
848
- }
849
- return layoutCanvas;
850
- };
851
- return GridLayout;
852
- }());
853
-
854
- var renderLayout = function (options, partialCanvas) {
855
- switch (options.layout) {
856
- case 'grid':
857
- return new GridLayout(options, partialCanvas).draw();
858
- default:
859
- return partialCanvas;
860
- }
861
- };
862
- var generateBackgroundSize = function (options) {
863
- var _a, _b, _c;
864
- switch (options.layout) {
865
- case 'grid':
866
- {
867
- var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
868
- var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
869
- var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
870
- return [
871
- options.width * cols + gap[0] * cols,
872
- options.height * rows + gap[1] * rows
873
- ];
874
- }
875
- default:
876
- return [
877
- options.width,
878
- options.height
879
- ];
880
- }
881
- };
882
-
883
- /**
884
- * Watermark class
885
- */
886
- var Watermark = /** @class */ (function () {
887
- /**
888
- * Watermark constructor
889
- * @param args - watermark args
890
- */
891
- function Watermark(args) {
892
- if (args === void 0) { args = {}; }
893
- this.parentElement = document.body;
894
- this.props = args;
895
- this.options = Object.assign({}, initialOptions, args);
896
- this.changeParentElement(this.options.parent);
897
- this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
898
- bootstrap();
899
- }
900
- /**
901
- * Change watermark options
902
- * @param args
903
- * @param mode
904
- * @param redraw
905
- */
906
- Watermark.prototype.changeOptions = function () {
907
- return __awaiter(this, arguments, void 0, function (args, mode, redraw) {
908
- if (args === void 0) { args = {}; }
909
- if (mode === void 0) { mode = 'overwrite'; }
910
- if (redraw === void 0) { redraw = true; }
911
- return __generator(this, function (_a) {
912
- switch (_a.label) {
913
- case 0:
914
- this.initConfigData(args, mode);
915
- if (!redraw) return [3 /*break*/, 2];
916
- this.remove();
917
- return [4 /*yield*/, this.create()];
918
- case 1:
919
- _a.sent();
920
- _a.label = 2;
921
- case 2: return [2 /*return*/];
922
- }
923
- });
924
- });
925
- };
926
- /**
927
- * Creating a watermark.
928
- */
929
- Watermark.prototype.create = function () {
930
- return __awaiter(this, void 0, void 0, function () {
931
- var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
932
- var _a, _b, _c, _d, _e, _f, _g;
933
- return __generator(this, function (_h) {
934
- switch (_h.label) {
935
- case 0:
936
- if (!this.validateUnique()) {
937
- return [2 /*return*/];
938
- }
939
- if (!this.validateContent()) {
940
- return [2 /*return*/];
941
- }
942
- firstDraw = isUndefined(this.watermarkDom);
943
- return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
944
- case 1:
945
- _h.sent();
946
- this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
947
- image = convertImage(this.layoutCanvas);
948
- (_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
949
- this.watermarkDom = document.createElement('div');
950
- watermarkInnerDom = document.createElement('div');
951
- this.watermarkDom.__WATERMARK__ = 'watermark';
952
- this.watermarkDom.__WATERMARK__INSTANCE__ = this;
953
- parentElementType = this.checkParentElementType();
954
- 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 ");
955
- backgroundSize = generateBackgroundSize(this.options);
956
- 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, ";\n ").concat(this.options.movable ? 'animation: 200s ease 0s infinite normal none running watermark !important;' : '', "\n ");
957
- this.watermarkDom.append(watermarkInnerDom);
958
- this.parentElement.appendChild(this.watermarkDom);
959
- if (this.options.mutationObserve) {
960
- try {
961
- this.bindMutationObserve();
962
- }
963
- catch (e) {
964
- (_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
965
- }
966
- }
967
- firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
968
- return [2 /*return*/];
969
- }
970
- });
971
- });
972
- };
973
- /**
974
- * Delete this watermark.
975
- */
976
- Watermark.prototype.destroy = function () {
977
- this.remove();
978
- this.watermarkDom = undefined;
979
- };
980
- Watermark.prototype.check = function () {
981
- return __awaiter(this, void 0, void 0, function () {
982
- return __generator(this, function (_a) {
983
- switch (_a.label) {
984
- case 0:
985
- if (!!this.parentElement.contains(this.watermarkDom)) return [3 /*break*/, 2];
986
- this.remove();
987
- return [4 /*yield*/, this.create()];
988
- case 1:
989
- _a.sent();
990
- _a.label = 2;
991
- case 2: return [2 /*return*/];
992
- }
993
- });
994
- });
995
- };
996
- Watermark.prototype.remove = function () {
997
- var _a, _b, _c, _d, _e, _f, _g;
998
- (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
999
- (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
1000
- (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
1001
- this.unbindCheckWatermarkElementEvent();
1002
- (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
1003
- (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
1004
- };
1005
- Watermark.prototype.initConfigData = function (args, mode) {
1006
- var _this = this;
1007
- if (mode === void 0) { mode = 'overwrite'; }
1008
- if (mode === 'append') {
1009
- Object.keys(args).forEach(function (key) {
1010
- _this.props && (_this.props[key] = args[key]);
1011
- });
1012
- }
1013
- else {
1014
- this.props = args;
1015
- }
1016
- this.options = Object.assign({}, initialOptions, this.props);
1017
- this.changeParentElement(this.options.parent);
1018
- this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1019
- };
1020
- Watermark.prototype.changeParentElement = function (parent) {
1021
- if (typeof parent === 'string') {
1022
- var parentElement = document.querySelector(parent);
1023
- parentElement && (this.parentElement = parentElement);
1024
- }
1025
- else {
1026
- this.parentElement = parent;
1027
- }
1028
- };
1029
- Watermark.prototype.validateUnique = function () {
1030
- var result = true;
1031
- this.parentElement.childNodes.forEach(function (node) {
1032
- if (!result) {
1033
- return;
1034
- }
1035
- if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
1036
- result = false;
1037
- // throw new Error('duplicate watermark error')
1038
- }
1039
- });
1040
- return result;
1041
- };
1042
- Watermark.prototype.validateContent = function () {
1043
- switch (this.options.contentType) {
1044
- case 'image':
1045
- return Object.hasOwnProperty.call(this.options, 'image');
1046
- case 'multi-line-text':
1047
- case 'rich-text':
1048
- case 'text':
1049
- return this.options.content.length > 0;
1050
- }
1051
- return false;
1052
- };
1053
- Watermark.prototype.checkParentElementType = function () {
1054
- if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
1055
- return 'root';
1056
- }
1057
- return 'custom';
1058
- };
1059
- Watermark.prototype.checkWatermarkElement = function () {
1060
- return __awaiter(this, void 0, void 0, function () {
1061
- return __generator(this, function (_a) {
1062
- switch (_a.label) {
1063
- case 0:
1064
- if (!!this.parentElement.contains(this.watermarkDom)) return [3 /*break*/, 2];
1065
- this.remove();
1066
- return [4 /*yield*/, this.create()];
1067
- case 1:
1068
- _a.sent();
1069
- _a.label = 2;
1070
- case 2:
1071
- this.bindCheckWatermarkElementEvent();
1072
- return [2 /*return*/];
1073
- }
1074
- });
1075
- });
1076
- };
1077
- Watermark.prototype.bindMutationObserve = function () {
1078
- var _this = this;
1079
- if (!this.watermarkDom) {
1080
- return;
1081
- }
1082
- this.bindCheckWatermarkElementEvent();
1083
- this.observer = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
1084
- return __generator(this, function (_a) {
1085
- switch (_a.label) {
1086
- case 0:
1087
- if (!(mutationsList.length > 0)) return [3 /*break*/, 2];
1088
- this.remove();
1089
- return [4 /*yield*/, this.create()];
1090
- case 1:
1091
- _a.sent();
1092
- _a.label = 2;
1093
- case 2: return [2 /*return*/];
1094
- }
1095
- });
1096
- }); });
1097
- this.observer.observe(this.watermarkDom, {
1098
- attributes: true, // 属性的变动
1099
- childList: true, // 子节点的变动(指新增,删除或者更改)
1100
- subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
1101
- characterData: true // 节点内容或节点文本的变动。
1102
- });
1103
- this.parentObserve = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
1104
- var _i, mutationsList_1, item;
1105
- var _a;
1106
- return __generator(this, function (_b) {
1107
- switch (_b.label) {
1108
- case 0:
1109
- _i = 0, mutationsList_1 = mutationsList;
1110
- _b.label = 1;
1111
- case 1:
1112
- if (!(_i < mutationsList_1.length)) return [3 /*break*/, 4];
1113
- item = mutationsList_1[_i];
1114
- if (!((item === null || item === void 0 ? void 0 : item.target) === this.watermarkDom ||
1115
- ((_a = item === null || item === void 0 ? void 0 : item.removedNodes) === null || _a === void 0 ? void 0 : _a[0]) === this.watermarkDom ||
1116
- (item.type === 'childList' && item.target === this.parentElement && item.target.lastChild !== this.watermarkDom))) return [3 /*break*/, 3];
1117
- this.remove();
1118
- return [4 /*yield*/, this.create()];
1119
- case 2:
1120
- _b.sent();
1121
- _b.label = 3;
1122
- case 3:
1123
- _i++;
1124
- return [3 /*break*/, 1];
1125
- case 4: return [2 /*return*/];
1126
- }
1127
- });
1128
- }); });
1129
- this.parentObserve.observe(this.parentElement, {
1130
- attributes: true, // 属性的变动
1131
- childList: true, // 子节点的变动(指新增,删除或者更改)
1132
- subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
1133
- characterData: true // 节点内容或节点文本的变动。
1134
- });
1135
- };
1136
- Watermark.prototype.bindCheckWatermarkElementEvent = function () {
1137
- this.unbindCheckWatermarkElementEvent();
1138
- this.checkWatermarkElementRequestID = requestAnimationFrame(this.checkWatermarkElement.bind(this));
1139
- };
1140
- Watermark.prototype.unbindCheckWatermarkElementEvent = function () {
1141
- if (!isUndefined(this.checkWatermarkElementRequestID)) {
1142
- cancelAnimationFrame(this.checkWatermarkElementRequestID);
1143
- }
1144
- };
1145
- return Watermark;
1146
- }());
119
+ var convertImage = function (canvas) {
120
+ return canvas.toDataURL('image/png', 1);
121
+ };
122
+ var isFunction = function (value) {
123
+ return typeof value === 'function';
124
+ };
125
+ var isUndefined = function (value) {
126
+ return value === undefined;
127
+ };
128
+ var isString = function (value) {
129
+ return typeof value === 'string';
130
+ };
131
+ var createSVGElement = function (tagName, attrs, namespaceURI) {
132
+ if (attrs === void 0) { attrs = {}; }
133
+ if (namespaceURI === void 0) { namespaceURI = 'http://www.w3.org/2000/svg'; }
134
+ var element = document.createElementNS(namespaceURI, tagName);
135
+ for (var attr in attrs) {
136
+ element.setAttribute(attr, attrs[attr]);
137
+ }
138
+ return element;
139
+ };
140
+ var getMultiLineData = function (ctx, text, maxWidth) {
141
+ var result = [];
142
+ var str = '';
143
+ for (var i = 0, len = text.length; i < len; i++) {
144
+ str += text.charAt(i);
145
+ if (ctx.measureText(str).width > maxWidth) {
146
+ result.push(str.substring(0, str.length - 1));
147
+ str = '';
148
+ i--;
149
+ }
150
+ }
151
+ result.push(str);
152
+ return result;
153
+ };
154
+ var createCustomContentSVG = function (ctx, options) { return __awaiter(void 0, void 0, void 0, function () {
155
+ var svgElement, bodyElement, _a, offsetHeight, offsetWidth, width, height, foreignObjectElement;
156
+ return __generator(this, function (_b) {
157
+ switch (_b.label) {
158
+ case 0:
159
+ svgElement = createSVGElement('svg', {
160
+ xmlns: 'http://www.w3.org/2000/svg'
161
+ });
162
+ bodyElement = document.createElement('div');
163
+ bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
164
+ 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");
165
+ bodyElement.innerHTML = "<div class='rich-text-content'>".concat(options.content, "</div>");
166
+ document.body.appendChild(bodyElement);
167
+ // convert all images to base64
168
+ return [4 /*yield*/, convertImgToBase64(bodyElement)];
169
+ case 1:
170
+ // convert all images to base64
171
+ _b.sent();
172
+ _a = bodyElement.querySelector('.rich-text-content'), offsetHeight = _a.offsetHeight, offsetWidth = _a.offsetWidth;
173
+ document.body.removeChild(bodyElement);
174
+ width = options.richTextWidth || offsetWidth || options.width;
175
+ height = options.richTextHeight || offsetHeight || options.height;
176
+ svgElement.setAttribute('width', width.toString());
177
+ svgElement.setAttribute('height', height.toString());
178
+ foreignObjectElement = createSVGElement('foreignObject', {
179
+ width: width.toString(),
180
+ height: height.toString()
181
+ });
182
+ foreignObjectElement.appendChild(bodyElement);
183
+ svgElement.appendChild(foreignObjectElement);
184
+ return [2 /*return*/, {
185
+ element: svgElement,
186
+ width: width,
187
+ height: height
188
+ }];
189
+ }
190
+ });
191
+ }); };
192
+ function convertImgToBase64(bodyElement) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var imgElements, _loop_1, _i, _a, img;
195
+ return __generator(this, function (_b) {
196
+ switch (_b.label) {
197
+ case 0:
198
+ imgElements = bodyElement.querySelectorAll('img');
199
+ _loop_1 = function (img) {
200
+ var src, response, blob_1, imgData, error_1;
201
+ return __generator(this, function (_c) {
202
+ switch (_c.label) {
203
+ case 0:
204
+ src = img.getAttribute('src');
205
+ if (!src) return [3 /*break*/, 6];
206
+ _c.label = 1;
207
+ case 1:
208
+ _c.trys.push([1, 5, , 6]);
209
+ return [4 /*yield*/, fetch(src)];
210
+ case 2:
211
+ response = _c.sent();
212
+ return [4 /*yield*/, response.blob()];
213
+ case 3:
214
+ blob_1 = _c.sent();
215
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
216
+ var reader = new FileReader();
217
+ reader.onloadend = function () { return resolve(reader.result); };
218
+ reader.onerror = reject;
219
+ reader.readAsDataURL(blob_1);
220
+ })];
221
+ case 4:
222
+ imgData = _c.sent();
223
+ if (isString(imgData)) {
224
+ img.setAttribute('src', imgData);
225
+ }
226
+ return [3 /*break*/, 6];
227
+ case 5:
228
+ error_1 = _c.sent();
229
+ console.error("Error converting ".concat(src, " to base64:"), error_1);
230
+ return [3 /*break*/, 6];
231
+ case 6: return [2 /*return*/];
232
+ }
233
+ });
234
+ };
235
+ _i = 0, _a = Array.from(imgElements);
236
+ _b.label = 1;
237
+ case 1:
238
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
239
+ img = _a[_i];
240
+ return [5 /*yield**/, _loop_1(img)];
241
+ case 2:
242
+ _b.sent();
243
+ _b.label = 3;
244
+ case 3:
245
+ _i++;
246
+ return [3 /*break*/, 1];
247
+ case 4: return [2 /*return*/];
248
+ }
249
+ });
250
+ });
251
+ }
252
+ var convertSVGToImage = function (svg) {
253
+ var richContent = svg.outerHTML.replace(/<img(.*?)>/g, '<img$1/>').replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
254
+ return "data:image/svg+xml;charset=utf-8,".concat(richContent);
255
+ };
256
+ var getValue = function (v1, v2) {
257
+ if (isUndefined(v1)) {
258
+ return v2;
259
+ }
260
+ else {
261
+ return v1;
262
+ }
263
+ };
264
+ var loadImage = function (url, width, height) {
265
+ if (width === void 0) { width = undefined; }
266
+ if (height === void 0) { height = undefined; }
267
+ var image = new Image();
268
+ image.setAttribute('crossOrigin', 'Anonymous');
269
+ !isUndefined(width) && (image.width = width);
270
+ !isUndefined(height) && (image.height = height);
271
+ image.src = url;
272
+ return new Promise(function (resolve) {
273
+ image.onload = function () {
274
+ resolve(image);
275
+ };
276
+ });
277
+ };
278
+ var generateMatrix = function (rows, columns, value) {
279
+ return Array.from({ length: rows }, function () { return new Array(columns).fill(value); });
280
+ };
1147
281
 
1148
- /**
1149
- * BlindWatermark class
1150
- */
1151
- var BlindWatermark = /** @class */ (function (_super) {
1152
- __extends(BlindWatermark, _super);
1153
- /**
1154
- * BlindWatermark constructor
1155
- * @param props - blind watermark options
1156
- */
1157
- function BlindWatermark(props) {
1158
- if (props === void 0) { props = {}; }
1159
- var defaultProps = {
1160
- globalAlpha: 0.005,
1161
- mode: 'blind'
1162
- };
1163
- return _super.call(this, __assign(__assign({}, props), defaultProps)) || this;
1164
- }
1165
- /**
1166
- * Decode blind watermark.
1167
- * @param props - decode options
1168
- */
1169
- BlindWatermark.decode = function (props) {
1170
- var _a = props.url, url = _a === void 0 ? '' : _a, _b = props.fillColor, fillColor = _b === void 0 ? '#000' : _b, _c = props.compositeOperation, compositeOperation = _c === void 0 ? 'color-burn' : _c, _d = props.mode, mode = _d === void 0 ? 'canvas' : _d, _e = props.compositeTimes, compositeTimes = _e === void 0 ? 3 : _e, onSuccess = props.onSuccess;
1171
- if (!url) {
1172
- return;
1173
- }
1174
- if (mode === 'canvas') {
1175
- var img_1 = new Image();
1176
- img_1.src = url;
1177
- img_1.addEventListener('load', function () {
1178
- var width = img_1.width, height = img_1.height;
1179
- var canvas = WatermarkCanvas.createCanvas(width, height);
1180
- var ctx = canvas.getContext('2d');
1181
- if (!ctx) {
1182
- throw new Error('get context error');
1183
- }
1184
- ctx.drawImage(img_1, 0, 0, width, height);
1185
- ctx.globalCompositeOperation = compositeOperation;
1186
- ctx.fillStyle = fillColor;
1187
- for (var i = 0; i < compositeTimes; i++) {
1188
- ctx.fillRect(0, 0, width, height);
1189
- }
1190
- var resultImage = convertImage(canvas);
1191
- if (isFunction(onSuccess)) {
1192
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(resultImage);
1193
- }
1194
- });
1195
- }
1196
- };
1197
- return BlindWatermark;
1198
- }(Watermark));
282
+ var initialOptions = {
283
+ width: 300,
284
+ height: 300,
285
+ rotate: 45,
286
+ layout: 'default',
287
+ auxiliaryLine: false,
288
+ translatePlacement: 'middle',
289
+ contentType: 'text',
290
+ content: 'hello watermark-js-plus',
291
+ textType: 'fill',
292
+ imageWidth: 0,
293
+ imageHeight: 0,
294
+ lineHeight: 30,
295
+ zIndex: 2147483647,
296
+ backgroundPosition: '0 0',
297
+ backgroundRepeat: 'repeat',
298
+ fontSize: '20px',
299
+ fontFamily: 'sans-serif',
300
+ fontStyle: '',
301
+ fontVariant: '',
302
+ fontColor: '#000',
303
+ fontWeight: 'normal',
304
+ filter: 'none',
305
+ letterSpacing: '0px',
306
+ globalAlpha: 0.5,
307
+ mode: 'default',
308
+ mutationObserve: true,
309
+ movable: false,
310
+ parent: 'body',
311
+ onSuccess: function () { },
312
+ onBeforeDestroy: function () { },
313
+ onDestroyed: function () { },
314
+ onObserveError: function () { }
315
+ };
316
+ var generateRecommendOptions = function (canvas, options, args) {
317
+ var ctx = canvas.getContext('2d');
318
+ if (ctx === null) {
319
+ throw new Error('get context error');
320
+ }
321
+ ctx.font = "".concat(options.fontStyle, " ").concat(options.fontVariant, " ").concat(options.fontWeight, " ").concat(options.fontSize, " ").concat(options.fontFamily);
322
+ ctx.filter = options.filter;
323
+ // @ts-ignore
324
+ ctx.letterSpacing = options.letterSpacing;
325
+ if (options === null || options === void 0 ? void 0 : options.rotate) {
326
+ options.rotate = (360 - options.rotate % 360) * (Math.PI / 180);
327
+ }
328
+ if (isUndefined(args.textRowMaxWidth)) {
329
+ options.textRowMaxWidth = options.width;
330
+ }
331
+ var result = {
332
+ image: {
333
+ rect: {
334
+ width: options.imageWidth,
335
+ height: options.imageHeight
336
+ },
337
+ position: {
338
+ x: 0,
339
+ y: 0
340
+ }
341
+ },
342
+ textLine: {
343
+ data: [],
344
+ yOffsetValue: 0
345
+ },
346
+ advancedStyleParams: {
347
+ linear: {
348
+ x0: 0,
349
+ x1: 0
350
+ },
351
+ radial: {
352
+ x0: 0,
353
+ y0: 0,
354
+ r0: 0,
355
+ x1: 0,
356
+ y1: 0,
357
+ r1: 0
358
+ },
359
+ conic: {
360
+ x: 0,
361
+ y: 0,
362
+ startAngle: 0
363
+ },
364
+ pattern: {}
365
+ }
366
+ };
367
+ switch (options.contentType) {
368
+ case 'text':
369
+ result.textLine.data = [options.content];
370
+ break;
371
+ case 'multi-line-text':
372
+ result.textLine.data = getMultiLineData(ctx, options.content, options.textRowMaxWidth);
373
+ break;
374
+ // case 'image':
375
+ // break
376
+ // case 'rich-text':
377
+ // break
378
+ }
379
+ var translateX = options.width / 2;
380
+ var translateY = options.height / 2;
381
+ var textBaseline = 'middle';
382
+ var textAlign = 'center';
383
+ if (!isUndefined(args === null || args === void 0 ? void 0 : args.translateX) && !isUndefined(args === null || args === void 0 ? void 0 : args.translateY)) {
384
+ translateX = args === null || args === void 0 ? void 0 : args.translateX;
385
+ translateY = args === null || args === void 0 ? void 0 : args.translateY;
386
+ textBaseline = 'top';
387
+ textAlign = 'left';
388
+ }
389
+ else {
390
+ // default middle
391
+ // translateX = options.width / 2
392
+ // translateY = options.height / 2
393
+ // TextBaselineType = 'middle'
394
+ // textAlign = 'center'
395
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
396
+ result.advancedStyleParams.linear.x1 = options.width / 2;
397
+ // result.advancedStyleParams.radial.x0 = 0
398
+ // result.advancedStyleParams.radial.y0 = 0
399
+ result.advancedStyleParams.radial.r0 = 0;
400
+ // result.advancedStyleParams.radial.x1 = 0
401
+ // result.advancedStyleParams.radial.y1 = 0
402
+ result.advancedStyleParams.radial.r1 = options.width / 2;
403
+ // result.advancedStyleParams.conic.x = 0
404
+ // result.advancedStyleParams.conic.y = 0
405
+ }
406
+ switch (args.translatePlacement) {
407
+ case 'top':
408
+ translateX = options.width / 2;
409
+ translateY = 0;
410
+ textBaseline = 'top';
411
+ // textAlign = 'center'
412
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
413
+ result.advancedStyleParams.linear.x1 = options.width / 2;
414
+ // result.advancedStyleParams.radial.x0 = 0
415
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
416
+ // result.advancedStyleParams.radial.r0 = 0
417
+ // result.advancedStyleParams.radial.x1 = 0
418
+ // result.advancedStyleParams.radial.y1 = 0
419
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
420
+ // result.advancedStyleParams.radial.r1 = options.width / 2
421
+ // result.advancedStyleParams.conic.x = 0
422
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
423
+ break;
424
+ case 'top-start':
425
+ translateX = 0;
426
+ translateY = 0;
427
+ textBaseline = 'top';
428
+ textAlign = 'start';
429
+ result.advancedStyleParams.linear.x0 = 0;
430
+ result.advancedStyleParams.linear.x1 = options.width;
431
+ result.advancedStyleParams.radial.x0 = options.width / 2;
432
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
433
+ // result.advancedStyleParams.radial.r0 = 0
434
+ result.advancedStyleParams.radial.x1 = options.width / 2;
435
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
436
+ // result.advancedStyleParams.radial.r1 = options.width / 2
437
+ result.advancedStyleParams.conic.x = options.width / 2;
438
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
439
+ break;
440
+ case 'top-end':
441
+ translateX = options.width;
442
+ translateY = 0;
443
+ textBaseline = 'top';
444
+ textAlign = 'end';
445
+ result.advancedStyleParams.linear.x0 = 0;
446
+ result.advancedStyleParams.linear.x1 = -options.width;
447
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
448
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
449
+ // result.advancedStyleParams.radial.r0 = 0
450
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
451
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
452
+ // result.advancedStyleParams.radial.r1 = options.width / 2
453
+ result.advancedStyleParams.conic.x = -options.width / 2;
454
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
455
+ break;
456
+ case 'bottom':
457
+ translateX = options.width / 2;
458
+ translateY = options.height;
459
+ textBaseline = 'bottom';
460
+ // textAlign = 'center'
461
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
462
+ result.advancedStyleParams.linear.x1 = options.width / 2;
463
+ // result.advancedStyleParams.radial.x0 = 0
464
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
465
+ // result.advancedStyleParams.radial.r0 = 0
466
+ // result.advancedStyleParams.radial.x1 = 0
467
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
468
+ // result.advancedStyleParams.radial.r1 = options.width / 2
469
+ result.advancedStyleParams.conic.x = 0;
470
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
471
+ break;
472
+ case 'bottom-start':
473
+ translateX = 0;
474
+ translateY = options.height;
475
+ textBaseline = 'bottom';
476
+ textAlign = 'start';
477
+ result.advancedStyleParams.linear.x0 = 0;
478
+ result.advancedStyleParams.linear.x1 = options.width;
479
+ result.advancedStyleParams.radial.x0 = options.width / 2;
480
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
481
+ // result.advancedStyleParams.radial.r0 = 0
482
+ result.advancedStyleParams.radial.x1 = options.width / 2;
483
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
484
+ // result.advancedStyleParams.radial.r1 = options.width / 2
485
+ result.advancedStyleParams.conic.x = options.width / 2;
486
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
487
+ break;
488
+ case 'bottom-end':
489
+ translateX = options.width;
490
+ translateY = options.height;
491
+ textBaseline = 'bottom';
492
+ textAlign = 'end';
493
+ result.advancedStyleParams.linear.x0 = 0;
494
+ result.advancedStyleParams.linear.x1 = -options.width;
495
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
496
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
497
+ // result.advancedStyleParams.radial.r0 = 0
498
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
499
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
500
+ // result.advancedStyleParams.radial.r1 = options.width / 2
501
+ result.advancedStyleParams.conic.x = -options.width / 2;
502
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
503
+ break;
504
+ case 'left':
505
+ translateX = 0;
506
+ translateY = options.height / 2;
507
+ // TextBaselineType = 'middle'
508
+ textAlign = 'start';
509
+ result.advancedStyleParams.linear.x0 = 0;
510
+ result.advancedStyleParams.linear.x1 = options.width;
511
+ result.advancedStyleParams.radial.x0 = options.width / 2;
512
+ // result.advancedStyleParams.radial.y0 = 0
513
+ // result.advancedStyleParams.radial.r0 = 0
514
+ result.advancedStyleParams.radial.x1 = options.width / 2;
515
+ // result.advancedStyleParams.radial.y1 = 0
516
+ // result.advancedStyleParams.radial.r1 = options.width / 2
517
+ result.advancedStyleParams.conic.x = options.width / 2;
518
+ result.advancedStyleParams.conic.y = 0;
519
+ break;
520
+ case 'right':
521
+ translateX = options.width;
522
+ translateY = options.height / 2;
523
+ // TextBaselineType = 'middle'
524
+ textAlign = 'end';
525
+ result.advancedStyleParams.linear.x0 = 0;
526
+ result.advancedStyleParams.linear.x1 = -options.width;
527
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
528
+ // result.advancedStyleParams.radial.y0 = 0
529
+ // result.advancedStyleParams.radial.r0 = 0
530
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
531
+ // result.advancedStyleParams.radial.y1 = 0
532
+ // result.advancedStyleParams.radial.r1 = options.width / 2
533
+ result.advancedStyleParams.conic.x = -options.width / 2;
534
+ result.advancedStyleParams.conic.y = 0;
535
+ break;
536
+ }
537
+ options.translateX = translateX;
538
+ options.translateY = translateY;
539
+ isUndefined(args === null || args === void 0 ? void 0 : args.textBaseline) && (options.textBaseline = textBaseline);
540
+ isUndefined(args === null || args === void 0 ? void 0 : args.textAlign) && (options.textAlign = textAlign);
541
+ if (['text', 'multi-line-text'].includes(options.contentType)) {
542
+ switch (options.textBaseline) {
543
+ case 'middle':
544
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight / 2;
545
+ break;
546
+ case 'bottom':
547
+ case 'alphabetic':
548
+ case 'ideographic':
549
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight + (options.lineHeight - parseInt(options.fontSize)) / 2;
550
+ break;
551
+ case 'top':
552
+ case 'hanging':
553
+ result.textLine.yOffsetValue = -options.lineHeight / 2 + parseInt(options.fontSize) / 2;
554
+ break;
555
+ }
556
+ }
557
+ return result;
558
+ };
1199
559
 
1200
- /**
1201
- * ImageWatermark class
1202
- */
1203
- var ImageWatermark = /** @class */ (function () {
1204
- /**
1205
- * ImageWatermark constructor
1206
- * @param args - image watermark args
1207
- */
1208
- function ImageWatermark(args) {
1209
- if (args === void 0) { args = {}; }
1210
- var _a;
1211
- this.drew = false;
1212
- this.props = args;
1213
- this.options = Object.assign({}, initialOptions, args);
1214
- this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1215
- this.originalSrc = (_a = this.props.dom) === null || _a === void 0 ? void 0 : _a.src;
1216
- this.backgroundImage = this.getBackgroundImage();
1217
- }
1218
- ImageWatermark.prototype.create = function () {
1219
- return __awaiter(this, void 0, void 0, function () {
1220
- var _a, _b, _c, _d, _e;
1221
- return __generator(this, function (_f) {
1222
- switch (_f.label) {
1223
- case 0:
1224
- if (this.drew) {
1225
- return [2 /*return*/];
1226
- }
1227
- return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
1228
- case 1:
1229
- _f.sent();
1230
- this.options.layout = 'grid';
1231
- this.options.gridLayoutOptions = __assign(__assign({}, this.options.gridLayoutOptions), { width: (_b = this.backgroundImage) === null || _b === void 0 ? void 0 : _b.width, height: (_c = this.backgroundImage) === null || _c === void 0 ? void 0 : _c.height, backgroundImage: this.backgroundImage });
1232
- this.layoutCanvas = renderLayout(this.options, (_d = this.watermarkCanvas) === null || _d === void 0 ? void 0 : _d.getCanvas());
1233
- this.options.dom.src = convertImage(this.layoutCanvas);
1234
- (_e = this.watermarkCanvas) === null || _e === void 0 ? void 0 : _e.clear();
1235
- this.drew = true;
1236
- return [2 /*return*/];
1237
- }
1238
- });
1239
- });
1240
- };
1241
- ImageWatermark.prototype.destroy = function () {
1242
- this.options.dom.src = this.originalSrc;
1243
- this.drew = false;
1244
- };
1245
- ImageWatermark.prototype.getBackgroundImage = function () {
1246
- if (this.options.dom) {
1247
- return this.options.dom;
1248
- }
1249
- };
1250
- return ImageWatermark;
1251
- }());
560
+ var bootstrap = (function () {
561
+ if (typeof window !== 'undefined') {
562
+ Object.defineProperty(window, 'MutationObserver', {
563
+ writable: false,
564
+ configurable: false
565
+ });
566
+ Object.defineProperty(window, 'requestAnimationFrame', {
567
+ writable: false,
568
+ configurable: false
569
+ });
570
+ }
571
+ });
1252
572
 
1253
- function styleInject(css, ref) {
1254
- if ( ref === void 0 ) ref = {};
1255
- var insertAt = ref.insertAt;
573
+ var WatermarkCanvas = /** @class */ (function () {
574
+ function WatermarkCanvas(args, options) {
575
+ this.props = args;
576
+ this.options = options;
577
+ this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
578
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
579
+ }
580
+ /**
581
+ * Create an HD canvas.
582
+ * @param width - width of canvas
583
+ * @param height - height of canvas
584
+ */
585
+ WatermarkCanvas.createCanvas = function (width, height) {
586
+ var _a;
587
+ var ratio = window.devicePixelRatio || 1;
588
+ var canvas = document.createElement('canvas');
589
+ canvas.width = width * ratio; // actual rendered pixel
590
+ canvas.height = height * ratio; // actual rendered pixel
591
+ canvas.style.width = "".concat(width, "px"); // control display size
592
+ canvas.style.height = "".concat(height, "px"); // control display size
593
+ (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
594
+ return canvas;
595
+ };
596
+ /**
597
+ * Clean the canvas
598
+ * @param canvas
599
+ */
600
+ WatermarkCanvas.clearCanvas = function (canvas) {
601
+ var ctx = canvas.getContext('2d');
602
+ if (ctx === null) {
603
+ throw new Error('get context error');
604
+ }
605
+ ctx.restore();
606
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
607
+ };
608
+ WatermarkCanvas.prototype.getCanvas = function () {
609
+ return this.canvas;
610
+ };
611
+ WatermarkCanvas.prototype.clear = function () {
612
+ WatermarkCanvas.clearCanvas(this.canvas);
613
+ };
614
+ WatermarkCanvas.prototype.draw = function () {
615
+ var _this = this;
616
+ var ctx = this.canvas.getContext('2d');
617
+ if (ctx === null) {
618
+ throw new Error('get context error');
619
+ }
620
+ if (this.options.auxiliaryLine) {
621
+ ctx.beginPath();
622
+ ctx.rect(0, 0, this.options.width, this.options.height);
623
+ ctx.lineWidth = 1;
624
+ ctx.strokeStyle = '#000';
625
+ ctx.stroke();
626
+ ctx.closePath();
627
+ ctx.beginPath();
628
+ ctx.rect(this.options.translateX, this.options.translateY, 1, 1);
629
+ ctx.lineWidth = 1;
630
+ ctx.strokeStyle = '#f00';
631
+ ctx.stroke();
632
+ ctx.closePath();
633
+ }
634
+ this.setStyle(ctx);
635
+ ctx.save();
636
+ ctx.translate(this.options.translateX, this.options.translateY);
637
+ ctx.rotate(this.options.rotate);
638
+ return new Promise(function (resolve) {
639
+ switch (_this.options.contentType) {
640
+ case 'text':
641
+ _this.drawText(ctx, resolve);
642
+ break;
643
+ case 'image':
644
+ _this.drawImage(ctx, resolve);
645
+ break;
646
+ case 'multi-line-text':
647
+ _this.drawMultiLineText(ctx, resolve);
648
+ break;
649
+ case 'rich-text':
650
+ _this.drawRichText(ctx, resolve);
651
+ break;
652
+ }
653
+ });
654
+ };
655
+ WatermarkCanvas.prototype.setStyle = function (ctx) {
656
+ var _a;
657
+ var propName = 'fillStyle';
658
+ if (this.options.textType === 'stroke') {
659
+ propName = 'strokeStyle';
660
+ }
661
+ var style = this.options.fontColor;
662
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) {
663
+ switch (this.options.advancedStyle.type) {
664
+ case 'linear':
665
+ style = this.createLinearGradient(ctx);
666
+ break;
667
+ case 'radial':
668
+ style = this.createRadialGradient(ctx);
669
+ break;
670
+ case 'conic':
671
+ style = this.createConicGradient(ctx);
672
+ break;
673
+ case 'pattern':
674
+ style = this.createPattern(ctx);
675
+ break;
676
+ }
677
+ }
678
+ ctx[propName] && style && (ctx[propName] = style);
679
+ this.options.textAlign && (ctx.textAlign = this.options.textAlign);
680
+ this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
681
+ ctx.globalAlpha = this.options.globalAlpha;
682
+ if (this.options.shadowStyle) {
683
+ ctx.shadowBlur = getValue(this.options.shadowStyle.shadowBlur, 0);
684
+ ctx.shadowColor = getValue(this.options.shadowStyle.shadowColor, '#00000000');
685
+ ctx.shadowOffsetX = getValue(this.options.shadowStyle.shadowOffsetX, 0);
686
+ ctx.shadowOffsetY = getValue(this.options.shadowStyle.shadowOffsetY, 0);
687
+ }
688
+ if (isFunction(this.options.extraDrawFunc)) {
689
+ this.options.extraDrawFunc(ctx);
690
+ }
691
+ };
692
+ WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
693
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
694
+ 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));
695
+ (_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) {
696
+ gradient.addColorStop(item.offset, item.color);
697
+ });
698
+ return gradient;
699
+ };
700
+ WatermarkCanvas.prototype.createConicGradient = function (ctx) {
701
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
702
+ 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));
703
+ (_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) {
704
+ gradient.addColorStop(item.offset, item.color);
705
+ });
706
+ return gradient;
707
+ };
708
+ WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
709
+ 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;
710
+ 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));
711
+ (_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) {
712
+ gradient.addColorStop(item.offset, item.color);
713
+ });
714
+ return gradient;
715
+ };
716
+ WatermarkCanvas.prototype.createPattern = function (ctx) {
717
+ var _a, _b, _c, _d, _e, _f, _g, _h;
718
+ 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) || '');
719
+ };
720
+ WatermarkCanvas.prototype.setText = function (ctx, params) {
721
+ var methodName = 'fillText';
722
+ if (this.options.textType === 'stroke') {
723
+ methodName = 'strokeText';
724
+ }
725
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
726
+ };
727
+ WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
728
+ this.setText(ctx, {
729
+ text: this.options.content,
730
+ x: 0,
731
+ y: 0 - this.recommendOptions.textLine.yOffsetValue,
732
+ maxWidth: this.options.textRowMaxWidth || this.options.width
733
+ });
734
+ resolve(ctx.canvas);
735
+ };
736
+ WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
737
+ var _this = this;
738
+ loadImage(this.options.image).then(function (image) {
739
+ var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
740
+ var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
741
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
742
+ resolve(ctx.canvas);
743
+ });
744
+ };
745
+ WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
746
+ var _this = this;
747
+ // image.width = this.options.width
748
+ // image.height = this.options.height
749
+ // const element = createCustomContentSvg(context, this.options)
750
+ // image.src = convertSVGToImage(element)
751
+ // image.onload = () => {
752
+ // context.translate(this.options.width / 2, this.options.height / 2)
753
+ // context.rotate(this.options.rotate)
754
+ // context.drawImage(
755
+ // image,
756
+ // -this.options.width / 2,
757
+ // -this.options.height / 2,
758
+ // context.canvas.width,
759
+ // context.canvas.height
760
+ // )
761
+ // resolve(canvas)
762
+ // }
763
+ var lines = this.recommendOptions.textLine.data;
764
+ var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
765
+ lines.forEach(function (text, index) {
766
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
767
+ });
768
+ resolve(ctx.canvas);
769
+ };
770
+ WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
771
+ return __awaiter(this, void 0, void 0, function () {
772
+ var obj;
773
+ var _this = this;
774
+ return __generator(this, function (_a) {
775
+ switch (_a.label) {
776
+ case 0: return [4 /*yield*/, createCustomContentSVG(ctx, this.options)];
777
+ case 1:
778
+ obj = _a.sent();
779
+ loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
780
+ var imagePosition = _this.getDrawImagePosition(image.width, image.height);
781
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, image.width, image.height);
782
+ resolve(ctx.canvas);
783
+ });
784
+ return [2 /*return*/];
785
+ }
786
+ });
787
+ });
788
+ };
789
+ WatermarkCanvas.prototype.getImageRect = function (image) {
790
+ var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
791
+ switch (true) {
792
+ case rect.width !== 0 && rect.height === 0:
793
+ rect.height = rect.width * image.height / image.width;
794
+ break;
795
+ case rect.width === 0 && rect.height !== 0:
796
+ rect.width = rect.height * image.width / image.height;
797
+ break;
798
+ case rect.width === 0 && rect.height === 0:
799
+ rect.width = image.width;
800
+ rect.height = image.height;
801
+ break;
802
+ }
803
+ return rect;
804
+ };
805
+ WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
806
+ var _a, _b;
807
+ var result = {
808
+ x: -imageWidth / 2,
809
+ y: -imageHeight / 2
810
+ };
811
+ switch (this.options.translatePlacement) {
812
+ case 'top':
813
+ result.x = -imageWidth / 2;
814
+ result.y = 0;
815
+ break;
816
+ case 'top-start':
817
+ result.x = 0;
818
+ result.y = 0;
819
+ break;
820
+ case 'top-end':
821
+ result.x = -imageWidth;
822
+ result.y = 0;
823
+ break;
824
+ case 'bottom':
825
+ result.x = -imageWidth / 2;
826
+ result.y = -imageHeight;
827
+ break;
828
+ case 'bottom-start':
829
+ result.x = 0;
830
+ result.y = -imageHeight;
831
+ break;
832
+ case 'bottom-end':
833
+ result.x = -imageWidth;
834
+ result.y = -imageHeight;
835
+ break;
836
+ case 'left':
837
+ result.x = 0;
838
+ result.y = -imageHeight / 2;
839
+ break;
840
+ case 'right':
841
+ result.x = -imageWidth;
842
+ result.y = -imageHeight / 2;
843
+ break;
844
+ }
845
+ !isUndefined((_a = this.props) === null || _a === void 0 ? void 0 : _a.translateX) && (result.x = 0);
846
+ !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
847
+ return result;
848
+ };
849
+ return WatermarkCanvas;
850
+ }());
1256
851
 
1257
- if (typeof document === 'undefined') { return; }
852
+ var GridLayout = /** @class */ (function () {
853
+ function GridLayout(args, partialCanvas) {
854
+ var _a, _b, _c, _d;
855
+ this.options = args;
856
+ this.partialWidth = this.options.width;
857
+ this.partialHeight = this.options.height;
858
+ this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
859
+ this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
860
+ this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
861
+ this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
862
+ this.partialCanvas = partialCanvas;
863
+ }
864
+ GridLayout.prototype.draw = function () {
865
+ var _a, _b, _c, _d, _e, _f, _g, _h;
866
+ var layoutCanvas = WatermarkCanvas.createCanvas(((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.width) || (this.partialWidth * this.cols + this.gap[0] * this.cols), ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.height) || (this.partialHeight * this.rows + this.gap[1] * this.rows));
867
+ var layoutContext = layoutCanvas.getContext('2d');
868
+ if ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.backgroundImage) {
869
+ layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.backgroundImage, 0, 0, (_e = this.options.gridLayoutOptions) === null || _e === void 0 ? void 0 : _e.width, (_f = this.options.gridLayoutOptions) === null || _f === void 0 ? void 0 : _f.height);
870
+ }
871
+ for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
872
+ for (var colIndex = 0; colIndex < this.cols; colIndex++) {
873
+ if (!((_h = (_g = this.matrix) === null || _g === void 0 ? void 0 : _g[rowIndex]) === null || _h === void 0 ? void 0 : _h[colIndex])) {
874
+ continue;
875
+ }
876
+ 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);
877
+ }
878
+ }
879
+ return layoutCanvas;
880
+ };
881
+ return GridLayout;
882
+ }());
1258
883
 
1259
- var head = document.head || document.getElementsByTagName('head')[0];
1260
- var style = document.createElement('style');
1261
- style.type = 'text/css';
884
+ var renderLayout = function (options, partialCanvas) {
885
+ switch (options.layout) {
886
+ case 'grid':
887
+ return new GridLayout(options, partialCanvas).draw();
888
+ default:
889
+ return partialCanvas;
890
+ }
891
+ };
892
+ var generateBackgroundSize = function (options) {
893
+ var _a, _b, _c;
894
+ switch (options.layout) {
895
+ case 'grid':
896
+ {
897
+ var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
898
+ var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
899
+ var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
900
+ return [
901
+ options.width * cols + gap[0] * cols,
902
+ options.height * rows + gap[1] * rows
903
+ ];
904
+ }
905
+ default:
906
+ return [
907
+ options.width,
908
+ options.height
909
+ ];
910
+ }
911
+ };
1262
912
 
1263
- if (insertAt === 'top') {
1264
- if (head.firstChild) {
1265
- head.insertBefore(style, head.firstChild);
1266
- } else {
1267
- head.appendChild(style);
1268
- }
1269
- } else {
1270
- head.appendChild(style);
913
+ /**
914
+ * Watermark class
915
+ */
916
+ var Watermark = /** @class */ (function () {
917
+ /**
918
+ * Watermark constructor
919
+ * @param args - watermark args
920
+ */
921
+ function Watermark(args) {
922
+ if (args === void 0) { args = {}; }
923
+ this.parentElement = document.body;
924
+ this.props = args;
925
+ this.options = Object.assign({}, initialOptions, args);
926
+ this.changeParentElement(this.options.parent);
927
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
928
+ bootstrap();
1271
929
  }
930
+ /**
931
+ * Change watermark options
932
+ * @param args
933
+ * @param mode
934
+ * @param redraw
935
+ */
936
+ Watermark.prototype.changeOptions = function () {
937
+ return __awaiter(this, arguments, void 0, function (args, mode, redraw) {
938
+ if (args === void 0) { args = {}; }
939
+ if (mode === void 0) { mode = 'overwrite'; }
940
+ if (redraw === void 0) { redraw = true; }
941
+ return __generator(this, function (_a) {
942
+ switch (_a.label) {
943
+ case 0:
944
+ this.initConfigData(args, mode);
945
+ if (!redraw) return [3 /*break*/, 2];
946
+ this.remove();
947
+ return [4 /*yield*/, this.create()];
948
+ case 1:
949
+ _a.sent();
950
+ _a.label = 2;
951
+ case 2: return [2 /*return*/];
952
+ }
953
+ });
954
+ });
955
+ };
956
+ /**
957
+ * Creating a watermark.
958
+ */
959
+ Watermark.prototype.create = function () {
960
+ return __awaiter(this, void 0, void 0, function () {
961
+ var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
962
+ var _a, _b, _c, _d, _e, _f, _g;
963
+ return __generator(this, function (_h) {
964
+ switch (_h.label) {
965
+ case 0:
966
+ if (!this.validateUnique()) {
967
+ return [2 /*return*/];
968
+ }
969
+ if (!this.validateContent()) {
970
+ return [2 /*return*/];
971
+ }
972
+ firstDraw = isUndefined(this.watermarkDom);
973
+ return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
974
+ case 1:
975
+ _h.sent();
976
+ this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
977
+ image = convertImage(this.layoutCanvas);
978
+ (_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
979
+ this.watermarkDom = document.createElement('div');
980
+ watermarkInnerDom = document.createElement('div');
981
+ this.watermarkDom.__WATERMARK__ = 'watermark';
982
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
983
+ parentElementType = this.checkParentElementType();
984
+ 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 ");
985
+ backgroundSize = generateBackgroundSize(this.options);
986
+ 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, ";\n ").concat(this.options.movable ? 'animation: 200s ease 0s infinite normal none running watermark !important;' : '', "\n ");
987
+ this.watermarkDom.append(watermarkInnerDom);
988
+ this.parentElement.appendChild(this.watermarkDom);
989
+ if (this.options.mutationObserve) {
990
+ try {
991
+ this.bindMutationObserve();
992
+ }
993
+ catch (e) {
994
+ (_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
995
+ }
996
+ }
997
+ firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
998
+ return [2 /*return*/];
999
+ }
1000
+ });
1001
+ });
1002
+ };
1003
+ /**
1004
+ * Delete this watermark.
1005
+ */
1006
+ Watermark.prototype.destroy = function () {
1007
+ this.remove();
1008
+ this.watermarkDom = undefined;
1009
+ };
1010
+ Watermark.prototype.check = function () {
1011
+ return __awaiter(this, void 0, void 0, function () {
1012
+ return __generator(this, function (_a) {
1013
+ switch (_a.label) {
1014
+ case 0:
1015
+ if (!!this.parentElement.contains(this.watermarkDom)) return [3 /*break*/, 2];
1016
+ this.remove();
1017
+ return [4 /*yield*/, this.create()];
1018
+ case 1:
1019
+ _a.sent();
1020
+ _a.label = 2;
1021
+ case 2: return [2 /*return*/];
1022
+ }
1023
+ });
1024
+ });
1025
+ };
1026
+ Watermark.prototype.remove = function () {
1027
+ var _a, _b, _c, _d, _e, _f, _g;
1028
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
1029
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
1030
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
1031
+ this.unbindCheckWatermarkElementEvent();
1032
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
1033
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
1034
+ };
1035
+ Watermark.prototype.initConfigData = function (args, mode) {
1036
+ var _this = this;
1037
+ if (mode === void 0) { mode = 'overwrite'; }
1038
+ if (mode === 'append') {
1039
+ Object.keys(args).forEach(function (key) {
1040
+ _this.props && (_this.props[key] = args[key]);
1041
+ });
1042
+ }
1043
+ else {
1044
+ this.props = args;
1045
+ }
1046
+ this.options = Object.assign({}, initialOptions, this.props);
1047
+ this.changeParentElement(this.options.parent);
1048
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1049
+ };
1050
+ Watermark.prototype.changeParentElement = function (parent) {
1051
+ if (typeof parent === 'string') {
1052
+ var parentElement = document.querySelector(parent);
1053
+ parentElement && (this.parentElement = parentElement);
1054
+ }
1055
+ else {
1056
+ this.parentElement = parent;
1057
+ }
1058
+ };
1059
+ Watermark.prototype.validateUnique = function () {
1060
+ var result = true;
1061
+ this.parentElement.childNodes.forEach(function (node) {
1062
+ if (!result) {
1063
+ return;
1064
+ }
1065
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
1066
+ result = false;
1067
+ // throw new Error('duplicate watermark error')
1068
+ }
1069
+ });
1070
+ return result;
1071
+ };
1072
+ Watermark.prototype.validateContent = function () {
1073
+ switch (this.options.contentType) {
1074
+ case 'image':
1075
+ return Object.hasOwnProperty.call(this.options, 'image');
1076
+ case 'multi-line-text':
1077
+ case 'rich-text':
1078
+ case 'text':
1079
+ return this.options.content.length > 0;
1080
+ }
1081
+ return false;
1082
+ };
1083
+ Watermark.prototype.checkParentElementType = function () {
1084
+ if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
1085
+ return 'root';
1086
+ }
1087
+ return 'custom';
1088
+ };
1089
+ Watermark.prototype.checkWatermarkElement = function () {
1090
+ return __awaiter(this, void 0, void 0, function () {
1091
+ return __generator(this, function (_a) {
1092
+ switch (_a.label) {
1093
+ case 0:
1094
+ if (!!this.parentElement.contains(this.watermarkDom)) return [3 /*break*/, 2];
1095
+ this.remove();
1096
+ return [4 /*yield*/, this.create()];
1097
+ case 1:
1098
+ _a.sent();
1099
+ _a.label = 2;
1100
+ case 2:
1101
+ this.bindCheckWatermarkElementEvent();
1102
+ return [2 /*return*/];
1103
+ }
1104
+ });
1105
+ });
1106
+ };
1107
+ Watermark.prototype.bindMutationObserve = function () {
1108
+ var _this = this;
1109
+ if (!this.watermarkDom) {
1110
+ return;
1111
+ }
1112
+ this.bindCheckWatermarkElementEvent();
1113
+ this.observer = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
1114
+ return __generator(this, function (_a) {
1115
+ switch (_a.label) {
1116
+ case 0:
1117
+ if (!(mutationsList.length > 0)) return [3 /*break*/, 2];
1118
+ this.remove();
1119
+ return [4 /*yield*/, this.create()];
1120
+ case 1:
1121
+ _a.sent();
1122
+ _a.label = 2;
1123
+ case 2: return [2 /*return*/];
1124
+ }
1125
+ });
1126
+ }); });
1127
+ this.observer.observe(this.watermarkDom, {
1128
+ attributes: true, // 属性的变动
1129
+ childList: true, // 子节点的变动(指新增,删除或者更改)
1130
+ subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
1131
+ characterData: true // 节点内容或节点文本的变动。
1132
+ });
1133
+ this.parentObserve = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
1134
+ var _i, mutationsList_1, item;
1135
+ var _a;
1136
+ return __generator(this, function (_b) {
1137
+ switch (_b.label) {
1138
+ case 0:
1139
+ _i = 0, mutationsList_1 = mutationsList;
1140
+ _b.label = 1;
1141
+ case 1:
1142
+ if (!(_i < mutationsList_1.length)) return [3 /*break*/, 4];
1143
+ item = mutationsList_1[_i];
1144
+ if (!((item === null || item === void 0 ? void 0 : item.target) === this.watermarkDom ||
1145
+ ((_a = item === null || item === void 0 ? void 0 : item.removedNodes) === null || _a === void 0 ? void 0 : _a[0]) === this.watermarkDom ||
1146
+ (item.type === 'childList' && item.target === this.parentElement && item.target.lastChild !== this.watermarkDom))) return [3 /*break*/, 3];
1147
+ this.remove();
1148
+ return [4 /*yield*/, this.create()];
1149
+ case 2:
1150
+ _b.sent();
1151
+ _b.label = 3;
1152
+ case 3:
1153
+ _i++;
1154
+ return [3 /*break*/, 1];
1155
+ case 4: return [2 /*return*/];
1156
+ }
1157
+ });
1158
+ }); });
1159
+ this.parentObserve.observe(this.parentElement, {
1160
+ attributes: true, // 属性的变动
1161
+ childList: true, // 子节点的变动(指新增,删除或者更改)
1162
+ subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
1163
+ characterData: true // 节点内容或节点文本的变动。
1164
+ });
1165
+ };
1166
+ Watermark.prototype.bindCheckWatermarkElementEvent = function () {
1167
+ this.unbindCheckWatermarkElementEvent();
1168
+ this.checkWatermarkElementRequestID = requestAnimationFrame(this.checkWatermarkElement.bind(this));
1169
+ };
1170
+ Watermark.prototype.unbindCheckWatermarkElementEvent = function () {
1171
+ if (!isUndefined(this.checkWatermarkElementRequestID)) {
1172
+ cancelAnimationFrame(this.checkWatermarkElementRequestID);
1173
+ }
1174
+ };
1175
+ return Watermark;
1176
+ }());
1272
1177
 
1273
- if (style.styleSheet) {
1274
- style.styleSheet.cssText = css;
1275
- } else {
1276
- style.appendChild(document.createTextNode(css));
1178
+ /**
1179
+ * BlindWatermark class
1180
+ */
1181
+ var BlindWatermark = /** @class */ (function (_super) {
1182
+ __extends(BlindWatermark, _super);
1183
+ /**
1184
+ * BlindWatermark constructor
1185
+ * @param props - blind watermark options
1186
+ */
1187
+ function BlindWatermark(props) {
1188
+ if (props === void 0) { props = {}; }
1189
+ var defaultProps = {
1190
+ globalAlpha: 0.005,
1191
+ mode: 'blind'
1192
+ };
1193
+ return _super.call(this, __assign(__assign({}, props), defaultProps)) || this;
1277
1194
  }
1278
- }
1195
+ /**
1196
+ * Decode blind watermark.
1197
+ * @param props - decode options
1198
+ */
1199
+ BlindWatermark.decode = function (props) {
1200
+ var _a = props.url, url = _a === void 0 ? '' : _a, _b = props.fillColor, fillColor = _b === void 0 ? '#000' : _b, _c = props.compositeOperation, compositeOperation = _c === void 0 ? 'color-burn' : _c, _d = props.mode, mode = _d === void 0 ? 'canvas' : _d, _e = props.compositeTimes, compositeTimes = _e === void 0 ? 3 : _e, onSuccess = props.onSuccess;
1201
+ if (!url) {
1202
+ return;
1203
+ }
1204
+ if (mode === 'canvas') {
1205
+ var img_1 = new Image();
1206
+ img_1.src = url;
1207
+ img_1.addEventListener('load', function () {
1208
+ var width = img_1.width, height = img_1.height;
1209
+ var canvas = WatermarkCanvas.createCanvas(width, height);
1210
+ var ctx = canvas.getContext('2d');
1211
+ if (!ctx) {
1212
+ throw new Error('get context error');
1213
+ }
1214
+ ctx.drawImage(img_1, 0, 0, width, height);
1215
+ ctx.globalCompositeOperation = compositeOperation;
1216
+ ctx.fillStyle = fillColor;
1217
+ for (var i = 0; i < compositeTimes; i++) {
1218
+ ctx.fillRect(0, 0, width, height);
1219
+ }
1220
+ var resultImage = convertImage(canvas);
1221
+ if (isFunction(onSuccess)) {
1222
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(resultImage);
1223
+ }
1224
+ });
1225
+ }
1226
+ };
1227
+ return BlindWatermark;
1228
+ }(Watermark));
1279
1229
 
1280
- var css_248z = "@keyframes watermark{0%{background-position:0 0}25%{background-position:100% 100%}50%{background-position:0 0}75%{background-position:100% -100%}to{background-position:0 0}}";
1281
- styleInject(css_248z);
1230
+ /**
1231
+ * ImageWatermark class
1232
+ */
1233
+ var ImageWatermark = /** @class */ (function () {
1234
+ /**
1235
+ * ImageWatermark constructor
1236
+ * @param args - image watermark args
1237
+ */
1238
+ function ImageWatermark(args) {
1239
+ if (args === void 0) { args = {}; }
1240
+ var _a;
1241
+ this.drew = false;
1242
+ this.props = args;
1243
+ this.options = Object.assign({}, initialOptions, args);
1244
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1245
+ this.originalSrc = (_a = this.props.dom) === null || _a === void 0 ? void 0 : _a.src;
1246
+ this.backgroundImage = this.getBackgroundImage();
1247
+ }
1248
+ ImageWatermark.prototype.create = function () {
1249
+ return __awaiter(this, void 0, void 0, function () {
1250
+ var _a, _b, _c, _d, _e;
1251
+ return __generator(this, function (_f) {
1252
+ switch (_f.label) {
1253
+ case 0:
1254
+ if (this.drew) {
1255
+ return [2 /*return*/];
1256
+ }
1257
+ return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
1258
+ case 1:
1259
+ _f.sent();
1260
+ this.options.layout = 'grid';
1261
+ this.options.gridLayoutOptions = __assign(__assign({}, this.options.gridLayoutOptions), { width: (_b = this.backgroundImage) === null || _b === void 0 ? void 0 : _b.width, height: (_c = this.backgroundImage) === null || _c === void 0 ? void 0 : _c.height, backgroundImage: this.backgroundImage });
1262
+ this.layoutCanvas = renderLayout(this.options, (_d = this.watermarkCanvas) === null || _d === void 0 ? void 0 : _d.getCanvas());
1263
+ this.options.dom.src = convertImage(this.layoutCanvas);
1264
+ (_e = this.watermarkCanvas) === null || _e === void 0 ? void 0 : _e.clear();
1265
+ this.drew = true;
1266
+ return [2 /*return*/];
1267
+ }
1268
+ });
1269
+ });
1270
+ };
1271
+ ImageWatermark.prototype.destroy = function () {
1272
+ this.options.dom.src = this.originalSrc;
1273
+ this.drew = false;
1274
+ };
1275
+ ImageWatermark.prototype.getBackgroundImage = function () {
1276
+ if (this.options.dom) {
1277
+ return this.options.dom;
1278
+ }
1279
+ };
1280
+ return ImageWatermark;
1281
+ }());
1282
1282
 
1283
- exports.BlindWatermark = BlindWatermark;
1284
- exports.ImageWatermark = ImageWatermark;
1285
- exports.Watermark = Watermark;
1283
+ exports.BlindWatermark = BlindWatermark;
1284
+ exports.ImageWatermark = ImageWatermark;
1285
+ exports.Watermark = Watermark;
1286
1286
 
1287
- return exports;
1287
+ return exports;
1288
1288
 
1289
1289
  })({});
1290
1290
  //# sourceMappingURL=index.iife.js.map