watermark-js-plus 0.4.0 → 0.6.0

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.
@@ -33,6 +33,8 @@ export default class Watermark {
33
33
  private validateUnique;
34
34
  private validateContent;
35
35
  private draw;
36
+ private setStyle;
37
+ private setText;
36
38
  private drawText;
37
39
  private drawImage;
38
40
  private drawMultiLineText;
@@ -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,6 +18,8 @@ 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;
@@ -28,11 +31,15 @@ export interface WatermarkOptions {
28
31
  backgroundRepeat: string;
29
32
  fontSize: number;
30
33
  fontFamily: string;
34
+ fontStyle: string;
35
+ fontColor: string;
36
+ fontWeight: string;
31
37
  textAlign?: TextAlignType;
32
38
  textBaseline?: TextBaselineType;
33
- fontColor: string;
39
+ filter: string;
40
+ shadowStyle?: Partial<CanvasShadowStyles>;
34
41
  globalAlpha: number;
35
- fontWeight: string;
42
+ extraDrawFunc?: Function;
36
43
  mode: CreateWatermarkModeType;
37
44
  mutationObserve: boolean;
38
45
  unique: boolean;
@@ -148,6 +148,7 @@ var Watermark = /** @class */ (function () {
148
148
  translatePlacement: 'middle',
149
149
  contentType: 'text',
150
150
  content: 'hello watermark-js-plus',
151
+ textType: 'fill',
151
152
  imageWidth: 0,
152
153
  imageHeight: 0,
153
154
  lineHeight: 30,
@@ -156,9 +157,11 @@ var Watermark = /** @class */ (function () {
156
157
  backgroundRepeat: 'repeat',
157
158
  fontSize: 20,
158
159
  fontFamily: 'sans-serif',
160
+ fontStyle: '',
159
161
  fontColor: '#000',
160
- globalAlpha: 0.5,
161
162
  fontWeight: 'normal',
163
+ filter: 'none',
164
+ globalAlpha: 0.5,
162
165
  mode: 'default',
163
166
  mutationObserve: true,
164
167
  unique: true,
@@ -346,11 +349,7 @@ var Watermark = /** @class */ (function () {
346
349
  if (ctx === null) {
347
350
  throw new Error('get context error');
348
351
  }
349
- ctx.font = "".concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
350
- this.options.textAlign && (ctx.textAlign = this.options.textAlign);
351
- this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
352
- ctx.fillStyle = this.options.fontColor;
353
- ctx.globalAlpha = this.options.globalAlpha;
352
+ this.setStyle(ctx);
354
353
  ctx.translate(this.options.translateX, this.options.translateY);
355
354
  ctx.rotate(this.options.rotate);
356
355
  return new Promise(function (resolve) {
@@ -370,8 +369,41 @@ var Watermark = /** @class */ (function () {
370
369
  }
371
370
  });
372
371
  };
372
+ Watermark.prototype.setStyle = function (ctx) {
373
+ var propName = 'fillStyle';
374
+ if (this.options.textType === 'stroke') {
375
+ propName = 'strokeStyle';
376
+ }
377
+ ctx[propName] && (ctx[propName] = this.options.fontColor);
378
+ ctx.font = "".concat(this.options.fontStyle, " ").concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
379
+ ctx.filter = this.options.filter;
380
+ this.options.textAlign && (ctx.textAlign = this.options.textAlign);
381
+ this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
382
+ ctx.globalAlpha = this.options.globalAlpha;
383
+ if (this.options.shadowStyle) {
384
+ ctx.shadowBlur = this.options.shadowStyle.shadowBlur || 0;
385
+ ctx.shadowColor = this.options.shadowStyle.shadowColor || '#00000000';
386
+ ctx.shadowOffsetX = this.options.shadowStyle.shadowOffsetX || 0;
387
+ ctx.shadowOffsetY = this.options.shadowStyle.shadowOffsetY || 0;
388
+ }
389
+ if (isFunction(this.options.extraDrawFunc)) {
390
+ this.options.extraDrawFunc(ctx);
391
+ }
392
+ };
393
+ Watermark.prototype.setText = function (ctx, params) {
394
+ var methodName = 'fillText';
395
+ if (this.options.textType === 'stroke') {
396
+ methodName = 'strokeText';
397
+ }
398
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
399
+ };
373
400
  Watermark.prototype.drawText = function (ctx, resolve) {
374
- ctx.fillText(this.options.content, 0, 0);
401
+ this.setText(ctx, {
402
+ text: this.options.content,
403
+ x: 0,
404
+ y: 0,
405
+ maxWidth: this.options.textRowMaxWidth
406
+ });
375
407
  resolve(ctx.canvas);
376
408
  };
377
409
  Watermark.prototype.drawImage = function (ctx, resolve) {
@@ -404,7 +436,8 @@ var Watermark = /** @class */ (function () {
404
436
  // )
405
437
  // resolve(canvas)
406
438
  // }
407
- var lines = getMultiLineData(ctx, this.options.content, this.options.width);
439
+ var width = this.options.textRowMaxWidth || this.options.width;
440
+ var lines = getMultiLineData(ctx, this.options.content, width);
408
441
  var yOffsetValue;
409
442
  switch (this.options.textBaseline) {
410
443
  case 'middle':
@@ -420,8 +453,8 @@ var Watermark = /** @class */ (function () {
420
453
  yOffsetValue = 0;
421
454
  break;
422
455
  }
423
- lines.forEach(function (txt, index) {
424
- ctx.fillText(txt, 0, _this.options.lineHeight * index - yOffsetValue);
456
+ lines.forEach(function (text, index) {
457
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
425
458
  });
426
459
  resolve(ctx.canvas);
427
460
  };
@@ -154,6 +154,7 @@
154
154
  translatePlacement: 'middle',
155
155
  contentType: 'text',
156
156
  content: 'hello watermark-js-plus',
157
+ textType: 'fill',
157
158
  imageWidth: 0,
158
159
  imageHeight: 0,
159
160
  lineHeight: 30,
@@ -162,9 +163,11 @@
162
163
  backgroundRepeat: 'repeat',
163
164
  fontSize: 20,
164
165
  fontFamily: 'sans-serif',
166
+ fontStyle: '',
165
167
  fontColor: '#000',
166
- globalAlpha: 0.5,
167
168
  fontWeight: 'normal',
169
+ filter: 'none',
170
+ globalAlpha: 0.5,
168
171
  mode: 'default',
169
172
  mutationObserve: true,
170
173
  unique: true,
@@ -352,11 +355,7 @@
352
355
  if (ctx === null) {
353
356
  throw new Error('get context error');
354
357
  }
355
- ctx.font = "".concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
356
- this.options.textAlign && (ctx.textAlign = this.options.textAlign);
357
- this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
358
- ctx.fillStyle = this.options.fontColor;
359
- ctx.globalAlpha = this.options.globalAlpha;
358
+ this.setStyle(ctx);
360
359
  ctx.translate(this.options.translateX, this.options.translateY);
361
360
  ctx.rotate(this.options.rotate);
362
361
  return new Promise(function (resolve) {
@@ -376,8 +375,41 @@
376
375
  }
377
376
  });
378
377
  };
378
+ Watermark.prototype.setStyle = function (ctx) {
379
+ var propName = 'fillStyle';
380
+ if (this.options.textType === 'stroke') {
381
+ propName = 'strokeStyle';
382
+ }
383
+ ctx[propName] && (ctx[propName] = this.options.fontColor);
384
+ ctx.font = "".concat(this.options.fontStyle, " ").concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
385
+ ctx.filter = this.options.filter;
386
+ this.options.textAlign && (ctx.textAlign = this.options.textAlign);
387
+ this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
388
+ ctx.globalAlpha = this.options.globalAlpha;
389
+ if (this.options.shadowStyle) {
390
+ ctx.shadowBlur = this.options.shadowStyle.shadowBlur || 0;
391
+ ctx.shadowColor = this.options.shadowStyle.shadowColor || '#00000000';
392
+ ctx.shadowOffsetX = this.options.shadowStyle.shadowOffsetX || 0;
393
+ ctx.shadowOffsetY = this.options.shadowStyle.shadowOffsetY || 0;
394
+ }
395
+ if (isFunction(this.options.extraDrawFunc)) {
396
+ this.options.extraDrawFunc(ctx);
397
+ }
398
+ };
399
+ Watermark.prototype.setText = function (ctx, params) {
400
+ var methodName = 'fillText';
401
+ if (this.options.textType === 'stroke') {
402
+ methodName = 'strokeText';
403
+ }
404
+ ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
405
+ };
379
406
  Watermark.prototype.drawText = function (ctx, resolve) {
380
- ctx.fillText(this.options.content, 0, 0);
407
+ this.setText(ctx, {
408
+ text: this.options.content,
409
+ x: 0,
410
+ y: 0,
411
+ maxWidth: this.options.textRowMaxWidth
412
+ });
381
413
  resolve(ctx.canvas);
382
414
  };
383
415
  Watermark.prototype.drawImage = function (ctx, resolve) {
@@ -410,7 +442,8 @@
410
442
  // )
411
443
  // resolve(canvas)
412
444
  // }
413
- var lines = getMultiLineData(ctx, this.options.content, this.options.width);
445
+ var width = this.options.textRowMaxWidth || this.options.width;
446
+ var lines = getMultiLineData(ctx, this.options.content, width);
414
447
  var yOffsetValue;
415
448
  switch (this.options.textBaseline) {
416
449
  case 'middle':
@@ -426,8 +459,8 @@
426
459
  yOffsetValue = 0;
427
460
  break;
428
461
  }
429
- lines.forEach(function (txt, index) {
430
- ctx.fillText(txt, 0, _this.options.lineHeight * index - yOffsetValue);
462
+ lines.forEach(function (text, index) {
463
+ _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
431
464
  });
432
465
  resolve(ctx.canvas);
433
466
  };
@@ -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,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",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),o.fillStyle=this.options.fontColor,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.drawText=function(t,e){t.fillText(this.options.content,0,0),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=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,this.options.width);switch(this.options.textBaseline){case"middle":o=(i.length-1)*this.options.lineHeight/2;break;case"bottom":case"alphabetic":case"ideographic":o=(i.length-1)*this.options.lineHeight;break;case"top":case"hanging":o=0}i.forEach((function(e,i){t.fillText(e,0,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}));
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,s){function r(t){try{h(n.next(t))}catch(t){s(t)}}function a(t){try{h(n.throw(t))}catch(t){s(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(r,a)}h((n=n.apply(t,e||[])).next())}))}function n(t,e){var o,n,i,s,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return s={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(s){return function(a){return function(s){if(o)throw new TypeError("Generator is already executing.");for(;r;)try{if(o=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,n=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){r.label=s[1];break}if(6===s[0]&&r.label<i[1]){r.label=i[1],i=s;break}if(i&&r.label<i[2]){r.label=i[2],r.ops.push(s);break}i[2]&&r.ops.pop(),r.trys.pop();continue}s=e.call(t,r)}catch(t){s=[6,t],n=0}finally{o=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}var i=function(t){return t.toDataURL("image/png",1)},s=function(t){return"function"==typeof t},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},h=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",fontStyle:"",fontColor:"#000",fontWeight:"normal",filter:"none",globalAlpha:.5,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,s,r,a;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(),s=i(o),this.watermarkDom=document.createElement("div"),r=document.createElement("div"),this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,a=this.checkParentElementType(),this.watermarkDom.style.cssText="\n z-index: ".concat(this.options.zIndex,";\n ").concat("custom"===a?"top: 0;bottom: 0;left: 0;right: 0;height: 100%;pointer-events: none;position: absolute":"position: relative","\n "),r.style.cssText="\n position: ".concat("root"===a?"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(s,");\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(r),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,s,r;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===(r=(s=this.options).onDestroyed)||void 0===r||r.call(s)},t.prototype.initializeOptions=function(){var t,e,o,n,i,s,a,h,c;(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":h=this.options.width/2,c=0,l="top";break;case"top-start":h=0,c=0,l="top",p="start";break;case"top-end":h=this.options.width,c=0,l="top",p="end";break;case"bottom":h=this.options.width/2,c=this.options.height,l="bottom";break;case"bottom-start":h=0,c=this.options.height,l="bottom",p="start";break;case"bottom-end":h=this.options.width,c=this.options.height,l="bottom",p="end";break;case"left":h=0,c=this.options.height/2,p="start";break;case"right":h=this.options.width,c=this.options.height/2,p="end";break;case"middle":h=this.options.width/2,c=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=h),r(null===(i=this.props)||void 0===i?void 0:i.translateY)&&(this.options.translateY=c),r(null===(s=this.props)||void 0===s?void 0:s.textBaseline)&&(this.options.textBaseline=l),r(null===(a=this.props)||void 0===a?void 0:a.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 this.setStyle(o),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.setStyle=function(t){var e="fillStyle";"stroke"===this.options.textType&&(e="strokeStyle"),t[e]&&(t[e]=this.options.fontColor),t.font="".concat(this.options.fontStyle," ").concat(this.options.fontWeight," ").concat(this.options.fontSize,"px ").concat(this.options.fontFamily),t.filter=this.options.filter,this.options.textAlign&&(t.textAlign=this.options.textAlign),this.options.textBaseline&&(t.textBaseline=this.options.textBaseline),t.globalAlpha=this.options.globalAlpha,this.options.shadowStyle&&(t.shadowBlur=this.options.shadowStyle.shadowBlur||0,t.shadowColor=this.options.shadowStyle.shadowColor||"#00000000",t.shadowOffsetX=this.options.shadowStyle.shadowOffsetX||0,t.shadowOffsetY=this.options.shadowStyle.shadowOffsetY||0),s(this.options.extraDrawFunc)&&this.options.extraDrawFunc(t)},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),s=i.width,r=i.height,a=o.getDrawImagePosition(s,r);t.drawImage(n,a.x,a.y,s,r),e(t.canvas)}},t.prototype.drawMultiLineText=function(t,e){var o,n=this,i=this.options.textRowMaxWidth||this.options.width,s=function(t,e,o){for(var n=[],i="",s=0,r=e.length;s<r;s++)i+=e.charAt(s),t.measureText(i).width>o&&(n.push(i.substring(0,i.length-1)),i="",s--);return n.push(i),n}(t,this.options.content,i);switch(this.options.textBaseline){case"middle":o=(s.length-1)*this.options.lineHeight/2;break;case"bottom":case"alphabetic":case"ideographic":o=(s.length-1)*this.options.lineHeight;break;case"top":case"hanging":o=0}s.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,s=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"),s=i.offsetHeight,r=i.offsetWidth;document.body.removeChild(n);var h=e.richTextWidth||r||e.width,c=e.richTextHeight||s||e.height,l=a("foreignObject",{width:h.toString(),height:c.toString()});return l.appendChild(n),o.appendChild(l),{element:o,width:h,height:c}}(t,this.options),r=new Image;r.width=s.width,r.height=s.height,r.src=(o=s.element,n=o.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23"),"data:image/svg+xml;charset=utf-8,".concat(n)),r.onload=function(){var o=i.getDrawImagePosition(r.width,r.height);t.drawImage(r,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=h.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 l=i(a);e.onSuccess&&s(e.onSuccess)&&(null===(t=e.onSuccess)||void 0===t||t.call(e,l))}}},o}(h);t.BlindWatermark=c,t.Watermark=h}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "watermark-js-plus",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "watermark for the browser",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",