auto-image-watermark
Version:
H5端一键生成海报,图片水印,图片合成等
1 lines • 6.36 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.H5SDK=t():e.H5SDK=t()}(window,(function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(module,__webpack_exports__,__webpack_require__){"use strict";__webpack_require__.d(__webpack_exports__,"a",(function(){return color2Rgb})),__webpack_require__.d(__webpack_exports__,"c",(function(){return hex2Rgb})),__webpack_require__.d(__webpack_exports__,"d",(function(){return rgb2rgba})),__webpack_require__.d(__webpack_exports__,"b",(function(){return configToData}));const color2Rgb=e=>{const t=document.createElement("div");t.style.color=e,document.body.appendChild(t);const n=window.getComputedStyle(t).color;return document.body.removeChild(t),n},mutiSort=e=>{e.sort((e,t)=>e.zIndex-t.zIndex)},hex2Rgb=hex=>{const rgb=[];if(/^\#[0-9A-F]{3}$/i.test(hex)){let e="#";hex.replace(/[0-9A-F]/gi,(function(t){e+=t+t})),hex=e}return/^#[0-9A-F]{6}$/i.test(hex)?(hex.replace(/[0-9A-F]{2}/gi,(function(kw){rgb.push(eval("0x"+kw))})),`rgb(${rgb.join(",")})`):"rgb(0,0,0)"},rgb2Hex=e=>{if(/^rgb\((\d{1,3}\,){2}\d{1,3}\)$/i.test(e)){let t="#";return e.replace(/\d{1,3}/g,(function(e){e=(e=parseInt(e,10).toString(16)).length<2?0+e:e,t+=e})),t}return"#000"},rgb2rgba=(e,t)=>{return`${e.split(")")[0]},${t})`},configToData=e=>{const{blocks:t=[],texts:n=[],images:o=[],lines:r=[]}=e,i=[];return t.length>0&&t.forEach(e=>{e.zIndex||(e.zIndex=0);const t=Object.assign(Object.assign({},e),{type:"blocks"});i.push(t)}),n.length>0&&n.forEach(e=>{e.zIndex||(e.zIndex=0);const t=Object.assign(Object.assign({},e),{type:"texts"});i.push(t)}),o.length>0&&o.forEach(e=>{e.zIndex||(e.zIndex=0);const t=Object.assign(Object.assign({},e),{type:"images"});i.push(t)}),r.length>0&&r.forEach(e=>{e.zIndex||(e.zIndex=0);const t=Object.assign(Object.assign({},e),{type:"lines"});i.push(t)}),mutiSort(i),i}},function(e,t,n){"use strict";n.r(t);var o=n(0);let r,i=globalThis.devicePixelRatio;const c=(e,t,n="#fff")=>{(r=document.createElement("canvas")).width=e,r.height=t,r.style.width=e/i+"px",r.style.height=t/i+"px";const o=r.getContext("2d");return o.beginPath(),o.fillStyle=n,o.fillRect(0,0,e,t),r},l=(e,t)=>(document.body.append(r),r.toDataURL()),s=(e,t,n=0,i=0,c=0,l=0)=>{const s=r.getContext("2d");let a;const{x:u,y:d,text:h="",fontSize:f,color:g="#000",opacity:b=1,lineHeight:p,lineNum:x,width:_=9999,marginLeft:y,marginRight:m,textDecoration:w,baseLine:P="top",textAlign:I="start",fontFamily:j="sans-serif",fontWeight:k="normal",fontStyle:v="normal"}=e;a="#"===g.substring(0,1)?Object(o.d)(Object(o.c)(g),b):"rgba"===g.substring(0,4)?g:"rgb"===g.substring(0,3)?Object(o.d)(g,b):Object(o.d)(Object(o.a)(g),b);const O=((e,t,n)=>{const o=e.measureText(t).width,r=t.length/o,i=o-n;return i>0&&(t=t.slice(0,t.length-i*r)+"..."),t})(s,h,_);if(t){const e=s.measureText(h).width;u>n&&u+e<n+c&&d>i+f&&d+f<i+l&&(s.font=`${v} ${k} ${f}px ${j}`,s.fillStyle=a,s.textBaseline=P,s.textAlign=I,s.fillText(O,u,d))}else s.font=`${v} ${k} ${f}px ${j}`,s.fillStyle=a,s.textBaseline=P,s.textAlign=I,s.fillText(O,u,d)},a=(e,t)=>{const{isBlock:n,x:o,y:i,width:c,height:l}=t;if(e[0])for(let t=0;t<e.length;t++)if(s(e[t],n,o,i,c,l),t===e.length-1)return r},u=e=>{const t=r.getContext("2d"),{x:n,y:o,width:i,height:c,borderWidth:l,borderColor:s,borderRadius:u=0,backgroundColor:d,text:h=""}=e,f=i,g=c,b=u/2;t.beginPath(),t.moveTo(n+b,o),t.lineTo(n+f-b,o),t.arc(n+f-b,o+b,b,2*Math.PI*.75,2*Math.PI*1),t.lineTo(n+f,o+g-b),t.arc(n+f-b,o+g-b,b,0,2*Math.PI*.25),t.lineTo(n+b,o+g),t.arc(n+b,o+g-b,b,2*Math.PI*.25,2*Math.PI*.5),t.lineTo(n,o+b),t.arc(n+b,o+b,b,2*Math.PI*.5,2*Math.PI*.75),(l||s)&&(t.lineWidth=l,t.strokeStyle=s,t.stroke()),t.fillStyle=d,t.fill(),""!==h&&a(h.text,{isBlock:!0,x:n,y:o,width:i,height:c})},d=e=>{const t=r.getContext("2d"),{startX:n,startY:o,endX:i,endY:c,width:l,color:s,zIndex:a}=e;t.beginPath(),t.moveTo(n,o),t.lineTo(i,c),t.strokeStyle=s,t.lineWidth=l,t.closePath(),t.stroke()},h=e=>{const t=r.getContext("2d");return new Promise(n=>{const{x:o,y:r,url:i,width:c,height:l,borderRadius:s=1,borderWidth:a,borderColor:u}=e,d=new Image;d.src=i,d.crossOrigin="Anonymous",d.onload=()=>{let e=c,i=l;c||l||(e=d.height,i=d.height,d.height>d.width&&(e=d.width,i=d.width)),!c&&l&&(e=l),c&&!l&&(i=c);const a=s/2,u=t.createPattern(d,"no-repeat");t.save(),t.beginPath(),t.moveTo(o+a,r),t.lineTo(o+e-a,r),t.arc(o+e-a,r+a,a,2*Math.PI*.75,2*Math.PI*1),t.lineTo(o+e,r+i-a),t.arc(o+e-a,r+i-a,a,0,2*Math.PI*.25),t.lineTo(o+a,r+i),t.arc(o+a,r+i-a,a,2*Math.PI*.25,2*Math.PI*.5),t.lineTo(o,r+a),t.arc(o+a,r+a,a,2*Math.PI*.5,2*Math.PI*.75),t.closePath(),t.fillStyle=u,t.fill(),t.clip(),t.drawImage(d,o,r,e,i),t.restore(),n(1)}})};var f=function(e,t,n,o){return new(n||(n=Promise))((function(r,i){function c(e){try{s(o.next(e))}catch(e){i(e)}}function l(e){try{s(o.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,l)}s((o=o.apply(e,t||[])).next())}))};function g(e){const{width:t,height:n,backgroundColor:r="#fff",debug:i,pixelRatio:a,blocks:g,texts:b,images:p,lines:x}=e;return new Promise((i,a)=>f(this,void 0,void 0,(function*(){const a=Object(o.b)(e);c(t,n,r);for(let e=0;e<a.length;e++){const t=a[e];"blocks"===t.type?u(t):"texts"===t.type?s(t,!1):"lines"===t.type?d(t):"images"===t.type&&(yield h(t))}const f=l(t,n);return i(f)})))}n.d(t,"create",(function(){return g}))}])}));