UNPKG

@fish-render/utils

Version:

Some commonly used canvas operation methods

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