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