@poster-render/shared
Version:
taro海报组件,兼容企微、支付宝
3 lines (2 loc) • 7.05 kB
JavaScript
;function t(t,e,o,n){return new(o||(o=Promise))((function(r,i){function a(t){try{l(n.next(t))}catch(t){i(t)}}function c(t){try{l(n.throw(t))}catch(t){i(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(a,c)}l((n=n.apply(t,e||[])).next())}))}function e(t){var e="function"==typeof Symbol&&Symbol.iterator,o=e&&t[e],n=0;if(o)return o.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function o(t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var o,n=t[Symbol.asyncIterator];return n?n.call(t):(t=e(t),o={},r("next"),r("throw"),r("return"),o[Symbol.asyncIterator]=function(){return this},o);function r(e){o[e]=t[e]&&function(o){return new Promise((function(n,r){(function(t,e,o,n){Promise.resolve(n).then((function(e){t({value:e,done:o})}),e)})(n,r,(o=t[e](o)).done,o.value)}))}}}function n(e,o){var n;return t(this,void 0,void 0,(function*(){const{ctx:t}=e;t.save(),t.setLineDash(o.lineDash||[]),t.lineWidth=o.lineWidth,t.strokeStyle=o.color,t.lineCap=o.lineCap||"butt",t.lineDashOffset=null!==(n=o.lineDashOffset)&&void 0!==n?n:0,t.beginPath(),t.moveTo(o.x,o.y),t.lineTo(o.destX,o.destY),t.closePath(),t.stroke(),t.restore()}))}function r(e,o){var n,r;return t(this,void 0,void 0,(function*(){const{ctx:t}=e,i=function(t=2){if("number"==typeof t)return{topLeft:Math.max(t,2),topRight:Math.max(t,2),bottomLeft:Math.max(t,2),bottomRight:Math.max(t,2)};if(Array.isArray(t))return{topLeft:Math.max(t[0],2),topRight:Math.max(t[1],2),bottomRight:Math.max(t[2],2),bottomLeft:Math.max(t[3],2)};return{topLeft:2,topRight:2,bottomLeft:2,bottomRight:2}}(o.radius),{x:a,y:c,width:l,height:s,borderWidth:h,borderColor:f,backgroundColor:u}=o;t.save(),t.setLineDash(null!==(n=o.lineDash)&&void 0!==n?n:[]),t.lineDashOffset=null!==(r=o.lineDashOffset)&&void 0!==r?r:0,t.beginPath(),t.moveTo(a+i.topLeft,c),t.lineTo(a+l-i.topRight,c),t.arcTo(a+l,c,a+l,c+i.topRight,i.topRight),t.lineTo(a+l,c+s-i.bottomRight),t.arcTo(a+l,c+s,a+l-i.bottomRight,c+s,i.bottomRight),t.lineTo(a+i.bottomLeft,c+s),t.arcTo(a,c+s,a,c+s-i.bottomLeft,i.bottomLeft),t.lineTo(a,c+i.topLeft),t.arcTo(a,c,a+i.topLeft,c,i.topLeft),t.closePath(),u&&(t.fillStyle=u,t.fill()),f&&h&&(t.strokeStyle=f,t.lineWidth=h,t.stroke()),t.restore()}))}Object.defineProperty(exports,"__esModule",{value:!0});const i=new Map;function a(e){return t(this,void 0,void 0,(function*(){return new Promise((t=>{const o=!!document&&!e.canvas.createImage,n=o?new Image:e.canvas.createImage();o&&n.setAttribute("crossOrigin","Anonymous"),n.onload=()=>t(n),n.onerror=()=>{e.src.startsWith("data:image")||console.error("[poster-render]: downloadImage error",e.src),t(void 0)},n.src=e.src,e.src.startsWith("data:image")&&!e.cacheKey&&console.warn("[poster-render]: 使用base64图片时建议指定cacheKey")}))}))}function c(e){var o,n;return t(this,void 0,void 0,(function*(){if(!e.src)return Promise.resolve(void 0);const t=null!==(o=e.cacheKey)&&void 0!==o?o:e.src;if(i.has(t))return Promise.resolve(i.get(t));const r=yield a(e);return r&&i.set(null!==(n=e.cacheKey)&&void 0!==n?n:e.src,r),r}))}function l(e,o){return t(this,void 0,void 0,(function*(){const{ctx:n,canvas:i}=e;let a=yield c({ctx:n,canvas:i,src:o.src,cacheKey:o.cacheKey});!a&&o.defaultSrc&&(a=yield c({ctx:n,canvas:i,src:o.defaultSrc,cacheKey:o.cacheKey})),a?(n.save(),yield r(e,o),n.clip(),yield function(e,o,n){var r,i;return t(this,void 0,void 0,(function*(){const t=n.mode||"fill",a=o.width/o.height,c=n.width/n.height;let l,s,h,f,u,d,y,m;"contain"===t?(a<=c?(m=n.height,y=m*a,u=n.x+(n.width-y)/2,d=n.y):(y=n.width,m=y/a,u=n.x,d=n.y+(n.height-m)/2),e.drawImage(o,u,d,y,m)):"cover"===t?(a<=c?(l=o.width,s=l/c,h=0,f=(o.height-s)/2):(s=o.height,l=s*c,h=(o.width-l)/2,f=0),e.drawImage(o,null!==(r=n.sx)&&void 0!==r?r:h,null!==(i=n.sy)&&void 0!==i?i:f,l,s,n.x,n.y,n.width,n.height)):e.drawImage(o,n.x,n.y,n.width,n.height)}))}(n,a,o),n.restore()):console.info("图片下载失败,跳过渲染!")}))}function s(t,e,o){if(t.save(),o){const{fontStyle:e="normal",fontFamily:n="normal",fontWeight:r="normal",fontSize:i,baseLine:a="top"}=o;t.textBaseline=a,t.font=`${e} ${r} ${i}px ${n}`}const n=t.measureText(e);return t.restore(),n}function h(e,o){return t(this,void 0,void 0,(function*(){const{ctx:t}=e,{textAlign:n="left",opacity:r=1,lineNum:i=1,lineHeight:a=0,baseLine:c="top",fontWeight:l="normal",fontStyle:h="normal",fontFamily:f="sans-serif",textDecoration:u}=o;t.save(),t.beginPath(),t.font=`${h} ${l} ${o.fontSize}px ${f}`,t.globalAlpha=r,t.fillStyle=o.color,t.textAlign=n,t.textBaseline=c;let d=s(t,o.text,{fontSize:o.fontSize,fontFamily:f,fontStyle:h,fontWeight:l,baseLine:c}).width;const y="number"==typeof o.width?o.width:o.width(d,((e,o)=>s(t,e,o))),m="number"==typeof o.x?o.x:o.x(d,((e,o)=>s(t,e,o))),v=[];if(d>y){let e="",n=1;for(let r=0;r<=o.text.length-1;r++)e+=o.text[r],s(t,e,{fontSize:o.fontSize,fontFamily:f,fontStyle:h,fontWeight:l,baseLine:c}).width>=y?(n===i&&r!==o.text.length-1&&(e=e.substring(0,e.length-1)+"..."),n<=i&&v.push(e),e="",n++):n<=i&&r===o.text.length-1&&v.push(e);d=y}else v.push(o.text);return v.forEach(((e,n)=>{var r;const i=o.y+(a||o.fontSize)*n;t.fillText(e,m,i);const{width:d,actualBoundingBoxAscent:y=0,actualBoundingBoxDescent:v=0}=s(t,e,{fontSize:o.fontSize,fontFamily:f,fontStyle:h,fontWeight:l,baseLine:c});if(u){const e=function(t,e){const{fontSize:o,textDecoration:n,baseLine:r="top"}=e,i=t||o;let a=0;return a="top"===r?"overline"===n?0:"underline"===n?i:i/2:"bottom"===r?"overline"===n?-i:"underline"===n?0:-i/2:"overline"===n?-i/2:"underline"===n?i/2:0,a}(y+v,o);t.moveTo(m,i+e),t.lineTo(m+d,i+e),t.lineWidth=null!==(r=o.textDecorationWidth)&&void 0!==r?r:2,t.strokeStyle=o.color,t.stroke()}})),t.closePath(),t.restore(),d}))}function f(e,o){return t(this,void 0,void 0,(function*(){switch(o.type){case"image":return yield l(e,o);case"text":return yield h(e,o);case"line":return yield n(e,o);case"rect":return yield r(e,o);default:throw new Error("[poster-render]: Unknown item type")}}))}exports.clearCanvas=function(t){t.ctx.clearRect(0,0,t.canvas.width,t.canvas.height)},exports.downloadImage=a,exports.drawImage=l,exports.drawLine=n,exports.drawRect=r,exports.drawText=h,exports.loadImage=c,exports.preloadImage=function(e,o,n){return t(this,void 0,void 0,(function*(){const t=Array.from(new Set(n.filter((t=>{var e;return!i.has(null!==(e=t.cacheKey)&&void 0!==e?e:t.src)}))));return!(yield Promise.all(t.map((t=>c({ctx:e,canvas:o,src:t.src,cacheKey:t.cacheKey}))))).includes(void 0)}))},exports.render=function(e,n){var r,i,a,c;return t(this,void 0,void 0,(function*(){try{try{for(r=o(n);!(i=yield r.next()).done;){const t=i.value;yield f(e,t)}}catch(t){a={error:t}}finally{try{i&&!i.done&&(c=r.return)&&(yield c.call(r))}finally{if(a)throw a.error}}return!0}catch(t){return!1}}))},exports.renderItem=f;
//# sourceMappingURL=index.js.map