UNPKG

@fish-render/utils

Version:

Some commonly used canvas operation methods

2 lines (1 loc) 5.88 kB
(function(a,c){typeof exports=="object"&&typeof module!="undefined"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(a=typeof globalThis!="undefined"?globalThis:a||self,c(a.FishRenderUtils={}))})(this,function(a){"use strict";var J=Object.defineProperty,K=Object.defineProperties;var Q=Object.getOwnPropertyDescriptors;var Y=Object.getOwnPropertySymbols;var V=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var M=(a,c,d)=>c in a?J(a,c,{enumerable:!0,configurable:!0,writable:!0,value:d}):a[c]=d,D=(a,c)=>{for(var d in c||(c={}))V.call(c,d)&&M(a,d,c[d]);if(Y)for(var d of Y(c))Z.call(c,d)&&M(a,d,c[d]);return a},$=(a,c)=>K(a,Q(c));function c(e=0){if(typeof e=="number")return{topLeft:e,topRight:e,bottomLeft:e,bottomRight:e};if(Array.isArray(e)){const[t,i,o,n]=e;switch(e.length){case 1:return{topLeft:t,topRight:t,bottomRight:t,bottomLeft:t};case 2:return{topLeft:t,topRight:t,bottomRight:i,bottomLeft:i};case 4:return{topLeft:t,topRight:i,bottomRight:o,bottomLeft:n}}}return{topLeft:0,topRight:0,bottomLeft:0,bottomRight:0}}function d(e,t,i){if(e.save(),i){const{fontStyle:n="normal",fontFamily:r="normal",fontWeight:l="normal",fontSize:g=16,baseLine:x="top"}=i;e.textBaseline=x,e.font=`${n} ${l} ${g}px ${r}`}const o=e.measureText(t);return e.restore(),o}function I(e,t){const{fontSize:i,textDecoration:o,baseLine:n="top"}=t,r=e||i;let l=0;return n==="top"?o==="overline"?l=0:o==="underline"?l=r:l=r/2:n==="bottom"?o==="overline"?l=-r:o==="underline"?l=0:l=-r/2:o==="overline"?l=-r/2:o==="underline"?l=r/2:l=0,l}function X(e){const t={x:0,y:0};return e.forEach(i=>{const[o,n]=i;t.x+=o,t.y+=n}),t.x/=e.length,t.y/=e.length,t}function B(e){const{ctx:t,style:i,x:o,y:n,destX:r,destY:l}=e;v({ctx:t,points:[[o,n],[r,l]],style:$(D({},i),{lineColor:(i==null?void 0:i.color)||""})})}function v(e){const{ctx:t,style:i,points:o}=e,{rotate:n,rotateX:r,rotateY:l,color:g,lineColor:x="",lineDash:u=[],lineWidth:p=1,lineCap:b="butt",lineDashOffset:f}=i||{};if(o&&o.length>=2){if(t.save(),n){let h=0,P=0;if(typeof r=="number"&&typeof l=="number")h=r,P=l;else{const{x:L,y:R}=X(o);h=L,P=R}t.translate(h,P),t.rotate(n*Math.PI/180),t.translate(-h,-P)}t.setLineDash(u||[]),t.lineWidth=p||1,t.strokeStyle=x||"",t.lineCap=b||"butt",t.lineDashOffset=f!=null?f:0,t.beginPath();const[w,y]=o[0];t.moveTo(w,y);for(let h=1;h<o.length;h++){const[P,L]=o[h];t.lineTo(P,L)}g&&(t.fillStyle=g||"",t.fill()),t.closePath(),t.stroke(),t.restore()}}function z(e){const{ctx:t,style:i,points:o}=e,{rotate:n,rotateX:r,rotateY:l,color:g,borderColor:x,borderDash:u,borderWidth:p,borderCap:b,borderDashOffset:f}=i||{};if(o&&o.length>=3){const w=[...o,o[0]];v({ctx:t,points:w,style:{rotate:n,rotateX:r,rotateY:l,color:g,lineColor:x,lineDash:u,lineWidth:p,lineCap:b,lineDashOffset:f}})}}function F(e){const{ctx:t,style:i,x:o,y:n,width:r,height:l}=e,{rotate:g,round:x,borderWidth:u,borderColor:p,color:b,lineDash:f,lineDashOffset:w}=i||{},y=c(x);if(t.save(),g){const h=o+r/2,P=n+l/2;t.translate(h,P),t.rotate(g*Math.PI/180),t.translate(-h,-P)}t.setLineDash(f!=null?f:[]),t.lineDashOffset=w!=null?w:0,t.beginPath(),t.moveTo(o+y.topLeft,n),t.lineTo(o+r-y.topRight,n),t.arcTo(o+r,n,o+r,n+y.topRight,y.topRight),t.lineTo(o+r,n+l-y.bottomRight),t.arcTo(o+r,n+l,o+r-y.bottomRight,n+l,y.bottomRight),t.lineTo(o+y.bottomLeft,n+l),t.arcTo(o,n+l,o,n+l-y.bottomLeft,y.bottomLeft),t.lineTo(o,n+y.topLeft),t.arcTo(o,n,o+y.topLeft,n,y.topLeft),t.closePath(),b&&(t.fillStyle=b,t.fill()),p&&u&&(t.strokeStyle=p,t.lineWidth=u,t.stroke()),t.restore()}function O(e){const{ctx:t,style:i,x:o,y:n,r,startAngle:l,endAngle:g,clockwise:x}=e,{color:u,lineWidth:p=1,lineDash:b,lineDashOffset:f,lineCap:w}=i||{};t.save(),t.beginPath(),t.setLineDash(b!=null?b:[]),t.lineDashOffset=f!=null?f:0,t.lineCap=w||"butt",t.arc(o,n,r-p/2,Math.PI/(180/l),Math.PI/(180/g),typeof x=="boolean"?x:!0),t.lineWidth=p,u&&(t.strokeStyle=u),t.stroke()}function j(e){const{ctx:t,style:i,x:o,y:n,r}=e,{rotate:l,color:g,borderColor:x="normal",borderWidth:u=1}=i||{};t.save(),t.beginPath(),l&&(t.translate(o,n),t.rotate(l*Math.PI/180),t.translate(-o,-n)),t.arc(o,n,r-u/2,0,2*Math.PI),t.lineWidth=u,t.strokeStyle=x,g&&(t.fillStyle=g,t.fill()),t.stroke(),l&&t.restore()}function E(e){const{ctx:t,lineNum:i=1,style:o,text:n}=e,{rotate:r,color:l="normal",textAlign:g="left",opacity:x=1,lineHeight:u=0,baseLine:p="top",fontWeight:b="normal",fontStyle:f="normal",fontFamily:w="sans-serif",textDecoration:y,fontSize:h=16,textDecorationWidth:P}=o||{};t.save(),t.beginPath(),t.font=`${f} ${b} ${h}px ${w}`,t.globalAlpha=x,t.fillStyle=l||"",t.textAlign=g,t.textBaseline=p;let L=d(t,n,{fontSize:h,fontFamily:w,fontStyle:f,fontWeight:b,baseLine:p}).width;const R=typeof e.width=="number"?e.width:e.width(L,(s,m)=>d(t,s,m)),S=typeof e.x=="number"?e.x:e.x(L,(s,m)=>d(t,s,m)),C=[];if(L>R){let s="",m=1;for(let T=0;T<=e.text.length-1;T++)s=s+e.text[T],d(t,s,{fontSize:h,fontFamily:w,fontStyle:f,fontWeight:b,baseLine:p}).width>=R?(m===i&&T!==e.text.length-1&&(s=s.substring(0,s.length-1)+"..."),m<=i&&C.push(s),s="",m++):m<=i&&T===e.text.length-1&&C.push(s);L=R}else C.push(e.text);const W=(u||h)*C.length,k=typeof e.y=="number"?e.y:e.y(W,(s,m)=>d(t,s,m));if(r){const s=S+R/2,m=k+W/2;t.translate(s,m),t.rotate(r*Math.PI/180),t.translate(-s,-m)}return C.forEach((s,m)=>{const T=k+(u||h)*m;t.fillText(s,S,T);const{width:N,actualBoundingBoxAscent:U=0,actualBoundingBoxDescent:q=0}=d(t,s,{fontSize:h,fontFamily:w,fontStyle:f,fontWeight:b,baseLine:p}),G=U+q;if(y){const A=I(G,e.style);t.moveTo(S,T+A),t.lineTo(S+N,T+A),t.lineWidth=P!=null?P:2,t.strokeStyle=l,t.stroke()}}),t.closePath(),t.restore(),L}function H(e){const{ctx:t,width:i,height:o}=e;t.clearRect(0,0,i,o)}a.clearCanvas=H,a.drawArc=O,a.drawCircle=j,a.drawLine=B,a.drawPolygon=z,a.drawPolyline=v,a.drawRect=F,a.drawText=E,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});