watermark-js-plus 0.8.23 → 0.9.2

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