@inottn/miniposter
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -1,1 +0,1 @@ | ||
"use strict";var p=require("@inottn/fp-utils"),z=Object.defineProperty,$=Object.defineProperties,D=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertySymbols,L=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable,A=(o,e,t)=>e in o?z(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,T=(o,e)=>{for(var t in e||(e={}))L.call(e,t)&&A(o,t,e[t]);if(j)for(var t of j(e))U.call(e,t)&&A(o,t,e[t]);return o},W=(o,e)=>$(o,D(e));const k=function(o,e){let t=0,r=o.length;for(;t<r;){const s=t+(r-t>>1);e(s)?r=s:t=s+1}return r-1},M=function({left:o,textAlign:e,textWidth:t,width:r}){if(p.isUndefined(r))return o;const s=r-(t||0);switch(e){case"center":return o+s/2;case"right":return o+s;default:return o}},N=function(o,e){if(p.isUndefined(e))return o;const t=p.isFunction(o.left)?o.left():o.left,r=p.isFunction(o.top)?o.top():o.top;return W(T({},o),{left:e.left+t,top:e.top+r})},v=function(o){const e=T({},o);return p.isFunction(o.left)&&(e.left=o.left()),p.isFunction(o.top)&&(e.top=o.top()),e};var q=Object.defineProperty,B=Object.defineProperties,H=Object.getOwnPropertyDescriptors,R=Object.getOwnPropertySymbols,G=Object.prototype.hasOwnProperty,J=Object.prototype.propertyIsEnumerable,b=(o,e,t)=>e in o?q(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,x=(o,e)=>{for(var t in e||(e={}))G.call(e,t)&&b(o,t,e[t]);if(R)for(var t of R(e))J.call(e,t)&&b(o,t,e[t]);return o},C=(o,e)=>B(o,H(e)),w=(o,e,t)=>(b(o,typeof e!="symbol"?e+"":e,t),t);class K{constructor(e,t){w(this,"canvas"),w(this,"context"),w(this,"options"),w(this,"images",new Map),w(this,"fonts",new Map),w(this,"sizes",new Map),this.canvas=e,this.context=e.getContext("2d"),this.options=t}async render(e){const{canvas:t,context:r,options:s}=this,{width:i,height:a,pixelRatio:n=1}=x(x({},s),e);if(!i||!a)throw Error("\u7F3A\u5C11 width \u6216 height \u53C2\u6570");t.width=i*n,t.height=a*n,r.scale(n,n),await this.renderContainer(C(x({type:"container"},e),{left:0,top:0,width:i,height:a}))}async draw(e){if(Array.isArray(e))for(const t of e)await this.draw(t);else e.type==="container"&&await this.renderContainer(v(e)),e.type==="image"&&await this.renderImage(v(e)),e.type==="text"&&await this.renderText(v(e))}async renderContainer(e){const{context:t}=this,{left:r,top:s,width:i,height:a,backgroundColor:n,borderRadius:c=0,overflow:l,children:h}=e;if(t.save(),this.drawRoundedRect(r,s,i,a,c),t.clip(),n&&(t.fillStyle=n,t.fillRect(r,s,i,a)),l!=="hidden"&&t.restore(),p.isNonEmptyArray(h)){this.loadAssets(h);for(const f of h){const d=N(f,{left:r,top:s});await this.draw(d)}}l==="hidden"&&t.restore()}async renderImage(e){const{context:t}=this,{src:r,backgroundColor:s,borderRadius:i=0,objectFit:a="fill"}=e,[n,c]=this.images.get(r);let{left:l,top:h,width:f,height:d}=e;await c,t.save(),this.drawRoundedRect(l,h,f,d,i),t.clip(),s&&(t.fillStyle=s,t.fillRect(l,h,f,d));const u=n.width/n.height,g=f/d;if(!(a==="fill"||u===g)&&(a==="contain"||a==="cover"))if(a==="contain"?u>g:u<g){const y=n.width/f,m=d;d=n.height/y,h+=(m-d)*.5}else{const y=n.height/d,m=f;f=n.width/y,l+=(m-f)*.5}t.drawImage(n,l,h,f,d),t.restore()}async renderText(e){const{context:t}=this,{id:r,content:s,left:i,top:a,width:n,fontSize:c=14,lineHeight:l=c*1.43,color:h="#333",fontFamily:f="sans-serif",fontWeight:d=400,fontSrc:u,textAlign:g="left",textDecoration:y}=e;u&&await this.fonts.get(u),t.save(),n&&(t.textAlign=g),t.textBaseline="alphabetic",t.fillStyle=h,t.font=`${d} ${c}px ${f}`;const m=M({left:i,textAlign:g,width:n}),P=n?this.getAllLines(e):[s];P.forEach((O,S)=>{const F=a+(l-c)/2+l*S;if(t.fillText(O,m,F+c),y==="line-through"){const{width:I}=t.measureText(O),E=M({left:i,textAlign:g,textWidth:I,width:n});t.fillRect(E,F+c*.64,I,c/14)}}),r&&this.sizes.set(r,{width:n||t.measureText(s).width,height:l*P.length}),t.restore()}getAllLines(e){const{context:t}=this,{width:r,content:s,lineClamp:i=1/0}=e,a=[];let n=0;for(;n<s.length&&a.length<i;){const c=n;n=k(s,l=>t.measureText(s.slice(n,l+1)).width>r)+1,n===c&&(n=c+1),i===a.length+1?a.push(s.slice(c,n-1)+"..."):a.push(s.slice(c,n))}return a}getSize(e){return this.sizes.get(e)}drawRoundedRect(e,t,r,s,i){const{context:a}=this;typeof i=="number"?i=[i,i,i,i]:i.length===1?i=[i[0],i[0],i[0],i[0]]:i.length===2?i=[i[0],i[1],i[0],i[1]]:i.length===3&&(i=[i[0],i[1],i[2],i[1]]);const[n,c,l,h]=i.map(f=>Math.min(f,r/2,s/2));a.save(),a.translate(e,t),a.beginPath(),a.moveTo(n,0),a.lineTo(r-c,0),a.arc(r-c,c,c,Math.PI*3/2,0,!1),a.lineTo(r,s-l),a.arc(r-l,s-l,l,0,Math.PI/2,!1),a.lineTo(h,s),a.arc(h,s-h,h,Math.PI/2,Math.PI,!1),a.lineTo(0,n),a.arc(n,n,n,Math.PI,Math.PI*3/2,!1),a.closePath(),a.restore()}loadAssets(e){e.forEach(t=>{const{type:r}=t;r==="image"&&this.loadImage(t),r==="text"&&t.fontFamily&&this.loadFont(t)})}loadImage(e){const{src:t}=e;if(!this.images.has(t)){const r=this.canvas.createImage();r.src=t,this.images.set(t,[r,new Promise((s,i)=>{r.onload=s,r.onerror=i})])}}loadFont(e){const{fontFamily:t,fontSrc:r}=e;this.fonts.has(r)||this.fonts.set(r,new Promise((s,i)=>{my.loadFontFace({family:t,source:`url('${r}')`,success:s,fail:i})}))}export(e){const{canvas:t}=this,{promise:r,resolve:s,reject:i}=p.withResolvers();return t.toTempFilePath(C(x({x:0,y:0,width:t.width,height:t.height},e),{success:s,fail:i})),r}}exports.MiniPoster=K; | ||
"use strict";var p=require("@inottn/fp-utils"),U=Object.defineProperty,H=Object.defineProperties,N=Object.getOwnPropertyDescriptors,A=Object.getOwnPropertySymbols,q=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable,$=(i,e,t)=>e in i?U(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,x=(i,e)=>{for(var t in e||(e={}))q.call(e,t)&&$(i,t,e[t]);if(A)for(var t of A(e))B.call(e,t)&&$(i,t,e[t]);return i},R=(i,e)=>H(i,N(e));const v=typeof wx=="object",M=typeof my=="object",G=()=>{let i;return v?i=wx:M&&(i=my),i},P=G(),J=i=>{const{fontFamily:e,fontSrc:t}=i,{resolve:o,reject:s,promise:r}=p.withResolvers();return P?P.loadFontFace(R(x({family:e,source:`url('${t}')`},v&&{scopes:["native"]}),{success:o,fail:s})):s("platform sdk not found"),r},K=(i,e)=>{const{resolve:t,reject:o,promise:s}=p.withResolvers();let r;return M?r=i.toTempFilePath:v?r=P.canvasToTempFilePath:o("platform sdk not found"),r&&r(R(x(x({},e),v&&{canvas:i}),{success:t,fail:o})),s};var Q=Object.defineProperty,V=Object.defineProperties,X=Object.getOwnPropertyDescriptors,C=Object.getOwnPropertySymbols,Y=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,E=(i,e,t)=>e in i?Q(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,z=(i,e)=>{for(var t in e||(e={}))Y.call(e,t)&&E(i,t,e[t]);if(C)for(var t of C(e))Z.call(e,t)&&E(i,t,e[t]);return i},_=(i,e)=>V(i,X(e));const tt=function(i,e){let t=0,o=i.length;for(;t<o;){const s=t+(o-t>>1);e(s)?o=s:t=s+1}return o-1},k=function({left:i,textAlign:e,textWidth:t,width:o}){if(p.isUndefined(o))return i;const s=o-(t||0);switch(e){case"center":return i+s/2;case"right":return i+s;default:return i}},et=function(i,e){if(p.isUndefined(e))return i;const t=p.isFunction(i.left)?i.left():i.left,o=p.isFunction(i.top)?i.top():i.top;return _(z({},i),{left:e.left+t,top:e.top+o})},O=function(i){const e=z({},i);return p.isFunction(i.left)&&(e.left=i.left()),p.isFunction(i.top)&&(e.top=i.top()),e};var it=Object.defineProperty,rt=Object.defineProperties,ot=Object.getOwnPropertyDescriptors,D=Object.getOwnPropertySymbols,st=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable,j=(i,e,t)=>e in i?it(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,b=(i,e)=>{for(var t in e||(e={}))st.call(e,t)&&j(i,t,e[t]);if(D)for(var t of D(e))nt.call(e,t)&&j(i,t,e[t]);return i},at=(i,e)=>rt(i,ot(e)),w=(i,e,t)=>(j(i,typeof e!="symbol"?e+"":e,t),t);class lt{constructor(e,t){w(this,"canvas"),w(this,"context"),w(this,"options"),w(this,"images",new Map),w(this,"fonts",new Map),w(this,"sizes",new Map),this.canvas=e,this.context=e.getContext("2d"),this.options=t}async render(e){const{canvas:t,context:o,options:s}=this,{width:r,height:a,pixelRatio:n=1}=b(b({},s),e);if(!r||!a)throw Error("\u7F3A\u5C11 width \u6216 height \u53C2\u6570");t.width=r*n,t.height=a*n,o.scale(n,n),await this.renderContainer(at(b({type:"container"},e),{left:0,top:0,width:r,height:a}))}async draw(e){if(Array.isArray(e))for(const t of e)await this.draw(t);else e.type==="container"&&await this.renderContainer(O(e)),e.type==="image"&&await this.renderImage(O(e)),e.type==="text"&&await this.renderText(O(e))}async renderContainer(e){const{context:t}=this,{left:o,top:s,width:r,height:a,backgroundColor:n,borderRadius:l=0,overflow:c,children:h}=e;if(t.save(),this.drawRoundedRect(o,s,r,a,l),t.clip(),n&&(t.fillStyle=n,t.fillRect(o,s,r,a)),c!=="hidden"&&t.restore(),p.isNonEmptyArray(h)){this.loadAssets(h);for(const f of h){const d=et(f,{left:o,top:s});await this.draw(d)}}c==="hidden"&&t.restore()}async renderImage(e){const{context:t}=this,{src:o,backgroundColor:s,borderRadius:r=0,objectFit:a="fill"}=e,[n,l]=this.images.get(o);let{left:c,top:h,width:f,height:d}=e;await l,t.save(),this.drawRoundedRect(c,h,f,d,r),t.clip(),s&&(t.fillStyle=s,t.fillRect(c,h,f,d));const g=n.width/n.height,u=f/d;if(!(a==="fill"||g===u)&&(a==="contain"||a==="cover"))if(a==="contain"?g>u:g<u){const y=n.width/f,m=d;d=n.height/y,h+=(m-d)*.5}else{const y=n.height/d,m=f;f=n.width/y,c+=(m-f)*.5}t.drawImage(n,c,h,f,d),t.restore()}async renderText(e){const{context:t}=this,{id:o,content:s,left:r,top:a,width:n,fontSize:l=14,lineHeight:c=l*1.43,color:h="#333",fontFamily:f="sans-serif",fontWeight:d=400,fontSrc:g,textAlign:u="left",textDecoration:y}=e;g&&await this.fonts.get(g),t.save(),n&&(t.textAlign=u),t.textBaseline="alphabetic",t.fillStyle=h,t.font=`${d} ${l}px ${f}`;const m=k({left:r,textAlign:u,width:n}),F=n?this.getAllLines(e):[s];F.forEach((I,W)=>{const T=a+(c-l)/2+c*W;if(t.fillText(I,m,T+l),y==="line-through"){const{width:S}=t.measureText(I),L=k({left:r,textAlign:u,textWidth:S,width:n});t.fillRect(L,T+l*.64,S,l/14)}}),o&&this.sizes.set(o,{width:n||t.measureText(s).width,height:c*F.length}),t.restore()}getAllLines(e){const{context:t}=this,{width:o,content:s,lineClamp:r=1/0}=e,a=[];let n=0;for(;n<s.length&&a.length<r;){const l=n;n=tt(s,c=>t.measureText(s.slice(n,c+1)).width>o)+1,n===l&&(n=l+1),r===a.length+1?a.push(s.slice(l,n-1)+"..."):a.push(s.slice(l,n))}return a}getSize(e){return this.sizes.get(e)}drawRoundedRect(e,t,o,s,r){const{context:a}=this;typeof r=="number"?r=[r,r,r,r]:r.length===1?r=[r[0],r[0],r[0],r[0]]:r.length===2?r=[r[0],r[1],r[0],r[1]]:r.length===3&&(r=[r[0],r[1],r[2],r[1]]);const[n,l,c,h]=r.map(f=>Math.min(f,o/2,s/2));a.save(),a.translate(e,t),a.beginPath(),a.moveTo(n,0),a.lineTo(o-l,0),a.arc(o-l,l,l,Math.PI*3/2,0,!1),a.lineTo(o,s-c),a.arc(o-c,s-c,c,0,Math.PI/2,!1),a.lineTo(h,s),a.arc(h,s-h,h,Math.PI/2,Math.PI,!1),a.lineTo(0,n),a.arc(n,n,n,Math.PI,Math.PI*3/2,!1),a.closePath(),a.restore()}loadAssets(e){e.forEach(t=>{const{type:o}=t;o==="image"&&this.loadImage(t),o==="text"&&t.fontFamily&&t.fontSrc&&this.loadFont(t)})}loadImage(e){const{src:t}=e;if(!this.images.has(t)){const o=this.canvas.createImage();o.src=t,this.images.set(t,[o,new Promise((s,r)=>{o.onload=s,o.onerror=r})])}}loadFont(e){const{fontFamily:t,fontSrc:o}=e;this.fonts.has(o)||this.fonts.set(o,J({fontFamily:t,fontSrc:o}))}export(e){const{canvas:t}=this;return K(t,b({x:0,y:0,width:t.width,height:t.height,destWidth:t.width,destHeight:t.height},e))}}exports.MiniPoster=lt; |
@@ -1,1 +0,1 @@ | ||
import{isUndefined as A,isFunction as y,isNonEmptyArray as $,withResolvers as D}from"@inottn/fp-utils";var L=Object.defineProperty,W=Object.defineProperties,k=Object.getOwnPropertyDescriptors,T=Object.getOwnPropertySymbols,N=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable,F=(o,e,t)=>e in o?L(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,R=(o,e)=>{for(var t in e||(e={}))N.call(e,t)&&F(o,t,e[t]);if(T)for(var t of T(e))B.call(e,t)&&F(o,t,e[t]);return o},H=(o,e)=>W(o,k(e));const U=function(o,e){let t=0,r=o.length;for(;t<r;){const s=t+(r-t>>1);e(s)?r=s:t=s+1}return r-1},C=function({left:o,textAlign:e,textWidth:t,width:r}){if(A(r))return o;const s=r-(t||0);switch(e){case"center":return o+s/2;case"right":return o+s;default:return o}},q=function(o,e){if(A(e))return o;const t=y(o.left)?o.left():o.left,r=y(o.top)?o.top():o.top;return H(R({},o),{left:e.left+t,top:e.top+r})},b=function(o){const e=R({},o);return y(o.left)&&(e.left=o.left()),y(o.top)&&(e.top=o.top()),e};var G=Object.defineProperty,J=Object.defineProperties,K=Object.getOwnPropertyDescriptors,M=Object.getOwnPropertySymbols,Q=Object.prototype.hasOwnProperty,V=Object.prototype.propertyIsEnumerable,v=(o,e,t)=>e in o?G(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,x=(o,e)=>{for(var t in e||(e={}))Q.call(e,t)&&v(o,t,e[t]);if(M)for(var t of M(e))V.call(e,t)&&v(o,t,e[t]);return o},S=(o,e)=>J(o,K(e)),w=(o,e,t)=>(v(o,typeof e!="symbol"?e+"":e,t),t);class X{constructor(e,t){w(this,"canvas"),w(this,"context"),w(this,"options"),w(this,"images",new Map),w(this,"fonts",new Map),w(this,"sizes",new Map),this.canvas=e,this.context=e.getContext("2d"),this.options=t}async render(e){const{canvas:t,context:r,options:s}=this,{width:i,height:a,pixelRatio:n=1}=x(x({},s),e);if(!i||!a)throw Error("\u7F3A\u5C11 width \u6216 height \u53C2\u6570");t.width=i*n,t.height=a*n,r.scale(n,n),await this.renderContainer(S(x({type:"container"},e),{left:0,top:0,width:i,height:a}))}async draw(e){if(Array.isArray(e))for(const t of e)await this.draw(t);else e.type==="container"&&await this.renderContainer(b(e)),e.type==="image"&&await this.renderImage(b(e)),e.type==="text"&&await this.renderText(b(e))}async renderContainer(e){const{context:t}=this,{left:r,top:s,width:i,height:a,backgroundColor:n,borderRadius:l=0,overflow:c,children:h}=e;if(t.save(),this.drawRoundedRect(r,s,i,a,l),t.clip(),n&&(t.fillStyle=n,t.fillRect(r,s,i,a)),c!=="hidden"&&t.restore(),$(h)){this.loadAssets(h);for(const f of h){const d=q(f,{left:r,top:s});await this.draw(d)}}c==="hidden"&&t.restore()}async renderImage(e){const{context:t}=this,{src:r,backgroundColor:s,borderRadius:i=0,objectFit:a="fill"}=e,[n,l]=this.images.get(r);let{left:c,top:h,width:f,height:d}=e;await l,t.save(),this.drawRoundedRect(c,h,f,d,i),t.clip(),s&&(t.fillStyle=s,t.fillRect(c,h,f,d));const g=n.width/n.height,p=f/d;if(!(a==="fill"||g===p)&&(a==="contain"||a==="cover"))if(a==="contain"?g>p:g<p){const u=n.width/f,m=d;d=n.height/u,h+=(m-d)*.5}else{const u=n.height/d,m=f;f=n.width/u,c+=(m-f)*.5}t.drawImage(n,c,h,f,d),t.restore()}async renderText(e){const{context:t}=this,{id:r,content:s,left:i,top:a,width:n,fontSize:l=14,lineHeight:c=l*1.43,color:h="#333",fontFamily:f="sans-serif",fontWeight:d=400,fontSrc:g,textAlign:p="left",textDecoration:u}=e;g&&await this.fonts.get(g),t.save(),n&&(t.textAlign=p),t.textBaseline="alphabetic",t.fillStyle=h,t.font=`${d} ${l}px ${f}`;const m=C({left:i,textAlign:p,width:n}),P=n?this.getAllLines(e):[s];P.forEach((O,E)=>{const I=a+(c-l)/2+c*E;if(t.fillText(O,m,I+l),u==="line-through"){const{width:j}=t.measureText(O),z=C({left:i,textAlign:p,textWidth:j,width:n});t.fillRect(z,I+l*.64,j,l/14)}}),r&&this.sizes.set(r,{width:n||t.measureText(s).width,height:c*P.length}),t.restore()}getAllLines(e){const{context:t}=this,{width:r,content:s,lineClamp:i=1/0}=e,a=[];let n=0;for(;n<s.length&&a.length<i;){const l=n;n=U(s,c=>t.measureText(s.slice(n,c+1)).width>r)+1,n===l&&(n=l+1),i===a.length+1?a.push(s.slice(l,n-1)+"..."):a.push(s.slice(l,n))}return a}getSize(e){return this.sizes.get(e)}drawRoundedRect(e,t,r,s,i){const{context:a}=this;typeof i=="number"?i=[i,i,i,i]:i.length===1?i=[i[0],i[0],i[0],i[0]]:i.length===2?i=[i[0],i[1],i[0],i[1]]:i.length===3&&(i=[i[0],i[1],i[2],i[1]]);const[n,l,c,h]=i.map(f=>Math.min(f,r/2,s/2));a.save(),a.translate(e,t),a.beginPath(),a.moveTo(n,0),a.lineTo(r-l,0),a.arc(r-l,l,l,Math.PI*3/2,0,!1),a.lineTo(r,s-c),a.arc(r-c,s-c,c,0,Math.PI/2,!1),a.lineTo(h,s),a.arc(h,s-h,h,Math.PI/2,Math.PI,!1),a.lineTo(0,n),a.arc(n,n,n,Math.PI,Math.PI*3/2,!1),a.closePath(),a.restore()}loadAssets(e){e.forEach(t=>{const{type:r}=t;r==="image"&&this.loadImage(t),r==="text"&&t.fontFamily&&this.loadFont(t)})}loadImage(e){const{src:t}=e;if(!this.images.has(t)){const r=this.canvas.createImage();r.src=t,this.images.set(t,[r,new Promise((s,i)=>{r.onload=s,r.onerror=i})])}}loadFont(e){const{fontFamily:t,fontSrc:r}=e;this.fonts.has(r)||this.fonts.set(r,new Promise((s,i)=>{my.loadFontFace({family:t,source:`url('${r}')`,success:s,fail:i})}))}export(e){const{canvas:t}=this,{promise:r,resolve:s,reject:i}=D();return t.toTempFilePath(S(x({x:0,y:0,width:t.width,height:t.height},e),{success:s,fail:i})),r}}export{X as MiniPoster}; | ||
import{withResolvers as A,isUndefined as $,isFunction as m,isNonEmptyArray as B}from"@inottn/fp-utils";var U=Object.defineProperty,q=Object.defineProperties,G=Object.getOwnPropertyDescriptors,R=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable,C=(o,e,t)=>e in o?U(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,x=(o,e)=>{for(var t in e||(e={}))J.call(e,t)&&C(o,t,e[t]);if(R)for(var t of R(e))K.call(e,t)&&C(o,t,e[t]);return o},M=(o,e)=>q(o,G(e));const b=typeof wx=="object",E=typeof my=="object",Q=()=>{let o;return b?o=wx:E&&(o=my),o},P=Q(),V=o=>{const{fontFamily:e,fontSrc:t}=o,{resolve:i,reject:n,promise:r}=A();return P?P.loadFontFace(M(x({family:e,source:`url('${t}')`},b&&{scopes:["native"]}),{success:i,fail:n})):n("platform sdk not found"),r},X=(o,e)=>{const{resolve:t,reject:i,promise:n}=A();let r;return E?r=o.toTempFilePath:b?r=P.canvasToTempFilePath:i("platform sdk not found"),r&&r(M(x(x({},e),b&&{canvas:o}),{success:t,fail:i})),n};var Y=Object.defineProperty,Z=Object.defineProperties,_=Object.getOwnPropertyDescriptors,z=Object.getOwnPropertySymbols,tt=Object.prototype.hasOwnProperty,et=Object.prototype.propertyIsEnumerable,k=(o,e,t)=>e in o?Y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,D=(o,e)=>{for(var t in e||(e={}))tt.call(e,t)&&k(o,t,e[t]);if(z)for(var t of z(e))et.call(e,t)&&k(o,t,e[t]);return o},ot=(o,e)=>Z(o,_(e));const rt=function(o,e){let t=0,i=o.length;for(;t<i;){const n=t+(i-t>>1);e(n)?i=n:t=n+1}return i-1},W=function({left:o,textAlign:e,textWidth:t,width:i}){if($(i))return o;const n=i-(t||0);switch(e){case"center":return o+n/2;case"right":return o+n;default:return o}},it=function(o,e){if($(e))return o;const t=m(o.left)?o.left():o.left,i=m(o.top)?o.top():o.top;return ot(D({},o),{left:e.left+t,top:e.top+i})},O=function(o){const e=D({},o);return m(o.left)&&(e.left=o.left()),m(o.top)&&(e.top=o.top()),e};var nt=Object.defineProperty,st=Object.defineProperties,at=Object.getOwnPropertyDescriptors,L=Object.getOwnPropertySymbols,lt=Object.prototype.hasOwnProperty,ct=Object.prototype.propertyIsEnumerable,j=(o,e,t)=>e in o?nt(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,v=(o,e)=>{for(var t in e||(e={}))lt.call(e,t)&&j(o,t,e[t]);if(L)for(var t of L(e))ct.call(e,t)&&j(o,t,e[t]);return o},ht=(o,e)=>st(o,at(e)),u=(o,e,t)=>(j(o,typeof e!="symbol"?e+"":e,t),t);class ft{constructor(e,t){u(this,"canvas"),u(this,"context"),u(this,"options"),u(this,"images",new Map),u(this,"fonts",new Map),u(this,"sizes",new Map),this.canvas=e,this.context=e.getContext("2d"),this.options=t}async render(e){const{canvas:t,context:i,options:n}=this,{width:r,height:a,pixelRatio:s=1}=v(v({},n),e);if(!r||!a)throw Error("\u7F3A\u5C11 width \u6216 height \u53C2\u6570");t.width=r*s,t.height=a*s,i.scale(s,s),await this.renderContainer(ht(v({type:"container"},e),{left:0,top:0,width:r,height:a}))}async draw(e){if(Array.isArray(e))for(const t of e)await this.draw(t);else e.type==="container"&&await this.renderContainer(O(e)),e.type==="image"&&await this.renderImage(O(e)),e.type==="text"&&await this.renderText(O(e))}async renderContainer(e){const{context:t}=this,{left:i,top:n,width:r,height:a,backgroundColor:s,borderRadius:l=0,overflow:c,children:h}=e;if(t.save(),this.drawRoundedRect(i,n,r,a,l),t.clip(),s&&(t.fillStyle=s,t.fillRect(i,n,r,a)),c!=="hidden"&&t.restore(),B(h)){this.loadAssets(h);for(const f of h){const p=it(f,{left:i,top:n});await this.draw(p)}}c==="hidden"&&t.restore()}async renderImage(e){const{context:t}=this,{src:i,backgroundColor:n,borderRadius:r=0,objectFit:a="fill"}=e,[s,l]=this.images.get(i);let{left:c,top:h,width:f,height:p}=e;await l,t.save(),this.drawRoundedRect(c,h,f,p,r),t.clip(),n&&(t.fillStyle=n,t.fillRect(c,h,f,p));const g=s.width/s.height,d=f/p;if(!(a==="fill"||g===d)&&(a==="contain"||a==="cover"))if(a==="contain"?g>d:g<d){const w=s.width/f,y=p;p=s.height/w,h+=(y-p)*.5}else{const w=s.height/p,y=f;f=s.width/w,c+=(y-f)*.5}t.drawImage(s,c,h,f,p),t.restore()}async renderText(e){const{context:t}=this,{id:i,content:n,left:r,top:a,width:s,fontSize:l=14,lineHeight:c=l*1.43,color:h="#333",fontFamily:f="sans-serif",fontWeight:p=400,fontSrc:g,textAlign:d="left",textDecoration:w}=e;g&&await this.fonts.get(g),t.save(),s&&(t.textAlign=d),t.textBaseline="alphabetic",t.fillStyle=h,t.font=`${p} ${l}px ${f}`;const y=W({left:r,textAlign:d,width:s}),F=s?this.getAllLines(e):[n];F.forEach((I,H)=>{const T=a+(c-l)/2+c*H;if(t.fillText(I,y,T+l),w==="line-through"){const{width:S}=t.measureText(I),N=W({left:r,textAlign:d,textWidth:S,width:s});t.fillRect(N,T+l*.64,S,l/14)}}),i&&this.sizes.set(i,{width:s||t.measureText(n).width,height:c*F.length}),t.restore()}getAllLines(e){const{context:t}=this,{width:i,content:n,lineClamp:r=1/0}=e,a=[];let s=0;for(;s<n.length&&a.length<r;){const l=s;s=rt(n,c=>t.measureText(n.slice(s,c+1)).width>i)+1,s===l&&(s=l+1),r===a.length+1?a.push(n.slice(l,s-1)+"..."):a.push(n.slice(l,s))}return a}getSize(e){return this.sizes.get(e)}drawRoundedRect(e,t,i,n,r){const{context:a}=this;typeof r=="number"?r=[r,r,r,r]:r.length===1?r=[r[0],r[0],r[0],r[0]]:r.length===2?r=[r[0],r[1],r[0],r[1]]:r.length===3&&(r=[r[0],r[1],r[2],r[1]]);const[s,l,c,h]=r.map(f=>Math.min(f,i/2,n/2));a.save(),a.translate(e,t),a.beginPath(),a.moveTo(s,0),a.lineTo(i-l,0),a.arc(i-l,l,l,Math.PI*3/2,0,!1),a.lineTo(i,n-c),a.arc(i-c,n-c,c,0,Math.PI/2,!1),a.lineTo(h,n),a.arc(h,n-h,h,Math.PI/2,Math.PI,!1),a.lineTo(0,s),a.arc(s,s,s,Math.PI,Math.PI*3/2,!1),a.closePath(),a.restore()}loadAssets(e){e.forEach(t=>{const{type:i}=t;i==="image"&&this.loadImage(t),i==="text"&&t.fontFamily&&t.fontSrc&&this.loadFont(t)})}loadImage(e){const{src:t}=e;if(!this.images.has(t)){const i=this.canvas.createImage();i.src=t,this.images.set(t,[i,new Promise((n,r)=>{i.onload=n,i.onerror=r})])}}loadFont(e){const{fontFamily:t,fontSrc:i}=e;this.fonts.has(i)||this.fonts.set(i,V({fontFamily:t,fontSrc:i}))}export(e){const{canvas:t}=this;return X(t,v({x:0,y:0,width:t.width,height:t.height,destWidth:t.width,destHeight:t.height},e))}}export{ft as MiniPoster}; |
{ | ||
"name": "@inottn/miniposter", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"packageManager": "pnpm@8.7.0", | ||
@@ -5,0 +5,0 @@ "description": "使用 canvas 轻松绘制小程序海报", |
@@ -18,2 +18,3 @@ <p align="center"> | ||
- 使用 新版 canvas 2d 接口,性能更佳 | ||
- 支持 微信 / 支付宝小程序 | ||
@@ -20,0 +21,0 @@ ## 安装 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25133
152
179