UNPKG

@poster-render/shared

Version:

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

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