@rfkit/renderer 0.1.16 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -2
- package/package.json +1 -1
- package/renderers/cartesian/Heatmap.d.ts +3 -3
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
let t=new Map;function e(t,e){let a,r,i,l;if(!/^#([A-Fa-f0-9]{3}){1,2}([A-Fa-f0-9]{2})?$/.test(t))throw Error("Invalid hex color format");let s=t.replace("#","");if(3===s.length)a=Number.parseInt(s[0]+s[0],16),r=Number.parseInt(s[1]+s[1],16),i=Number.parseInt(s[2]+s[2],16),l=255;else if(6===s.length)a=Number.parseInt(s.substring(0,2),16),r=Number.parseInt(s.substring(2,4),16),i=Number.parseInt(s.substring(4,6),16),l=255;else if(8===s.length)a=Number.parseInt(s.substring(0,2),16),r=Number.parseInt(s.substring(2,4),16),i=Number.parseInt(s.substring(4,6),16),l=Number.parseInt(s.substring(6,8),16);else throw Error("Invalid hex color length");return"number"==typeof e&&(l=Math.round(255*Math.max(0,Math.min(1,e)))),{r:a,g:r,b:i,a:l}}function a(t,e,r,i=255){let l=t=>t.toString(16).padStart(2,"0"),s=`#${l(t)}${l(e)}${l(r)}`;return i<255?`${s}${l(i)}`:s}function r(e){if("string"==typeof e){let a=t.get(e);if(a)return a;let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(e),i=r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16),a:r[4]?parseInt(r[4],16):255}:{r:0,g:0,b:0,a:255};return t.set(e,i),i}return{r:e.r,g:e.g,b:e.b,a:e.a??255}}function i(t,e=100){let a=[];for(let r=0;r<=e;r++){let i=Math.round(r/e*255).toString(16).padStart(2,"0").toUpperCase(),l=`${t}${i}`;a.push(l)}return a}function l(){t.clear()}class s{colorsPresetList=[];range=[0,0,0];static transparent={r:0,g:0,b:0,a:0};constructor(t,e,a,r=.1){this.setColors(t,e,a,r)}setColors(t,e,a,i){this.colorsPresetList=[];let l=Math.max(1,Math.floor((a-e)/i)),s=a-e;this.range=[e,a,s];let n=t.map(r);for(let t=0;t<=l;t++){let a=(e+t*i-e)/s;this.colorsPresetList.push(this.interpolateColor(n,a))}}interpolateColor(t,e){let r=Math.floor(e*(t.length-1)),i=e*(t.length-1)-r,l=t[r],s=t[r+1]||t[r],n={r:Math.round(l.r+i*(s.r-l.r)),g:Math.round(l.g+i*(s.g-l.g)),b:Math.round(l.b+i*(s.b-l.b)),a:Math.round(l.a+i*(s.a-l.a))};return{...n,hax:a(n.r,n.g,n.b,n.a)}}getColor(t){if(!Number.isFinite(t))return s.transparent;let[e,,a]=this.range,r=(t-e)/a;if(r<=0)return this.colorsPresetList[0];if(r>=1)return this.colorsPresetList[this.colorsPresetList.length-1];let i=Math.min(Math.floor(r*this.colorsPresetList.length),this.colorsPresetList.length-1);return this.colorsPresetList[i]}}class n{state;constructor(t){this.state={id:t.id,container:void 0,canvas:null,ctx:null,range:t.range??[-20,100]},this.init(t)}updateProps(t){this.state={...this.state,...t}}init(t){let{id:e}=this.state,a=document.getElementById(e),r=document.createElement("canvas"),i=r.getContext?.("2d");r.style.transform="scaleY(-1)",i&&(i.imageSmoothingEnabled=!1,i.lineJoin="miter",i.lineCap="butt",i.textBaseline="middle"),i&&this.updateProps({container:a??void 0,canvas:r,ctx:i}),a?.appendChild(r),this.resize(!1)}clearRect(){let{ctx:t,canvas:e}=this.state;t.clearRect(0,0,e.width,e.height)}resize(t=!0){let{canvas:e,container:a}=this.state,{clientWidth:r,clientHeight:i}=a;r&&i&&(e.width=r,e.height=i,t&&setTimeout(()=>{this.draw()}))}draw(){}dispose(){let{canvas:t,container:e}=this.state;e?.removeChild(t)}}let o={COLOR:"#000000",THICKNESS:1,FILL_STYLE:"#ffffffB0",FILL_STYLE_PRIMARY:"#1890ff",FILL_STYLE_TRANSPARENT_BASE:"#ffffff10",LINE_COLOR:"#00ff00",POINT_COLOR:"#ff0000",DIAL_NORTH_COLOR:"#ff4d4f",DIAL_SOUTH_COLOR:"#1890ff",FLUORESCENCE_COLORS:["#000080","#0000FF","#00FFFF","#00FF00","#FFFF00","#FF0000"],RANGE:[-20,100]},h={LEVEL_MIN:-20,LEVEL_MAX:140,LEVEL_RANGE:161,MAX_GAUSSIAN_RADIUS:30},u={Y_RANGE:[-1,1],SEGMENT_SIZE:5,MODE:"classic",DECAY_FACTOR:.98,MAX_ACCUMULATION:1e3,MODERN_COLORS:["#000080","#0000FF","#00FFFF","#00FF00","#FFFF00","#FF0000"],CLASSIC_COLORS:["#00ff00","#ff0000"],CLASSIC_ALPHA_GAIN:50};var c=/*#__PURE__*/function(t){return t.Horizontal="horizontal",t.Vertical="vertical",t}({}),d=/*#__PURE__*/function(t){return t.Circle="circle",t.Rect="rect",t.Line="line",t.Stepline="stepline",t.Bar="bar",t.Area="area",t}({}),f=/*#__PURE__*/function(t){return t.Canvas="canvas",t.WebGL="webgl",t}({}),g=/*#__PURE__*/function(t){return t.Gaussian="gaussian",t.Grid="grid",t}({});class p extends n{init(t){super.init(t);let{colors:e,display:a,fluorescenceRenderMode:r}=t;this.updateProps({colors:e,data:new Uint32Array(0),display:a,renderMode:r??g.Gaussian}),this.resize()}updateProps(t){let e=t.range&&(t.range[0]!==this.state.range?.[0]||t.range[1]!==this.state.range?.[1]),a=void 0!==t.renderMode&&t.renderMode!==this.state.renderMode;super.updateProps(t),(e||a)&&this.state.data?.length>0&&this.draw()}setRenderMode(t){this.updateProps({renderMode:t})}clearImageData(){let{ctx:t,canvas:{height:e,width:a}}=this.state;e&&a&&this.updateProps({imageData:t.createImageData(a,e)})}clearRect(){super.clearRect(),this.clearImageData()}clear(){this.clearRect(),this.updateProps({data:new Uint32Array(0)})}dispose(){this.intensityMatrixCache=null,this.gridCentersCache=null,this.weightLookupCache=null,this.colorLookupCache=null,this.colorCache.clear(),this.lastRenderParams=null,this.blockPositionsCache=null,this.lastBlockRenderParams=null}drawBlocks(){let{imageData:t,data:e,canvas:a,ctx:r,colors:i=o.FLUORESCENCE_COLORS,range:l=o.RANGE}=this.state;if(!e||0===e.length||!t)return;let{width:s,height:n}=a,[u,c]=l,d=c-u;if(d<=0)return;let f=e.length/h.LEVEL_RANGE;if(!Number.isInteger(f)||f<=0)return;let g={dataLength:f,width:s,height:n,rangeMin:u,rangeMax:c};if(!this.lastBlockRenderParams||Object.keys(g).some(t=>this.lastBlockRenderParams?.[t]!==g[t])||!this.blockPositionsCache||this.blockPositionsCache.length!==f){this.blockPositionsCache=Array(f);let t=Math.max(1,Math.floor(s/f));for(let e=0;e<f;e++){let a=Math.floor(e*s/f),r=Math.min(s-1,a+t-1);this.blockPositionsCache[e]={startX:a,endX:r,width:r-a+1}}}this.lastBlockRenderParams=g;let p=t.data;p.fill(0);let m=1/d,C=n-1,T=Math.max(1,Math.ceil(n/d)),M=0;for(let t=0;t<f;t++){let a=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let r=e[a+t];if(r<=0)continue;let i=t+h.LEVEL_MIN;i<u||i>c||!(r>M)||(M=r)}}let E=Math.log(Math.max(M,1)+1);for(let t=0;t<f;t++){let{startX:a,endX:r}=this.blockPositionsCache[t],l=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let n=e[l+t];if(n<=0)continue;let o=t+h.LEVEL_MIN;if(o<u||o>c)continue;let d=Math.max(0,Math.floor((o-u)*m*C-T/2)),f=Math.min(C,d+T-1),g=Math.log(n+1)/E,M=Math.round(255*g),{r:R,g:L,b:A}=this.interpolateColor(i,g);for(let t=d;t<=f;t++){let e=t*s;for(let t=a;t<=r;t++){let a=(e+t)*4;p[a]=R,p[a+1]=L,p[a+2]=A,p[a+3]=M}}}}r.putImageData(t,0,0)}resize(){super.resize(),this.clearImageData()}setRange(t){t&&(this.updateProps({range:t}),this.draw())}render(t){t?.fluorescenceData?.length>=0&&((this.state.data?.length??0)!==t.fluorescenceData.length&&(this.clearImageData(),this.intensityMatrixCache=null,this.gridCentersCache=null,this.blockPositionsCache=null,this.lastRenderParams=null,this.lastBlockRenderParams=null),this.state.data=t.fluorescenceData,this.state.fluorescenceMaxCount=t.fluorescenceMaxCount,this.draw())}interpolateColor(t,e){if(!t||0===t.length)return{r:255,g:255,b:255,a:255};e=Math.max(0,Math.min(1,e));let a=t=>"string"==typeof t?this.hexToRgb(t):{r:t.r,g:t.g,b:t.b};if(1===t.length||0===e)return{...a(t[0]),a:255};if(1===e)return{...a(t[t.length-1]),a:255};let r=e*(t.length-1),i=Math.floor(r),l=r-i,s=Math.min(i,t.length-2),n=a(t[s]),o=a(t[s+1]);return{r:Math.round(n.r+(o.r-n.r)*l),g:Math.round(n.g+(o.g-n.g)*l),b:Math.round(n.b+(o.b-n.b)*l),a:255}}colorCache=new Map;hexToRgb(t){let e=this.colorCache.get(t);if(e)return e;let a=r(t),i={r:a.r,g:a.g,b:a.b};return this.colorCache.set(t,i),i}lastRenderParams=null;intensityMatrixCache=null;gridCentersCache=null;weightLookupCache=null;colorLookupCache=null;blockPositionsCache=null;lastBlockRenderParams=null;draw(){let{imageData:t,data:e,canvas:a,ctx:r,colors:i=o.FLUORESCENCE_COLORS,range:l=o.RANGE,fluorescenceMaxCount:s=1,display:n,renderMode:u=g.Gaussian}=this.state;if(!n||!e||0===e.length||!t)return;if(u===g.Grid){this.drawBlocks();return}let{width:c,height:d}=a,[f,p]=l,m=p-f;if(m<=0)return;let C=e.length/h.LEVEL_RANGE;if(!Number.isInteger(C)||C<=0)return;let T={dataLength:C,rangeMin:f,rangeMax:p,width:c,height:d,maxCount:s},M=!this.lastRenderParams||this.lastRenderParams.dataLength!==T.dataLength||this.lastRenderParams.width!==T.width,E=!this.lastRenderParams||this.lastRenderParams.height!==T.height,R=!this.colorLookupCache;this.lastRenderParams=T;let L=t.data;L.fill(0);let A=Math.min(Math.max(2,Math.ceil(d/m),Math.floor(d/50)),h.MAX_GAUSSIAN_RADIUS),S=A*A,_=c*d,P=Math.min(Math.max(A,Math.ceil(c/C)),h.MAX_GAUSSIAN_RADIUS);this.intensityMatrixCache&&this.intensityMatrixCache.length===_?this.intensityMatrixCache.fill(0):this.intensityMatrixCache=new Float32Array(_);let I=this.intensityMatrixCache;if(M||!this.gridCentersCache||this.gridCentersCache.length!==C){this.gridCentersCache=new Float32Array(C);let t=c/C;for(let e=0;e<C;e++){let a=Math.floor(t*e),r=Math.ceil(t*(e+1));this.gridCentersCache[e]=(a+r)/2}}let x=this.gridCentersCache,b=Math.ceil(10*A)+1;if(E||!this.weightLookupCache||this.weightLookupCache.length!==b){this.weightLookupCache=new Float32Array(b);for(let t=0;t<=10*A;t++){let e=t/10;this.weightLookupCache[t]=Math.exp(-(e*e)/(2*S))}}let w=this.weightLookupCache,y=0;for(let t=0;t<C;t++){let a=x[t],r=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let i=e[r+t];if(i<=0)continue;let l=t+h.LEVEL_MIN;if(l<f||l>p)continue;let n=(l-f)/m*(d-1),o=Math.log(i+1)/Math.log(Math.max(s,1)+1),u=Math.max(0,Math.floor(a-P)),g=Math.min(c-1,Math.ceil(a+P)),C=Math.max(0,Math.floor(n-A)),T=Math.min(d-1,Math.ceil(n+A));for(let t=C;t<=T;t++){let e=t-n,r=e*e,i=t*c;for(let t=u;t<=g;t++){let e=(t-a)*A/P,l=e*e+r;if(l>S)continue;let s=Math.floor(10*Math.sqrt(l)),n=s<w.length?w[s]:0,h=i+t,u=I[h]+o*n;I[h]=u,u>y&&(y=u)}}}}let N=y>0?1/y:0;if(R||!this.colorLookupCache||256!==this.colorLookupCache.length){this.colorLookupCache=Array(256);for(let t=0;t<256;t++){let e=t/255;this.colorLookupCache[t]=this.interpolateColor(i,e)}}let v=this.colorLookupCache;for(let t=0;t<I.length;t++){let e=I[t];if(e<=.001)continue;let a=Math.min(e*N,1),r=v[Math.min(Math.floor(255*a),255)],i=4*t;L[i]=r.r,L[i+1]=r.g,L[i+2]=r.b,L[i+3]=Math.round(255*a)}r.putImageData(t,0,0)}}function m(t,e,a,r,i){if(!a)return;let l=a.length,s=e/l,n=0;for(let e=0;e<l;e++){let l=a[e],o=l.length;if(0===o)continue;0===n&&(n=t/o);let h=Math.floor(s*e),u=Math.ceil(s*(e+1));for(let e=0;e<o;e++){let a=l[e],s=Math.floor(n*e),o=Math.ceil(n*(e+1)),c=i(a);for(let e=h;e<u;e++){let a=e*t;for(let t=s;t<o;t++){let e=(a+t)*4;r[e]=c.r,r[e+1]=c.g,r[e+2]=c.b,r[e+3]=c.a}}}}}function C(t){if(!Array.isArray(t)||0===t.length)throw Error("Input must be a non-empty array.");let e=t[0],a=t[0];for(let r=1;r<t.length;r++)t[r]<e?e=t[r]:t[r]>a&&(a=t[r]);return[e,a]}function T(t){return"number"==typeof t&&Number.isFinite(t)}function M(t){return new Float64Array(t.flatMap(e=>0===e.length?Array(t[0].length).fill(Number.NaN):e))}let E=null;function R(){if(null!==E)return E;try{let t=document.createElement("canvas").getContext("webgl2");if(E=!!t,t){let e=t.getExtension("WEBGL_lose_context");e?.loseContext()}}catch{E=!1}return E}class L extends n{init(t){super.init(t),this.state.canvas.style.transform="scaleY(1)";let e=t.fillStyle??o.FILL_STYLE,a=t.fillStylePrimary??o.FILL_STYLE_PRIMARY,r=t.fillStyleTransparentBase??o.FILL_STYLE_TRANSPARENT_BASE;this.updateProps({fillStyle:e,fillStylePrimary:a,fillStyleTransparentBase:r,baseWidth:6,padding:t.padding??0,step:t.ticksStep??10,lineWidth:1,data:{value:0,limit:0}})}clear(){this.clearRect();let{ctx:t,BGCanvas:e}=this.state;e&&t.drawImage(e,0,0)}resize(){super.resize(),this.state.lineWidth&&this.drawBackground()}render(t){this.state.data={...this.state.data,...t},this.draw()}draw(){let{data:{value:t},range:[e,a],canvas:r,ctx:i,baseWidth:l,padding:s,BGCanvas:n,fillStylePrimary:o}=this.state;this.clearRect(),n&&i.drawImage(n,0,0);let h=3*l,u=r.height-2*s,c=(r.width-h)/2,d=(t-e)/(a-e)*u;i.fillStyle=o,i.fillRect(c,u-d+s,h,d),this.drawLimit()}drawLimit(){let{data:{limit:t},range:[e,a],canvas:r,ctx:i,baseWidth:l,padding:s,lineWidth:n,fillStylePrimary:o}=this.state;if(!T(t))return;let h=3*l,u=r.height-2*s,c=(r.width-h)/2,d=u*(a-t)/(a-e)+s;i.beginPath(),i.moveTo(c+h,d),i.lineTo(c+h+9,d-9),i.lineTo(c+h+9+30,d-9),i.lineTo(c+h+9+30,d+9),i.lineTo(c+h+9,d+9),i.lineTo(c+h,d),i.lineTo(c,d),i.lineWidth=n,i.strokeStyle=o,i.stroke(),i.fillStyle=o,i.font="12px Arial",i.textAlign="center",i.textBaseline="middle",i.fillText(t.toString(),c+h+9+15,d+1)}drawBackground(){let{canvas:t,baseWidth:e,padding:a,lineWidth:r,step:i,range:[l,s],fillStyle:n,fillStyleTransparentBase:o}=this.state,h=document.createElement("canvas");h.width=t.width,h.height=t.height;let u=h.getContext("2d");if(!u)return;let c=3*e,d=t.height-2*a,f=(t.width-c)/2,g=Array.from({length:11},(t,e)=>l+Math.round(e*(s-l)/10/i)*i);u.fillStyle=o,u.fillRect(f,a,c,d);for(let t=l;t<=s;t+=i){let i=g.includes(t),o=d*(s-t)/(s-l)+a;u.beginPath(),u.moveTo(f-(i?e:e/2),o),u.lineTo(f,o),u.strokeStyle=n,u.lineWidth=i?r:r/2,u.stroke(),i&&(u.fillStyle=n,u.font="12px Arial",u.textAlign="right",u.textBaseline="middle",u.fillText(t.toString(),f-1.2*e,o))}this.state.BGCanvas=h}}class A{state;constructor(t){this.state={id:t.id,container:void 0,canvas:null,gl:null,range:t.range??[-20,100]},this.init(t)}updateProps(t){this.state={...this.state,...t}}init(t){let{id:e}=this.state,a=document.getElementById(e),r=document.createElement("canvas"),i=r.getContext("webgl2",{alpha:!0,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1});if(!i){console.error("WebGL 2.0 not supported");return}r.style.transform="scaleY(-1)",this.updateProps({container:a??void 0,canvas:r,gl:i}),a?.appendChild(r),this.resize(!1)}compileShader(t,e){let{gl:a}=this.state,r=a.createShader(t);return r?(a.shaderSource(r,e),a.compileShader(r),a.getShaderParameter(r,a.COMPILE_STATUS))?r:(console.error("Shader compile error:",a.getShaderInfoLog(r)),a.deleteShader(r),null):null}createProgram(t){let{gl:e}=this.state,a=this.compileShader(e.VERTEX_SHADER,t.vertex),r=this.compileShader(e.FRAGMENT_SHADER,t.fragment);if(!a||!r)return null;let i=e.createProgram();return i?(e.attachShader(i,a),e.attachShader(i,r),e.linkProgram(i),e.getProgramParameter(i,e.LINK_STATUS))?(e.deleteShader(a),e.deleteShader(r),i):(console.error("Program link error:",e.getProgramInfoLog(i)),e.deleteProgram(i),null):null}createTexture(){let{gl:t}=this.state,e=t.createTexture();return e?(t.bindTexture(t.TEXTURE_2D,e),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),e):null}createQuadBuffer(){let{gl:t}=this.state,e=t.createBuffer();if(!e)return null;t.bindBuffer(t.ARRAY_BUFFER,e);let a=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);return t.bufferData(t.ARRAY_BUFFER,a,t.STATIC_DRAW),e}clearRect(){let{gl:t}=this.state;t.clearColor(0,0,0,0),t.clear(t.COLOR_BUFFER_BIT)}resize(t=!0){let{canvas:e,container:a,gl:r}=this.state;if(!a)return;let{clientWidth:i,clientHeight:l}=a;i&&l&&(e.width=i,e.height=l,r.viewport(0,0,i,l),t&&setTimeout(()=>{this.draw()}))}draw(){}dispose(){let{gl:t,canvas:e}=this.state;if(t){let e=t.getExtension("WEBGL_lose_context");e&&e.loseContext()}e?.remove()}}let S=`#version 300 es
|
|
1
|
+
let t=new Map;function e(t,e){let a,r,i,l;if(!/^#([A-Fa-f0-9]{3}){1,2}([A-Fa-f0-9]{2})?$/.test(t))throw Error("Invalid hex color format");let s=t.replace("#","");if(3===s.length)a=Number.parseInt(s[0]+s[0],16),r=Number.parseInt(s[1]+s[1],16),i=Number.parseInt(s[2]+s[2],16),l=255;else if(6===s.length)a=Number.parseInt(s.substring(0,2),16),r=Number.parseInt(s.substring(2,4),16),i=Number.parseInt(s.substring(4,6),16),l=255;else if(8===s.length)a=Number.parseInt(s.substring(0,2),16),r=Number.parseInt(s.substring(2,4),16),i=Number.parseInt(s.substring(4,6),16),l=Number.parseInt(s.substring(6,8),16);else throw Error("Invalid hex color length");return"number"==typeof e&&(l=Math.round(255*Math.max(0,Math.min(1,e)))),{r:a,g:r,b:i,a:l}}function a(t,e,r,i=255){let l=t=>t.toString(16).padStart(2,"0"),s=`#${l(t)}${l(e)}${l(r)}`;return i<255?`${s}${l(i)}`:s}function r(e){if("string"==typeof e){let a=t.get(e);if(a)return a;let r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(e),i=r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16),a:r[4]?parseInt(r[4],16):255}:{r:0,g:0,b:0,a:255};return t.set(e,i),i}return{r:e.r,g:e.g,b:e.b,a:e.a??255}}function i(t,e=100){let a=[];for(let r=0;r<=e;r++){let i=Math.round(r/e*255).toString(16).padStart(2,"0").toUpperCase(),l=`${t}${i}`;a.push(l)}return a}function l(){t.clear()}class s{colorsPresetList=[];range=[0,0,0];static transparent={r:0,g:0,b:0,a:0};constructor(t,e,a,r=.1){this.setColors(t,e,a,r)}setColors(t,e,a,i){this.colorsPresetList=[];let l=Math.max(1,Math.floor((a-e)/i)),s=a-e;this.range=[e,a,s];let n=t.map(r);for(let t=0;t<=l;t++){let a=(e+t*i-e)/s;this.colorsPresetList.push(this.interpolateColor(n,a))}}interpolateColor(t,e){let r=Math.floor(e*(t.length-1)),i=e*(t.length-1)-r,l=t[r],s=t[r+1]||t[r],n={r:Math.round(l.r+i*(s.r-l.r)),g:Math.round(l.g+i*(s.g-l.g)),b:Math.round(l.b+i*(s.b-l.b)),a:Math.round(l.a+i*(s.a-l.a))};return{...n,hax:a(n.r,n.g,n.b,n.a)}}getColor(t){if(!Number.isFinite(t))return s.transparent;let[e,,a]=this.range,r=(t-e)/a;if(r<=0)return this.colorsPresetList[0];if(r>=1)return this.colorsPresetList[this.colorsPresetList.length-1];let i=Math.min(Math.floor(r*this.colorsPresetList.length),this.colorsPresetList.length-1);return this.colorsPresetList[i]}}class n{state;constructor(t){this.state={id:t.id,container:void 0,canvas:null,ctx:null,range:t.range??[-20,100]},this.init(t)}updateProps(t){this.state={...this.state,...t}}init(t){let{id:e}=this.state,a=document.getElementById(e),r=document.createElement("canvas"),i=r.getContext?.("2d");r.style.transform="scaleY(-1)",i&&(i.imageSmoothingEnabled=!1,i.lineJoin="miter",i.lineCap="butt",i.textBaseline="middle"),i&&this.updateProps({container:a??void 0,canvas:r,ctx:i}),a?.appendChild(r),this.resize(!1)}clearRect(){let{ctx:t,canvas:e}=this.state;t.clearRect(0,0,e.width,e.height)}resize(t=!0){let{canvas:e,container:a}=this.state,{clientWidth:r,clientHeight:i}=a;r&&i&&(e.width=r,e.height=i,t&&setTimeout(()=>{this.draw()}))}draw(){}dispose(){let{canvas:t,container:e}=this.state;e?.removeChild(t)}}let o={COLOR:"#000000",THICKNESS:1,FILL_STYLE:"#ffffffB0",FILL_STYLE_PRIMARY:"#1890ff",FILL_STYLE_TRANSPARENT_BASE:"#ffffff10",LINE_COLOR:"#00ff00",POINT_COLOR:"#ff0000",DIAL_NORTH_COLOR:"#ff4d4f",DIAL_SOUTH_COLOR:"#1890ff",FLUORESCENCE_COLORS:["#000080","#0000FF","#00FFFF","#00FF00","#FFFF00","#FF0000"],RANGE:[-20,100]},h={LEVEL_MIN:-20,LEVEL_MAX:140,LEVEL_RANGE:161,MAX_GAUSSIAN_RADIUS:30},u={Y_RANGE:[-1,1],SEGMENT_SIZE:5,MODE:"classic",DECAY_FACTOR:.98,MAX_ACCUMULATION:1e3,MODERN_COLORS:["#000080","#0000FF","#00FFFF","#00FF00","#FFFF00","#FF0000"],CLASSIC_COLORS:["#00ff00","#ff0000"],CLASSIC_ALPHA_GAIN:50};var c=/*#__PURE__*/function(t){return t.Horizontal="horizontal",t.Vertical="vertical",t}({}),d=/*#__PURE__*/function(t){return t.Circle="circle",t.Rect="rect",t.Line="line",t.Stepline="stepline",t.Bar="bar",t.Area="area",t}({}),f=/*#__PURE__*/function(t){return t.Canvas="canvas",t.WebGL="webgl",t}({}),g=/*#__PURE__*/function(t){return t.Gaussian="gaussian",t.Grid="grid",t}({});class p extends n{init(t){super.init(t);let{colors:e,display:a,fluorescenceRenderMode:r}=t;this.updateProps({colors:e,data:new Uint32Array(0),display:a,renderMode:r??g.Gaussian}),this.resize()}updateProps(t){let e=t.range&&(t.range[0]!==this.state.range?.[0]||t.range[1]!==this.state.range?.[1]),a=void 0!==t.renderMode&&t.renderMode!==this.state.renderMode;super.updateProps(t),(e||a)&&this.state.data?.length>0&&this.draw()}setRenderMode(t){this.updateProps({renderMode:t})}clearImageData(){let{ctx:t,canvas:{height:e,width:a}}=this.state;e&&a&&this.updateProps({imageData:t.createImageData(a,e)})}clearRect(){super.clearRect(),this.clearImageData()}clear(){this.clearRect(),this.updateProps({data:new Uint32Array(0)})}dispose(){this.intensityMatrixCache=null,this.gridCentersCache=null,this.weightLookupCache=null,this.colorLookupCache=null,this.colorCache.clear(),this.lastRenderParams=null,this.blockPositionsCache=null,this.lastBlockRenderParams=null}drawBlocks(){let{imageData:t,data:e,canvas:a,ctx:r,colors:i=o.FLUORESCENCE_COLORS,range:l=o.RANGE}=this.state;if(!e||0===e.length||!t)return;let{width:s,height:n}=a,[u,c]=l,d=c-u;if(d<=0)return;let f=e.length/h.LEVEL_RANGE;if(!Number.isInteger(f)||f<=0)return;let g={dataLength:f,width:s,height:n,rangeMin:u,rangeMax:c};if(!this.lastBlockRenderParams||Object.keys(g).some(t=>this.lastBlockRenderParams?.[t]!==g[t])||!this.blockPositionsCache||this.blockPositionsCache.length!==f){this.blockPositionsCache=Array(f);let t=Math.max(1,Math.floor(s/f));for(let e=0;e<f;e++){let a=Math.floor(e*s/f),r=Math.min(s-1,a+t-1);this.blockPositionsCache[e]={startX:a,endX:r,width:r-a+1}}}this.lastBlockRenderParams=g;let p=t.data;p.fill(0);let m=1/d,T=n-1,C=Math.max(1,Math.ceil(n/d)),M=0;for(let t=0;t<f;t++){let a=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let r=e[a+t];if(r<=0)continue;let i=t+h.LEVEL_MIN;i<u||i>c||!(r>M)||(M=r)}}let E=Math.log(Math.max(M,1)+1);for(let t=0;t<f;t++){let{startX:a,endX:r}=this.blockPositionsCache[t],l=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let n=e[l+t];if(n<=0)continue;let o=t+h.LEVEL_MIN;if(o<u||o>c)continue;let d=Math.max(0,Math.floor((o-u)*m*T-C/2)),f=Math.min(T,d+C-1),g=Math.log(n+1)/E,M=Math.round(255*g),{r:R,g:L,b:A}=this.interpolateColor(i,g);for(let t=d;t<=f;t++){let e=t*s;for(let t=a;t<=r;t++){let a=(e+t)*4;p[a]=R,p[a+1]=L,p[a+2]=A,p[a+3]=M}}}}r.putImageData(t,0,0)}resize(){super.resize(),this.clearImageData()}setRange(t){t&&(this.updateProps({range:t}),this.draw())}render(t){t?.fluorescenceData?.length>=0&&((this.state.data?.length??0)!==t.fluorescenceData.length&&(this.clearImageData(),this.intensityMatrixCache=null,this.gridCentersCache=null,this.blockPositionsCache=null,this.lastRenderParams=null,this.lastBlockRenderParams=null),this.state.data=t.fluorescenceData,this.state.fluorescenceMaxCount=t.fluorescenceMaxCount,this.draw())}interpolateColor(t,e){if(!t||0===t.length)return{r:255,g:255,b:255,a:255};e=Math.max(0,Math.min(1,e));let a=t=>"string"==typeof t?this.hexToRgb(t):{r:t.r,g:t.g,b:t.b};if(1===t.length||0===e)return{...a(t[0]),a:255};if(1===e)return{...a(t[t.length-1]),a:255};let r=e*(t.length-1),i=Math.floor(r),l=r-i,s=Math.min(i,t.length-2),n=a(t[s]),o=a(t[s+1]);return{r:Math.round(n.r+(o.r-n.r)*l),g:Math.round(n.g+(o.g-n.g)*l),b:Math.round(n.b+(o.b-n.b)*l),a:255}}colorCache=new Map;hexToRgb(t){let e=this.colorCache.get(t);if(e)return e;let a=r(t),i={r:a.r,g:a.g,b:a.b};return this.colorCache.set(t,i),i}lastRenderParams=null;intensityMatrixCache=null;gridCentersCache=null;weightLookupCache=null;colorLookupCache=null;blockPositionsCache=null;lastBlockRenderParams=null;draw(){let{imageData:t,data:e,canvas:a,ctx:r,colors:i=o.FLUORESCENCE_COLORS,range:l=o.RANGE,fluorescenceMaxCount:s=1,display:n,renderMode:u=g.Gaussian}=this.state;if(!n||!e||0===e.length||!t)return;if(u===g.Grid){this.drawBlocks();return}let{width:c,height:d}=a,[f,p]=l,m=p-f;if(m<=0)return;let T=e.length/h.LEVEL_RANGE;if(!Number.isInteger(T)||T<=0)return;let C={dataLength:T,rangeMin:f,rangeMax:p,width:c,height:d,maxCount:s},M=!this.lastRenderParams||this.lastRenderParams.dataLength!==C.dataLength||this.lastRenderParams.width!==C.width,E=!this.lastRenderParams||this.lastRenderParams.height!==C.height,R=!this.colorLookupCache;this.lastRenderParams=C;let L=t.data;L.fill(0);let A=Math.min(Math.max(2,Math.ceil(d/m),Math.floor(d/50)),h.MAX_GAUSSIAN_RADIUS),S=A*A,_=c*d,P=Math.min(Math.max(A,Math.ceil(c/T)),h.MAX_GAUSSIAN_RADIUS);this.intensityMatrixCache&&this.intensityMatrixCache.length===_?this.intensityMatrixCache.fill(0):this.intensityMatrixCache=new Float32Array(_);let I=this.intensityMatrixCache;if(M||!this.gridCentersCache||this.gridCentersCache.length!==T){this.gridCentersCache=new Float32Array(T);let t=c/T;for(let e=0;e<T;e++){let a=Math.floor(t*e),r=Math.ceil(t*(e+1));this.gridCentersCache[e]=(a+r)/2}}let x=this.gridCentersCache,b=Math.ceil(10*A)+1;if(E||!this.weightLookupCache||this.weightLookupCache.length!==b){this.weightLookupCache=new Float32Array(b);for(let t=0;t<=10*A;t++){let e=t/10;this.weightLookupCache[t]=Math.exp(-(e*e)/(2*S))}}let w=this.weightLookupCache,y=0;for(let t=0;t<T;t++){let a=x[t],r=t*h.LEVEL_RANGE;for(let t=0;t<h.LEVEL_RANGE;t++){let i=e[r+t];if(i<=0)continue;let l=t+h.LEVEL_MIN;if(l<f||l>p)continue;let n=(l-f)/m*(d-1),o=Math.log(i+1)/Math.log(Math.max(s,1)+1),u=Math.max(0,Math.floor(a-P)),g=Math.min(c-1,Math.ceil(a+P)),T=Math.max(0,Math.floor(n-A)),C=Math.min(d-1,Math.ceil(n+A));for(let t=T;t<=C;t++){let e=t-n,r=e*e,i=t*c;for(let t=u;t<=g;t++){let e=(t-a)*A/P,l=e*e+r;if(l>S)continue;let s=Math.floor(10*Math.sqrt(l)),n=s<w.length?w[s]:0,h=i+t,u=I[h]+o*n;I[h]=u,u>y&&(y=u)}}}}let N=y>0?1/y:0;if(R||!this.colorLookupCache||256!==this.colorLookupCache.length){this.colorLookupCache=Array(256);for(let t=0;t<256;t++){let e=t/255;this.colorLookupCache[t]=this.interpolateColor(i,e)}}let v=this.colorLookupCache;for(let t=0;t<I.length;t++){let e=I[t];if(e<=.001)continue;let a=Math.min(e*N,1),r=v[Math.min(Math.floor(255*a),255)],i=4*t;L[i]=r.r,L[i+1]=r.g,L[i+2]=r.b,L[i+3]=Math.round(255*a)}r.putImageData(t,0,0)}}function m(t,e,a,r,i){if(!a)return;let l=a.length,s=e/l,n=0;for(let e=0;e<l;e++){let l=a[e],o=l.length;if(0===o)continue;0===n&&(n=t/o);let h=Math.floor(s*e),u=Math.ceil(s*(e+1));for(let e=0;e<o;e++){let a=l[e],s=Math.floor(n*e),o=Math.ceil(n*(e+1)),c=i(a);for(let e=h;e<u;e++){let a=e*t;for(let t=s;t<o;t++){let e=(a+t)*4;r[e]=c.r,r[e+1]=c.g,r[e+2]=c.b,r[e+3]=c.a}}}}}function T(t){if(!Array.isArray(t)||0===t.length)throw Error("Input must be a non-empty array.");let e=t[0],a=t[0];for(let r=1;r<t.length;r++)t[r]<e?e=t[r]:t[r]>a&&(a=t[r]);return[e,a]}function C(t){return"number"==typeof t&&Number.isFinite(t)}function M(t){return new Float64Array(t.flatMap(e=>0===e.length?Array(t[0].length).fill(Number.NaN):e))}let E=null;function R(){if(null!==E)return E;try{let t=document.createElement("canvas").getContext("webgl2");if(E=!!t,t){let e=t.getExtension("WEBGL_lose_context");e?.loseContext()}}catch{E=!1}return E}class L extends n{init(t){super.init(t),this.state.canvas.style.transform="scaleY(1)";let e=t.fillStyle??o.FILL_STYLE,a=t.fillStylePrimary??o.FILL_STYLE_PRIMARY,r=t.fillStyleTransparentBase??o.FILL_STYLE_TRANSPARENT_BASE;this.updateProps({fillStyle:e,fillStylePrimary:a,fillStyleTransparentBase:r,baseWidth:6,padding:t.padding??0,step:t.ticksStep??10,lineWidth:1,data:{value:0,limit:0}})}clear(){this.clearRect();let{ctx:t,BGCanvas:e}=this.state;e&&t.drawImage(e,0,0)}resize(){super.resize(),this.state.lineWidth&&this.drawBackground()}render(t){this.state.data={...this.state.data,...t},this.draw()}draw(){let{data:{value:t},range:[e,a],canvas:r,ctx:i,baseWidth:l,padding:s,BGCanvas:n,fillStylePrimary:o}=this.state;this.clearRect(),n&&i.drawImage(n,0,0);let h=3*l,u=r.height-2*s,c=(r.width-h)/2,d=(t-e)/(a-e)*u;i.fillStyle=o,i.fillRect(c,u-d+s,h,d),this.drawLimit()}drawLimit(){let{data:{limit:t},range:[e,a],canvas:r,ctx:i,baseWidth:l,padding:s,lineWidth:n,fillStylePrimary:o}=this.state;if(!C(t))return;let h=3*l,u=r.height-2*s,c=(r.width-h)/2,d=u*(a-t)/(a-e)+s;i.beginPath(),i.moveTo(c+h,d),i.lineTo(c+h+9,d-9),i.lineTo(c+h+9+30,d-9),i.lineTo(c+h+9+30,d+9),i.lineTo(c+h+9,d+9),i.lineTo(c+h,d),i.lineTo(c,d),i.lineWidth=n,i.strokeStyle=o,i.stroke(),i.fillStyle=o,i.font="12px Arial",i.textAlign="center",i.textBaseline="middle",i.fillText(t.toString(),c+h+9+15,d+1)}drawBackground(){let{canvas:t,baseWidth:e,padding:a,lineWidth:r,step:i,range:[l,s],fillStyle:n,fillStyleTransparentBase:o}=this.state,h=document.createElement("canvas");h.width=t.width,h.height=t.height;let u=h.getContext("2d");if(!u)return;let c=3*e,d=t.height-2*a,f=(t.width-c)/2,g=Array.from({length:11},(t,e)=>l+Math.round(e*(s-l)/10/i)*i);u.fillStyle=o,u.fillRect(f,a,c,d);for(let t=l;t<=s;t+=i){let i=g.includes(t),o=d*(s-t)/(s-l)+a;u.beginPath(),u.moveTo(f-(i?e:e/2),o),u.lineTo(f,o),u.strokeStyle=n,u.lineWidth=i?r:r/2,u.stroke(),i&&(u.fillStyle=n,u.font="12px Arial",u.textAlign="right",u.textBaseline="middle",u.fillText(t.toString(),f-1.2*e,o))}this.state.BGCanvas=h}}class A{state;constructor(t){this.state={id:t.id,container:void 0,canvas:null,gl:null,range:t.range??[-20,100]},this.init(t)}updateProps(t){this.state={...this.state,...t}}init(t){let{id:e}=this.state,a=document.getElementById(e),r=document.createElement("canvas"),i=r.getContext("webgl2",{alpha:!0,antialias:!1,premultipliedAlpha:!0,preserveDrawingBuffer:!1});if(!i){console.error("WebGL 2.0 not supported");return}r.style.transform="scaleY(-1)",this.updateProps({container:a??void 0,canvas:r,gl:i}),a?.appendChild(r),this.resize(!1)}compileShader(t,e){let{gl:a}=this.state,r=a.createShader(t);return r?(a.shaderSource(r,e),a.compileShader(r),a.getShaderParameter(r,a.COMPILE_STATUS))?r:(console.error("Shader compile error:",a.getShaderInfoLog(r)),a.deleteShader(r),null):null}createProgram(t){let{gl:e}=this.state,a=this.compileShader(e.VERTEX_SHADER,t.vertex),r=this.compileShader(e.FRAGMENT_SHADER,t.fragment);if(!a||!r)return null;let i=e.createProgram();return i?(e.attachShader(i,a),e.attachShader(i,r),e.linkProgram(i),e.getProgramParameter(i,e.LINK_STATUS))?(e.deleteShader(a),e.deleteShader(r),i):(console.error("Program link error:",e.getProgramInfoLog(i)),e.deleteProgram(i),null):null}createTexture(){let{gl:t}=this.state,e=t.createTexture();return e?(t.bindTexture(t.TEXTURE_2D,e),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),e):null}createQuadBuffer(){let{gl:t}=this.state,e=t.createBuffer();if(!e)return null;t.bindBuffer(t.ARRAY_BUFFER,e);let a=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);return t.bufferData(t.ARRAY_BUFFER,a,t.STATIC_DRAW),e}clearRect(){let{gl:t}=this.state;t.clearColor(0,0,0,0),t.clear(t.COLOR_BUFFER_BIT)}resize(t=!0){let{canvas:e,container:a,gl:r}=this.state;if(!a)return;let{clientWidth:i,clientHeight:l}=a;i&&l&&(e.width=i,e.height=l,r.viewport(0,0,i,l),t&&setTimeout(()=>{this.draw()}))}draw(){}dispose(){let{gl:t,canvas:e}=this.state;if(t){let e=t.getExtension("WEBGL_lose_context");e&&e.loseContext()}e?.remove()}}let S=`#version 300 es
|
|
2
2
|
in vec2 a_position;
|
|
3
3
|
out vec2 v_texCoord;
|
|
4
4
|
|
|
@@ -43,4 +43,4 @@ void main() {
|
|
|
43
43
|
// 从颜色查找纹理获取颜色
|
|
44
44
|
fragColor = texture(u_colorTexture, vec2(normalized, 0.5));
|
|
45
45
|
}
|
|
46
|
-
`;class I extends A{init(t){super.init(t);let{gl:e}=this.state;if(!e)return;let a=t.colors,r=a&&a.length>0?a:o.FLUORESCENCE_COLORS,i=this.createProgram({vertex:S,fragment:P}),l=this.createQuadBuffer(),n=this.createTexture(),h=this.createTexture(),u=e.getParameter(e.MAX_TEXTURE_SIZE);if(n){e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,n);let t=new Float32Array([-34028235e31]);e.texImage2D(e.TEXTURE_2D,0,e.R32F,1,1,0,e.RED,e.FLOAT,t)}let[c,d]=t.range??o.RANGE,f=new s(r,c,d,.1);this.updateProps({data:[],colors:r,program:i,quadBuffer:l,dataTexture:n,dataTextures:n?[n]:[],dataTiles:[],colorTexture:h,dataWidth:0,dataHeight:0,CI:f,maxTextureSize:u}),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),this.updateColorTexture(),e.clearColor(0,0,0,0),e.clear(e.COLOR_BUFFER_BIT),this.resize()}updateColorTexture(){let{gl:t,colorTexture:e,CI:a,range:r}=this.state;if(!t||!e||!a)return;let[i,l]=r,s=new Uint8Array(1024);for(let t=0;t<256;t++){let e=i+t/255*(l-i),r=a.getColor(e);s[4*t]=r.r,s[4*t+1]=r.g,s[4*t+2]=r.b,s[4*t+3]=r.a}t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,e),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,256,1,0,t.RGBA,t.UNSIGNED_BYTE,s)}updateDataTexture(){let{gl:t,data:e}=this.state;if(!t||0===e.length)return;let a=e.length,r=0;for(let t=0;t<a;t++){let a=e[t]?.length??0;a>r&&(r=a)}if(0===r)return;let i=this.state.maxTextureSize||t.getParameter(t.MAX_TEXTURE_SIZE);if(!i||i<=0)return;let l=Math.ceil(r/i),s=Math.ceil(a/i),n=l*s,o=this.state.dataTextures??[];if(o.length<n){let t=n-o.length;for(let e=0;e<t;e++){let t=this.createTexture();t&&o.push(t)}}else if(o.length>n){for(let e=n;e<o.length;e++)t.deleteTexture(o[e]);o=o.slice(0,n)}let h=[];t.activeTexture(t.TEXTURE0);for(let n=0;n<s;n++){let s=n*i,u=Math.min(i,a-s);for(let a=0;a<l;a++){let c=a*i,d=Math.min(i,r-c),f=n*l+a,g=o[f];if(!g)continue;let p=new Float32Array(d*u);for(let t=0;t<u;t++){let a=e[s+t],r=a?.length??0,i=t*d;for(let t=0;t<d;t++){let e=c+t;if(0===r||e>=r){p[i+t]=-34028235e31;continue}let l=a[e];void 0===l||Number.isNaN(l)?p[i+t]=-34028235e31:p[i+t]=l}}t.bindTexture(t.TEXTURE_2D,g),t.texImage2D(t.TEXTURE_2D,0,t.R32F,d,u,0,t.RED,t.FLOAT,p),h[f]={startCol:c,startRow:s,width:d,height:u}}}this.updateProps({dataTexture:o[0]??null,dataTextures:o,dataTiles:h,dataWidth:r,dataHeight:a})}updateProps(t){super.updateProps(t);let e=t.colors??this.state.colors,a=t.range??this.state.range;if((t.colors||t.range)&&e.length>0){let[t,r]=a;this.state.CI?this.state.CI.setColors(e,t,r,.1):this.state.CI=new s(e,t,r,.1),this.updateColorTexture()}}setRange(t){t&&(this.updateProps({range:t}),this.draw())}clear(){this.clearRect(),this.updateProps({data:[]})}render(t){t?.length>0&&(this.state.data=t,this.updateDataTexture(),this.draw())}draw(){let{gl:t,program:e,quadBuffer:a,dataTextures:r,dataTiles:i,colorTexture:l,data:s,range:n,dataWidth:o,dataHeight:h,canvas:u}=this.state;if(!t||!e||!a||!l||(t.clearColor(0,0,0,0),t.clear(t.COLOR_BUFFER_BIT),0===s.length||0===o||0===h||0===i.length||0===r.length))return;let[c,d]=n;t.useProgram(e),t.bindBuffer(t.ARRAY_BUFFER,a);let f=t.getAttribLocation(e,"a_position");t.enableVertexAttribArray(f),t.vertexAttribPointer(f,2,t.FLOAT,!1,0,0);let g=t.getUniformLocation(e,"u_dataTexture"),p=t.getUniformLocation(e,"u_colorTexture"),m=t.getUniformLocation(e,"u_rangeMin"),C=t.getUniformLocation(e,"u_rangeMax");if(!g||!p||!m||!C)return;t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,l),t.uniform1i(p,1),t.uniform1f(m,c),t.uniform1f(C,d);let T=u.width,M=u.height,E=t=>Math.round(t/o*T),R=t=>Math.round(t/h*M);for(let e=0;e<i.length;e++){let a=i[e],l=r[e];if(!a||!l)continue;let s=E(a.startCol),n=E(a.startCol+a.width),o=R(a.startRow),h=M-R(a.startRow+a.height),u=n-s,c=M-o-h;u<=0||c<=0||(t.viewport(s,h,u,c),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,l),t.uniform1i(g,0),t.drawArrays(t.TRIANGLES,0,6))}t.viewport(0,0,T,M)}resize(){super.resize()}dispose(){let{gl:t,program:e,quadBuffer:a,dataTextures:r,colorTexture:i}=this.state;if(t){for(let i of(e&&t.deleteProgram(e),a&&t.deleteBuffer(a),r))t.deleteTexture(i);i&&t.deleteTexture(i)}super.dispose()}}class x extends n{init(t){super.init(t);let{colors:e}=t;this.updateProps({colors:e,data:[]}),this.resize()}updateProps(t){super.updateProps(t);let{range:e=this.state.range,colors:a=this.state.colors}=t;if(e&&a){let[t,r]=e;T(t)&&T(r)&&t<r&&(this.state.CI?this.state.CI.setColors(a,t,r,.1):this.updateProps({CI:new s(a,t,r,.1)}))}}clearImageData(){let{ctx:t,canvas:{height:e,width:a}}=this.state;e&&a&&this.updateProps({imageData:t.createImageData(a,e)})}clearRect(){super.clearRect(),this.clearImageData()}clear(){this.clearRect(),this.updateProps({data:[]})}resize(){super.resize(),this.clearImageData()}setRange(t){t&&(this.updateProps({range:t}),this.draw())}render(t){t?.length>=0&&(this.state.data=t,this.draw())}draw(){let{imageData:t,data:e,canvas:a,ctx:r,CI:i}=this.state;0!==e.length&&(m(a.width,a.height,e,t.data,t=>i.getColor(t)),r.putImageData(t,0,0))}}function b(t){let{renderer:e=f.Canvas}=t;if(e===f.WebGL){if(R())return new I(t);console.warn("[Heatmap] WebGL 2.0 not supported, falling back to Canvas")}return new x(t)}let w=function(t){return b(t)},y=w,N=!1;class v extends n{init(t){super.init(t);let{series:e,barValue2Color:a,disabledClearRect:r,colors:i}=t;this.updateProps({interval:0,series:{},barValue2Color:a,disabledClearRect:r,colors:i}),this.setRange(this.state.range),Array.isArray(e)&&e.forEach(t=>{this.setSeries(t)})}updateProps(t){super.updateProps(t);let{colors:e=this.state.colors}=t;this.state.barValue2Color&&!this.state.CI&&e&&this.updateProps({CI:new s(e,0,100,.1)})}clear(){super.clearRect();let{series:t}=this.state;Object.entries(t).forEach(([t,e])=>{this.setSeries({...e,name:t,data:void 0})})}setRange(t){this.updateProps({range:[t[0],t[1],t[1]-t[0]]}),this.draw()}setIntervel(t){t&&this.updateProps({interval:t})}setSeries(t){if(t?.name){let{name:e}=t,{series:a}=this.state;a[e]={thickness:o.THICKNESS,display:!0,color:o.COLOR,type:d.Line,data:void 0,path:void 0,orientation:c.Vertical,...a[e],...t},this.draw()}}render(t){t&&this.setSeries(t),this.draw()}draw(){let{series:t,ctx:a,disabledClearRect:r}=this.state;r||this.clearRect();let i=Object.values(t),{range:l,canvas:s}=this.state,n=l[0],h=l[1],{width:u,height:f}=s,g=h-n;a.save(),a.translate(.5,.5);for(let t=0;t<i.length;t+=1){let r=i[t],{display:l,type:s,data:h,orientation:p}=r,m=r.thickness??o.THICKNESS,C=r.color??o.COLOR;if(!h||!l)continue;let T=h.length,M=u/T;switch(s){case d.Line:case d.Stepline:a.lineWidth=m,a.strokeStyle=C;break;case d.Circle:case d.Rect:case d.Bar:case d.Area:a.fillStyle=C}if(s===d.Line){let t=[];for(let e=0;e<T;e+=1){let r=h[e];if(void 0===r||Number.isNaN(r)){if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++)a.lineTo(t[e][0],t[e][1]);a.lineWidth=m,a.strokeStyle=C,a.stroke(),t=[]}}else{let a=Math.round((e+.5)*M),i=Math.round((r-n)/g*f);t.push([a,i])}}if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++)a.lineTo(t[e][0],t[e][1]);a.lineWidth=m,a.strokeStyle=C,a.stroke()}}if(s===d.Stepline){let t=[];for(let e=0;e<T;e+=1){let r=h[e];if(void 0===r||Number.isNaN(r)){if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}let[e,r]=t[t.length-1],i=Math.round(e+M);a.lineTo(i,r),a.lineWidth=m,a.strokeStyle=C,a.stroke(),t=[]}}else{let a=Math.round(e*M),i=Math.round((r-n)/g*f);t.push([a,i])}}if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}let[e,r]=t[t.length-1],i=Math.round(e+M);a.lineTo(i,r),a.lineWidth=m,a.strokeStyle=C,a.stroke()}}if(s===d.Circle){let t=+m/2,e=2*Math.PI;for(let r=0;r<T;r+=1){let i=Math.round((r+.5)*M),l=Math.round((h[r]-n)/g*f);a.beginPath(),a.arc(i,l,t,0,e),a.fillStyle=C,a.fill()}}if(s===d.Rect){let t=+m;for(let e=0;e<T;e+=1){let r=Math.round((e+.5)*M),i=Math.round((h[e]-n)/g*f);a.beginPath(),a.rect(r,i,t,t),a.fillStyle=C,a.fill()}}if(s===d.Area){let t=[],{r,g:i,b:l}=e(C??o.COLOR),s=t=>{if(0===t.length)return;let[e,s]=t[0],[n,o]=t[t.length-1],h=Math.round(n+M);a.beginPath(),a.moveTo(e,0),a.lineTo(e,s);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}a.lineTo(h,o),a.lineTo(h,0),a.closePath(),a.fillStyle=`rgba(${r}, ${i}, ${l}, 1)`,a.fill()};for(let e=0;e<T;e+=1){let a=h[e];void 0===a||Number.isNaN(a)?(s(t),t=[]):t.push([Math.round(e*M),Math.round((a-n)/g*f)])}s(t)}if(s===d.Bar){if(p===c.Horizontal){let t=f/T,e=Math.round(t);for(let r=0;r<T;r+=1){let i=Math.round(f-(r+1)*t),l=Math.round((h[r]-n)/g*u),s=u-l;a.beginPath(),a.rect(s,i,l,e),a.fillStyle=C,a.fill()}}else for(let t=0;t<T;t+=1){let e=Math.floor(t*u/T),r=Math.floor((t+1)*u/T)-e,i=Math.round((h[t]-n)/g*f);a.beginPath(),a.rect(e,0,r,i),a.fillStyle=this.state.barValue2Color?this.state.CI?.getColor(h[t]).hax??C:C,a.fill()}}}a.restore()}}function k(t){return new v(t)}let O=function(t){return k(t)},D=O;function F(t,e,a){return[t+(t-e)*Math.cos(a),t+(t-e)*Math.sin(a)]}function U(t){let e=0;for(let a of t.values)a>e&&(e=a);return e}class B extends n{getSpecialTickConfig(t){}getAngleOffset(){return 0}init(t){super.init(t),this.state.canvas.style.transform="scaleY(1)";let e=t.fillStyle??o.FILL_STYLE,a=t.fillStylePrimary??o.FILL_STYLE_PRIMARY,r=t.fillStyleTransparentBase??o.FILL_STYLE_TRANSPARENT_BASE;this.updateProps({fillStyle:e,fillStylePrimary:a,fillStyleTransparentBase:r,baseWidth:6,padding:60,ticksStep:6,ticksRenderLength:5,markedAngles:Array.from({length:12},(t,e)=>30*e),color2intensity:i(a),data:{series:[],range:[0,0],polygon:[],yawAngle:0,isNorthFacing:!0}})}clear(){this.clearRect()}resize(){super.resize(),this.state.markedAngles&&this.drawBackground(),this.state.data&&this.drawTicks()}render(t){let e=this.state.data,a={...e,...t},r=void 0!==t.yawAngle&&t.yawAngle!==e?.yawAngle||void 0!==t.isNorthFacing&&t.isNorthFacing!==e?.isNorthFacing;this.state.data=a,r&&this.drawTicks(),this.draw()}drawTicks(){let{canvas:t,baseWidth:e,fillStyle:a,fillStylePrimary:r,ticksStep:i,ticksRenderLength:l,markedAngles:s,data:n}=this.state,{yawAngle:o=0,isNorthFacing:h=!0}=n,u=document.createElement("canvas");u.width=t.width,u.height=t.height;let c=u.getContext("2d");if(!c)return;let d=t.width/2,f=h?0:-o;for(let t=0;t<360;t+=i){let r=(t+f)*Math.PI/180,i=s.includes(t),[n,o]=F(d,3.5*e+l*(i?1.2:1)+3*!!i,r),[h,u]=F(d,3.5*e,r);c.beginPath(),c.moveTo(n,o),c.lineTo(h,u),c.strokeStyle=a,c.lineWidth=i?2:.5,c.stroke()}c.font=`${2*e}px Arial`,c.textAlign="center",c.textBaseline="middle",s.forEach(t=>{let[r,i]=F(d,8*e,(t+f-90)*Math.PI/180),l=this.getSpecialTickConfig(t);l?(c.fillStyle=l.color,c.fillText(l.alias,r,i)):(c.fillStyle=a,c.fillText(t.toString(),r,i))});let g=t.width/18;c.translate(t.width/2,t.height/2),c.scale(1,-1),c.rotate((h?-o:0)*Math.PI/180),c.lineJoin="round",c.lineCap="round",c.strokeStyle=r,c.fillStyle=r,c.lineWidth=g/6,c.beginPath(),c.moveTo(0,g/2),c.lineTo(g/2,-g/2),c.lineTo(0,g/2-1/((1+Math.sqrt(5))/2)*g),c.lineTo(-g/2,-g/2),c.lineTo(0,g/2),c.fill(),c.stroke(),this.state.ticksCanvas=u}drawBackground(){let{canvas:t,baseWidth:e,padding:a,fillStyleTransparentBase:r}=this.state,i=document.createElement("canvas");i.width=t.width,i.height=t.height;let l=i.getContext("2d");if(!l)return;let s=t.width/2;l.beginPath(),l.arc(s,s,s-2*e,0,2*Math.PI),l.strokeStyle=r,l.lineWidth=e,l.stroke(),[,,,,,].fill(1).forEach((t,i)=>{l.beginPath(),l.arc(s,s,(s-a)*(i+1)/5,0,2*Math.PI),l.strokeStyle=r,l.lineWidth=e/4,l.stroke()}),this.state.BGCanvas=i}draw(){let{data:t,canvas:e,ctx:a,baseWidth:r,padding:i,BGCanvas:l,ticksCanvas:s,color2intensity:n,fillStylePrimary:o,fillStyleTransparentBase:h}=this.state,{series:u,range:c,polygon:d,isNorthFacing:f=!0,yawAngle:g=0}=t;if(!u&&!d)return;this.clearRect();let p=e.width/2,m=f?0:-g,C=this.getAngleOffset();if(l&&a.drawImage(l,0,0),c){let t=360/c.length;c.forEach((e,r)=>{let l=(r*t+m+C)*Math.PI/180,s=((r+1)*t+m+C)*Math.PI/180;a.beginPath(),a.moveTo(p,p),a.arc(p,p,p-i,l,s),a.closePath(),a.fillStyle=n[e],a.fill()})}if(d?.length)for(let t of[...d].sort((t,e)=>U(e)-U(t))){let{values:e,color:l,showDots:s}=t;if(!e||e.length<3)continue;let n=360/e.length,h=l??o;a.beginPath();let u=[];e.forEach((t,e)=>{let r=Math.max(0,Math.min(1,t))*(p-i),[l,s]=F(p,p-r,(e*n+m+C)*Math.PI/180);u.push([l,s]),0===e?a.moveTo(l,s):a.lineTo(l,s)}),a.closePath(),a.save();let c=a.createRadialGradient(p,p,0,p,p,p-i);if(c.addColorStop(0,"transparent"),c.addColorStop(1,h),a.fillStyle=c,a.fill(),a.strokeStyle=h,a.lineWidth=1,a.lineJoin="round",a.lineCap="round",a.stroke(),!1!==s){a.fillStyle=h;let t=r/2;for(let[e,r]of u)a.beginPath(),a.arc(e,r,t,0,2*Math.PI),a.fill()}a.restore()}u?.forEach(({value:t,color:e,lineWidth:l=r,radio:s=1})=>{if(null==t)return;let n=(t+m)*Math.PI/180-Math.PI/2,[o,u]=F(p,p,n),[c,d]=F(p,i+(1-s)*(p-i)+l/2,n);1!==s&&(a.beginPath(),a.moveTo(...F(p,i+l/2,n)),a.lineTo(c,d),a.strokeStyle=h,a.stroke()),a.beginPath(),a.moveTo(c,d),a.lineTo(o,u),a.strokeStyle=e,a.lineWidth=l,a.lineJoin="round",a.lineCap="round",a.stroke()}),s&&a.drawImage(s,0,0)}}let G={0:{color:o.DIAL_NORTH_COLOR,alias:"北"},180:{color:o.DIAL_SOUTH_COLOR,alias:"南"}};class X extends B{getSpecialTickConfig(t){return G[t]}getAngleOffset(){return -90}}class W extends B{getSpecialTickConfig(t){}getAngleOffset(){return 0}}class z extends n{init(t){super.init(t),this.updateProps({lineColor:t.lineColor??o.LINE_COLOR,pointColor:t.pointColor??o.POINT_COLOR})}clear(){this.clearRect(),this.updateProps({data:{IData:[],QData:[]}})}render(t){t.IData&&t.QData&&(this.state.data=t,this.draw())}draw(){let{data:t,canvas:e,pointColor:a,lineColor:r,ctx:i}=this.state;if(!t)return;let{IData:l,QData:s}=t;if(0===l.length||0===s.length)return;let{width:n,height:o}=i.canvas;i.clearRect(0,0,e.width,e.height);let[h,u]=C(l),[c,d]=C([h,u,...s]),f=n/(u-h),g=o/(d-c),p=l.map((t,e)=>({x:(t-h)*f,y:(s[e]-c)*g}));i.beginPath(),p.forEach((t,e)=>{i[0===e?"moveTo":"lineTo"](t.x,t.y)}),i.strokeStyle=r,i.lineWidth=1,i.stroke(),p.forEach(t=>{i.beginPath(),i.arc(t.x,t.y,2,0,2*Math.PI),i.fillStyle=a,i.fill()})}}let H=4,Y=256,V=t=>{let e=[];for(let a=0;a<256;a++){let i=a/255,l=t.length-1,s=Math.min(Math.floor(i*l),l-1),n=i*l-s,o=r(t[s]),h=r(t[s+1]);e.push({r:Math.round(o.r+(h.r-o.r)*n),g:Math.round(o.g+(h.g-o.g)*n),b:Math.round(o.b+(h.b-o.b)*n)})}return e},$=()=>{let t=V(u.CLASSIC_COLORS),e=new Uint8ClampedArray((u.MAX_ACCUMULATION+1)*4),a=t.length-1;for(let r=1;r<=u.MAX_ACCUMULATION;r++){let i=t[Math.min(Math.floor(Math.min(r*u.CLASSIC_ALPHA_GAIN/255,1)*a),a)],l=4*r;e[l]=i.r,e[l+1]=i.g,e[l+2]=i.b,e[l+3]=Math.min(r*u.CLASSIC_ALPHA_GAIN,255)}return e},Q=$();class q extends n{init(t){super.init(t),this.state.iqEyeMode=this.resolveMode(t.iqEyeMode),this.initColorLookup()}resize(){super.resize();let{width:t,height:e}=this.state.canvas;this.state.accumulator=new Uint16Array(t*e),this.state.imageData=this.state.ctx.createImageData(t,e)}clear(){this.clearRect(),this.state.accumulator?.fill(0),this.updateProps({data:{IData:[],QData:[]}})}render(t){t.IData?.length&&t.QData?.length&&(this.state.data=t,this.state.accumulator&&("classic"===this.state.iqEyeMode?this.state.accumulator.fill(0):this.applyDecay(),this.accumulateData(t),this.draw()))}draw(){if("classic"===this.state.iqEyeMode){this.drawClassic();return}this.drawModern()}drawClassic(){let{accumulator:t,imageData:e,ctx:a}=this.state;if(!t||!e)return;let r=e.data;for(let e=0;e<t.length;e++){let a=t[e],i=4*e;if(a>0){let t=4*a;r[i]=Q[t],r[i+1]=Q[t+1],r[i+2]=Q[t+2],r[i+3]=Q[t+3]}else r[i+3]=0}a.putImageData(e,0,0)}drawModern(){let{accumulator:t,imageData:e,ctx:a,colorLookup:r}=this.state;if(!t||!e||!r)return;let i=e.data,l=u.MAX_ACCUMULATION;for(let e=0;e<t.length;e++){let a=t[e],s=4*e;if(a>0){let t=Math.min(a/l,1),e=Math.min(Math.floor(t*(r.length-1)),r.length-1),n=r[e];i[s]=n.r,i[s+1]=n.g,i[s+2]=n.b,i[s+3]=Math.round(255*Math.min(2*t,1))}else i[s+3]=0}a.putImageData(e,0,0)}initColorLookup(){let t="classic"===this.state.iqEyeMode?u.CLASSIC_COLORS:u.MODERN_COLORS;this.state.colorLookup=V(t)}resolveMode(t){return"modern"===t?"modern":u.MODE}applyDecay(){let{accumulator:t}=this.state;if(!t)return;let e=u.DECAY_FACTOR;for(let a=0;a<t.length;a++)t[a]=Math.floor(t[a]*e)}accumulateData(t){let{accumulator:e,canvas:a}=this.state;if(!e)return;let{width:r,height:i}=a,[l,s]=u.Y_RANGE,n=s-l,o=u.SEGMENT_SIZE,h=r/(o-1);this.accumulateSegments(t.IData,l,n,r,i,o,h,e),this.accumulateSegments(t.QData,l,n,r,i,o,h,e)}accumulateSegments(t,e,a,r,i,l,s,n){let o=Math.ceil(t.length/l);for(let h=0;h<o;h++){let o=-1,u=-1;for(let c=0;c<l;c++){let d=h*l+c;if(d>=t.length)break;let f=t[d];if(void 0===f)continue;let g=Math.round(c*s),p=Math.round((f-e)/a*(i-1));o>=0&&this.drawLine(o,u,g,p,r,i,n),o=g,u=p}}}drawLine(t,e,a,r,i,l,s){let n=Math.abs(a-t),o=Math.abs(r-e),h=t<a?1:-1,c=e<r?1:-1,d=t,f=e,g=n-o,p=u.MAX_ACCUMULATION;for(;;){if(d>=0&&d<i&&f>=0&&f<l){let t=f*i+d;s[t]<p&&s[t]++}if(d===a&&f===r)break;let t=2*g;t>-o&&(g-=o,d+=h),t<n&&(g+=n,f+=c)}}}export{s as ColorInterpolator,X as Dial,p as Fluorescence,g as FluorescenceRenderMode,L as Gauge,d as GraphicType,y as Heatmap,x as HeatmapCanvas,I as HeatmapWebGL,z as IQ,q as IQEye,c as OrientationType,W as Radar,f as RendererType,D as Series,v as SeriesCanvas,i as color2intensity,b as createHeatmap,k as createSeries,e as hexToRGBA,a as rgbToHex};
|
|
46
|
+
`;class I extends A{init(t){super.init(t);let{gl:e}=this.state;if(!e)return;let a=t.colors,r=a&&a.length>0?a:o.FLUORESCENCE_COLORS,i=this.createProgram({vertex:S,fragment:P}),l=this.createQuadBuffer(),n=this.createTexture(),h=this.createTexture(),u=e.getParameter(e.MAX_TEXTURE_SIZE);if(n){e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,n);let t=new Float32Array([-34028235e31]);e.texImage2D(e.TEXTURE_2D,0,e.R32F,1,1,0,e.RED,e.FLOAT,t)}let[c,d]=t.range??o.RANGE,f=new s(r,c,d,.1);this.updateProps({data:[],colors:r,program:i,quadBuffer:l,dataTexture:n,dataTextures:n?[n]:[],dataTiles:[],colorTexture:h,dataWidth:0,dataHeight:0,CI:f,maxTextureSize:u}),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),this.updateColorTexture(),e.clearColor(0,0,0,0),e.clear(e.COLOR_BUFFER_BIT),this.resize()}updateColorTexture(){let{gl:t,colorTexture:e,CI:a,range:r}=this.state;if(!t||!e||!a)return;let[i,l]=r,s=new Uint8Array(1024);for(let t=0;t<256;t++){let e=i+t/255*(l-i),r=a.getColor(e);s[4*t]=r.r,s[4*t+1]=r.g,s[4*t+2]=r.b,s[4*t+3]=r.a}t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,e),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,256,1,0,t.RGBA,t.UNSIGNED_BYTE,s)}updateDataTexture(){let{gl:t,data:e}=this.state;if(!t||0===e.length)return;let a=e.length,r=0;for(let t=0;t<a;t++){let a=e[t]?.length??0;a>r&&(r=a)}if(0===r)return;let i=this.state.maxTextureSize||t.getParameter(t.MAX_TEXTURE_SIZE);if(!i||i<=0)return;let l=Math.ceil(r/i),s=Math.ceil(a/i),n=l*s,o=this.state.dataTextures??[];if(o.length<n){let t=n-o.length;for(let e=0;e<t;e++){let t=this.createTexture();t&&o.push(t)}}else if(o.length>n){for(let e=n;e<o.length;e++)t.deleteTexture(o[e]);o=o.slice(0,n)}let h=[];t.activeTexture(t.TEXTURE0);for(let n=0;n<s;n++){let s=n*i,u=Math.min(i,a-s);for(let a=0;a<l;a++){let c=a*i,d=Math.min(i,r-c),f=n*l+a,g=o[f];if(!g)continue;let p=new Float32Array(d*u);for(let t=0;t<u;t++){let a=e[s+t],r=a?.length??0,i=t*d;for(let t=0;t<d;t++){let e=c+t;if(0===r||e>=r){p[i+t]=-34028235e31;continue}let l=a[e];void 0===l||Number.isNaN(l)?p[i+t]=-34028235e31:p[i+t]=l}}t.bindTexture(t.TEXTURE_2D,g),t.texImage2D(t.TEXTURE_2D,0,t.R32F,d,u,0,t.RED,t.FLOAT,p),h[f]={startCol:c,startRow:s,width:d,height:u}}}this.updateProps({dataTexture:o[0]??null,dataTextures:o,dataTiles:h,dataWidth:r,dataHeight:a})}updateProps(t){super.updateProps(t);let e=t.colors??this.state.colors,a=t.range??this.state.range;if((t.colors||t.range)&&e.length>0){let[t,r]=a;this.state.CI?this.state.CI.setColors(e,t,r,.1):this.state.CI=new s(e,t,r,.1),this.updateColorTexture()}}setRange(t){t&&(this.updateProps({range:t}),this.draw())}clear(){this.clearRect(),this.updateProps({data:[]})}render(t){t?.length>0&&(this.state.data=t,this.updateDataTexture(),this.draw())}draw(){let{gl:t,program:e,quadBuffer:a,dataTextures:r,dataTiles:i,colorTexture:l,data:s,range:n,dataWidth:o,dataHeight:h,canvas:u}=this.state;if(!t||!e||!a||!l||(t.clearColor(0,0,0,0),t.clear(t.COLOR_BUFFER_BIT),0===s.length||0===o||0===h||0===i.length||0===r.length))return;let[c,d]=n;t.useProgram(e),t.bindBuffer(t.ARRAY_BUFFER,a);let f=t.getAttribLocation(e,"a_position");t.enableVertexAttribArray(f),t.vertexAttribPointer(f,2,t.FLOAT,!1,0,0);let g=t.getUniformLocation(e,"u_dataTexture"),p=t.getUniformLocation(e,"u_colorTexture"),m=t.getUniformLocation(e,"u_rangeMin"),T=t.getUniformLocation(e,"u_rangeMax");if(!g||!p||!m||!T)return;t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,l),t.uniform1i(p,1),t.uniform1f(m,c),t.uniform1f(T,d);let C=u.width,M=u.height,E=t=>Math.round(t/o*C),R=t=>Math.round(t/h*M);for(let e=0;e<i.length;e++){let a=i[e],l=r[e];if(!a||!l)continue;let s=E(a.startCol),n=E(a.startCol+a.width),o=R(a.startRow),h=M-R(a.startRow+a.height),u=n-s,c=M-o-h;u<=0||c<=0||(t.viewport(s,h,u,c),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,l),t.uniform1i(g,0),t.drawArrays(t.TRIANGLES,0,6))}t.viewport(0,0,C,M)}resize(){super.resize()}dispose(){let{gl:t,program:e,quadBuffer:a,dataTextures:r,colorTexture:i}=this.state;if(t){for(let i of(e&&t.deleteProgram(e),a&&t.deleteBuffer(a),r))t.deleteTexture(i);i&&t.deleteTexture(i)}super.dispose()}}class x extends n{init(t){super.init(t);let{colors:e}=t;this.updateProps({colors:e,data:[]}),this.resize()}updateProps(t){super.updateProps(t);let{range:e=this.state.range,colors:a=this.state.colors}=t;if(e&&a){let[t,r]=e;C(t)&&C(r)&&t<r&&(this.state.CI?this.state.CI.setColors(a,t,r,.1):this.updateProps({CI:new s(a,t,r,.1)}))}}clearImageData(){let{ctx:t,canvas:{height:e,width:a}}=this.state;e&&a&&this.updateProps({imageData:t.createImageData(a,e)})}clearRect(){super.clearRect(),this.clearImageData()}clear(){this.clearRect(),this.updateProps({data:[]})}resize(){super.resize(),this.clearImageData()}setRange(t){t&&(this.updateProps({range:t}),this.draw())}render(t){t?.length>=0&&(this.state.data=t,this.draw())}draw(){let{imageData:t,data:e,canvas:a,ctx:r,CI:i}=this.state;0!==e.length&&(m(a.width,a.height,e,t.data,t=>i.getColor(t)),r.putImageData(t,0,0))}}function b(t){let{renderer:e=f.WebGL}=t;if(e===f.WebGL){if(R())return new I(t);console.warn("[Heatmap] WebGL 2.0 not supported, falling back to Canvas")}return new x(t)}let w=function(t){return b(t)},y=w,N=!1;class v extends n{init(t){super.init(t);let{series:e,barValue2Color:a,disabledClearRect:r,colors:i}=t;this.updateProps({interval:0,series:{},barValue2Color:a,disabledClearRect:r,colors:i}),this.setRange(this.state.range),Array.isArray(e)&&e.forEach(t=>{this.setSeries(t)})}updateProps(t){super.updateProps(t);let{colors:e=this.state.colors}=t;this.state.barValue2Color&&!this.state.CI&&e&&this.updateProps({CI:new s(e,0,100,.1)})}clear(){super.clearRect();let{series:t}=this.state;Object.entries(t).forEach(([t,e])=>{this.setSeries({...e,name:t,data:void 0})})}setRange(t){this.updateProps({range:[t[0],t[1],t[1]-t[0]]}),this.draw()}setIntervel(t){t&&this.updateProps({interval:t})}setSeries(t){if(t?.name){let{name:e}=t,{series:a}=this.state;a[e]={thickness:o.THICKNESS,display:!0,color:o.COLOR,type:d.Line,data:void 0,path:void 0,orientation:c.Vertical,...a[e],...t},this.draw()}}render(t){t&&this.setSeries(t),this.draw()}draw(){let{series:t,ctx:a,disabledClearRect:r}=this.state;r||this.clearRect();let i=Object.values(t),{range:l,canvas:s}=this.state,n=l[0],h=l[1],{width:u,height:f}=s,g=h-n;a.save(),a.translate(.5,.5);for(let t=0;t<i.length;t+=1){let r=i[t],{display:l,type:s,data:h,orientation:p}=r,m=r.thickness??o.THICKNESS,T=r.color??o.COLOR;if(!h||!l)continue;let C=h.length,M=u/C;switch(s){case d.Line:case d.Stepline:a.lineWidth=m,a.strokeStyle=T;break;case d.Circle:case d.Rect:case d.Bar:case d.Area:a.fillStyle=T}if(s===d.Line){let t=[];for(let e=0;e<C;e+=1){let r=h[e];if(void 0===r||Number.isNaN(r)){if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++)a.lineTo(t[e][0],t[e][1]);a.lineWidth=m,a.strokeStyle=T,a.stroke(),t=[]}}else{let a=Math.round((e+.5)*M),i=Math.round((r-n)/g*f);t.push([a,i])}}if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++)a.lineTo(t[e][0],t[e][1]);a.lineWidth=m,a.strokeStyle=T,a.stroke()}}if(s===d.Stepline){let t=[];for(let e=0;e<C;e+=1){let r=h[e];if(void 0===r||Number.isNaN(r)){if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}let[e,r]=t[t.length-1],i=Math.round(e+M);a.lineTo(i,r),a.lineWidth=m,a.strokeStyle=T,a.stroke(),t=[]}}else{let a=Math.round(e*M),i=Math.round((r-n)/g*f);t.push([a,i])}}if(t.length>0){a.beginPath(),a.moveTo(t[0][0],t[0][1]);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}let[e,r]=t[t.length-1],i=Math.round(e+M);a.lineTo(i,r),a.lineWidth=m,a.strokeStyle=T,a.stroke()}}if(s===d.Circle){let t=+m/2,e=2*Math.PI;for(let r=0;r<C;r+=1){let i=Math.round((r+.5)*M),l=Math.round((h[r]-n)/g*f);a.beginPath(),a.arc(i,l,t,0,e),a.fillStyle=T,a.fill()}}if(s===d.Rect){let t=+m;for(let e=0;e<C;e+=1){let r=Math.round((e+.5)*M),i=Math.round((h[e]-n)/g*f);a.beginPath(),a.rect(r,i,t,t),a.fillStyle=T,a.fill()}}if(s===d.Area){let t=[],{r,g:i,b:l}=e(T??o.COLOR),s=t=>{if(0===t.length)return;let[e,s]=t[0],[n,o]=t[t.length-1],h=Math.round(n+M);a.beginPath(),a.moveTo(e,0),a.lineTo(e,s);for(let e=1;e<t.length;e++){let[r,i]=t[e-1],[l,s]=t[e];a.lineTo(l,i),a.lineTo(l,s)}a.lineTo(h,o),a.lineTo(h,0),a.closePath(),a.fillStyle=`rgba(${r}, ${i}, ${l}, 1)`,a.fill()};for(let e=0;e<C;e+=1){let a=h[e];void 0===a||Number.isNaN(a)?(s(t),t=[]):t.push([Math.round(e*M),Math.round((a-n)/g*f)])}s(t)}if(s===d.Bar){if(p===c.Horizontal){let t=f/C,e=Math.round(t);for(let r=0;r<C;r+=1){let i=Math.round(f-(r+1)*t),l=Math.round((h[r]-n)/g*u),s=u-l;a.beginPath(),a.rect(s,i,l,e),a.fillStyle=T,a.fill()}}else for(let t=0;t<C;t+=1){let e=Math.floor(t*u/C),r=Math.floor((t+1)*u/C)-e,i=Math.round((h[t]-n)/g*f);a.beginPath(),a.rect(e,0,r,i),a.fillStyle=this.state.barValue2Color?this.state.CI?.getColor(h[t]).hax??T:T,a.fill()}}}a.restore()}}function k(t){return new v(t)}let O=function(t){return k(t)},D=O;function F(t,e,a){return[t+(t-e)*Math.cos(a),t+(t-e)*Math.sin(a)]}function U(t){let e=0;for(let a of t.values)a>e&&(e=a);return e}class B extends n{getSpecialTickConfig(t){}getAngleOffset(){return 0}init(t){super.init(t),this.state.canvas.style.transform="scaleY(1)";let e=t.fillStyle??o.FILL_STYLE,a=t.fillStylePrimary??o.FILL_STYLE_PRIMARY,r=t.fillStyleTransparentBase??o.FILL_STYLE_TRANSPARENT_BASE;this.updateProps({fillStyle:e,fillStylePrimary:a,fillStyleTransparentBase:r,baseWidth:6,padding:60,ticksStep:6,ticksRenderLength:5,markedAngles:Array.from({length:12},(t,e)=>30*e),color2intensity:i(a),data:{series:[],range:[0,0],polygon:[],yawAngle:0,isNorthFacing:!0}})}clear(){this.clearRect()}resize(){super.resize(),this.state.markedAngles&&this.drawBackground(),this.state.data&&this.drawTicks()}render(t){let e=this.state.data,a={...e,...t},r=void 0!==t.yawAngle&&t.yawAngle!==e?.yawAngle||void 0!==t.isNorthFacing&&t.isNorthFacing!==e?.isNorthFacing;this.state.data=a,r&&this.drawTicks(),this.draw()}drawTicks(){let{canvas:t,baseWidth:e,fillStyle:a,fillStylePrimary:r,ticksStep:i,ticksRenderLength:l,markedAngles:s,data:n}=this.state,{yawAngle:o=0,isNorthFacing:h=!0}=n,u=document.createElement("canvas");u.width=t.width,u.height=t.height;let c=u.getContext("2d");if(!c)return;let d=t.width/2,f=h?0:-o;for(let t=0;t<360;t+=i){let r=(t+f)*Math.PI/180,i=s.includes(t),[n,o]=F(d,3.5*e+l*(i?1.2:1)+3*!!i,r),[h,u]=F(d,3.5*e,r);c.beginPath(),c.moveTo(n,o),c.lineTo(h,u),c.strokeStyle=a,c.lineWidth=i?2:.5,c.stroke()}c.font=`${2*e}px Arial`,c.textAlign="center",c.textBaseline="middle",s.forEach(t=>{let[r,i]=F(d,8*e,(t+f-90)*Math.PI/180),l=this.getSpecialTickConfig(t);l?(c.fillStyle=l.color,c.fillText(l.alias,r,i)):(c.fillStyle=a,c.fillText(t.toString(),r,i))});let g=t.width/18;c.translate(t.width/2,t.height/2),c.scale(1,-1),c.rotate((h?-o:0)*Math.PI/180),c.lineJoin="round",c.lineCap="round",c.strokeStyle=r,c.fillStyle=r,c.lineWidth=g/6,c.beginPath(),c.moveTo(0,g/2),c.lineTo(g/2,-g/2),c.lineTo(0,g/2-1/((1+Math.sqrt(5))/2)*g),c.lineTo(-g/2,-g/2),c.lineTo(0,g/2),c.fill(),c.stroke(),this.state.ticksCanvas=u}drawBackground(){let{canvas:t,baseWidth:e,padding:a,fillStyleTransparentBase:r}=this.state,i=document.createElement("canvas");i.width=t.width,i.height=t.height;let l=i.getContext("2d");if(!l)return;let s=t.width/2;l.beginPath(),l.arc(s,s,s-2*e,0,2*Math.PI),l.strokeStyle=r,l.lineWidth=e,l.stroke(),[,,,,,].fill(1).forEach((t,i)=>{l.beginPath(),l.arc(s,s,(s-a)*(i+1)/5,0,2*Math.PI),l.strokeStyle=r,l.lineWidth=e/4,l.stroke()}),this.state.BGCanvas=i}draw(){let{data:t,canvas:e,ctx:a,baseWidth:r,padding:i,BGCanvas:l,ticksCanvas:s,color2intensity:n,fillStylePrimary:o,fillStyleTransparentBase:h}=this.state,{series:u,range:c,polygon:d,isNorthFacing:f=!0,yawAngle:g=0}=t;if(!u&&!d)return;this.clearRect();let p=e.width/2,m=f?0:-g,T=this.getAngleOffset();if(l&&a.drawImage(l,0,0),c){let t=360/c.length;c.forEach((e,r)=>{let l=(r*t+m+T)*Math.PI/180,s=((r+1)*t+m+T)*Math.PI/180;a.beginPath(),a.moveTo(p,p),a.arc(p,p,p-i,l,s),a.closePath(),a.fillStyle=n[e],a.fill()})}if(d?.length)for(let t of[...d].sort((t,e)=>U(e)-U(t))){let{values:e,color:l,showDots:s}=t;if(!e||e.length<3)continue;let n=360/e.length,h=l??o;a.beginPath();let u=[];e.forEach((t,e)=>{let r=Math.max(0,Math.min(1,t))*(p-i),[l,s]=F(p,p-r,(e*n+m+T)*Math.PI/180);u.push([l,s]),0===e?a.moveTo(l,s):a.lineTo(l,s)}),a.closePath(),a.save();let c=a.createRadialGradient(p,p,0,p,p,p-i);if(c.addColorStop(0,"transparent"),c.addColorStop(1,h),a.fillStyle=c,a.fill(),a.strokeStyle=h,a.lineWidth=1,a.lineJoin="round",a.lineCap="round",a.stroke(),!1!==s){a.fillStyle=h;let t=r/2;for(let[e,r]of u)a.beginPath(),a.arc(e,r,t,0,2*Math.PI),a.fill()}a.restore()}u?.forEach(({value:t,color:e,lineWidth:l=r,radio:s=1})=>{if(null==t)return;let n=(t+m)*Math.PI/180-Math.PI/2,[o,u]=F(p,p,n),[c,d]=F(p,i+(1-s)*(p-i)+l/2,n);1!==s&&(a.beginPath(),a.moveTo(...F(p,i+l/2,n)),a.lineTo(c,d),a.strokeStyle=h,a.stroke()),a.beginPath(),a.moveTo(c,d),a.lineTo(o,u),a.strokeStyle=e,a.lineWidth=l,a.lineJoin="round",a.lineCap="round",a.stroke()}),s&&a.drawImage(s,0,0)}}let G={0:{color:o.DIAL_NORTH_COLOR,alias:"北"},180:{color:o.DIAL_SOUTH_COLOR,alias:"南"}};class X extends B{getSpecialTickConfig(t){return G[t]}getAngleOffset(){return -90}}class W extends B{getSpecialTickConfig(t){}getAngleOffset(){return 0}}class z extends n{init(t){super.init(t),this.updateProps({lineColor:t.lineColor??o.LINE_COLOR,pointColor:t.pointColor??o.POINT_COLOR})}clear(){this.clearRect(),this.updateProps({data:{IData:[],QData:[]}})}render(t){t.IData&&t.QData&&(this.state.data=t,this.draw())}draw(){let{data:t,canvas:e,pointColor:a,lineColor:r,ctx:i}=this.state;if(!t)return;let{IData:l,QData:s}=t;if(0===l.length||0===s.length)return;let{width:n,height:o}=i.canvas;i.clearRect(0,0,e.width,e.height);let[h,u]=T(l),[c,d]=T([h,u,...s]),f=n/(u-h),g=o/(d-c),p=l.map((t,e)=>({x:(t-h)*f,y:(s[e]-c)*g}));i.beginPath(),p.forEach((t,e)=>{i[0===e?"moveTo":"lineTo"](t.x,t.y)}),i.strokeStyle=r,i.lineWidth=1,i.stroke(),p.forEach(t=>{i.beginPath(),i.arc(t.x,t.y,2,0,2*Math.PI),i.fillStyle=a,i.fill()})}}let H=4,Y=256,V=t=>{let e=[];for(let a=0;a<256;a++){let i=a/255,l=t.length-1,s=Math.min(Math.floor(i*l),l-1),n=i*l-s,o=r(t[s]),h=r(t[s+1]);e.push({r:Math.round(o.r+(h.r-o.r)*n),g:Math.round(o.g+(h.g-o.g)*n),b:Math.round(o.b+(h.b-o.b)*n)})}return e},$=()=>{let t=V(u.CLASSIC_COLORS),e=new Uint8ClampedArray((u.MAX_ACCUMULATION+1)*4),a=t.length-1;for(let r=1;r<=u.MAX_ACCUMULATION;r++){let i=t[Math.min(Math.floor(Math.min(r*u.CLASSIC_ALPHA_GAIN/255,1)*a),a)],l=4*r;e[l]=i.r,e[l+1]=i.g,e[l+2]=i.b,e[l+3]=Math.min(r*u.CLASSIC_ALPHA_GAIN,255)}return e},Q=$();class q extends n{init(t){super.init(t),this.state.iqEyeMode=this.resolveMode(t.iqEyeMode),this.initColorLookup()}resize(){super.resize();let{width:t,height:e}=this.state.canvas;this.state.accumulator=new Uint16Array(t*e),this.state.imageData=this.state.ctx.createImageData(t,e)}clear(){this.clearRect(),this.state.accumulator?.fill(0),this.updateProps({data:{IData:[],QData:[]}})}render(t){t.IData?.length&&t.QData?.length&&(this.state.data=t,this.state.accumulator&&("classic"===this.state.iqEyeMode?this.state.accumulator.fill(0):this.applyDecay(),this.accumulateData(t),this.draw()))}draw(){if("classic"===this.state.iqEyeMode){this.drawClassic();return}this.drawModern()}drawClassic(){let{accumulator:t,imageData:e,ctx:a}=this.state;if(!t||!e)return;let r=e.data;for(let e=0;e<t.length;e++){let a=t[e],i=4*e;if(a>0){let t=4*a;r[i]=Q[t],r[i+1]=Q[t+1],r[i+2]=Q[t+2],r[i+3]=Q[t+3]}else r[i+3]=0}a.putImageData(e,0,0)}drawModern(){let{accumulator:t,imageData:e,ctx:a,colorLookup:r}=this.state;if(!t||!e||!r)return;let i=e.data,l=u.MAX_ACCUMULATION;for(let e=0;e<t.length;e++){let a=t[e],s=4*e;if(a>0){let t=Math.min(a/l,1),e=Math.min(Math.floor(t*(r.length-1)),r.length-1),n=r[e];i[s]=n.r,i[s+1]=n.g,i[s+2]=n.b,i[s+3]=Math.round(255*Math.min(2*t,1))}else i[s+3]=0}a.putImageData(e,0,0)}initColorLookup(){let t="classic"===this.state.iqEyeMode?u.CLASSIC_COLORS:u.MODERN_COLORS;this.state.colorLookup=V(t)}resolveMode(t){return"modern"===t?"modern":u.MODE}applyDecay(){let{accumulator:t}=this.state;if(!t)return;let e=u.DECAY_FACTOR;for(let a=0;a<t.length;a++)t[a]=Math.floor(t[a]*e)}accumulateData(t){let{accumulator:e,canvas:a}=this.state;if(!e)return;let{width:r,height:i}=a,[l,s]=u.Y_RANGE,n=s-l,o=u.SEGMENT_SIZE,h=r/(o-1);this.accumulateSegments(t.IData,l,n,r,i,o,h,e),this.accumulateSegments(t.QData,l,n,r,i,o,h,e)}accumulateSegments(t,e,a,r,i,l,s,n){let o=Math.ceil(t.length/l);for(let h=0;h<o;h++){let o=-1,u=-1;for(let c=0;c<l;c++){let d=h*l+c;if(d>=t.length)break;let f=t[d];if(void 0===f)continue;let g=Math.round(c*s),p=Math.round((f-e)/a*(i-1));o>=0&&this.drawLine(o,u,g,p,r,i,n),o=g,u=p}}}drawLine(t,e,a,r,i,l,s){let n=Math.abs(a-t),o=Math.abs(r-e),h=t<a?1:-1,c=e<r?1:-1,d=t,f=e,g=n-o,p=u.MAX_ACCUMULATION;for(;;){if(d>=0&&d<i&&f>=0&&f<l){let t=f*i+d;s[t]<p&&s[t]++}if(d===a&&f===r)break;let t=2*g;t>-o&&(g-=o,d+=h),t<n&&(g+=n,f+=c)}}}export{s as ColorInterpolator,X as Dial,p as Fluorescence,g as FluorescenceRenderMode,L as Gauge,d as GraphicType,y as Heatmap,x as HeatmapCanvas,I as HeatmapWebGL,z as IQ,q as IQEye,c as OrientationType,W as Radar,f as RendererType,D as Series,v as SeriesCanvas,i as color2intensity,b as createHeatmap,k as createSeries,e as hexToRGBA,a as rgbToHex};
|
package/package.json
CHANGED
|
@@ -24,11 +24,11 @@ export declare function createHeatmap(props: InitProps): IHeatmap;
|
|
|
24
24
|
* 支持 new Heatmap() 或 Heatmap() 两种调用方式
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
|
-
* //
|
|
27
|
+
* // WebGL 版本(默认)
|
|
28
28
|
* const heatmap = new Heatmap({ id: 'canvas', colors, range });
|
|
29
29
|
*
|
|
30
|
-
* //
|
|
31
|
-
* const heatmap = new Heatmap({ id: 'canvas', colors, range, renderer: RendererType.
|
|
30
|
+
* // Canvas 2D 版本
|
|
31
|
+
* const heatmap = new Heatmap({ id: 'canvas', colors, range, renderer: RendererType.Canvas });
|
|
32
32
|
*/
|
|
33
33
|
declare const Heatmap: HeatmapConstructor;
|
|
34
34
|
export default Heatmap;
|