UNPKG

@poster-render/shared

Version:

taro海报组件,兼容企微、支付宝

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