watermark-js-plus 0.3.0 → 0.5.1

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.
@@ -8,6 +8,7 @@ export default class Watermark {
8
8
  private observer?;
9
9
  private parentObserve?;
10
10
  private watermarkDom?;
11
+ private props?;
11
12
  /**
12
13
  * Watermark constructor
13
14
  * @param props - watermark options
@@ -32,11 +33,14 @@ export default class Watermark {
32
33
  private validateUnique;
33
34
  private validateContent;
34
35
  private draw;
36
+ private setTextStyle;
37
+ private setText;
35
38
  private drawText;
36
39
  private drawImage;
37
40
  private drawMultiLineText;
38
41
  private drawRichText;
39
42
  private getImageRect;
43
+ private getDrawImagePosition;
40
44
  private checkParentElementType;
41
45
  private bindMutationObserve;
42
46
  }
@@ -4,6 +4,7 @@ export type TextBaselineType = 'alphabetic' | 'hanging' | 'ideographic' | 'top'
4
4
  export type CreateWatermarkModeType = 'default' | 'blind';
5
5
  export type DecodeBlindWatermarkModeType = 'canvas' | 'html' | 'svg';
6
6
  export type TranslatePlacementType = 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'right' | 'middle';
7
+ export type TextType = 'fill' | 'stroke';
7
8
  export interface WatermarkDom extends HTMLDivElement {
8
9
  __WATERMARK__?: string;
9
10
  __WATERMARK__INSTANCE__?: any;
@@ -17,9 +18,13 @@ export interface WatermarkOptions {
17
18
  translateY?: number;
18
19
  contentType: ContentTypeType;
19
20
  content: string;
21
+ textType: TextType;
22
+ textRowMaxWidth?: number;
20
23
  image?: string;
21
24
  imageWidth: number;
22
25
  imageHeight: number;
26
+ richTextWidth?: number;
27
+ richTextHeight?: number;
23
28
  lineHeight: number;
24
29
  zIndex: number;
25
30
  backgroundPosition: string;
@@ -46,3 +51,8 @@ export interface DecodeBlindWatermarkOptions {
46
51
  mode: DecodeBlindWatermarkModeType;
47
52
  onSuccess: Function;
48
53
  }
54
+ export interface CustomContentSVGType {
55
+ element: Element;
56
+ width: number;
57
+ height: number;
58
+ }
@@ -1,4 +1,4 @@
1
- import { WatermarkOptions } from '../types';
1
+ import { CustomContentSVGType, WatermarkOptions } from '../types';
2
2
  export declare const convertImage: (canvas: HTMLCanvasElement) => string;
3
3
  export declare const isFunction: (value: Function) => boolean;
4
4
  export declare const isUndefined: (value: any) => boolean;
@@ -6,5 +6,5 @@ export declare const createSVGElement: (tagName: string, attrs?: {
6
6
  [key: string]: string;
7
7
  }, namespaceURI?: string) => Element;
8
8
  export declare const getMultiLineData: (ctx: CanvasRenderingContext2D, text: string, maxWidth: number) => string[];
9
- export declare const createCustomContentSVG: (ctx: CanvasRenderingContext2D, options: WatermarkOptions) => Element;
9
+ export declare const createCustomContentSVG: (ctx: CanvasRenderingContext2D, options: WatermarkOptions) => CustomContentSVGType;
10
10
  export declare const convertSVGToImage: (svg: Element) => string;
@@ -103,17 +103,26 @@ var createCustomContentSVG = function (ctx, options) {
103
103
  var svgElement = createSVGElement('svg', {
104
104
  xmlns: 'http://www.w3.org/2000/svg'
105
105
  });
106
- var foreignObjectElement = createSVGElement('foreignObject', {
107
- width: options.width.toString(),
108
- height: options.height.toString()
109
- });
110
106
  var bodyElement = document.createElement('div');
111
107
  bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
112
- 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 ");
113
- bodyElement.innerHTML = options.content;
108
+ 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");
109
+ bodyElement.innerHTML = "<div class=\"rich-text-content\">".concat(options.content, "</div>");
110
+ document.body.appendChild(bodyElement);
111
+ var _a = bodyElement.querySelector('.rich-text-content'), offsetHeight = _a.offsetHeight, offsetWidth = _a.offsetWidth;
112
+ document.body.removeChild(bodyElement);
113
+ var width = options.richTextWidth || offsetWidth || options.width;
114
+ var height = options.richTextHeight || offsetHeight || options.height;
115
+ var foreignObjectElement = createSVGElement('foreignObject', {
116
+ width: width.toString(),
117
+ height: height.toString()
118
+ });
114
119
  foreignObjectElement.appendChild(bodyElement);
115
120
  svgElement.appendChild(foreignObjectElement);
116
- return svgElement;
121
+ return {
122
+ element: svgElement,
123
+ width: width,
124
+ height: height
125
+ };
117
126
  };
118
127
  var convertSVGToImage = function (svg) {
119
128
  var richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
@@ -131,6 +140,7 @@ var Watermark = /** @class */ (function () {
131
140
  function Watermark(props) {
132
141
  if (props === void 0) { props = {}; }
133
142
  this.parentElement = document.body;
143
+ this.props = props;
134
144
  this.options = Object.assign({
135
145
  width: 300,
136
146
  height: 300,
@@ -138,6 +148,7 @@ var Watermark = /** @class */ (function () {
138
148
  translatePlacement: 'middle',
139
149
  contentType: 'text',
140
150
  content: 'hello watermark-js-plus',
151
+ textType: 'fill',
141
152
  imageWidth: 0,
142
153
  imageHeight: 0,
143
154
  lineHeight: 30,
@@ -235,7 +246,7 @@ var Watermark = /** @class */ (function () {
235
246
  (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
236
247
  };
237
248
  Watermark.prototype.initializeOptions = function () {
238
- var _a;
249
+ var _a, _b, _c, _d, _e, _f, _g;
239
250
  if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
240
251
  this.options.rotate = (360 - this.options.rotate % 360) * (Math.PI / 180);
241
252
  }
@@ -293,10 +304,14 @@ var Watermark = /** @class */ (function () {
293
304
  translateY = this.options.height / 2;
294
305
  break;
295
306
  }
296
- isUndefined(this.options.translateX) && (this.options.translateX = translateX);
297
- isUndefined(this.options.translateY) && (this.options.translateY = translateY);
298
- isUndefined(this.options.textBaseline) && (this.options.textBaseline = textBaseline);
299
- isUndefined(this.options.textAlign) && (this.options.textAlign = textAlign);
307
+ if (!isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateX) || !isUndefined((_c = this.props) === null || _c === void 0 ? void 0 : _c.translateY)) {
308
+ textBaseline = 'top';
309
+ textAlign = 'left';
310
+ }
311
+ isUndefined((_d = this.props) === null || _d === void 0 ? void 0 : _d.translateX) && (this.options.translateX = translateX);
312
+ isUndefined((_e = this.props) === null || _e === void 0 ? void 0 : _e.translateY) && (this.options.translateY = translateY);
313
+ isUndefined((_f = this.props) === null || _f === void 0 ? void 0 : _f.textBaseline) && (this.options.textBaseline = textBaseline);
314
+ isUndefined((_g = this.props) === null || _g === void 0 ? void 0 : _g.textAlign) && (this.options.textAlign = textAlign);
300
315
  };
301
316
  Watermark.prototype.validateUnique = function () {
302
317
  var result = true;
@@ -335,7 +350,7 @@ var Watermark = /** @class */ (function () {
335
350
  ctx.font = "".concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
336
351
  this.options.textAlign && (ctx.textAlign = this.options.textAlign);
337
352
  this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
338
- ctx.fillStyle = this.options.fontColor;
353
+ this.setTextStyle(ctx);
339
354
  ctx.globalAlpha = this.options.globalAlpha;
340
355
  ctx.translate(this.options.translateX, this.options.translateY);
341
356
  ctx.rotate(this.options.rotate);
@@ -356,8 +371,27 @@ var Watermark = /** @class */ (function () {
356
371
  }
357
372
  });
358
373
  };
374
+ Watermark.prototype.setTextStyle = function (ctx) {
375
+ var propName = 'fillStyle';
376
+ if (this.options.textType === 'stroke') {
377
+ propName = 'strokeStyle';
378
+ }
379
+ ctx[propName] && (ctx[propName] = this.options.fontColor);
380
+ };
381
+ Watermark.prototype.setText = function (ctx, params) {
382
+ var methodName = 'fillText';
383
+ if (this.options.textType === 'stroke') {
384
+ methodName = 'strokeText';
385
+ }
386
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
387
+ };
359
388
  Watermark.prototype.drawText = function (ctx, resolve) {
360
- ctx.fillText(this.options.content, 0, 0);
389
+ this.setText(ctx, {
390
+ text: this.options.content,
391
+ x: 0,
392
+ y: 0,
393
+ maxWidth: this.options.textRowMaxWidth
394
+ });
361
395
  resolve(ctx.canvas);
362
396
  };
363
397
  Watermark.prototype.drawImage = function (ctx, resolve) {
@@ -367,7 +401,8 @@ var Watermark = /** @class */ (function () {
367
401
  image.src = this.options.image;
368
402
  image.onload = function () {
369
403
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
370
- ctx.drawImage(image, 0 - imageWidth / 2, 0 - imageHeight / 2, imageWidth, imageHeight);
404
+ var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
405
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
371
406
  resolve(ctx.canvas);
372
407
  };
373
408
  };
@@ -389,22 +424,38 @@ var Watermark = /** @class */ (function () {
389
424
  // )
390
425
  // resolve(canvas)
391
426
  // }
392
- var lines = getMultiLineData(ctx, this.options.content, this.options.width);
393
- var yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
394
- lines.forEach(function (txt, index) {
395
- ctx.fillText(txt, 0, _this.options.lineHeight * index - yOffsetValue);
427
+ var width = this.options.textRowMaxWidth || this.options.width;
428
+ var lines = getMultiLineData(ctx, this.options.content, width);
429
+ var yOffsetValue;
430
+ switch (this.options.textBaseline) {
431
+ case 'middle':
432
+ yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
433
+ break;
434
+ case 'bottom':
435
+ case 'alphabetic':
436
+ case 'ideographic':
437
+ yOffsetValue = (lines.length - 1) * this.options.lineHeight;
438
+ break;
439
+ case 'top':
440
+ case 'hanging':
441
+ yOffsetValue = 0;
442
+ break;
443
+ }
444
+ lines.forEach(function (text, index) {
445
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
396
446
  });
397
447
  resolve(ctx.canvas);
398
448
  };
399
449
  Watermark.prototype.drawRichText = function (ctx, resolve) {
400
450
  var _this = this;
451
+ var obj = createCustomContentSVG(ctx, this.options);
401
452
  var image = new Image();
402
- image.width = this.options.width;
403
- image.height = this.options.height;
404
- var element = createCustomContentSVG(ctx, this.options);
405
- image.src = convertSVGToImage(element);
453
+ image.width = obj.width;
454
+ image.height = obj.height;
455
+ image.src = convertSVGToImage(obj.element);
406
456
  image.onload = function () {
407
- ctx.drawImage(image, -_this.options.width / 2, -_this.options.height / 2, ctx.canvas.width, ctx.canvas.height);
457
+ var imagePosition = _this.getDrawImagePosition(image.width, image.height);
458
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
408
459
  resolve(ctx.canvas);
409
460
  };
410
461
  };
@@ -424,6 +475,50 @@ var Watermark = /** @class */ (function () {
424
475
  }
425
476
  return rect;
426
477
  };
478
+ Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
479
+ var _a, _b;
480
+ var result = {
481
+ x: -imageWidth / 2,
482
+ y: -imageHeight / 2
483
+ };
484
+ switch (this.options.translatePlacement) {
485
+ case 'top':
486
+ result.x = -imageWidth / 2;
487
+ result.y = 0;
488
+ break;
489
+ case 'top-start':
490
+ result.x = 0;
491
+ result.y = 0;
492
+ break;
493
+ case 'top-end':
494
+ result.x = -imageWidth;
495
+ result.y = 0;
496
+ break;
497
+ case 'bottom':
498
+ result.x = -imageWidth / 2;
499
+ result.y = -imageHeight;
500
+ break;
501
+ case 'bottom-start':
502
+ result.x = 0;
503
+ result.y = -imageHeight;
504
+ break;
505
+ case 'bottom-end':
506
+ result.x = -imageWidth;
507
+ result.y = -imageHeight;
508
+ break;
509
+ case 'left':
510
+ result.x = 0;
511
+ result.y = -imageHeight / 2;
512
+ break;
513
+ case 'right':
514
+ result.x = -imageWidth;
515
+ result.y = -imageHeight / 2;
516
+ break;
517
+ }
518
+ !isUndefined((_a = this.props) === null || _a === void 0 ? void 0 : _a.translateX) && (result.x = 0);
519
+ !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
520
+ return result;
521
+ };
427
522
  Watermark.prototype.checkParentElementType = function () {
428
523
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
429
524
  return 'root';
@@ -109,17 +109,26 @@
109
109
  var svgElement = createSVGElement('svg', {
110
110
  xmlns: 'http://www.w3.org/2000/svg'
111
111
  });
112
- var foreignObjectElement = createSVGElement('foreignObject', {
113
- width: options.width.toString(),
114
- height: options.height.toString()
115
- });
116
112
  var bodyElement = document.createElement('div');
117
113
  bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
118
- 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 ");
119
- bodyElement.innerHTML = options.content;
114
+ 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");
115
+ bodyElement.innerHTML = "<div class=\"rich-text-content\">".concat(options.content, "</div>");
116
+ document.body.appendChild(bodyElement);
117
+ var _a = bodyElement.querySelector('.rich-text-content'), offsetHeight = _a.offsetHeight, offsetWidth = _a.offsetWidth;
118
+ document.body.removeChild(bodyElement);
119
+ var width = options.richTextWidth || offsetWidth || options.width;
120
+ var height = options.richTextHeight || offsetHeight || options.height;
121
+ var foreignObjectElement = createSVGElement('foreignObject', {
122
+ width: width.toString(),
123
+ height: height.toString()
124
+ });
120
125
  foreignObjectElement.appendChild(bodyElement);
121
126
  svgElement.appendChild(foreignObjectElement);
122
- return svgElement;
127
+ return {
128
+ element: svgElement,
129
+ width: width,
130
+ height: height
131
+ };
123
132
  };
124
133
  var convertSVGToImage = function (svg) {
125
134
  var richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
@@ -137,6 +146,7 @@
137
146
  function Watermark(props) {
138
147
  if (props === void 0) { props = {}; }
139
148
  this.parentElement = document.body;
149
+ this.props = props;
140
150
  this.options = Object.assign({
141
151
  width: 300,
142
152
  height: 300,
@@ -144,6 +154,7 @@
144
154
  translatePlacement: 'middle',
145
155
  contentType: 'text',
146
156
  content: 'hello watermark-js-plus',
157
+ textType: 'fill',
147
158
  imageWidth: 0,
148
159
  imageHeight: 0,
149
160
  lineHeight: 30,
@@ -241,7 +252,7 @@
241
252
  (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
242
253
  };
243
254
  Watermark.prototype.initializeOptions = function () {
244
- var _a;
255
+ var _a, _b, _c, _d, _e, _f, _g;
245
256
  if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
246
257
  this.options.rotate = (360 - this.options.rotate % 360) * (Math.PI / 180);
247
258
  }
@@ -299,10 +310,14 @@
299
310
  translateY = this.options.height / 2;
300
311
  break;
301
312
  }
302
- isUndefined(this.options.translateX) && (this.options.translateX = translateX);
303
- isUndefined(this.options.translateY) && (this.options.translateY = translateY);
304
- isUndefined(this.options.textBaseline) && (this.options.textBaseline = textBaseline);
305
- isUndefined(this.options.textAlign) && (this.options.textAlign = textAlign);
313
+ if (!isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateX) || !isUndefined((_c = this.props) === null || _c === void 0 ? void 0 : _c.translateY)) {
314
+ textBaseline = 'top';
315
+ textAlign = 'left';
316
+ }
317
+ isUndefined((_d = this.props) === null || _d === void 0 ? void 0 : _d.translateX) && (this.options.translateX = translateX);
318
+ isUndefined((_e = this.props) === null || _e === void 0 ? void 0 : _e.translateY) && (this.options.translateY = translateY);
319
+ isUndefined((_f = this.props) === null || _f === void 0 ? void 0 : _f.textBaseline) && (this.options.textBaseline = textBaseline);
320
+ isUndefined((_g = this.props) === null || _g === void 0 ? void 0 : _g.textAlign) && (this.options.textAlign = textAlign);
306
321
  };
307
322
  Watermark.prototype.validateUnique = function () {
308
323
  var result = true;
@@ -341,7 +356,7 @@
341
356
  ctx.font = "".concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
342
357
  this.options.textAlign && (ctx.textAlign = this.options.textAlign);
343
358
  this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
344
- ctx.fillStyle = this.options.fontColor;
359
+ this.setTextStyle(ctx);
345
360
  ctx.globalAlpha = this.options.globalAlpha;
346
361
  ctx.translate(this.options.translateX, this.options.translateY);
347
362
  ctx.rotate(this.options.rotate);
@@ -362,8 +377,27 @@
362
377
  }
363
378
  });
364
379
  };
380
+ Watermark.prototype.setTextStyle = function (ctx) {
381
+ var propName = 'fillStyle';
382
+ if (this.options.textType === 'stroke') {
383
+ propName = 'strokeStyle';
384
+ }
385
+ ctx[propName] && (ctx[propName] = this.options.fontColor);
386
+ };
387
+ Watermark.prototype.setText = function (ctx, params) {
388
+ var methodName = 'fillText';
389
+ if (this.options.textType === 'stroke') {
390
+ methodName = 'strokeText';
391
+ }
392
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
393
+ };
365
394
  Watermark.prototype.drawText = function (ctx, resolve) {
366
- ctx.fillText(this.options.content, 0, 0);
395
+ this.setText(ctx, {
396
+ text: this.options.content,
397
+ x: 0,
398
+ y: 0,
399
+ maxWidth: this.options.textRowMaxWidth
400
+ });
367
401
  resolve(ctx.canvas);
368
402
  };
369
403
  Watermark.prototype.drawImage = function (ctx, resolve) {
@@ -373,7 +407,8 @@
373
407
  image.src = this.options.image;
374
408
  image.onload = function () {
375
409
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
376
- ctx.drawImage(image, 0 - imageWidth / 2, 0 - imageHeight / 2, imageWidth, imageHeight);
410
+ var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
411
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
377
412
  resolve(ctx.canvas);
378
413
  };
379
414
  };
@@ -395,22 +430,38 @@
395
430
  // )
396
431
  // resolve(canvas)
397
432
  // }
398
- var lines = getMultiLineData(ctx, this.options.content, this.options.width);
399
- var yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
400
- lines.forEach(function (txt, index) {
401
- ctx.fillText(txt, 0, _this.options.lineHeight * index - yOffsetValue);
433
+ var width = this.options.textRowMaxWidth || this.options.width;
434
+ var lines = getMultiLineData(ctx, this.options.content, width);
435
+ var yOffsetValue;
436
+ switch (this.options.textBaseline) {
437
+ case 'middle':
438
+ yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
439
+ break;
440
+ case 'bottom':
441
+ case 'alphabetic':
442
+ case 'ideographic':
443
+ yOffsetValue = (lines.length - 1) * this.options.lineHeight;
444
+ break;
445
+ case 'top':
446
+ case 'hanging':
447
+ yOffsetValue = 0;
448
+ break;
449
+ }
450
+ lines.forEach(function (text, index) {
451
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
402
452
  });
403
453
  resolve(ctx.canvas);
404
454
  };
405
455
  Watermark.prototype.drawRichText = function (ctx, resolve) {
406
456
  var _this = this;
457
+ var obj = createCustomContentSVG(ctx, this.options);
407
458
  var image = new Image();
408
- image.width = this.options.width;
409
- image.height = this.options.height;
410
- var element = createCustomContentSVG(ctx, this.options);
411
- image.src = convertSVGToImage(element);
459
+ image.width = obj.width;
460
+ image.height = obj.height;
461
+ image.src = convertSVGToImage(obj.element);
412
462
  image.onload = function () {
413
- ctx.drawImage(image, -_this.options.width / 2, -_this.options.height / 2, ctx.canvas.width, ctx.canvas.height);
463
+ var imagePosition = _this.getDrawImagePosition(image.width, image.height);
464
+ ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
414
465
  resolve(ctx.canvas);
415
466
  };
416
467
  };
@@ -430,6 +481,50 @@
430
481
  }
431
482
  return rect;
432
483
  };
484
+ Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
485
+ var _a, _b;
486
+ var result = {
487
+ x: -imageWidth / 2,
488
+ y: -imageHeight / 2
489
+ };
490
+ switch (this.options.translatePlacement) {
491
+ case 'top':
492
+ result.x = -imageWidth / 2;
493
+ result.y = 0;
494
+ break;
495
+ case 'top-start':
496
+ result.x = 0;
497
+ result.y = 0;
498
+ break;
499
+ case 'top-end':
500
+ result.x = -imageWidth;
501
+ result.y = 0;
502
+ break;
503
+ case 'bottom':
504
+ result.x = -imageWidth / 2;
505
+ result.y = -imageHeight;
506
+ break;
507
+ case 'bottom-start':
508
+ result.x = 0;
509
+ result.y = -imageHeight;
510
+ break;
511
+ case 'bottom-end':
512
+ result.x = -imageWidth;
513
+ result.y = -imageHeight;
514
+ break;
515
+ case 'left':
516
+ result.x = 0;
517
+ result.y = -imageHeight / 2;
518
+ break;
519
+ case 'right':
520
+ result.x = -imageWidth;
521
+ result.y = -imageHeight / 2;
522
+ break;
523
+ }
524
+ !isUndefined((_a = this.props) === null || _a === void 0 ? void 0 : _a.translateX) && (result.x = 0);
525
+ !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
526
+ return result;
527
+ };
433
528
  Watermark.prototype.checkParentElementType = function () {
434
529
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
435
530
  return 'root';
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).watermark={})}(this,(function(t){"use strict";var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},e(t,n)};function n(t,e,n,o){return new(n||(n=Promise))((function(i,r){function a(t){try{c(o.next(t))}catch(t){r(t)}}function s(t){try{c(o.throw(t))}catch(t){r(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}c((o=o.apply(t,e||[])).next())}))}function o(t,e){var n,o,i,r,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function s(r){return function(s){return function(r){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,o&&(i=2&r[0]?o.return:r[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,r[1])).done)return i;switch(o=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return a.label++,{value:r[1],done:!1};case 5:a.label++,o=r[1],r=[0];continue;case 7:r=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){a=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){a.label=r[1];break}if(6===r[0]&&a.label<i[1]){a.label=i[1],i=r;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(r);break}i[2]&&a.ops.pop(),a.trys.pop();continue}r=e.call(t,a)}catch(t){r=[6,t],o=0}finally{n=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,s])}}}var i=function(t){return t.toDataURL("image/png",1)},r=function(t){return void 0===t},a=function(t,e,n){void 0===e&&(e={}),void 0===n&&(n="http://www.w3.org/2000/svg");var o=document.createElementNS(n,t);for(var i in e)o.setAttribute(i,e[i]);return o},s=function(){function t(t){void 0===t&&(t={}),this.parentElement=document.body,this.options=Object.assign({width:300,height:300,rotate:45,translatePlacement:"middle",contentType:"text",content:"hello watermark-js-plus",imageWidth:0,imageHeight:0,lineHeight:30,zIndex:1e4,backgroundPosition:"0 0, 0 0",backgroundRepeat:"repeat",fontSize:20,fontFamily:"sans-serif",fontColor:"#000",globalAlpha:.5,fontWeight:"normal",mode:"default",mutationObserve:!0,unique:!0,parent:"body",onSuccess:function(){},onBeforeDestroy:function(){},onDestroyed:function(){}},t),this.changeParentElement(this.options.parent),this.initializeOptions()}return t.createCanvas=function(t,e){var n,o=window.devicePixelRatio||1,i=document.createElement("canvas");return i.width=t*o,i.height=e*o,i.style.width="".concat(t,"px"),i.style.height="".concat(e,"px"),null===(n=i.getContext("2d"))||void 0===n||n.setTransform(o,0,0,o,0,0),i},t.prototype.changeParentElement=function(t){if("string"==typeof t){var e=document.querySelector(t);e&&(this.parentElement=e)}else this.parentElement=t},t.prototype.create=function(){var t,e;return n(this,void 0,void 0,(function(){var n,r,a,s;return o(this,(function(o){switch(o.label){case 0:return this.validateUnique()&&this.validateContent()?[4,this.draw()]:[2];case 1:return n=o.sent(),r=i(n),this.watermarkDom=document.createElement("div"),a=document.createElement("div"),this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,s=this.checkParentElementType(),this.watermarkDom.style.cssText="\n z-index: ".concat(this.options.zIndex,";\n ").concat("custom"===s?"top: 0;bottom: 0;left: 0;right: 0;height: 100%;pointer-events: none;position: absolute":"position: relative","\n "),a.style.cssText="\n position: ".concat("root"===s?"fixed;":"absolute;","\n z-index: ").concat(this.options.zIndex,";\n pointer-events: none;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n background-image: url(").concat(r,");\n background-repeat: ").concat(this.options.backgroundRepeat,";\n background-size: ").concat(this.options.width,"px ").concat(this.options.height,"px;\n background-position: ").concat(this.options.backgroundPosition,";\n -webkit-print-color-adjust: exact;\n "),this.watermarkDom.append(a),this.parentElement.appendChild(this.watermarkDom),this.options.mutationObserve&&this.bindMutationObserve(),null===(e=(t=this.options).onSuccess)||void 0===e||e.call(t),[2]}}))}))},t.prototype.destroy=function(){var t,e,n,o,i,r,a;null===(e=(t=this.options).onBeforeDestroy)||void 0===e||e.call(t),null===(n=this.observer)||void 0===n||n.disconnect(),null===(o=this.parentObserve)||void 0===o||o.disconnect(),null===(i=this.watermarkDom)||void 0===i||i.remove(),null===(a=(r=this.options).onDestroyed)||void 0===a||a.call(r)},t.prototype.initializeOptions=function(){var t,e,n;(null===(t=this.options)||void 0===t?void 0:t.rotate)&&(this.options.rotate=(360-this.options.rotate%360)*(Math.PI/180));var o="middle",i="center";switch(this.options.translatePlacement){case"top":e=this.options.width/2,n=0,o="top";break;case"top-start":e=0,n=0,o="top",i="start";break;case"top-end":e=this.options.width,n=0,o="top",i="end";break;case"bottom":e=this.options.width/2,n=this.options.height,o="bottom";break;case"bottom-start":e=0,n=this.options.height,o="bottom",i="start";break;case"bottom-end":e=this.options.width,n=this.options.height,o="bottom",i="end";break;case"left":e=0,n=this.options.height/2,i="start";break;case"right":e=this.options.width,n=this.options.height/2,i="end";break;case"middle":e=this.options.width/2,n=this.options.height/2}r(this.options.translateX)&&(this.options.translateX=e),r(this.options.translateY)&&(this.options.translateY=n),r(this.options.textBaseline)&&(this.options.textBaseline=o),r(this.options.textAlign)&&(this.options.textAlign=i)},t.prototype.validateUnique=function(){var t=!0;return this.options.unique&&this.parentElement.childNodes.forEach((function(e){t&&Object.hasOwnProperty.call(e,"__WATERMARK__")&&(t=!1)})),t},t.prototype.validateContent=function(){switch(this.options.contentType){case"image":return Object.hasOwnProperty.call(this.options,"image");case"multi-line-text":case"rich-text":case"text":return this.options.content.length>0}return!1},t.prototype.draw=function(){var e=this,n=t.createCanvas(this.options.width,this.options.height).getContext("2d");if(null===n)throw new Error("get context error");return n.font="".concat(this.options.fontWeight," ").concat(this.options.fontSize,"px ").concat(this.options.fontFamily),this.options.textAlign&&(n.textAlign=this.options.textAlign),this.options.textBaseline&&(n.textBaseline=this.options.textBaseline),n.fillStyle=this.options.fontColor,n.globalAlpha=this.options.globalAlpha,n.translate(this.options.translateX,this.options.translateY),n.rotate(this.options.rotate),new Promise((function(t){switch(e.options.contentType){case"text":e.drawText(n,t);break;case"image":e.drawImage(n,t);break;case"multi-line-text":e.drawMultiLineText(n,t);break;case"rich-text":e.drawRichText(n,t)}}))},t.prototype.drawText=function(t,e){t.fillText(this.options.content,0,0),e(t.canvas)},t.prototype.drawImage=function(t,e){var n=this,o=new Image;o.setAttribute("crossOrigin","Anonymous"),o.src=this.options.image,o.onload=function(){var i=n.getImageRect(o),r=i.width,a=i.height;t.drawImage(o,0-r/2,0-a/2,r,a),e(t.canvas)}},t.prototype.drawMultiLineText=function(t,e){var n=this,o=function(t,e,n){for(var o=[],i="",r=0,a=e.length;r<a;r++)i+=e.charAt(r),t.measureText(i).width>n&&(o.push(i.substring(0,i.length-1)),i="",r--);return o.push(i),o}(t,this.options.content,this.options.width),i=(o.length-1)*this.options.lineHeight/2;o.forEach((function(e,o){t.fillText(e,0,n.options.lineHeight*o-i)})),e(t.canvas)},t.prototype.drawRichText=function(t,e){var n=this,o=new Image;o.width=this.options.width,o.height=this.options.height;var i,r=function(t,e){var n=a("svg",{xmlns:"http://www.w3.org/2000/svg"}),o=a("foreignObject",{width:e.width.toString(),height:e.height.toString()}),i=document.createElement("div");return i.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),i.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(t.font,";\n color: ").concat(e.fontColor,";\n "),i.innerHTML=e.content,o.appendChild(i),n.appendChild(o),n}(t,this.options);o.src=(i=r.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23"),"data:image/svg+xml;charset=utf-8,".concat(i)),o.onload=function(){t.drawImage(o,-n.options.width/2,-n.options.height/2,t.canvas.width,t.canvas.height),e(t.canvas)}},t.prototype.getImageRect=function(t){var e={width:this.options.imageWidth,height:this.options.imageHeight};switch(!0){case 0!==e.width&&0===e.height:e.height=e.width*t.height/t.width;break;case 0===e.width&&0!==e.height:e.width=e.height*t.width/t.height;break;case 0===e.width&&0===e.height:e.width=t.width,e.height=t.height}return e},t.prototype.checkParentElementType=function(){return["html","body"].includes(this.parentElement.tagName.toLocaleLowerCase())?"root":"custom"},t.prototype.bindMutationObserve=function(){var t=this;this.watermarkDom&&(this.observer=new MutationObserver((function(e){e.length>0&&(t.destroy(),t.create())})),this.observer.observe(this.watermarkDom,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.parentObserve=new MutationObserver((function(e){e.forEach((function(e){var n;(null==e?void 0:e.target)!==t.watermarkDom&&(null===(n=null==e?void 0:e.removedNodes)||void 0===n?void 0:n[0])!==t.watermarkDom||(t.destroy(),t.create())}))})),this.parentObserve.observe(this.parentElement,{attributes:!0,childList:!0,subtree:!0,characterData:!0}))},t}(),c=function(t){function n(e){return void 0===e&&(e={}),e.globalAlpha=.005,e.mode="blind",t.call(this,e)||this}return function(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function o(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(o.prototype=n.prototype,new o)}(n,t),n.decode=function(t){var e=Object.assign({url:"",fillColor:"#000",compositeOperation:"color-burn",mode:"canvas"},t);if(e.url&&"canvas"===e.mode){var n=new Image;n.src=e.url,n.onload=function(){var t,o=n.width,r=n.height,a=s.createCanvas(o,r),c=a.getContext("2d");if(null===c)throw new Error("get context error");c.drawImage(n,0,0,o,r),c.globalCompositeOperation=e.compositeOperation,c.fillStyle=e.fillColor,c.fillRect(0,0,o,r);var h=i(a);e.onSuccess&&"function"==typeof e.onSuccess&&(null===(t=e.onSuccess)||void 0===t||t.call(e,h))}}},n}(s);t.BlindWatermark=c,t.Watermark=s}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).watermark={})}(this,(function(t){"use strict";var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},e(t,o)};function o(t,e,o,n){return new(o||(o=Promise))((function(i,r){function a(t){try{c(n.next(t))}catch(t){r(t)}}function s(t){try{c(n.throw(t))}catch(t){r(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(a,s)}c((n=n.apply(t,e||[])).next())}))}function n(t,e){var o,n,i,r,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function s(r){return function(s){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,n&&(i=2&r[0]?n.return:r[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,r[1])).done)return i;switch(n=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return a.label++,{value:r[1],done:!1};case 5:a.label++,n=r[1],r=[0];continue;case 7:r=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){a=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){a.label=r[1];break}if(6===r[0]&&a.label<i[1]){a.label=i[1],i=r;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(r);break}i[2]&&a.ops.pop(),a.trys.pop();continue}r=e.call(t,a)}catch(t){r=[6,t],n=0}finally{o=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,s])}}}var i=function(t){return t.toDataURL("image/png",1)},r=function(t){return void 0===t},a=function(t,e,o){void 0===e&&(e={}),void 0===o&&(o="http://www.w3.org/2000/svg");var n=document.createElementNS(o,t);for(var i in e)n.setAttribute(i,e[i]);return n},s=function(){function t(t){void 0===t&&(t={}),this.parentElement=document.body,this.props=t,this.options=Object.assign({width:300,height:300,rotate:45,translatePlacement:"middle",contentType:"text",content:"hello watermark-js-plus",textType:"fill",imageWidth:0,imageHeight:0,lineHeight:30,zIndex:1e4,backgroundPosition:"0 0, 0 0",backgroundRepeat:"repeat",fontSize:20,fontFamily:"sans-serif",fontColor:"#000",globalAlpha:.5,fontWeight:"normal",mode:"default",mutationObserve:!0,unique:!0,parent:"body",onSuccess:function(){},onBeforeDestroy:function(){},onDestroyed:function(){}},t),this.changeParentElement(this.options.parent),this.initializeOptions()}return t.createCanvas=function(t,e){var o,n=window.devicePixelRatio||1,i=document.createElement("canvas");return i.width=t*n,i.height=e*n,i.style.width="".concat(t,"px"),i.style.height="".concat(e,"px"),null===(o=i.getContext("2d"))||void 0===o||o.setTransform(n,0,0,n,0,0),i},t.prototype.changeParentElement=function(t){if("string"==typeof t){var e=document.querySelector(t);e&&(this.parentElement=e)}else this.parentElement=t},t.prototype.create=function(){var t,e;return o(this,void 0,void 0,(function(){var o,r,a,s;return n(this,(function(n){switch(n.label){case 0:return this.validateUnique()&&this.validateContent()?[4,this.draw()]:[2];case 1:return o=n.sent(),r=i(o),this.watermarkDom=document.createElement("div"),a=document.createElement("div"),this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,s=this.checkParentElementType(),this.watermarkDom.style.cssText="\n z-index: ".concat(this.options.zIndex,";\n ").concat("custom"===s?"top: 0;bottom: 0;left: 0;right: 0;height: 100%;pointer-events: none;position: absolute":"position: relative","\n "),a.style.cssText="\n position: ".concat("root"===s?"fixed;":"absolute;","\n z-index: ").concat(this.options.zIndex,";\n pointer-events: none;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n background-image: url(").concat(r,");\n background-repeat: ").concat(this.options.backgroundRepeat,";\n background-size: ").concat(this.options.width,"px ").concat(this.options.height,"px;\n background-position: ").concat(this.options.backgroundPosition,";\n -webkit-print-color-adjust: exact;\n "),this.watermarkDom.append(a),this.parentElement.appendChild(this.watermarkDom),this.options.mutationObserve&&this.bindMutationObserve(),null===(e=(t=this.options).onSuccess)||void 0===e||e.call(t),[2]}}))}))},t.prototype.destroy=function(){var t,e,o,n,i,r,a;null===(e=(t=this.options).onBeforeDestroy)||void 0===e||e.call(t),null===(o=this.observer)||void 0===o||o.disconnect(),null===(n=this.parentObserve)||void 0===n||n.disconnect(),null===(i=this.watermarkDom)||void 0===i||i.remove(),null===(a=(r=this.options).onDestroyed)||void 0===a||a.call(r)},t.prototype.initializeOptions=function(){var t,e,o,n,i,a,s,c,h;(null===(t=this.options)||void 0===t?void 0:t.rotate)&&(this.options.rotate=(360-this.options.rotate%360)*(Math.PI/180));var l="middle",p="center";switch(this.options.translatePlacement){case"top":c=this.options.width/2,h=0,l="top";break;case"top-start":c=0,h=0,l="top",p="start";break;case"top-end":c=this.options.width,h=0,l="top",p="end";break;case"bottom":c=this.options.width/2,h=this.options.height,l="bottom";break;case"bottom-start":c=0,h=this.options.height,l="bottom",p="start";break;case"bottom-end":c=this.options.width,h=this.options.height,l="bottom",p="end";break;case"left":c=0,h=this.options.height/2,p="start";break;case"right":c=this.options.width,h=this.options.height/2,p="end";break;case"middle":c=this.options.width/2,h=this.options.height/2}r(null===(e=this.props)||void 0===e?void 0:e.translateX)&&r(null===(o=this.props)||void 0===o?void 0:o.translateY)||(l="top",p="left"),r(null===(n=this.props)||void 0===n?void 0:n.translateX)&&(this.options.translateX=c),r(null===(i=this.props)||void 0===i?void 0:i.translateY)&&(this.options.translateY=h),r(null===(a=this.props)||void 0===a?void 0:a.textBaseline)&&(this.options.textBaseline=l),r(null===(s=this.props)||void 0===s?void 0:s.textAlign)&&(this.options.textAlign=p)},t.prototype.validateUnique=function(){var t=!0;return this.options.unique&&this.parentElement.childNodes.forEach((function(e){t&&Object.hasOwnProperty.call(e,"__WATERMARK__")&&(t=!1)})),t},t.prototype.validateContent=function(){switch(this.options.contentType){case"image":return Object.hasOwnProperty.call(this.options,"image");case"multi-line-text":case"rich-text":case"text":return this.options.content.length>0}return!1},t.prototype.draw=function(){var e=this,o=t.createCanvas(this.options.width,this.options.height).getContext("2d");if(null===o)throw new Error("get context error");return o.font="".concat(this.options.fontWeight," ").concat(this.options.fontSize,"px ").concat(this.options.fontFamily),this.options.textAlign&&(o.textAlign=this.options.textAlign),this.options.textBaseline&&(o.textBaseline=this.options.textBaseline),this.setTextStyle(o),o.globalAlpha=this.options.globalAlpha,o.translate(this.options.translateX,this.options.translateY),o.rotate(this.options.rotate),new Promise((function(t){switch(e.options.contentType){case"text":e.drawText(o,t);break;case"image":e.drawImage(o,t);break;case"multi-line-text":e.drawMultiLineText(o,t);break;case"rich-text":e.drawRichText(o,t)}}))},t.prototype.setTextStyle=function(t){var e="fillStyle";"stroke"===this.options.textType&&(e="strokeStyle"),t[e]&&(t[e]=this.options.fontColor)},t.prototype.setText=function(t,e){var o="fillText";"stroke"===this.options.textType&&(o="strokeText"),t[o]&&t[o](e.text,e.x,e.y,e.maxWidth)},t.prototype.drawText=function(t,e){this.setText(t,{text:this.options.content,x:0,y:0,maxWidth:this.options.textRowMaxWidth}),e(t.canvas)},t.prototype.drawImage=function(t,e){var o=this,n=new Image;n.setAttribute("crossOrigin","Anonymous"),n.src=this.options.image,n.onload=function(){var i=o.getImageRect(n),r=i.width,a=i.height,s=o.getDrawImagePosition(r,a);t.drawImage(n,s.x,s.y,r,a),e(t.canvas)}},t.prototype.drawMultiLineText=function(t,e){var o,n=this,i=this.options.textRowMaxWidth||this.options.width,r=function(t,e,o){for(var n=[],i="",r=0,a=e.length;r<a;r++)i+=e.charAt(r),t.measureText(i).width>o&&(n.push(i.substring(0,i.length-1)),i="",r--);return n.push(i),n}(t,this.options.content,i);switch(this.options.textBaseline){case"middle":o=(r.length-1)*this.options.lineHeight/2;break;case"bottom":case"alphabetic":case"ideographic":o=(r.length-1)*this.options.lineHeight;break;case"top":case"hanging":o=0}r.forEach((function(e,i){n.setText(t,{text:e,x:0,y:n.options.lineHeight*i-o})})),e(t.canvas)},t.prototype.drawRichText=function(t,e){var o,n,i=this,r=function(t,e){var o=a("svg",{xmlns:"http://www.w3.org/2000/svg"}),n=document.createElement("div");n.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),n.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(t.font,";\n color: ").concat(e.fontColor,";\n"),n.innerHTML='<div class="rich-text-content">'.concat(e.content,"</div>"),document.body.appendChild(n);var i=n.querySelector(".rich-text-content"),r=i.offsetHeight,s=i.offsetWidth;document.body.removeChild(n);var c=e.richTextWidth||s||e.width,h=e.richTextHeight||r||e.height,l=a("foreignObject",{width:c.toString(),height:h.toString()});return l.appendChild(n),o.appendChild(l),{element:o,width:c,height:h}}(t,this.options),s=new Image;s.width=r.width,s.height=r.height,s.src=(o=r.element,n=o.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23"),"data:image/svg+xml;charset=utf-8,".concat(n)),s.onload=function(){var o=i.getDrawImagePosition(s.width,s.height);t.drawImage(s,o.x,o.y,t.canvas.width,t.canvas.height),e(t.canvas)}},t.prototype.getImageRect=function(t){var e={width:this.options.imageWidth,height:this.options.imageHeight};switch(!0){case 0!==e.width&&0===e.height:e.height=e.width*t.height/t.width;break;case 0===e.width&&0!==e.height:e.width=e.height*t.width/t.height;break;case 0===e.width&&0===e.height:e.width=t.width,e.height=t.height}return e},t.prototype.getDrawImagePosition=function(t,e){var o,n,i={x:-t/2,y:-e/2};switch(this.options.translatePlacement){case"top":i.x=-t/2,i.y=0;break;case"top-start":i.x=0,i.y=0;break;case"top-end":i.x=-t,i.y=0;break;case"bottom":i.x=-t/2,i.y=-e;break;case"bottom-start":i.x=0,i.y=-e;break;case"bottom-end":i.x=-t,i.y=-e;break;case"left":i.x=0,i.y=-e/2;break;case"right":i.x=-t,i.y=-e/2}return!r(null===(o=this.props)||void 0===o?void 0:o.translateX)&&(i.x=0),!r(null===(n=this.props)||void 0===n?void 0:n.translateY)&&(i.y=0),i},t.prototype.checkParentElementType=function(){return["html","body"].includes(this.parentElement.tagName.toLocaleLowerCase())?"root":"custom"},t.prototype.bindMutationObserve=function(){var t=this;this.watermarkDom&&(this.observer=new MutationObserver((function(e){e.length>0&&(t.destroy(),t.create())})),this.observer.observe(this.watermarkDom,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.parentObserve=new MutationObserver((function(e){e.forEach((function(e){var o;(null==e?void 0:e.target)!==t.watermarkDom&&(null===(o=null==e?void 0:e.removedNodes)||void 0===o?void 0:o[0])!==t.watermarkDom||(t.destroy(),t.create())}))})),this.parentObserve.observe(this.parentElement,{attributes:!0,childList:!0,subtree:!0,characterData:!0}))},t}(),c=function(t){function o(e){return void 0===e&&(e={}),e.globalAlpha=.005,e.mode="blind",t.call(this,e)||this}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}(o,t),o.decode=function(t){var e=Object.assign({url:"",fillColor:"#000",compositeOperation:"color-burn",mode:"canvas"},t);if(e.url&&"canvas"===e.mode){var o=new Image;o.src=e.url,o.onload=function(){var t,n=o.width,r=o.height,a=s.createCanvas(n,r),c=a.getContext("2d");if(null===c)throw new Error("get context error");c.drawImage(o,0,0,n,r),c.globalCompositeOperation=e.compositeOperation,c.fillStyle=e.fillColor,c.fillRect(0,0,n,r);var h=i(a);e.onSuccess&&"function"==typeof e.onSuccess&&(null===(t=e.onSuccess)||void 0===t||t.call(e,h))}}},o}(s);t.BlindWatermark=c,t.Watermark=s}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "watermark-js-plus",
3
- "version": "0.3.0",
3
+ "version": "0.5.1",
4
4
  "description": "watermark for the browser",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -44,20 +44,20 @@
44
44
  "dist"
45
45
  ],
46
46
  "devDependencies": {
47
- "@babel/core": "^7.20.7",
47
+ "@babel/core": "^7.20.12",
48
48
  "@babel/eslint-parser": "^7.19.1",
49
49
  "@babel/plugin-transform-runtime": "^7.19.6",
50
50
  "@babel/preset-env": "^7.20.2",
51
- "@commitlint/cli": "^17.3.0",
52
- "@commitlint/config-conventional": "^17.3.0",
51
+ "@commitlint/cli": "^17.4.0",
52
+ "@commitlint/config-conventional": "^17.4.0",
53
53
  "@element-plus/icons-vue": "^2.0.10",
54
54
  "@rollup/plugin-babel": "^6.0.3",
55
55
  "@rollup/plugin-commonjs": "^24.0.0",
56
56
  "@rollup/plugin-eslint": "^9.0.1",
57
57
  "@rollup/plugin-node-resolve": "^15.0.1",
58
58
  "@rollup/plugin-replace": "^5.0.2",
59
- "@rollup/plugin-terser": "0.2.1",
60
- "@rollup/plugin-typescript": "^10.0.1",
59
+ "@rollup/plugin-terser": "0.3.0",
60
+ "@rollup/plugin-typescript": "^11.0.0",
61
61
  "@types/markdown-it": "^12.2.3",
62
62
  "@typescript-eslint/parser": "^5.48.0",
63
63
  "@vue/theme": "^1.3.0",
@@ -75,10 +75,10 @@
75
75
  "markdown-it": "^13.0.1",
76
76
  "rollup": "^3.9.1",
77
77
  "rollup-plugin-filesize": "^9.1.2",
78
- "rollup-plugin-your-function": "^0.4.6",
78
+ "rollup-plugin-your-function": "^0.4.7",
79
79
  "terser": "^5.16.1",
80
80
  "typescript": "^4.9.4",
81
81
  "unplugin-element-plus": "^0.4.1",
82
- "vitepress": "^1.0.0-alpha.34"
82
+ "vitepress": "^1.0.0-alpha.35"
83
83
  }
84
84
  }