watermark-js-plus 1.3.39 → 1.4.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.
- package/dist/index.cjs.js +22 -24
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.esm.js +22 -24
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.iife.js +22 -24
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.umd.js +22 -24
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/types/docs/components/config.d.ts +1 -0
- package/dist/types/src/types/index.d.ts +1 -0
- package/package.json +7 -7
package/dist/index.cjs.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=function(e,i){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])},t(e,i)};var e=function(){return e=Object.assign||function(t){for(var e,i=1,a=arguments.length;i<a;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},e.apply(this,arguments)};function i(t,e,i,a){return new(i||(i=Promise))((function(n,o){function r(t){try{l(a.next(t))}catch(t){o(t)}}function s(t){try{l(a.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,s)}l((a=a.apply(t,e||[])).next())}))}function a(t,e){var i,a,n,o,r={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(s){return function(l){return function(s){if(i)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(r=0)),r;)try{if(i=1,a&&(n=2&s[0]?a.return:s[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,s[1])).done)return n;switch(a=0,n&&(s=[2&s[0],n.value]),s[0]){case 0:case 1:n=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,a=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(n=r.trys,(n=n.length>0&&n[n.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!n||s[1]>n[0]&&s[1]<n[3])){r.label=s[1];break}if(6===s[0]&&r.label<n[1]){r.label=n[1],n=s;break}if(n&&r.label<n[2]){r.label=n[2],r.ops.push(s);break}n[2]&&r.ops.pop(),r.trys.pop();continue}s=e.call(t,r)}catch(t){s=[6,t],a=0}finally{i=n=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,l])}}}var n=function(t){return t.toDataURL("image/png",1)},o=function(t){return"function"==typeof t},r=function(t){return void 0===t},s=function(t,e,i){void 0===e&&(e={}),void 0===i&&(i="http://www.w3.org/2000/svg");var a=document.createElementNS(i,t);for(var n in e)a.setAttribute(n,e[n]);return a},l=function(t,e){return r(t)?e:t},d=function(t,e,i){void 0===e&&(e=void 0),void 0===i&&(i=void 0);var a=new Image;return a.setAttribute("crossOrigin","Anonymous"),!r(e)&&(a.width=e),!r(i)&&(a.height=i),a.src=t,new Promise((function(t){a.onload=function(){t(a)}}))},c={width:300,height:300,rotate:45,layout:"default",auxiliaryLine:!1,translatePlacement:"middle",contentType:"text",content:"hello watermark-js-plus",textType:"fill",imageWidth:0,imageHeight:0,lineHeight:30,zIndex:2147483647,backgroundPosition:"0 0",backgroundRepeat:"repeat",fontSize:"20px",fontFamily:"sans-serif",fontStyle:"",fontVariant:"",fontColor:"#000",fontWeight:"normal",filter:"none",globalAlpha:.5,mode:"default",mutationObserve:!0,movable:!1,parent:"body",onSuccess:function(){},onBeforeDestroy:function(){},onDestroyed:function(){},onObserveError:function(){}},h=function(){function t(e,i){this.props=e,this.options=i,this.canvas=t.createCanvas(this.options.width,this.options.height),this.recommendOptions=function(t,e,i){var a=t.getContext("2d");if(null===a)throw new Error("get context error");a.font="".concat(e.fontStyle," ").concat(e.fontVariant," ").concat(e.fontWeight," ").concat(e.fontSize," ").concat(e.fontFamily),a.filter=e.filter,(null==e?void 0:e.rotate)&&(e.rotate=(360-e.rotate%360)*(Math.PI/180)),r(i.textRowMaxWidth)&&(e.textRowMaxWidth=e.width);var n={image:{rect:{width:e.imageWidth,height:e.imageHeight},position:{x:0,y:0}},textLine:{data:[],yOffsetValue:0},advancedStyleParams:{linear:{x0:0,x1:0},radial:{x0:0,y0:0,r0:0,x1:0,y1:0,r1:0},conic:{x:0,y:0,startAngle:0},pattern:{}}};switch(e.contentType){case"text":n.textLine.data=[e.content];break;case"multi-line-text":n.textLine.data=function(t,e,i){for(var a=[],n="",o=0,r=e.length;o<r;o++)n+=e.charAt(o),t.measureText(n).width>i&&(a.push(n.substring(0,n.length-1)),n="",o--);return a.push(n),a}(a,e.content,e.textRowMaxWidth)}var o=e.width/2,s=e.height/2,l="middle",d="center";switch(r(null==i?void 0:i.translateX)||r(null==i?void 0:i.translateY)?(n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.r0=0,n.advancedStyleParams.radial.r1=e.width/2):(o=null==i?void 0:i.translateX,s=null==i?void 0:i.translateY,l="top",d="left"),i.translatePlacement){case"top":o=e.width/2,s=0,l="top",n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"top-start":o=0,s=0,l="top",d="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"top-end":o=e.width,s=0,l="top",d="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"bottom":o=e.width/2,s=e.height,l="bottom",n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=0,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"bottom-start":o=0,s=e.height,l="bottom",d="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"bottom-end":o=e.width,s=e.height,l="bottom",d="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"left":o=0,s=e.height/2,d="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=0;break;case"right":o=e.width,s=e.height/2,d="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=0}if(e.translateX=o,e.translateY=s,r(null==i?void 0:i.textBaseline)&&(e.textBaseline=l),r(null==i?void 0:i.textAlign)&&(e.textAlign=d),["text","multi-line-text"].includes(e.contentType))switch(e.textBaseline){case"middle":n.textLine.yOffsetValue=(n.textLine.data.length-1)*e.lineHeight/2;break;case"bottom":case"alphabetic":case"ideographic":n.textLine.yOffsetValue=(n.textLine.data.length-1)*e.lineHeight+(e.lineHeight-parseInt(e.fontSize))/2;break;case"top":case"hanging":n.textLine.yOffsetValue=-e.lineHeight/2+parseInt(e.fontSize)/2}return n}(this.canvas,this.options,this.props)}return t.createCanvas=function(t,e){var i,a=window.devicePixelRatio||1,n=document.createElement("canvas");return n.width=t*a,n.height=e*a,n.style.width="".concat(t,"px"),n.style.height="".concat(e,"px"),null===(i=n.getContext("2d"))||void 0===i||i.setTransform(a,0,0,a,0,0),n},t.clearCanvas=function(t){var e=t.getContext("2d");if(null===e)throw new Error("get context error");e.restore(),e.clearRect(0,0,t.width,t.height)},t.prototype.getCanvas=function(){return this.canvas},t.prototype.clear=function(){t.clearCanvas(this.canvas)},t.prototype.draw=function(){var t=this,e=this.canvas.getContext("2d");if(null===e)throw new Error("get context error");return this.options.auxiliaryLine&&(e.beginPath(),e.rect(0,0,this.options.width,this.options.height),e.lineWidth=1,e.strokeStyle="#000",e.stroke(),e.closePath(),e.beginPath(),e.rect(this.options.translateX,this.options.translateY,1,1),e.lineWidth=1,e.strokeStyle="#f00",e.stroke(),e.closePath()),this.setStyle(e),e.save(),e.translate(this.options.translateX,this.options.translateY),e.rotate(this.options.rotate),new Promise((function(i){switch(t.options.contentType){case"text":t.drawText(e,i);break;case"image":t.drawImage(e,i);break;case"multi-line-text":t.drawMultiLineText(e,i);break;case"rich-text":t.drawRichText(e,i)}}))},t.prototype.setStyle=function(t){var e,i="fillStyle";"stroke"===this.options.textType&&(i="strokeStyle");var a=this.options.fontColor;if(null===(e=this.options)||void 0===e?void 0:e.advancedStyle)switch(this.options.advancedStyle.type){case"linear":a=this.createLinearGradient(t);break;case"radial":a=this.createRadialGradient(t);break;case"conic":a=this.createConicGradient(t);break;case"pattern":a=this.createPattern(t)}t[i]&&a&&(t[i]=a),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=l(this.options.shadowStyle.shadowBlur,0),t.shadowColor=l(this.options.shadowStyle.shadowColor,"#00000000"),t.shadowOffsetX=l(this.options.shadowStyle.shadowOffsetX,0),t.shadowOffsetY=l(this.options.shadowStyle.shadowOffsetY,0)),o(this.options.extraDrawFunc)&&this.options.extraDrawFunc(t)},t.prototype.createLinearGradient=function(t){var e,i,a,n,o,r,s,d,c,h,v,u,p,m,y,g=t.createLinearGradient(l(null===(a=null===(i=null===(e=this.options.advancedStyle)||void 0===e?void 0:e.params)||void 0===i?void 0:i.linear)||void 0===a?void 0:a.x0,this.recommendOptions.advancedStyleParams.linear.x0),l(null===(r=null===(o=null===(n=this.options.advancedStyle)||void 0===n?void 0:n.params)||void 0===o?void 0:o.linear)||void 0===r?void 0:r.y0,0),l(null===(c=null===(d=null===(s=this.options.advancedStyle)||void 0===s?void 0:s.params)||void 0===d?void 0:d.linear)||void 0===c?void 0:c.x1,this.recommendOptions.advancedStyleParams.linear.x1),l(null===(u=null===(v=null===(h=this.options.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.linear)||void 0===u?void 0:u.y1,0));return null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.colorStops)||void 0===y||y.forEach((function(t){g.addColorStop(t.offset,t.color)})),g},t.prototype.createConicGradient=function(t){var e,i,a,n,o,r,s,d,c,h,v,u,p,m,y,g=t.createConicGradient(l(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.conic)||void 0===n?void 0:n.startAngle,0),l(null===(d=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.conic)||void 0===d?void 0:d.x,this.recommendOptions.advancedStyleParams.conic.x),l(null===(u=null===(v=null===(h=null===(c=this.options)||void 0===c?void 0:c.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.conic)||void 0===u?void 0:u.y,this.recommendOptions.advancedStyleParams.conic.y));return null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.colorStops)||void 0===y||y.forEach((function(t){g.addColorStop(t.offset,t.color)})),g},t.prototype.createRadialGradient=function(t){var e,i,a,n,o,r,s,d,c,h,v,u,p,m,y,g,f,w,x,b,S,k,P,O,C,L,E,T=t.createRadialGradient(l(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.radial)||void 0===n?void 0:n.x0,this.recommendOptions.advancedStyleParams.radial.x0),l(null===(d=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.radial)||void 0===d?void 0:d.y0,this.recommendOptions.advancedStyleParams.radial.y0),l(null===(u=null===(v=null===(h=null===(c=this.options)||void 0===c?void 0:c.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.radial)||void 0===u?void 0:u.r0,this.recommendOptions.advancedStyleParams.radial.r0),l(null===(g=null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.params)||void 0===y?void 0:y.radial)||void 0===g?void 0:g.x1,this.recommendOptions.advancedStyleParams.radial.x1),l(null===(b=null===(x=null===(w=null===(f=this.options)||void 0===f?void 0:f.advancedStyle)||void 0===w?void 0:w.params)||void 0===x?void 0:x.radial)||void 0===b?void 0:b.y1,this.recommendOptions.advancedStyleParams.radial.y1),l(null===(O=null===(P=null===(k=null===(S=this.options)||void 0===S?void 0:S.advancedStyle)||void 0===k?void 0:k.params)||void 0===P?void 0:P.radial)||void 0===O?void 0:O.r1,this.recommendOptions.advancedStyleParams.radial.r1));return null===(E=null===(L=null===(C=this.options)||void 0===C?void 0:C.advancedStyle)||void 0===L?void 0:L.colorStops)||void 0===E||E.forEach((function(t){T.addColorStop(t.offset,t.color)})),T},t.prototype.createPattern=function(t){var e,i,a,n,o,r,s,l;return t.createPattern(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.pattern)||void 0===n?void 0:n.image,(null===(l=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.pattern)||void 0===l?void 0:l.repetition)||"")},t.prototype.setText=function(t,e){var i="fillText";"stroke"===this.options.textType&&(i="strokeText"),t[i]&&t[i](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-this.recommendOptions.textLine.yOffsetValue,maxWidth:this.options.textRowMaxWidth||this.options.width}),e(t.canvas)},t.prototype.drawImage=function(t,e){var i=this;d(this.options.image).then((function(a){var n=i.getImageRect(a),o=n.width,r=n.height,s=i.getDrawImagePosition(o,r);t.drawImage(a,s.x,s.y,o,r),e(t.canvas)}))},t.prototype.drawMultiLineText=function(t,e){var i=this,a=this.recommendOptions.textLine.data,n=this.recommendOptions.textLine.yOffsetValue;a.forEach((function(e,a){i.setText(t,{text:e,x:0,y:i.options.lineHeight*a-n})})),e(t.canvas)},t.prototype.drawRichText=function(t,e){var i,a,n=this,o=function(t,e){var i=s("svg",{xmlns:"http://www.w3.org/2000/svg"}),a=document.createElement("div");a.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),a.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"),a.innerHTML='<div class="rich-text-content">'.concat(e.content,"</div>"),document.body.appendChild(a);var n=a.querySelector(".rich-text-content"),o=n.offsetHeight,r=n.offsetWidth;document.body.removeChild(a);var l=e.richTextWidth||r||e.width,d=e.richTextHeight||o||e.height;i.setAttribute("width",l.toString()),i.setAttribute("height",d.toString());var c=s("foreignObject",{width:l.toString(),height:d.toString()});return c.appendChild(a),i.appendChild(c),{element:i,width:l,height:d}}(t,this.options);d((i=o.element,a=i.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23"),"data:image/svg+xml;charset=utf-8,".concat(a)),o.width,o.height).then((function(i){var a=n.getDrawImagePosition(i.width,i.height);t.drawImage(i,a.x,a.y,i.width,i.height),e(t.canvas)}))},t.prototype.getImageRect=function(t){var e={width:this.options.imageWidth||0,height:this.options.imageHeight||0};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 i,a,n={x:-t/2,y:-e/2};switch(this.options.translatePlacement){case"top":n.x=-t/2,n.y=0;break;case"top-start":n.x=0,n.y=0;break;case"top-end":n.x=-t,n.y=0;break;case"bottom":n.x=-t/2,n.y=-e;break;case"bottom-start":n.x=0,n.y=-e;break;case"bottom-end":n.x=-t,n.y=-e;break;case"left":n.x=0,n.y=-e/2;break;case"right":n.x=-t,n.y=-e/2}return!r(null===(i=this.props)||void 0===i?void 0:i.translateX)&&(n.x=0),!r(null===(a=this.props)||void 0===a?void 0:a.translateY)&&(n.y=0),n},t}(),v=function(){function t(t,e){var i,a,n,o,r,s,l;this.options=t,this.partialWidth=this.options.width,this.partialHeight=this.options.height,this.rows=(null===(i=this.options.gridLayoutOptions)||void 0===i?void 0:i.rows)||1,this.cols=(null===(a=this.options.gridLayoutOptions)||void 0===a?void 0:a.cols)||1,this.matrix=(null===(n=this.options.gridLayoutOptions)||void 0===n?void 0:n.matrix)||(r=this.rows,s=this.cols,l=1,Array.from({length:r},(function(){return new Array(s).fill(l)}))),this.gap=(null===(o=this.options.gridLayoutOptions)||void 0===o?void 0:o.gap)||[0,0],this.partialCanvas=e}return t.prototype.draw=function(){var t,e,i,a,n,o,r,s,l=h.createCanvas((null===(t=this.options.gridLayoutOptions)||void 0===t?void 0:t.width)||this.partialWidth*this.cols+this.gap[0]*this.cols,(null===(e=this.options.gridLayoutOptions)||void 0===e?void 0:e.height)||this.partialHeight*this.rows+this.gap[1]*this.rows),d=l.getContext("2d");(null===(i=this.options.gridLayoutOptions)||void 0===i?void 0:i.backgroundImage)&&(null==d||d.drawImage(null===(a=this.options.gridLayoutOptions)||void 0===a?void 0:a.backgroundImage,0,0,null===(n=this.options.gridLayoutOptions)||void 0===n?void 0:n.width,null===(o=this.options.gridLayoutOptions)||void 0===o?void 0:o.height));for(var c=0;c<this.rows;c++)for(var v=0;v<this.cols;v++)(null===(s=null===(r=this.matrix)||void 0===r?void 0:r[c])||void 0===s?void 0:s[v])&&(null==d||d.drawImage(this.partialCanvas,this.partialWidth*v+this.gap[0]*v,this.partialHeight*c+this.gap[1]*c,this.partialWidth,this.partialHeight));return l},t}(),u=function(t,e){return"grid"===t.layout?new v(t,e).draw():e},p=function(){function t(t){void 0===t&&(t={}),this.parentElement=document.body,this.props=t,this.options=Object.assign({},c,t),this.changeParentElement(this.options.parent),this.watermarkCanvas=new h(this.props,this.options),"undefined"!=typeof window&&(Object.defineProperty(window,"MutationObserver",{writable:!1,configurable:!1}),Object.defineProperty(window,"requestAnimationFrame",{writable:!1,configurable:!1}))}return t.prototype.changeOptions=function(t,e,n){return void 0===t&&(t={}),void 0===e&&(e="overwrite"),void 0===n&&(n=!0),i(this,void 0,void 0,(function(){return a(this,(function(i){switch(i.label){case 0:return this.initConfigData(t,e),n?(this.remove(),[4,this.create()]):[3,2];case 1:i.sent(),i.label=2;case 2:return[2]}}))}))},t.prototype.create=function(){var t,e,o,s,l,d,c;return i(this,void 0,void 0,(function(){var i,h,v,p,m;return a(this,(function(a){switch(a.label){case 0:return this.validateUnique()&&this.validateContent()?(i=r(this.watermarkDom),[4,null===(t=this.watermarkCanvas)||void 0===t?void 0:t.draw()]):[2];case 1:if(a.sent(),this.layoutCanvas=u(this.options,null===(e=this.watermarkCanvas)||void 0===e?void 0:e.getCanvas()),h=n(this.layoutCanvas),null===(o=this.watermarkCanvas)||void 0===o||o.clear(),this.watermarkDom=document.createElement("div"),v=document.createElement("div"),this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,p=this.checkParentElementType(),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("custom"===p?"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 "),m=function(t){var e,i,a;if("grid"===t.layout){var n=(null===(e=t.gridLayoutOptions)||void 0===e?void 0:e.cols)||1,o=(null===(i=t.gridLayoutOptions)||void 0===i?void 0:i.rows)||1,r=(null===(a=t.gridLayoutOptions)||void 0===a?void 0:a.gap)||[0,0];return[t.width*n+r[0]*n,t.height*o+r[1]*o]}return[t.width,t.height]}(this.options),v.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("root"===p?"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(h,")!important;background-repeat:").concat(this.options.backgroundRepeat,"!important;\n background-size:").concat(m[0],"px ").concat(m[1],"px!important;background-position:").concat(this.options.backgroundPosition,";\n ").concat(this.options.movable?"animation: 200s ease 0s infinite normal none running watermark !important;":"","\n "),this.watermarkDom.append(v),this.parentElement.appendChild(this.watermarkDom),this.options.mutationObserve)try{this.bindMutationObserve()}catch(t){null===(l=(s=this.options).onObserveError)||void 0===l||l.call(s)}return i&&(null===(c=(d=this.options).onSuccess)||void 0===c||c.call(d)),[2]}}))}))},t.prototype.destroy=function(){this.remove(),this.watermarkDom=void 0},t.prototype.check=function(){return i(this,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return this.parentElement.contains(this.watermarkDom)?[3,2]:(this.remove(),[4,this.create()]);case 1:t.sent(),t.label=2;case 2:return[2]}}))}))},t.prototype.remove=function(){var t,e,i,a,n,o,r;null===(e=(t=this.options).onBeforeDestroy)||void 0===e||e.call(t),null===(i=this.observer)||void 0===i||i.disconnect(),null===(a=this.parentObserve)||void 0===a||a.disconnect(),this.unbindCheckWatermarkElementEvent(),null===(n=this.watermarkDom)||void 0===n||n.remove(),null===(r=(o=this.options).onDestroyed)||void 0===r||r.call(o)},t.prototype.initConfigData=function(t,e){var i=this;void 0===e&&(e="overwrite"),"append"===e?Object.keys(t).forEach((function(e){i.props&&(i.props[e]=t[e])})):this.props=t,this.options=Object.assign({},c,this.props),this.changeParentElement(this.options.parent),this.watermarkCanvas=new h(this.props,this.options)},t.prototype.changeParentElement=function(t){if("string"==typeof t){var e=document.querySelector(t);e&&(this.parentElement=e)}else this.parentElement=t},t.prototype.validateUnique=function(){var t=!0;return 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.checkParentElementType=function(){return["html","body"].includes(this.parentElement.tagName.toLocaleLowerCase())?"root":"custom"},t.prototype.checkWatermarkElement=function(){return i(this,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return this.parentElement.contains(this.watermarkDom)?[3,2]:(this.remove(),[4,this.create()]);case 1:t.sent(),t.label=2;case 2:return this.bindCheckWatermarkElementEvent(),[2]}}))}))},t.prototype.bindMutationObserve=function(){var t=this;this.watermarkDom&&(this.bindCheckWatermarkElementEvent(),this.observer=new MutationObserver((function(e){return i(t,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return e.length>0?(this.remove(),[4,this.create()]):[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))})),this.observer.observe(this.watermarkDom,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.parentObserve=new MutationObserver((function(e){return i(t,void 0,void 0,(function(){var t,i,n,o;return a(this,(function(a){switch(a.label){case 0:t=0,i=e,a.label=1;case 1:return t<i.length?(null==(n=i[t])?void 0:n.target)===this.watermarkDom||(null===(o=null==n?void 0:n.removedNodes)||void 0===o?void 0:o[0])===this.watermarkDom||"childList"===n.type&&n.target===this.parentElement&&n.target.lastChild!==this.watermarkDom?(this.remove(),[4,this.create()]):[3,3]:[3,4];case 2:a.sent(),a.label=3;case 3:return t++,[3,1];case 4:return[2]}}))}))})),this.parentObserve.observe(this.parentElement,{attributes:!0,childList:!0,subtree:!0,characterData:!0}))},t.prototype.bindCheckWatermarkElementEvent=function(){this.unbindCheckWatermarkElementEvent(),this.checkWatermarkElementRequestID=requestAnimationFrame(this.checkWatermarkElement.bind(this))},t.prototype.unbindCheckWatermarkElementEvent=function(){r(this.checkWatermarkElementRequestID)||cancelAnimationFrame(this.checkWatermarkElementRequestID)},t}(),m=function(e){function i(t){return void 0===t&&(t={}),t.globalAlpha=.005,t.mode="blind",e.call(this,t)||this}return function(e,i){if("function"!=typeof i&&null!==i)throw new TypeError("Class extends value "+String(i)+" is not a constructor or null");function a(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(a.prototype=i.prototype,new a)}(i,e),i.decode=function(t){var e=Object.assign({url:"",fillColor:"#000",compositeOperation:"color-burn",mode:"canvas",compositeTimes:3},t);if(e.url&&"canvas"===e.mode){var i=new Image;i.src=e.url,i.onload=function(){var t,a=i.width,r=i.height,s=h.createCanvas(a,r),l=s.getContext("2d");if(null===l)throw new Error("get context error");l.drawImage(i,0,0,a,r),l.globalCompositeOperation=e.compositeOperation,l.fillStyle=e.fillColor;for(var d=0;d<e.compositeTimes;d++)l.fillRect(0,0,a,r);var c=n(s);e.onSuccess&&o(e.onSuccess)&&(null===(t=e.onSuccess)||void 0===t||t.call(e,c))}}},i}(p),y=function(){function t(t){var e;void 0===t&&(t={}),this.drew=!1,this.props=t,this.options=Object.assign({},c,t),this.watermarkCanvas=new h(this.props,this.options),this.originalSrc=null===(e=this.props.dom)||void 0===e?void 0:e.src,this.backgroundImage=this.getBackgroundImage()}return t.prototype.create=function(){var t,o,r,s,l;return i(this,void 0,void 0,(function(){return a(this,(function(i){switch(i.label){case 0:return this.drew?[2]:[4,null===(t=this.watermarkCanvas)||void 0===t?void 0:t.draw()];case 1:return i.sent(),this.options.layout="grid",this.options.gridLayoutOptions=e(e({},this.options.gridLayoutOptions),{width:null===(o=this.backgroundImage)||void 0===o?void 0:o.width,height:null===(r=this.backgroundImage)||void 0===r?void 0:r.height,backgroundImage:this.backgroundImage}),this.layoutCanvas=u(this.options,null===(s=this.watermarkCanvas)||void 0===s?void 0:s.getCanvas()),this.options.dom.src=n(this.layoutCanvas),null===(l=this.watermarkCanvas)||void 0===l||l.clear(),this.drew=!0,[2]}}))}))},t.prototype.destroy=function(){this.options.dom.src=this.originalSrc,this.drew=!1},t.prototype.getBackgroundImage=function(){if(this.options.dom)return this.options.dom},t}();!function(t,e){void 0===e&&(e={});var i=e.insertAt;if(t&&"undefined"!=typeof document){var a=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===i&&a.firstChild?a.insertBefore(n,a.firstChild):a.appendChild(n),n.styleSheet?n.styleSheet.cssText=t:n.appendChild(document.createTextNode(t))}}("@keyframes watermark{0%{background-position:0 0}25%{background-position:100% 100%}50%{background-position:0 0}75%{background-position:100% -100%}to{background-position:0 0}}"),exports.BlindWatermark=m,exports.ImageWatermark=y,exports.Watermark=p;
|
|
1
|
+
"use strict";var t=function(e,i){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])},t(e,i)};var e=function(){return e=Object.assign||function(t){for(var e,i=1,a=arguments.length;i<a;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},e.apply(this,arguments)};function i(t,e,i,a){return new(i||(i=Promise))((function(n,o){function r(t){try{d(a.next(t))}catch(t){o(t)}}function s(t){try{d(a.throw(t))}catch(t){o(t)}}function d(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,s)}d((a=a.apply(t,e||[])).next())}))}function a(t,e){var i,a,n,o,r={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(s){return function(d){return function(s){if(i)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(r=0)),r;)try{if(i=1,a&&(n=2&s[0]?a.return:s[0]?a.throw||((n=a.return)&&n.call(a),0):a.next)&&!(n=n.call(a,s[1])).done)return n;switch(a=0,n&&(s=[2&s[0],n.value]),s[0]){case 0:case 1:n=s;break;case 4:return r.label++,{value:s[1],done:!1};case 5:r.label++,a=s[1],s=[0];continue;case 7:s=r.ops.pop(),r.trys.pop();continue;default:if(!(n=r.trys,(n=n.length>0&&n[n.length-1])||6!==s[0]&&2!==s[0])){r=0;continue}if(3===s[0]&&(!n||s[1]>n[0]&&s[1]<n[3])){r.label=s[1];break}if(6===s[0]&&r.label<n[1]){r.label=n[1],n=s;break}if(n&&r.label<n[2]){r.label=n[2],r.ops.push(s);break}n[2]&&r.ops.pop(),r.trys.pop();continue}s=e.call(t,r)}catch(t){s=[6,t],a=0}finally{i=n=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,d])}}}var n=function(t){return t.toDataURL("image/png",1)},o=function(t){return"function"==typeof t},r=function(t){return void 0===t},s=function(t,e,i){void 0===e&&(e={}),void 0===i&&(i="http://www.w3.org/2000/svg");var a=document.createElementNS(i,t);for(var n in e)a.setAttribute(n,e[n]);return a},d=function(t,e){return r(t)?e:t},l=function(t,e,i){void 0===e&&(e=void 0),void 0===i&&(i=void 0);var a=new Image;return a.setAttribute("crossOrigin","Anonymous"),!r(e)&&(a.width=e),!r(i)&&(a.height=i),a.src=t,new Promise((function(t){a.onload=function(){t(a)}}))},c={width:300,height:300,rotate:45,layout:"default",auxiliaryLine:!1,translatePlacement:"middle",contentType:"text",content:"hello watermark-js-plus",textType:"fill",imageWidth:0,imageHeight:0,lineHeight:30,zIndex:2147483647,backgroundPosition:"0 0",backgroundRepeat:"repeat",fontSize:"20px",fontFamily:"sans-serif",fontStyle:"",fontVariant:"",fontColor:"#000",fontWeight:"normal",filter:"none",letterSpacing:"0px",globalAlpha:.5,mode:"default",mutationObserve:!0,movable:!1,parent:"body",onSuccess:function(){},onBeforeDestroy:function(){},onDestroyed:function(){},onObserveError:function(){}},h=function(){function t(e,i){this.props=e,this.options=i,this.canvas=t.createCanvas(this.options.width,this.options.height),this.recommendOptions=function(t,e,i){var a=t.getContext("2d");if(null===a)throw new Error("get context error");a.font="".concat(e.fontStyle," ").concat(e.fontVariant," ").concat(e.fontWeight," ").concat(e.fontSize," ").concat(e.fontFamily),a.filter=e.filter,a.letterSpacing=e.letterSpacing,(null==e?void 0:e.rotate)&&(e.rotate=(360-e.rotate%360)*(Math.PI/180)),r(i.textRowMaxWidth)&&(e.textRowMaxWidth=e.width);var n={image:{rect:{width:e.imageWidth,height:e.imageHeight},position:{x:0,y:0}},textLine:{data:[],yOffsetValue:0},advancedStyleParams:{linear:{x0:0,x1:0},radial:{x0:0,y0:0,r0:0,x1:0,y1:0,r1:0},conic:{x:0,y:0,startAngle:0},pattern:{}}};switch(e.contentType){case"text":n.textLine.data=[e.content];break;case"multi-line-text":n.textLine.data=function(t,e,i){for(var a=[],n="",o=0,r=e.length;o<r;o++)n+=e.charAt(o),t.measureText(n).width>i&&(a.push(n.substring(0,n.length-1)),n="",o--);return a.push(n),a}(a,e.content,e.textRowMaxWidth)}var o=e.width/2,s=e.height/2,d="middle",l="center";switch(r(null==i?void 0:i.translateX)||r(null==i?void 0:i.translateY)?(n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.r0=0,n.advancedStyleParams.radial.r1=e.width/2):(o=null==i?void 0:i.translateX,s=null==i?void 0:i.translateY,d="top",l="left"),i.translatePlacement){case"top":o=e.width/2,s=0,d="top",n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"top-start":o=0,s=0,d="top",l="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"top-end":o=e.width,s=0,d="top",l="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.y0=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.radial.y1=n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=n.textLine.data.length*e.lineHeight/2;break;case"bottom":o=e.width/2,s=e.height,d="bottom",n.advancedStyleParams.linear.x0=-e.width/2,n.advancedStyleParams.linear.x1=e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=0,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"bottom-start":o=0,s=e.height,d="bottom",l="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"bottom-end":o=e.width,s=e.height,d="bottom",l="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.y0=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.radial.y1=-n.textLine.data.length*e.lineHeight/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=-n.textLine.data.length*e.lineHeight/2;break;case"left":o=0,s=e.height/2,l="start",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=e.width,n.advancedStyleParams.radial.x0=e.width/2,n.advancedStyleParams.radial.x1=e.width/2,n.advancedStyleParams.conic.x=e.width/2,n.advancedStyleParams.conic.y=0;break;case"right":o=e.width,s=e.height/2,l="end",n.advancedStyleParams.linear.x0=0,n.advancedStyleParams.linear.x1=-e.width,n.advancedStyleParams.radial.x0=-e.width/2,n.advancedStyleParams.radial.x1=-e.width/2,n.advancedStyleParams.conic.x=-e.width/2,n.advancedStyleParams.conic.y=0}if(e.translateX=o,e.translateY=s,r(null==i?void 0:i.textBaseline)&&(e.textBaseline=d),r(null==i?void 0:i.textAlign)&&(e.textAlign=l),["text","multi-line-text"].includes(e.contentType))switch(e.textBaseline){case"middle":n.textLine.yOffsetValue=(n.textLine.data.length-1)*e.lineHeight/2;break;case"bottom":case"alphabetic":case"ideographic":n.textLine.yOffsetValue=(n.textLine.data.length-1)*e.lineHeight+(e.lineHeight-parseInt(e.fontSize))/2;break;case"top":case"hanging":n.textLine.yOffsetValue=-e.lineHeight/2+parseInt(e.fontSize)/2}return n}(this.canvas,this.options,this.props)}return t.createCanvas=function(t,e){var i,a=window.devicePixelRatio||1,n=document.createElement("canvas");return n.width=t*a,n.height=e*a,n.style.width="".concat(t,"px"),n.style.height="".concat(e,"px"),null===(i=n.getContext("2d"))||void 0===i||i.setTransform(a,0,0,a,0,0),n},t.clearCanvas=function(t){var e=t.getContext("2d");if(null===e)throw new Error("get context error");e.restore(),e.clearRect(0,0,t.width,t.height)},t.prototype.getCanvas=function(){return this.canvas},t.prototype.clear=function(){t.clearCanvas(this.canvas)},t.prototype.draw=function(){var t=this,e=this.canvas.getContext("2d");if(null===e)throw new Error("get context error");return this.options.auxiliaryLine&&(e.beginPath(),e.rect(0,0,this.options.width,this.options.height),e.lineWidth=1,e.strokeStyle="#000",e.stroke(),e.closePath(),e.beginPath(),e.rect(this.options.translateX,this.options.translateY,1,1),e.lineWidth=1,e.strokeStyle="#f00",e.stroke(),e.closePath()),this.setStyle(e),e.save(),e.translate(this.options.translateX,this.options.translateY),e.rotate(this.options.rotate),new Promise((function(i){switch(t.options.contentType){case"text":t.drawText(e,i);break;case"image":t.drawImage(e,i);break;case"multi-line-text":t.drawMultiLineText(e,i);break;case"rich-text":t.drawRichText(e,i)}}))},t.prototype.setStyle=function(t){var e,i="fillStyle";"stroke"===this.options.textType&&(i="strokeStyle");var a=this.options.fontColor;if(null===(e=this.options)||void 0===e?void 0:e.advancedStyle)switch(this.options.advancedStyle.type){case"linear":a=this.createLinearGradient(t);break;case"radial":a=this.createRadialGradient(t);break;case"conic":a=this.createConicGradient(t);break;case"pattern":a=this.createPattern(t)}t[i]&&a&&(t[i]=a),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=d(this.options.shadowStyle.shadowBlur,0),t.shadowColor=d(this.options.shadowStyle.shadowColor,"#00000000"),t.shadowOffsetX=d(this.options.shadowStyle.shadowOffsetX,0),t.shadowOffsetY=d(this.options.shadowStyle.shadowOffsetY,0)),o(this.options.extraDrawFunc)&&this.options.extraDrawFunc(t)},t.prototype.createLinearGradient=function(t){var e,i,a,n,o,r,s,l,c,h,v,u,p,m,y,g=t.createLinearGradient(d(null===(a=null===(i=null===(e=this.options.advancedStyle)||void 0===e?void 0:e.params)||void 0===i?void 0:i.linear)||void 0===a?void 0:a.x0,this.recommendOptions.advancedStyleParams.linear.x0),d(null===(r=null===(o=null===(n=this.options.advancedStyle)||void 0===n?void 0:n.params)||void 0===o?void 0:o.linear)||void 0===r?void 0:r.y0,0),d(null===(c=null===(l=null===(s=this.options.advancedStyle)||void 0===s?void 0:s.params)||void 0===l?void 0:l.linear)||void 0===c?void 0:c.x1,this.recommendOptions.advancedStyleParams.linear.x1),d(null===(u=null===(v=null===(h=this.options.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.linear)||void 0===u?void 0:u.y1,0));return null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.colorStops)||void 0===y||y.forEach((function(t){g.addColorStop(t.offset,t.color)})),g},t.prototype.createConicGradient=function(t){var e,i,a,n,o,r,s,l,c,h,v,u,p,m,y,g=t.createConicGradient(d(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.conic)||void 0===n?void 0:n.startAngle,0),d(null===(l=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.conic)||void 0===l?void 0:l.x,this.recommendOptions.advancedStyleParams.conic.x),d(null===(u=null===(v=null===(h=null===(c=this.options)||void 0===c?void 0:c.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.conic)||void 0===u?void 0:u.y,this.recommendOptions.advancedStyleParams.conic.y));return null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.colorStops)||void 0===y||y.forEach((function(t){g.addColorStop(t.offset,t.color)})),g},t.prototype.createRadialGradient=function(t){var e,i,a,n,o,r,s,l,c,h,v,u,p,m,y,g,f,w,x,b,S,k,P,O,C,L,E,T=t.createRadialGradient(d(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.radial)||void 0===n?void 0:n.x0,this.recommendOptions.advancedStyleParams.radial.x0),d(null===(l=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.radial)||void 0===l?void 0:l.y0,this.recommendOptions.advancedStyleParams.radial.y0),d(null===(u=null===(v=null===(h=null===(c=this.options)||void 0===c?void 0:c.advancedStyle)||void 0===h?void 0:h.params)||void 0===v?void 0:v.radial)||void 0===u?void 0:u.r0,this.recommendOptions.advancedStyleParams.radial.r0),d(null===(g=null===(y=null===(m=null===(p=this.options)||void 0===p?void 0:p.advancedStyle)||void 0===m?void 0:m.params)||void 0===y?void 0:y.radial)||void 0===g?void 0:g.x1,this.recommendOptions.advancedStyleParams.radial.x1),d(null===(b=null===(x=null===(w=null===(f=this.options)||void 0===f?void 0:f.advancedStyle)||void 0===w?void 0:w.params)||void 0===x?void 0:x.radial)||void 0===b?void 0:b.y1,this.recommendOptions.advancedStyleParams.radial.y1),d(null===(O=null===(P=null===(k=null===(S=this.options)||void 0===S?void 0:S.advancedStyle)||void 0===k?void 0:k.params)||void 0===P?void 0:P.radial)||void 0===O?void 0:O.r1,this.recommendOptions.advancedStyleParams.radial.r1));return null===(E=null===(L=null===(C=this.options)||void 0===C?void 0:C.advancedStyle)||void 0===L?void 0:L.colorStops)||void 0===E||E.forEach((function(t){T.addColorStop(t.offset,t.color)})),T},t.prototype.createPattern=function(t){var e,i,a,n,o,r,s,d;return t.createPattern(null===(n=null===(a=null===(i=null===(e=this.options)||void 0===e?void 0:e.advancedStyle)||void 0===i?void 0:i.params)||void 0===a?void 0:a.pattern)||void 0===n?void 0:n.image,(null===(d=null===(s=null===(r=null===(o=this.options)||void 0===o?void 0:o.advancedStyle)||void 0===r?void 0:r.params)||void 0===s?void 0:s.pattern)||void 0===d?void 0:d.repetition)||"")},t.prototype.setText=function(t,e){var i="fillText";"stroke"===this.options.textType&&(i="strokeText"),t[i]&&t[i](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-this.recommendOptions.textLine.yOffsetValue,maxWidth:this.options.textRowMaxWidth||this.options.width}),e(t.canvas)},t.prototype.drawImage=function(t,e){var i=this;l(this.options.image).then((function(a){var n=i.getImageRect(a),o=n.width,r=n.height,s=i.getDrawImagePosition(o,r);t.drawImage(a,s.x,s.y,o,r),e(t.canvas)}))},t.prototype.drawMultiLineText=function(t,e){var i=this,a=this.recommendOptions.textLine.data,n=this.recommendOptions.textLine.yOffsetValue;a.forEach((function(e,a){i.setText(t,{text:e,x:0,y:i.options.lineHeight*a-n})})),e(t.canvas)},t.prototype.drawRichText=function(t,e){var i,a,n=this,o=function(t,e){var i=s("svg",{xmlns:"http://www.w3.org/2000/svg"}),a=document.createElement("div");a.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),a.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"),a.innerHTML='<div class="rich-text-content">'.concat(e.content,"</div>"),document.body.appendChild(a);var n=a.querySelector(".rich-text-content"),o=n.offsetHeight,r=n.offsetWidth;document.body.removeChild(a);var d=e.richTextWidth||r||e.width,l=e.richTextHeight||o||e.height;i.setAttribute("width",d.toString()),i.setAttribute("height",l.toString());var c=s("foreignObject",{width:d.toString(),height:l.toString()});return c.appendChild(a),i.appendChild(c),{element:i,width:d,height:l}}(t,this.options);l((i=o.element,a=i.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23"),"data:image/svg+xml;charset=utf-8,".concat(a)),o.width,o.height).then((function(i){var a=n.getDrawImagePosition(i.width,i.height);t.drawImage(i,a.x,a.y,i.width,i.height),e(t.canvas)}))},t.prototype.getImageRect=function(t){var e={width:this.options.imageWidth||0,height:this.options.imageHeight||0};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 i,a,n={x:-t/2,y:-e/2};switch(this.options.translatePlacement){case"top":n.x=-t/2,n.y=0;break;case"top-start":n.x=0,n.y=0;break;case"top-end":n.x=-t,n.y=0;break;case"bottom":n.x=-t/2,n.y=-e;break;case"bottom-start":n.x=0,n.y=-e;break;case"bottom-end":n.x=-t,n.y=-e;break;case"left":n.x=0,n.y=-e/2;break;case"right":n.x=-t,n.y=-e/2}return!r(null===(i=this.props)||void 0===i?void 0:i.translateX)&&(n.x=0),!r(null===(a=this.props)||void 0===a?void 0:a.translateY)&&(n.y=0),n},t}(),v=function(){function t(t,e){var i,a,n,o,r,s,d;this.options=t,this.partialWidth=this.options.width,this.partialHeight=this.options.height,this.rows=(null===(i=this.options.gridLayoutOptions)||void 0===i?void 0:i.rows)||1,this.cols=(null===(a=this.options.gridLayoutOptions)||void 0===a?void 0:a.cols)||1,this.matrix=(null===(n=this.options.gridLayoutOptions)||void 0===n?void 0:n.matrix)||(r=this.rows,s=this.cols,d=1,Array.from({length:r},(function(){return new Array(s).fill(d)}))),this.gap=(null===(o=this.options.gridLayoutOptions)||void 0===o?void 0:o.gap)||[0,0],this.partialCanvas=e}return t.prototype.draw=function(){var t,e,i,a,n,o,r,s,d=h.createCanvas((null===(t=this.options.gridLayoutOptions)||void 0===t?void 0:t.width)||this.partialWidth*this.cols+this.gap[0]*this.cols,(null===(e=this.options.gridLayoutOptions)||void 0===e?void 0:e.height)||this.partialHeight*this.rows+this.gap[1]*this.rows),l=d.getContext("2d");(null===(i=this.options.gridLayoutOptions)||void 0===i?void 0:i.backgroundImage)&&(null==l||l.drawImage(null===(a=this.options.gridLayoutOptions)||void 0===a?void 0:a.backgroundImage,0,0,null===(n=this.options.gridLayoutOptions)||void 0===n?void 0:n.width,null===(o=this.options.gridLayoutOptions)||void 0===o?void 0:o.height));for(var c=0;c<this.rows;c++)for(var v=0;v<this.cols;v++)(null===(s=null===(r=this.matrix)||void 0===r?void 0:r[c])||void 0===s?void 0:s[v])&&(null==l||l.drawImage(this.partialCanvas,this.partialWidth*v+this.gap[0]*v,this.partialHeight*c+this.gap[1]*c,this.partialWidth,this.partialHeight));return d},t}(),u=function(t,e){return"grid"===t.layout?new v(t,e).draw():e},p=function(){function t(t){void 0===t&&(t={}),this.parentElement=document.body,this.props=t,this.options=Object.assign({},c,t),this.changeParentElement(this.options.parent),this.watermarkCanvas=new h(this.props,this.options),"undefined"!=typeof window&&(Object.defineProperty(window,"MutationObserver",{writable:!1,configurable:!1}),Object.defineProperty(window,"requestAnimationFrame",{writable:!1,configurable:!1}))}return t.prototype.changeOptions=function(t,e,n){return void 0===t&&(t={}),void 0===e&&(e="overwrite"),void 0===n&&(n=!0),i(this,void 0,void 0,(function(){return a(this,(function(i){switch(i.label){case 0:return this.initConfigData(t,e),n?(this.remove(),[4,this.create()]):[3,2];case 1:i.sent(),i.label=2;case 2:return[2]}}))}))},t.prototype.create=function(){var t,e,o,s,d,l,c;return i(this,void 0,void 0,(function(){var i,h,v,p,m;return a(this,(function(a){switch(a.label){case 0:return this.validateUnique()&&this.validateContent()?(i=r(this.watermarkDom),[4,null===(t=this.watermarkCanvas)||void 0===t?void 0:t.draw()]):[2];case 1:if(a.sent(),this.layoutCanvas=u(this.options,null===(e=this.watermarkCanvas)||void 0===e?void 0:e.getCanvas()),h=n(this.layoutCanvas),null===(o=this.watermarkCanvas)||void 0===o||o.clear(),this.watermarkDom=document.createElement("div"),v=document.createElement("div"),this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,p=this.checkParentElementType(),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("custom"===p?"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 "),m=function(t){var e,i,a;if("grid"===t.layout){var n=(null===(e=t.gridLayoutOptions)||void 0===e?void 0:e.cols)||1,o=(null===(i=t.gridLayoutOptions)||void 0===i?void 0:i.rows)||1,r=(null===(a=t.gridLayoutOptions)||void 0===a?void 0:a.gap)||[0,0];return[t.width*n+r[0]*n,t.height*o+r[1]*o]}return[t.width,t.height]}(this.options),v.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("root"===p?"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(h,")!important;background-repeat:").concat(this.options.backgroundRepeat,"!important;\n background-size:").concat(m[0],"px ").concat(m[1],"px!important;background-position:").concat(this.options.backgroundPosition,";\n ").concat(this.options.movable?"animation: 200s ease 0s infinite normal none running watermark !important;":"","\n "),this.watermarkDom.append(v),this.parentElement.appendChild(this.watermarkDom),this.options.mutationObserve)try{this.bindMutationObserve()}catch(t){null===(d=(s=this.options).onObserveError)||void 0===d||d.call(s)}return i&&(null===(c=(l=this.options).onSuccess)||void 0===c||c.call(l)),[2]}}))}))},t.prototype.destroy=function(){this.remove(),this.watermarkDom=void 0},t.prototype.check=function(){return i(this,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return this.parentElement.contains(this.watermarkDom)?[3,2]:(this.remove(),[4,this.create()]);case 1:t.sent(),t.label=2;case 2:return[2]}}))}))},t.prototype.remove=function(){var t,e,i,a,n,o,r;null===(e=(t=this.options).onBeforeDestroy)||void 0===e||e.call(t),null===(i=this.observer)||void 0===i||i.disconnect(),null===(a=this.parentObserve)||void 0===a||a.disconnect(),this.unbindCheckWatermarkElementEvent(),null===(n=this.watermarkDom)||void 0===n||n.remove(),null===(r=(o=this.options).onDestroyed)||void 0===r||r.call(o)},t.prototype.initConfigData=function(t,e){var i=this;void 0===e&&(e="overwrite"),"append"===e?Object.keys(t).forEach((function(e){i.props&&(i.props[e]=t[e])})):this.props=t,this.options=Object.assign({},c,this.props),this.changeParentElement(this.options.parent),this.watermarkCanvas=new h(this.props,this.options)},t.prototype.changeParentElement=function(t){if("string"==typeof t){var e=document.querySelector(t);e&&(this.parentElement=e)}else this.parentElement=t},t.prototype.validateUnique=function(){var t=!0;return 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.checkParentElementType=function(){return["html","body"].includes(this.parentElement.tagName.toLocaleLowerCase())?"root":"custom"},t.prototype.checkWatermarkElement=function(){return i(this,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return this.parentElement.contains(this.watermarkDom)?[3,2]:(this.remove(),[4,this.create()]);case 1:t.sent(),t.label=2;case 2:return this.bindCheckWatermarkElementEvent(),[2]}}))}))},t.prototype.bindMutationObserve=function(){var t=this;this.watermarkDom&&(this.bindCheckWatermarkElementEvent(),this.observer=new MutationObserver((function(e){return i(t,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return e.length>0?(this.remove(),[4,this.create()]):[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))})),this.observer.observe(this.watermarkDom,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.parentObserve=new MutationObserver((function(e){return i(t,void 0,void 0,(function(){var t,i,n,o;return a(this,(function(a){switch(a.label){case 0:t=0,i=e,a.label=1;case 1:return t<i.length?(null==(n=i[t])?void 0:n.target)===this.watermarkDom||(null===(o=null==n?void 0:n.removedNodes)||void 0===o?void 0:o[0])===this.watermarkDom||"childList"===n.type&&n.target===this.parentElement&&n.target.lastChild!==this.watermarkDom?(this.remove(),[4,this.create()]):[3,3]:[3,4];case 2:a.sent(),a.label=3;case 3:return t++,[3,1];case 4:return[2]}}))}))})),this.parentObserve.observe(this.parentElement,{attributes:!0,childList:!0,subtree:!0,characterData:!0}))},t.prototype.bindCheckWatermarkElementEvent=function(){this.unbindCheckWatermarkElementEvent(),this.checkWatermarkElementRequestID=requestAnimationFrame(this.checkWatermarkElement.bind(this))},t.prototype.unbindCheckWatermarkElementEvent=function(){r(this.checkWatermarkElementRequestID)||cancelAnimationFrame(this.checkWatermarkElementRequestID)},t}(),m=function(i){function a(t){void 0===t&&(t={});return i.call(this,e(e({},t),{globalAlpha:.005,mode:"blind"}))||this}return function(e,i){if("function"!=typeof i&&null!==i)throw new TypeError("Class extends value "+String(i)+" is not a constructor or null");function a(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(a.prototype=i.prototype,new a)}(a,i),a.decode=function(t){var e=t.url,i=void 0===e?"":e,a=t.fillColor,r=void 0===a?"#000":a,s=t.compositeOperation,d=void 0===s?"color-burn":s,l=t.mode,c=void 0===l?"canvas":l,v=t.compositeTimes,u=void 0===v?3:v,p=t.onSuccess;if(i&&"canvas"===c){var m=new Image;m.src=i,m.addEventListener("load",(function(){var t=m.width,e=m.height,i=h.createCanvas(t,e),a=i.getContext("2d");if(!a)throw new Error("get context error");a.drawImage(m,0,0,t,e),a.globalCompositeOperation=d,a.fillStyle=r;for(var s=0;s<u;s++)a.fillRect(0,0,t,e);var l=n(i);o(p)&&(null==p||p(l))}))}},a}(p),y=function(){function t(t){var e;void 0===t&&(t={}),this.drew=!1,this.props=t,this.options=Object.assign({},c,t),this.watermarkCanvas=new h(this.props,this.options),this.originalSrc=null===(e=this.props.dom)||void 0===e?void 0:e.src,this.backgroundImage=this.getBackgroundImage()}return t.prototype.create=function(){var t,o,r,s,d;return i(this,void 0,void 0,(function(){return a(this,(function(i){switch(i.label){case 0:return this.drew?[2]:[4,null===(t=this.watermarkCanvas)||void 0===t?void 0:t.draw()];case 1:return i.sent(),this.options.layout="grid",this.options.gridLayoutOptions=e(e({},this.options.gridLayoutOptions),{width:null===(o=this.backgroundImage)||void 0===o?void 0:o.width,height:null===(r=this.backgroundImage)||void 0===r?void 0:r.height,backgroundImage:this.backgroundImage}),this.layoutCanvas=u(this.options,null===(s=this.watermarkCanvas)||void 0===s?void 0:s.getCanvas()),this.options.dom.src=n(this.layoutCanvas),null===(d=this.watermarkCanvas)||void 0===d||d.clear(),this.drew=!0,[2]}}))}))},t.prototype.destroy=function(){this.options.dom.src=this.originalSrc,this.drew=!1},t.prototype.getBackgroundImage=function(){if(this.options.dom)return this.options.dom},t}();!function(t,e){void 0===e&&(e={});var i=e.insertAt;if(t&&"undefined"!=typeof document){var a=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===i&&a.firstChild?a.insertBefore(n,a.firstChild):a.appendChild(n),n.styleSheet?n.styleSheet.cssText=t:n.appendChild(document.createTextNode(t))}}("@keyframes watermark{0%{background-position:0 0}25%{background-position:100% 100%}50%{background-position:0 0}75%{background-position:100% -100%}to{background-position:0 0}}"),exports.BlindWatermark=m,exports.ImageWatermark=y,exports.Watermark=p;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* watermark-js-plus v1.
|
|
2
|
+
* watermark-js-plus v1.4.0
|
|
3
3
|
* (c) 2022-2023 Michael Sun
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -195,6 +195,7 @@ var initialOptions = {
|
|
|
195
195
|
fontColor: '#000',
|
|
196
196
|
fontWeight: 'normal',
|
|
197
197
|
filter: 'none',
|
|
198
|
+
letterSpacing: '0px',
|
|
198
199
|
globalAlpha: 0.5,
|
|
199
200
|
mode: 'default',
|
|
200
201
|
mutationObserve: true,
|
|
@@ -212,6 +213,8 @@ var generateRecommendOptions = function (canvas, options, args) {
|
|
|
212
213
|
}
|
|
213
214
|
ctx.font = "".concat(options.fontStyle, " ").concat(options.fontVariant, " ").concat(options.fontWeight, " ").concat(options.fontSize, " ").concat(options.fontFamily);
|
|
214
215
|
ctx.filter = options.filter;
|
|
216
|
+
// @ts-ignore
|
|
217
|
+
ctx.letterSpacing = options.letterSpacing;
|
|
215
218
|
if (options === null || options === void 0 ? void 0 : options.rotate) {
|
|
216
219
|
options.rotate = (360 - options.rotate % 360) * (Math.PI / 180);
|
|
217
220
|
}
|
|
@@ -861,7 +864,7 @@ var Watermark = /** @class */ (function () {
|
|
|
861
864
|
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
862
865
|
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
863
866
|
parentElementType = this.checkParentElementType();
|
|
864
|
-
this.watermarkDom.style.cssText = "\n z-index:
|
|
867
|
+
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 ");
|
|
865
868
|
backgroundSize = generateBackgroundSize(this.options);
|
|
866
869
|
watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, ";\n ").concat(this.options.movable ? 'animation: 200s ease 0s infinite normal none running watermark !important;' : '', "\n ");
|
|
867
870
|
this.watermarkDom.append(watermarkInnerDom);
|
|
@@ -1066,47 +1069,42 @@ var BlindWatermark = /** @class */ (function (_super) {
|
|
|
1066
1069
|
*/
|
|
1067
1070
|
function BlindWatermark(props) {
|
|
1068
1071
|
if (props === void 0) { props = {}; }
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
+
var defaultProps = {
|
|
1073
|
+
globalAlpha: 0.005,
|
|
1074
|
+
mode: 'blind'
|
|
1075
|
+
};
|
|
1076
|
+
return _super.call(this, __assign(__assign({}, props), defaultProps)) || this;
|
|
1072
1077
|
}
|
|
1073
1078
|
/**
|
|
1074
1079
|
* Decode blind watermark.
|
|
1075
1080
|
* @param props - decode options
|
|
1076
1081
|
*/
|
|
1077
1082
|
BlindWatermark.decode = function (props) {
|
|
1078
|
-
var
|
|
1079
|
-
|
|
1080
|
-
fillColor: '#000',
|
|
1081
|
-
compositeOperation: 'color-burn',
|
|
1082
|
-
mode: 'canvas',
|
|
1083
|
-
compositeTimes: 3
|
|
1084
|
-
}, props);
|
|
1085
|
-
if (!options.url) {
|
|
1083
|
+
var _a = props.url, url = _a === void 0 ? '' : _a, _b = props.fillColor, fillColor = _b === void 0 ? '#000' : _b, _c = props.compositeOperation, compositeOperation = _c === void 0 ? 'color-burn' : _c, _d = props.mode, mode = _d === void 0 ? 'canvas' : _d, _e = props.compositeTimes, compositeTimes = _e === void 0 ? 3 : _e, onSuccess = props.onSuccess;
|
|
1084
|
+
if (!url) {
|
|
1086
1085
|
return;
|
|
1087
1086
|
}
|
|
1088
|
-
if (
|
|
1087
|
+
if (mode === 'canvas') {
|
|
1089
1088
|
var img_1 = new Image();
|
|
1090
|
-
img_1.src =
|
|
1091
|
-
img_1.
|
|
1092
|
-
var _a;
|
|
1089
|
+
img_1.src = url;
|
|
1090
|
+
img_1.addEventListener('load', function () {
|
|
1093
1091
|
var width = img_1.width, height = img_1.height;
|
|
1094
1092
|
var canvas = WatermarkCanvas.createCanvas(width, height);
|
|
1095
1093
|
var ctx = canvas.getContext('2d');
|
|
1096
|
-
if (ctx
|
|
1094
|
+
if (!ctx) {
|
|
1097
1095
|
throw new Error('get context error');
|
|
1098
1096
|
}
|
|
1099
1097
|
ctx.drawImage(img_1, 0, 0, width, height);
|
|
1100
|
-
ctx.globalCompositeOperation =
|
|
1101
|
-
ctx.fillStyle =
|
|
1102
|
-
for (var i = 0; i <
|
|
1098
|
+
ctx.globalCompositeOperation = compositeOperation;
|
|
1099
|
+
ctx.fillStyle = fillColor;
|
|
1100
|
+
for (var i = 0; i < compositeTimes; i++) {
|
|
1103
1101
|
ctx.fillRect(0, 0, width, height);
|
|
1104
1102
|
}
|
|
1105
1103
|
var resultImage = convertImage(canvas);
|
|
1106
|
-
if (
|
|
1107
|
-
|
|
1104
|
+
if (isFunction(onSuccess)) {
|
|
1105
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(resultImage);
|
|
1108
1106
|
}
|
|
1109
|
-
};
|
|
1107
|
+
});
|
|
1110
1108
|
}
|
|
1111
1109
|
};
|
|
1112
1110
|
return BlindWatermark;
|