kline-orderbook-chart 1.6.1 → 2.0.0

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.
Binary file
package/dist/index.d.ts CHANGED
@@ -66,6 +66,12 @@ export interface ChartEngine {
66
66
  pause(): void;
67
67
  resume(): void;
68
68
  readonly isPaused: boolean;
69
+ /**
70
+ * Show / hide the engine-drawn load spinner (pulsing bars centred in the
71
+ * chart area). Rendered on the canvas, so toggling it never mounts or
72
+ * unmounts a DOM layer over the chart.
73
+ */
74
+ setLoading(on: boolean): void;
69
75
 
70
76
  // Data
71
77
  setKlines(klines: any[]): void;
@@ -0,0 +1,14 @@
1
+ /*!
2
+ * chart-engine / kline-orderbook-chart
3
+ * Copyright (c) 2025-2026 Tape Delta. All rights reserved.
4
+ *
5
+ * Proprietary and confidential. Licensed, not sold. Unauthorized
6
+ * copying, redistribution, modification, reverse-engineering, or
7
+ * removal of this notice is strictly prohibited. See the LICENSE
8
+ * file shipped with this package for the full terms.
9
+ *
10
+ * Contact: support@tapedelta.com
11
+ * Web: https://tapedelta.com
12
+ * NPM: https://www.npmjs.com/package/kline-orderbook-chart
13
+ */
14
+ var m=null,_=null,h=null;async function A(){let t=typeof globalThis>"u"?null:globalThis,e=t&&t.__mrdWasmResp;if(!e)return null;t.__mrdWasmResp=null;try{let s=await e;return s&&s.ok?s:null}catch{return null}}function O(){if(typeof WorkerGlobalScope>"u"||!self.location?.search)return null;let t=e=>new URL(e+self.location.search,self.location.href).href;return{glue:t("./chart_engine.js"),wasm:t("./chart_engine_bg.wasm")}}async function b(){return m?{module:m,memory:_}:h||(h=(async()=>{let t=O(),e=t?await import(t.glue):await import("./chart_engine.js"),s=t?null:await A(),i=t?t.wasm:s;try{await(i?e.default({module_or_path:i}):e.default())}catch(n){console.warn("[mrd-chart] WebAssembly streaming init failed, retrying with arrayBuffer fallback:",n);let r=t?t.wasm:new URL("../wasm/chart_engine_bg.wasm",import.meta.url),o=await fetch(r);if(!o.ok)throw new Error(`WASM fetch failed (${o.status} ${o.statusText}) at ${r}`);let M=await o.arrayBuffer();await e.default(M)}return _=e.wasm_memory(),m=e,{module:e,memory:_}})(),h.catch(()=>{h=null}),h)}var l=class{constructor(e){this.url=e.url,this.role=e.role||"",this.binary=!!e.binary,this.viaGateway=!!e.viaGateway,this.h=e,this.ws=null,this.timer=null,this.backoffMs=1e3,this.lastMsgAt=0,this.stopped=!0}isOpen(){return!!this.ws&&this.ws.readyState===WebSocket.OPEN}open(){if(this.stopped=!1,this.ws)return;let e;try{e=new WebSocket(this.url),this.binary&&(e.binaryType="arraybuffer")}catch(i){console.warn(`[ingest] ws construct failed (${this.role}):`,i?.message||i),this.scheduleReconnect();return}this.ws=e;let s=!1;e.onopen=()=>{s=!0,this.backoffMs=1e3,this.lastMsgAt=Date.now(),this.h.onHandshake?.(!0),this.h.onOpen?.()},e.onmessage=i=>{this.ws===e&&(this.lastMsgAt=Date.now(),this.h.onMessage(i.data))},e.onclose=i=>{this.ws===e&&(this.ws=null,s||this.h.onHandshake?.(!1),!this.stopped&&(console.warn(`[ingest] ws closed (${this.role}, code ${i?.code}); reconnecting in ${this.backoffMs}ms`),this.scheduleReconnect()))},e.onerror=()=>{}}send(e){this.isOpen()&&this.ws.send(JSON.stringify(e))}scheduleReconnect(){this.timer||this.stopped||(this.timer=setTimeout(()=>{this.timer=null,this.backoffMs=Math.min(this.backoffMs*2,3e4),this.open()},this.backoffMs))}bounce(e){if(this.stopped)return;let s=this.isOpen();s&&Date.now()-this.lastMsgAt<e||(this.timer&&(clearTimeout(this.timer),this.timer=null),this.backoffMs=1e3,(s||this.ws)&&this.drop(),this.open())}kick(){if(this.ws)try{this.ws.close()}catch{}}drop(){let e=this.ws;if(this.ws=null,e)try{e.close()}catch{}}close(){this.stopped=!0,this.timer&&(clearTimeout(this.timer),this.timer=null),this.drop()}};function a(t,e){self.postMessage(t,e||[])}var c=class{constructor(e,s){this.id=s,this.feed=new e.BookFeed,this.gen=0,this.lastBookAt=0,this.pendingTrades=0,this.closed=!1}resetFeed(e,s,i,n){this.gen=e,this.feed.reset(s,i,n,Date.now()),this.lastBookAt=0,this.pendingTrades=0}noteTrade(){++this.pendingTrades>=2048&&this.drain(Date.now(),!1)}drain(e,s){let i=s&&e-this.lastBookAt>=100,n=this.feed.drain(e,i);if(this.pendingTrades=0,!(n&3))return;let r={t:"frame",id:this.id,gen:this.gen,tMs:e,bits:n,mid:0,tick:0},o=[];n&1&&(this.lastBookAt=e,r.bids=this.feed.bids(),r.asks=this.feed.asks(),r.mid=this.feed.mid(),o.push(r.bids.buffer,r.asks.buffer)),n&16&&(r.tick=this.feed.rowTick()),n&2&&(r.trades=this.feed.trades(),o.push(r.trades.buffer)),a(r,o)}health(){}park(){}unpark(){}free(){this.closed=!0,this.feed.free()}};var R=12e3,u=class extends c{constructor(e,s){super(e,s.id),this.legs=[],this.restart(s)}restart({gen:e,symbol:s,legs:i}){this.closeLegs(),this.resetFeed(e,s,1,""),this.legs=(i||[]).map(n=>{let r=new l({url:n.url,role:n.role,viaGateway:n.viaGateway,onOpen:()=>{n.carriesDepth&&this.needSnapshot()},onHandshake:o=>a({t:"ws",id:this.id,role:n.role,ok:o,viaGateway:r.viaGateway,reportable:!!n.reportable}),onMessage:o=>this.onMessage(o)});return r});for(let n of this.legs)n.open()}onMessage(e){typeof e=="string"&&(this.feed.binance(e)&1&&this.needSnapshot(),e.includes('"aggTrade"')&&this.noteTrade())}needSnapshot(){a({t:"need-snapshot",id:this.id,gen:this.gen})}snapshot(e,s){e!==this.gen||typeof s!="string"||this.feed.snapshot(s)&1&&this.needSnapshot()}setUrl(e,s,i){let n=this.legs.find(r=>r.role===e);n&&s&&(n.url=s,n.viaGateway=!!i)}resume(){for(let e of this.legs)e.bounce(R)}closeLegs(){for(let e of this.legs)e.close();this.legs=[]}close(){this.closeLegs(),this.free()}};var I=12e3,C=9e4,d=class extends c{constructor(e,s){super(e,s.id),this.parked=!1,this.leg=new l({url:s.url,role:"obag",binary:!0,onOpen:()=>{this.resetFeed(this.gen,this.symbol,2,this.venues.join(",")),this.subscribe(!0)},onMessage:i=>this.onMessage(i)}),this.restart(s)}restart({gen:e,symbol:s,venues:i}){this.symbol=s||"",this.venues=Array.isArray(i)?i:[],this.resetFeed(e,this.symbol,2,this.venues.join(",")),this.leg.isOpen()?this.subscribe(!1):this.parked||this.leg.open()}subscribe(e){let s=this.leg;s.send({type:"change_symbol",symbol:this.symbol}),s.send({type:"get_server_info"}),e&&(s.send({type:"subscribe_orderbook_raw",exchanges:this.venues}),s.send({type:"subscribe_trades"}))}onMessage(e){let s=Date.now(),i;typeof e=="string"?(i=this.feed.obag(e,s),e.includes('"type":"trade"')&&this.noteTrade()):i=this.feed.obagBinary(new Uint8Array(e),s),i&2&&this.leg.send({type:"get_orderbook_raw_snapshot",exchanges:this.venues})}health(e){this.leg.isOpen()&&e-this.leg.lastMsgAt>C&&(console.warn(`[ingest] obAG silent ${Math.round((e-this.leg.lastMsgAt)/1e3)}s \u2014 reconnecting`),this.leg.kick())}resume(){this.parked||this.leg.bounce(I)}park(){this.parked||(this.parked=!0,this.leg.close())}unpark(){this.parked&&(this.parked=!1,this.leg.open())}close(){this.leg.close(),this.free()}};var D=33,x=1e3,F={binance:u,obag:d},S=null,g=!1,p=null,k=0,f=new Map;function T(){let t=Date.now();for(let e of f.values())e.health(t),e.drain(t,!g)}function K(){let t=f.size===0?0:g?x:D;t!==k&&(p&&(clearInterval(p),p=null),k=t,t&&(p=setInterval(T,t)))}function v(t){let e=t.id!=null?f.get(t.id):null;switch(t.t){case"open":{let s=F[t.kind||"binance"];!e&&s&&f.set(t.id,new s(S,t));break}case"symbol":e?.restart(t);break;case"urls":e?.setUrl?.(t.role,t.url,t.viaGateway);break;case"snapshot":e?.snapshot?.(t.gen,t.text);break;case"resume":e?.resume();break;case"park":e?.park();break;case"unpark":e?.unpark();break;case"close":e&&(e.close(),f.delete(t.id));break;case"vis":g=!!t.hidden,g||T();break;default:return}K()}b().then(({module:t})=>{if(typeof t.BookFeed!="function")throw new Error("engine glue has no BookFeed (stale build)");S=t,self.onmessage=e=>{try{v(e.data||{})}catch(s){console.error("[ingest] message failed:",s)}},a({t:"ready"})}).catch(t=>a({t:"fatal",error:String(t?.message||t)}));
@@ -0,0 +1,56 @@
1
+ /*!
2
+ * chart-engine / kline-orderbook-chart
3
+ * Copyright (c) 2025-2026 Tape Delta. All rights reserved.
4
+ *
5
+ * Proprietary and confidential. Licensed, not sold. Unauthorized
6
+ * copying, redistribution, modification, reverse-engineering, or
7
+ * removal of this notice is strictly prohibited. See the LICENSE
8
+ * file shipped with this package for the full terms.
9
+ *
10
+ * Contact: support@tapedelta.com
11
+ * Web: https://tapedelta.com
12
+ * NPM: https://www.npmjs.com/package/kline-orderbook-chart
13
+ */
14
+ var k={FILL_RECT:1,FILL_ROUND_RECT:27,FILL_ELLIPSE:38,FILL_RECT_SMOOTH_X:30,STROKE_RECT:2,LINE:3,CANDLE:34,CANDLE_BORDERED:36,OHLC_BAR:37,DASHED_LINE:4,POLYLINE:5,TEXT:6,STYLED_TEXT:33,CIRCLE:7,TRIANGLE:8,SAVE:9,RESTORE:10,CLIP_RECT:11,ROTATED_TEXT:12,BUBBLE_3D:13,FILL_POLYGON:14,DASHED_POLYLINE:15,GRADIENT_RECT:16,GRADIENT_RECT_H:28,GRADIENT_RECT_H_MULTI:29,LINEAR_POLYLINE:17,SMOOTH_POLYLINE:24,BEGIN_HEATMAP_LAYER:18,END_HEATMAP_LAYER:19,HEATMAP_BITMAP:20,LINE_WITH_EXTEND:21,LABEL_CALLOUT:22,HEATMAP_WORLD:23,HEATMAP_BG:31,HEATMAP_WORLD_INTENSITY:32,OB_HEATMAP:35,DRAW_IMAGE:25,FILL_SMOOTH_BAND:26,END_FRAME:255},se=(()=>{try{return typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches}catch{return!1}})(),le=se?1.5:1/0;function ie(){return typeof window<"u"&&window.devicePixelRatio||1}function ce(e,a){return typeof OffscreenCanvas<"u"?new OffscreenCanvas(e,a):Object.assign(document.createElement("canvas"),{width:e,height:a})}var at=null,Yt=null;function ue(e,a){return at?(at.width!==e||at.height!==a)&&(at.width=e,at.height=a):(at=ce(e,a),Yt=at.getContext("2d")),Yt}var It=new Float32Array(0);function fe(e){return It.length<e&&(It=new Float32Array(e)),It}var bt=new Float64Array(0),Bt=new Float64Array(0),Ut=new Float64Array(0),mt=new Float64Array(0);function he(e){return bt.length<e&&(bt=new Float64Array(e)),bt}function ge(e){return Bt.length<e&&(Bt=new Float64Array(e)),Bt}function de(e){return Ut.length<e&&(Ut=new Float64Array(e)),Ut}function ye(e){return mt.length<e&&(mt=new Float64Array(e)),mt}function Ot(e,a){if(a<2)return;let o=a-1,m=he(o),_=ge(o),A=de(o);for(let F=0;F<o;F++){let O=e[F*2],n=e[F*2+1],r=e[(F+1)*2],t=e[(F+1)*2+1];m[F]=r-O,_[F]=t-n,A[F]=m[F]>0?_[F]/m[F]:0}let w=ye(a);w[0]=A[0],w[a-1]=A[o-1];for(let F=1;F<o;F++){let O=A[F-1],n=A[F];w[F]=O*n<=0?0:(O+n)*.5}for(let F=0;F<o;F++){let O=A[F];if(O===0){w[F]=0,w[F+1]=0;continue}let n=w[F]/O,r=w[F+1]/O,t=n*n+r*r;if(t>9){let T=3/Math.sqrt(t);w[F]=T*n*O,w[F+1]=T*r*O}}}function zt(e,a,o){let m=o-1,_=bt,A=mt;for(let w=0;w<m;w++){let F=a[w*2],O=a[w*2+1],n=a[(w+1)*2],r=a[(w+1)*2+1],t=_[w];if(t<=0){e.lineTo(n,r);continue}let T=t/3;e.bezierCurveTo(F+T,O+A[w]*T,n-T,r-A[w+1]*T,n,r)}}function be(e,a,o){let m=o-1,_=bt,A=mt;for(let w=m-1;w>=0;w--){let F=a[w*2],O=a[w*2+1],n=a[(w+1)*2],r=a[(w+1)*2+1],t=_[w];if(t<=0){e.lineTo(F,O);continue}let T=t/3;e.bezierCurveTo(n-T,r-A[w+1]*T,F+T,O+A[w]*T,F,O)}}var qt=["left","center","right"],At=new Map,me=4096;function _e(e,a,o,m){return e<<24|a<<16|o<<8|m}function H(e,a,o,m){let _=_e(e,a,o,m),A=At.get(_);return A||(A=`rgba(${e},${a},${o},${(m/255).toFixed(3)})`,At.size>=me&&At.clear(),At.set(_,A),A)}var Rt=new TextDecoder;function gt(e,a){return Math.round(e*a)/a}var Lt=new Map;function Mt(e,a){let o=Lt.get(a);if(o!==void 0)return o;let m=e.font,_=e.textBaseline;e.font=a,e.textBaseline="alphabetic";let A=e.measureText("0"),w=A.actualBoundingBoxAscent||0,F=A.actualBoundingBoxDescent||0;return o=(w-F)/2,(!Number.isFinite(o)||o===0)&&(o=(parseFloat(a)||11)*.35),Lt.size>=256&&Lt.clear(),Lt.set(a,o),e.font=m,e.textBaseline=_,o}var Vt=new WeakMap;function pe(e,a,o,m,_){let A=Vt.get(e);A||(A=new Map,Vt.set(e,A));let w=Math.round(a),F=`${w}|${o}|${m}|${_}`,O=A.get(F);if(O)return O;let n=-w*.25,r=-w*.25,t=e.createRadialGradient(n,r,w*.08,0,0,w);t.addColorStop(0,m),t.addColorStop(1,o);let T=null;_&&(T=e.createRadialGradient(0,0,w*.85,0,0,w*1.2),T.addColorStop(0,_),T.addColorStop(1,_.replace(/[\d.]+\)$/,"0)")));let P=w*.2,C=e.createRadialGradient(n,r,0,n,r,P);return C.addColorStop(0,"rgba(255,255,255,0.22)"),C.addColorStop(1,"rgba(255,255,255,0)"),O={body:t,glow:T,spec:C,hlX:n,hlY:r,specR:P},A.set(F,O),O}function Q(e,a,o){let m=Math.max(1,Math.round(a*o)),_=Math.round(e*o)/o;return m&1?_+.5/o:_}function ct(e,a,o,m,_){let A=Math.round(e*_),w=Math.round(a*_),F=Math.round((e+o)*_),O=Math.round((a+m)*_);return{x:A/_,y:w/_,w:(F-A)/_,h:(O-w)/_}}function Te(e,a,o,m,_){return{x:Math.round(e*_)/_,y:Math.round(a*_)/_,w:Math.max(1,Math.round(o*_))/_,h:Math.max(1,Math.round(m*_))/_}}function we(e,a,o,m,_){let A=Math.round(a*_),w=Math.round((a+m)*_);return{x:e,y:A/_,w:o,h:(w-A)/_}}function Kt(e,a,o,m,_=null,A=null,w=null,F=null){let O=a.buffer;if(o+m>O.byteLength)return;let n=new DataView(O,o,m),r=new Uint8Array(O,o,m),t=0,T="",P="",C=-1,Y="",c=Math.max(1,Math.abs(e.getTransform?.().a||ie())),lt=null,v=[],M=F&&F.geom&&F.geom.available?F.geom:null,z=e,tt=!1,ot="";for(;t<m;){let it=r[t];if(t+=1,it===k.END_FRAME)break;switch(it){case k.FILL_RECT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];if(t+=4,M){M.rect(s,u,h,l,i,f,d,b,0,0,0,0,0,0,0);break}let g=H(i,f,d,b);g!==T&&(e.fillStyle=g,T=g);let p=ct(s,u,h,l,c);e.fillRect(p.x,p.y,p.w,p.h);break}case k.FILL_RECT_SMOOTH_X:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];if(t+=4,M){M.rect(s,u,h,l,i,f,d,b,0,0,0,0,0,0,1);break}let g=H(i,f,d,b);g!==T&&(e.fillStyle=g,T=g);let p=we(s,u,h,l,c);e.fillRect(p.x,p.y,p.w,p.h);break}case k.FILL_ELLIPSE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];if(t+=4,M){M.rect(s-h,u-l,h*2,l*2,i,f,d,b,-1,0,0,0,0,0,2);break}let g=H(i,f,d,b);g!==T&&(e.fillStyle=g,T=g),e.beginPath(),e.ellipse(s,u,Math.max(h,.1),Math.max(l,.1),0,0,Math.PI*2),e.fill();break}case k.FILL_ROUND_RECT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];t+=4;let g=n.getFloat32(t,!0);if(t+=4,M){M.rect(s,u,h,l,i,f,d,b,g,0,0,0,0,0,0);break}let p=H(i,f,d,b);p!==T&&(e.fillStyle=p,T=p);let y=ct(s,u,h,l,c),E=Math.min(g,y.w*.5,y.h*.5);E<=.5?e.fillRect(y.x,y.y,y.w,y.h):(e.beginPath(),e.moveTo(y.x+E,y.y),e.lineTo(y.x+y.w-E,y.y),e.arcTo(y.x+y.w,y.y,y.x+y.w,y.y+E,E),e.lineTo(y.x+y.w,y.y+y.h-E),e.arcTo(y.x+y.w,y.y+y.h,y.x+y.w-E,y.y+y.h,E),e.lineTo(y.x+E,y.y+y.h),e.arcTo(y.x,y.y+y.h,y.x,y.y+y.h-E,E),e.lineTo(y.x,y.y+E),e.arcTo(y.x,y.y,y.x+E,y.y,E),e.closePath(),e.fill());break}case k.STROKE_RECT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];t+=4;let g=n.getFloat32(t,!0);if(t+=4,M){let N=g*.5;M.rect(s-N,u-N,h+g,l+g,0,0,0,0,0,g,i,f,d,b,0);break}let p=H(i,f,d,b);p!==P&&(e.strokeStyle=p,P=p),g!==C&&(e.lineWidth=g,C=g);let y=Q(s,g,c),E=Q(u,g,c),S=Math.round(h*c)/c,L=Math.round(l*c)/c;e.strokeRect(y,E,S,L);break}case k.LINE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];t+=4;let g=n.getFloat32(t,!0);if(t+=4,M){M.seg(s,u,h,l,i,f,d,b,g,0,0);break}let p=H(i,f,d,b);e.beginPath(),p!==P&&(e.strokeStyle=p,P=p),g!==C&&(e.lineWidth=g,C=g),e.setLineDash([]),e.moveTo(Q(s,g,c),Q(u,g,c)),e.lineTo(Q(h,g,c),Q(l,g,c)),e.stroke();break}case k.CANDLE:case k.CANDLE_BORDERED:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4;let b=n.getFloat32(t,!0);t+=4;let g=r[t+3],p=H(r[t],r[t+1],r[t+2],g);t+=4;let y=r[t+3],E=H(r[t],r[t+1],r[t+2],y);t+=4;let S=null;it===k.CANDLE_BORDERED&&(S=H(r[t],r[t+1],r[t+2],r[t+3]),t+=4);let L=Math.max(1,Math.floor(d*c)),N=Math.max(1,Math.floor(b*c));L>N&&(L=N);let I=Math.min(Math.max(1,Math.floor(f*c)),N);I<L?I=L:(I^L)&1&&(I-=1);let B=s*c,W=L&1?Math.round(B-.5)+.5:Math.round(B),U=Math.max(1,Math.round(c)),D=Math.round(Math.min(l,i)*c),$=Math.max(D+U,Math.round(Math.max(l,i)*c)),x=Math.round(Math.min(u,h)*c),R=Math.max(x+1,Math.round(Math.max(u,h)*c)),X=Math.min(u,h),Et=Math.max(u,h),St=Math.min(l,i),Ct=Math.max(l,i);if(X<St&&x>=D&&(x=D-1),Et>Ct&&R<=$&&(R=$+1),y>0&&(x<D||R>$)){E!==T&&(e.fillStyle=E,T=E);let Dt=(W-L/2)/c,Pt=L/c;g===255?e.fillRect(Dt,x/c,Pt,(R-x)/c):(x<D&&e.fillRect(Dt,x/c,Pt,(D-x)/c),R>$&&e.fillRect(Dt,$/c,Pt,(R-$)/c))}let V=W-I/2,j=$-D;if(S===null){g>0&&(p!==T&&(e.fillStyle=p,T=p),e.fillRect(V/c,D/c,I/c,j/c));break}let J=Math.max(1,Math.floor(c));if(S!==T&&(e.fillStyle=S,T=S),I<=2*J||j<=2*J){e.fillRect(V/c,D/c,I/c,j/c);break}g===255?e.fillRect(V/c,D/c,I/c,j/c):(e.fillRect(V/c,D/c,I/c,J/c),e.fillRect(V/c,($-J)/c,I/c,J/c),e.fillRect(V/c,(D+J)/c,J/c,(j-2*J)/c),e.fillRect((V+I-J)/c,(D+J)/c,J/c,(j-2*J)/c)),g>0&&(p!==T&&(e.fillStyle=p,T=p),e.fillRect((V+J)/c,(D+J)/c,(I-2*J)/c,(j-2*J)/c));break}case k.OHLC_BAR:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let b=r[t];t+=1;let g=Math.max(1,Math.floor(f*c)),p=Math.max(1,Math.floor(c)),y=b&1?p:Math.max(p,Math.floor(g/6));y>g&&(y=g);let E=s*c,L=(y&1?Math.round(E-.5)+.5:Math.round(E))-y/2,N=Math.round(Math.min(h,l)*c),I=Math.max(N+1,Math.round(Math.max(h,l)*c));d!==T&&(e.fillStyle=d,T=d),e.fillRect(L/c,N/c,y/c,(I-N)/c);let B=Math.floor((Math.floor(g*.8)-y)/2);if(B>=1){if(!(b&2)){let U=Math.round(u*c-y/2);e.fillRect((L-B)/c,U/c,B/c,y/c)}let W=Math.round(i*c-y/2);e.fillRect((L+y)/c,W/c,B/c,y/c)}break}case k.DASHED_LINE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t],f=r[t+1],d=r[t+2],b=r[t+3];t+=4;let g=n.getFloat32(t,!0);t+=4;let p=n.getFloat32(t,!0);t+=4;let y=n.getFloat32(t,!0);if(t+=4,M){M.seg(s,u,h,l,i,f,d,b,g,p,y);break}let E=H(i,f,d,b);e.beginPath(),E!==P&&(e.strokeStyle=E,P=E),g!==C&&(e.lineWidth=g,C=g),e.setLineDash([p,y]),e.moveTo(Q(s,g,c),Q(u,g,c)),e.lineTo(Q(h,g,c),Q(l,g,c)),e.stroke(),e.setLineDash([]);break}case k.LINEAR_POLYLINE:{let s=n.getUint16(t,!0);t+=2;let u=r[t],h=r[t+1],l=r[t+2],i=r[t+3];t+=4;let f=n.getFloat32(t,!0);if(t+=4,M){let b=0,g=0;for(let p=0;p<s;p++){let y=n.getFloat32(t,!0),E=n.getFloat32(t+4,!0);t+=8,p>0&&M.seg(b,g,y,E,u,h,l,i,f,0,0),b=y,g=E}break}let d=H(u,h,l,i);if(s>1){e.beginPath(),d!==P&&(e.strokeStyle=d,P=d),f!==C&&(e.lineWidth=f,C=f),e.setLineDash([]),e.lineJoin="miter",e.lineCap="butt",e.miterLimit=10;let b=n.getFloat32(t,!0);t+=4;let g=n.getFloat32(t,!0);t+=4,e.moveTo(b,g);for(let p=1;p<s;p++)e.lineTo(n.getFloat32(t,!0),n.getFloat32(t+4,!0)),t+=8;e.stroke()}else t+=s*8;break}case k.SMOOTH_POLYLINE:{let s=n.getUint16(t,!0);t+=2;let u=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let h=n.getFloat32(t,!0);if(t+=4,s>1){let l=new Float32Array(s*2);for(let i=0;i<s;i++)l[i*2]=n.getFloat32(t,!0),t+=4,l[i*2+1]=n.getFloat32(t,!0),t+=4;e.beginPath(),u!==P&&(e.strokeStyle=u,P=u),h!==C&&(e.lineWidth=h,C=h),e.setLineDash([]),e.lineJoin="round",e.lineCap="round",s===2?(e.moveTo(l[0],l[1]),e.lineTo(l[2],l[3])):(Ot(l,s),e.moveTo(l[0],l[1]),zt(e,l,s)),e.stroke()}else t+=s*8;break}case k.POLYLINE:{let s=n.getUint16(t,!0);t+=2;let u=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let h=n.getFloat32(t,!0);if(t+=4,s>0){let l=new Float32Array(s*2);for(let i=0;i<s;i++)l[i*2]=n.getFloat32(t,!0),t+=4,l[i*2+1]=n.getFloat32(t,!0),t+=4;if(e.beginPath(),u!==P&&(e.strokeStyle=u,P=u),h!==C&&(e.lineWidth=h,C=h),e.setLineDash([]),e.lineJoin="round",e.lineCap="round",s===2)e.moveTo(l[0],l[1]),e.lineTo(l[2],l[3]);else{e.moveTo(l[0],l[1]);for(let i=0;i<s-1;i++){let f=i>0?i-1:0,d=i,b=i+1,g=i<s-2?i+2:s-1,p=l[f*2],y=l[f*2+1],E=l[d*2],S=l[d*2+1],L=l[b*2],N=l[b*2+1],I=l[g*2],B=l[g*2+1],W=E+(L-p)/6,U=S+(N-y)/6,D=L-(I-E)/6,$=N-(B-S)/6;e.bezierCurveTo(W,U,D,$,L,N)}}e.stroke()}break}case k.TEXT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t];t+=1;let f=n.getUint16(t,!0);t+=2;let d=Rt.decode(new Uint8Array(a.buffer,o+t,f));t+=f,h!==T&&(e.fillStyle=h,T=h);let b=`${Math.round(l)}px "IBM Plex Mono",monospace`;b!==Y&&(e.font=b,Y=b),e.textAlign=qt[i]||"left",e.textBaseline="alphabetic";let g=u+Mt(e,b);e.fillText(d,gt(s,c),gt(g,c));break}case k.STYLED_TEXT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t];t+=1;let f=r[t];t+=1;let d=n.getUint16(t,!0);t+=2;let b=Rt.decode(new Uint8Array(a.buffer,o+t,d));t+=d,h!==T&&(e.fillStyle=h,T=h);let g=f&2?"italic ":"",p=f&1?"700 ":"",y=`${g}${p}${Math.round(l)}px "IBM Plex Mono",monospace`;y!==Y&&(e.font=y,Y=y),e.textAlign=qt[i]||"left",e.textBaseline="alphabetic";let E=u+Mt(e,y);e.fillText(b,gt(s,c),gt(E,c));break}case k.CIRCLE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=r[t],i=r[t+1],f=r[t+2],d=r[t+3],b=M?"":H(l,i,f,d);t+=4;let g=r[t],p=r[t+1],y=r[t+2],E=r[t+3],S=M?"":H(g,p,y,E);t+=4;let L=n.getFloat32(t,!0);if(t+=4,M){M.disc(s,u,h,l,i,f,d,g,p,y,E,0,0,0,0,L,0);break}e.beginPath(),e.arc(s,u,h,0,Math.PI*2),b!==T&&(e.fillStyle=b,T=b),e.fill(),L>0&&(S!==P&&(e.strokeStyle=S,P=S),L!==C&&(e.lineWidth=L,C=L),e.stroke());break}case k.TRIANGLE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=r[t],b=r[t+1],g=r[t+2],p=r[t+3];if(t+=4,M){M.tri(s,u,h,l,i,f,d,b,g,p);break}let y=H(d,b,g,p);e.beginPath(),e.moveTo(s,u),e.lineTo(h,l),e.lineTo(i,f),e.closePath(),y!==T&&(e.fillStyle=y,T=y),e.fill();break}case k.SAVE:{e.save(),v.push(lt);break}case k.RESTORE:{e.restore(),lt=v.length?v.pop():null,M&&M.setClip(lt),T="",P="",C=-1,Y="";break}case k.CLIP_RECT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=ct(s,u,h,l,c);e.beginPath(),e.rect(i.x,i.y,i.w,i.h),e.clip(),lt=i,M&&M.setClip(i);break}case k.ROTATED_TEXT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);t+=4;let f=n.getUint16(t,!0);t+=2;let d=Rt.decode(r.subarray(t,t+f));t+=f;let b=`${l}px sans-serif`;b!==Y&&(e.font=b,Y=b),h!==T&&(e.fillStyle=h,T=h),e.textAlign="center",e.textBaseline="alphabetic";let g=Mt(e,b);e.save(),e.translate(s,u),e.rotate(i),e.fillText(d,0,g),e.restore();break}case k.BUBBLE_3D:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=r[t],i=r[t+1],f=r[t+2],d=r[t+3];t+=4;let b=r[t],g=r[t+1],p=r[t+2],y=r[t+3];t+=4;let E=r[t],S=r[t+1],L=r[t+2],N=r[t+3];t+=4;let I=n.getFloat32(t,!0);t+=4;let B=h;if(M){M.disc(s,u,B,l,i,f,d,E,S,L,N,b,g,p,y,I,B<4?0:1);break}if(B<4){let W=`rgba(${l},${i},${f},${(d/255).toFixed(3)})`;e.beginPath(),e.arc(s,u,B,0,Math.PI*2),e.fillStyle=W,e.fill(),T=""}else{let W=Math.round(B),U=B>=6,D=B>=16,$=`rgba(${l},${i},${f},${(d/255).toFixed(3)})`,x=`rgba(${b},${g},${p},${(y/255).toFixed(3)})`,R=D?`rgba(${E},${S},${L},${(N/255*.5).toFixed(3)})`:null,X=pe(e,W,$,x,R);e.save(),e.translate(s,u),D&&X.glow&&(e.beginPath(),e.arc(0,0,W*1.2,0,Math.PI*2),e.fillStyle=X.glow,e.fill()),e.beginPath(),e.arc(0,0,W,0,Math.PI*2),e.fillStyle=X.body,e.fill(),U&&(e.beginPath(),e.arc(X.hlX,X.hlY,X.specR,0,Math.PI*2),e.fillStyle=X.spec,e.fill()),e.restore(),T=""}break}case k.FILL_POLYGON:{let s=n.getUint16(t,!0);t+=2;let u=r[t],h=r[t+1],l=r[t+2],i=r[t+3];if(t+=4,M){let d=fe(s*2);for(let b=0;b<s;b++)d[b*2]=n.getFloat32(t,!0),d[b*2+1]=n.getFloat32(t+4,!0),t+=8;M.poly(d,s,u,h,l,i);break}let f=H(u,h,l,i);if(s>2){e.beginPath();let d=n.getFloat32(t,!0);t+=4;let b=n.getFloat32(t,!0);t+=4,e.moveTo(d,b);for(let g=1;g<s;g++)e.lineTo(n.getFloat32(t,!0),n.getFloat32(t+4,!0)),t+=8;e.closePath(),f!==T&&(e.fillStyle=f,T=f),e.fill()}else t+=s*8;break}case k.FILL_SMOOTH_BAND:{let s=n.getUint16(t,!0);t+=2;let u=n.getUint16(t,!0);t+=2;let h=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let l=new Float32Array(s*2);for(let f=0;f<s;f++)l[f*2]=n.getFloat32(t,!0),t+=4,l[f*2+1]=n.getFloat32(t,!0),t+=4;let i=new Float32Array(u*2);for(let f=0;f<u;f++)i[f*2]=n.getFloat32(t,!0),t+=4,i[f*2+1]=n.getFloat32(t,!0),t+=4;s>=2&&u>=2&&(e.beginPath(),h!==T&&(e.fillStyle=h,T=h),s===2?(e.moveTo(l[0],l[1]),e.lineTo(l[2],l[3])):(Ot(l,s),e.moveTo(l[0],l[1]),zt(e,l,s)),e.lineTo(i[(u-1)*2],i[(u-1)*2+1]),u===2?e.lineTo(i[0],i[1]):(Ot(i,u),be(e,i,u)),e.closePath(),e.fill());break}case k.DASHED_POLYLINE:{let s=n.getUint16(t,!0);t+=2;let u=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);if(t+=4,s>1){e.beginPath(),u!==P&&(e.strokeStyle=u,P=u),h!==C&&(e.lineWidth=h,C=h),e.setLineDash([l,i]),e.lineJoin="round";let f=n.getFloat32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4,e.moveTo(f,d);for(let b=1;b<s;b++)e.lineTo(n.getFloat32(t,!0),n.getFloat32(t+4,!0)),t+=8;e.stroke(),e.setLineDash([])}else t+=s*8;break}case k.GRADIENT_RECT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let f=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let d=ct(s,u,h,l,c),b=e.createLinearGradient(d.x,d.y,d.x,d.y+d.h);b.addColorStop(0,i),b.addColorStop(1,f),e.fillStyle=b,T="",e.fillRect(d.x,d.y,d.w,d.h);break}case k.GRADIENT_RECT_H:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let f=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let d=ct(s,u,h,l,c),b=e.createLinearGradient(d.x,d.y,d.x+d.w,d.y);b.addColorStop(0,i),b.addColorStop(1,f),e.fillStyle=b,T="",e.fillRect(d.x,d.y,d.w,d.h);break}case k.GRADIENT_RECT_H_MULTI:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=r[t];t+=1;let f=ct(s,u,h,l,c),d=e.createLinearGradient(f.x,f.y,f.x+f.w,f.y);for(let b=0;b<i;b++){let g=n.getFloat32(t,!0);t+=4;let p=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4,d.addColorStop(g<0?0:g>1?1:g,p)}e.fillStyle=d,T="",e.fillRect(f.x,f.y,f.w,f.h);break}case k.BEGIN_HEATMAP_LAYER:{let s=t-1,u=n.getUint32(t,!0);t+=4;let h=n.getUint32(t,!0);t+=4;let l=n.getUint32(t,!0);if(t+=4,!_||l===0)break;let i=z.canvas.width,f=z.canvas.height,d=`${u}|${h}|${i}|${f}|${c}`;if(_.getKey()===d&&_.getCanvas()){let b=i/c,g=f/c;z.drawImage(_.getCanvas(),0,0,b,g),t=s+l}else{let b=Math.min(c,le),g=Math.max(1,Math.round(i/c*b)),p=Math.max(1,Math.round(f/c*b)),y=_.ensureSize(g,p,b);if(!y)break;y.save(),y.setTransform(1,0,0,1,0,0),y.clearRect(0,0,g,p),y.restore(),e=y,T="",P="",C=-1,Y="",tt=!0,ot=d}break}case k.END_HEATMAP_LAYER:{if(!tt)break;let s=z.canvas.width/c,u=z.canvas.height/c;z.drawImage(_.getCanvas(),0,0,s,u),_.setKey(ot),e=z,T="",P="",C=-1,Y="",tt=!1,ot="";break}case k.HEATMAP_BITMAP:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getUint32(t,!0);t+=4;let l=n.getUint32(t,!0);t+=4;let i=h*l*4;if(t+i>r.byteLength){t=r.byteLength;break}let f=new Uint8ClampedArray(r.buffer,r.byteOffset+t,i);t+=i;let d=new ImageData(f,h,l),b=ue(h,l);if(!b)break;b.putImageData(d,0,0),e.drawImage(at,s,u,h,l);break}case k.HEATMAP_WORLD:{let s=n.getUint32(t,!0);t+=4;let u=n.getUint32(t,!0);t+=4;let h=n.getUint32(t,!0);t+=4;let l=n.getUint32(t,!0);t+=4;let i=n.getUint32(t,!0);t+=4;let f=n.getUint32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4;let b=n.getFloat32(t,!0);t+=4;let g=n.getFloat32(t,!0);t+=4;let p=n.getFloat32(t,!0);t+=4;let y=n.getFloat32(t,!0);t+=4;let E=n.getFloat32(t,!0);t+=4;let S=n.getFloat32(t,!0);t+=4;let L=n.getFloat32(t,!0);t+=4;let N=r[t];t+=1;let I=(N&1)!==0,B=i*f*4;if(t+B>r.byteLength){t=r.byteLength;break}if(!A||s===0||u===0){t+=B;break}let W=A.ensureSize(s,u);if(!W){t+=B;break}if(I&&W.clearRect(0,0,s,u),B>0){let U=new Uint8ClampedArray(r.buffer,r.byteOffset+t,B),D=new ImageData(U,i,f);W.putImageData(D,h,l)}if(t+=B,g>0&&p>0&&S>0&&L>0){let U=Te(y,E,S,L,c);if(U.w>0&&U.h>0){let D=e.imageSmoothingEnabled;e.imageSmoothingEnabled=!1,e.drawImage(A.getCanvas(),d,b,g,p,U.x,U.y,U.w,U.h),e.imageSmoothingEnabled=D}}break}case k.LINE_WITH_EXTEND:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4;let b=n.getFloat32(t,!0);t+=4;let g=r[t];t+=1,i!==P&&(e.strokeStyle=i,P=i),f!==C&&(e.lineWidth=f,C=f),e.setLineDash(d>0?[d,b]:[]);let p=1e5,y=s,E=u,S=h,L=l,N=h-s,I=l-u,B=Math.hypot(N,I);if(B>0&&g!==0){let W=N/B,U=I/B;g&1&&(y=s-W*p,E=u-U*p),g&2&&(S=h+W*p,L=l+U*p),g&4&&U<0&&(y=s-W*p,E=u-U*p),g&8&&U>0&&(S=h+W*p,L=l+U*p)}e.beginPath(),e.moveTo(Q(y,f,c),Q(E,f,c)),e.lineTo(Q(S,f,c),Q(L,f,c)),e.stroke(),d>0&&e.setLineDash([]);break}case k.LABEL_CALLOUT:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let l=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let i=H(r[t],r[t+1],r[t+2],r[t+3]);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4;let b=r[t];t+=1;let g=r[t];t+=1;let p=n.getUint16(t,!0);t+=2;let y=Rt.decode(r.subarray(t,t+p));t+=p;let E=`${Math.round(f)}px "IBM Plex Mono",monospace`;E!==Y&&(e.font=E,Y=E);let S=4,L=6,N=y.indexOf(`
15
+ `)>=0?y.split(`
16
+ `):[y],I=Math.round(f),B=3,W=0;for(let V=0;V<N.length;V++){let j=e.measureText(N[V]).width;j>W&&(W=j)}let U=N.length*I+(N.length-1)*B+S*2,D=Math.ceil(W)+S*2,$,x;switch(g){case 1:x=u-L-U,$=s-D/2;break;case 2:x=u+L,$=s-D/2;break;case 3:x=u-U/2,$=s+L;break;case 4:x=u-U/2,$=s-L-D;break;default:x=u-U/2,b===1?$=s-D/2:b===2?$=s-D:$=s;break}let R=ct($,x,D,U,c),X=Math.min(3,R.w*.5,R.h*.5),Et=()=>{e.beginPath(),X<=.5?e.rect(R.x,R.y,R.w,R.h):(e.moveTo(R.x+X,R.y),e.lineTo(R.x+R.w-X,R.y),e.arcTo(R.x+R.w,R.y,R.x+R.w,R.y+X,X),e.lineTo(R.x+R.w,R.y+R.h-X),e.arcTo(R.x+R.w,R.y+R.h,R.x+R.w-X,R.y+R.h,X),e.lineTo(R.x+X,R.y+R.h),e.arcTo(R.x,R.y+R.h,R.x,R.y+R.h-X,X),e.lineTo(R.x,R.y+X),e.arcTo(R.x,R.y,R.x+X,R.y,X)),e.closePath()};if(h!==T&&(e.fillStyle=h,T=h),Et(),e.fill(),d>0&&(l!==P&&(e.strokeStyle=l,P=l),d!==C&&(e.lineWidth=d,C=d),e.setLineDash([]),Et(),e.stroke()),g>=1&&g<=4){switch(h!==T&&(e.fillStyle=h,T=h),e.beginPath(),g){case 1:e.moveTo(s-L/2,x+U),e.lineTo(s+L/2,x+U),e.lineTo(s,u);break;case 2:e.moveTo(s-L/2,x),e.lineTo(s+L/2,x),e.lineTo(s,u);break;case 3:e.moveTo($,u-L/2),e.lineTo($,u+L/2),e.lineTo(s,u);break;case 4:e.moveTo($+D,u-L/2),e.lineTo($+D,u+L/2),e.lineTo(s,u);break}e.closePath(),e.fill()}i!==T&&(e.fillStyle=i,T=i),e.textAlign="center",e.textBaseline="alphabetic";let St=Mt(e,E),Ct=$+D/2;for(let V=0;V<N.length;V++){let j=x+S+I/2+V*(I+B);e.fillText(N[V],gt(Ct,c),gt(j+St,c))}break}case k.DRAW_IMAGE:{let s=n.getFloat32(t,!0);t+=4;let u=n.getFloat32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getUint32(t,!0);if(t+=4,!w||i===0||h<=0||l<=0)break;let f=w.get(i);if(!f)break;try{e.drawImage(f,s,u,h,l)}catch(d){w.delete(i),console.warn("[canvasRenderer] drawImage failed for id="+i,d?.message||d)}break}case k.HEATMAP_BG:{let s=r[t],u=r[t+1],h=r[t+2],l=r[t+3],i=r[t+4],f=r[t+5],d=r[t+6],b=r[t+7],g=r[t+8]!==0;t+=9,F&&(e.save(),e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,e.canvas.width,e.canvas.height),e.restore(),F.bg(s,u,h,l,i,f,d,b,g));break}case k.HEATMAP_WORLD_INTENSITY:{let s=n.getUint32(t,!0);t+=4;let u=n.getUint32(t,!0);t+=4;let h=n.getUint32(t,!0);t+=4;let l=n.getUint32(t,!0);t+=4;let i=n.getUint32(t,!0);t+=4;let f=n.getUint32(t,!0);t+=4;let d=n.getFloat32(t,!0);t+=4;let b=n.getFloat32(t,!0);t+=4;let g=n.getFloat32(t,!0);t+=4;let p=n.getFloat32(t,!0);t+=4;let y=n.getFloat32(t,!0);t+=4;let E=n.getFloat32(t,!0);t+=4;let S=n.getFloat32(t,!0);t+=4;let L=n.getFloat32(t,!0);t+=4;let N=n.getUint32(t,!0);t+=4;let I=r[t];t+=1;let B=n.getFloat32(t,!0);t+=4;let W=(I&2)!==0,U=(I&4)!==0,D=I&8?1:0,$=i*f;if(t+$>r.byteLength){t=r.byteLength;break}if(F){let x=$>0?new Uint8Array(r.buffer,r.byteOffset+t,$):null;F.heatmap({worldCols:s,worldRows:u,patchX:h,patchY:l,patchW:i,patchH:f,srcX:d,srcY:b,srcW:g,srcH:p,destX:y,destY:E,destW:S,destH:L,lutSeq:N,hd:W,subpx:U,subTexelX:B,fullRebuild:(I&1)!==0,layer:D,clip:lt},x)}t+=$;break}case k.OB_HEATMAP:{let s=n.getUint32(t,!0);t+=4;let u=n.getUint32(t,!0);t+=4;let h=n.getFloat32(t,!0);t+=4;let l=n.getFloat32(t,!0);t+=4;let i=n.getFloat32(t,!0);t+=4;let f=n.getFloat32(t,!0);t+=4;let d=r[t],b=r[t+1],g=r[t+2],p=r[t+3];t+=4;let y=n.getFloat32(t,!0);t+=4;let E=n.getUint32(t,!0);t+=4,t+=1;let S=s*u*2;if(t+S>r.byteLength){t=r.byteLength;break}F&&typeof F.obHeatmap=="function"&&F.obHeatmap({cols:s,rows:u,destX:h,destY:l,destW:i,destH:f,bgR:d,bgG:b,bgB:g,bgA:p,alphaMul:y,lutSeq:E},S>0?new Uint8Array(r.buffer,r.byteOffset+t,S):null),t+=S;break}default:t=m;break}}if(tt&&_){let it=z.canvas.width/c,s=z.canvas.height/c;z.drawImage(_.getCanvas(),0,0,it,s),_.invalidate()}M&&M.flush()}var Fe=`#version 300 es
17
+ in vec2 aPos;
18
+ uniform vec4 uRect; // clip-space x0,y0 (bottom-left) .. x1,y1 (top-right)
19
+ out vec2 vUv;
20
+ void main() {
21
+ vUv = aPos;
22
+ gl_Position = vec4(mix(uRect.xy, uRect.zw, aPos), 0.0, 1.0);
23
+ }`,Ee=`#version 300 es
24
+ precision highp float;
25
+ in vec2 vUv;
26
+ uniform sampler2D uCells; // RG8: R = intensity (0 = empty), G = 0 bid / 1 ask
27
+ uniform sampler2D uLut; // 256x2 RGBA: row0 bid ramp, row1 ask ramp
28
+ uniform vec2 uGrid; // cols, rows
29
+ uniform float uAlpha;
30
+ uniform float uPool;
31
+ out vec4 outColor;
32
+
33
+ const int MAX_POOL = 32;
34
+
35
+ void main() {
36
+ float col = clamp(floor(vUv.x * uGrid.x), 0.0, uGrid.x - 1.0);
37
+ float row0 = floor(vUv.y * uGrid.y);
38
+ int pool = int(clamp(uPool, 1.0, float(MAX_POOL)));
39
+ // Not "half" \u2014 that is a GLSL ES reserved word and fails to compile.
40
+ float halfPool = floor(float(pool) * 0.5);
41
+
42
+ float best = 0.0;
43
+ float side = 0.0;
44
+ for (int k = 0; k < MAX_POOL; k++) {
45
+ if (k >= pool) break;
46
+ float r = row0 + float(k) - halfPool;
47
+ if (r < 0.0 || r >= uGrid.y) continue;
48
+ vec2 c = texture(uCells, (vec2(col, r) + 0.5) / uGrid).rg;
49
+ if (c.r > best) { best = c.r; side = c.g; }
50
+ }
51
+ if (best <= 0.0) discard;
52
+
53
+ // best is the normalised byte; land on the matching LUT texel centre.
54
+ vec4 lut = texture(uLut, vec2((best * 255.0 + 0.5) / 256.0, side > 0.5 ? 0.75 : 0.25));
55
+ outColor = vec4(lut.rgb, lut.a * uAlpha);
56
+ }`;function Jt(e,a,o){let m=e.createShader(a);if(e.shaderSource(m,o),e.compileShader(m),!e.getShaderParameter(m,e.COMPILE_STATUS)){let _=e.getShaderInfoLog(m);throw e.deleteShader(m),new Error(`ob shader compile failed: ${_}`)}return m}function Qt(e,a={}){let o=null,m=null,_=null,A=null,w=null,F=null,O=null,n=null,r=null,t=null,T=-1,P=1,C=1,Y=1,c=!1;try{o=e.getContext("webgl2",{alpha:!0,depth:!1,stencil:!1,antialias:!1,preserveDrawingBuffer:!1,desynchronized:!1,premultipliedAlpha:!1})}catch{o=null}if(o)try{let v=Jt(o,o.VERTEX_SHADER,Fe),M=Jt(o,o.FRAGMENT_SHADER,Ee);if(m=o.createProgram(),o.attachShader(m,v),o.attachShader(m,M),o.linkProgram(m),o.deleteShader(v),o.deleteShader(M),!o.getProgramParameter(m,o.LINK_STATUS))throw new Error(`ob program link failed: ${o.getProgramInfoLog(m)}`);_=o.createVertexArray(),o.bindVertexArray(_),A=o.createBuffer(),o.bindBuffer(o.ARRAY_BUFFER,A),o.bufferData(o.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),o.STATIC_DRAW);let z=o.getAttribLocation(m,"aPos");o.enableVertexAttribArray(z),o.vertexAttribPointer(z,2,o.FLOAT,!1,0,0),o.bindVertexArray(null),w=o.createTexture(),o.bindTexture(o.TEXTURE_2D,w);for(let[tt,ot]of[[o.TEXTURE_MIN_FILTER,o.NEAREST],[o.TEXTURE_MAG_FILTER,o.NEAREST],[o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE],[o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE]])o.texParameteri(o.TEXTURE_2D,tt,ot);F=o.createTexture(),o.bindTexture(o.TEXTURE_2D,F);for(let[tt,ot]of[[o.TEXTURE_MIN_FILTER,o.NEAREST],[o.TEXTURE_MAG_FILTER,o.NEAREST],[o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE],[o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE]])o.texParameteri(o.TEXTURE_2D,tt,ot);O=o.getUniformLocation(m,"uRect"),n=o.getUniformLocation(m,"uGrid"),r=o.getUniformLocation(m,"uAlpha"),t=o.getUniformLocation(m,"uPool"),o.useProgram(m),o.uniform1i(o.getUniformLocation(m,"uCells"),0),o.uniform1i(o.getUniformLocation(m,"uLut"),1),o.disable(o.DEPTH_TEST),o.enable(o.BLEND),o.blendFunc(o.SRC_ALPHA,o.ONE_MINUS_SRC_ALPHA),o.pixelStorei(o.UNPACK_ALIGNMENT,1),C=e.width,Y=e.height,o.viewport(0,0,C,Y),c=!0}catch(v){console.warn("[ObWebgl] init failed:",v?.message||v),c=!1}o&&typeof e.addEventListener=="function"&&e.addEventListener("webglcontextlost",v=>{v.preventDefault(),c=!1;try{a.onContextLost?.()}catch{}});function lt(v){if(v===T)return;let M=a.fetchLut?.();!M||M.length<512*4||(o.activeTexture(o.TEXTURE1),o.bindTexture(o.TEXTURE_2D,F),o.texImage2D(o.TEXTURE_2D,0,o.RGBA8,256,2,0,o.RGBA,o.UNSIGNED_BYTE,M),T=v)}return{get available(){return c},obHeatmap(v,M){if(!c||(o.viewport(0,0,C,Y),o.clearColor(v.bgR/255,v.bgG/255,v.bgB/255,v.bgA/255),o.clear(o.COLOR_BUFFER_BIT),!v.cols||!v.rows||!M||v.destW<=0||v.destH<=0)||M.length<v.cols*v.rows*2)return;o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,w),o.texImage2D(o.TEXTURE_2D,0,o.RG8,v.cols,v.rows,0,o.RG,o.UNSIGNED_BYTE,M),lt(v.lutSeq);let z=v.destX*P/C*2-1,tt=(v.destX+v.destW)*P/C*2-1,ot=1-v.destY*P/Y*2,it=1-(v.destY+v.destH)*P/Y*2;o.useProgram(m),o.bindVertexArray(_),o.uniform4f(O,z,it,tt,ot),o.uniform2f(n,v.cols,v.rows),o.uniform1f(r,v.alphaMul),o.uniform1f(t,Math.min(32,Math.max(1,Math.ceil(v.rows/Math.max(v.destH*P,1))))),o.drawArrays(o.TRIANGLE_STRIP,0,4),o.bindVertexArray(null)},resize(v,M,z){C=Math.max(1,v),Y=Math.max(1,M),P=z>0?z:1,c&&o.viewport(0,0,C,Y)},destroy(){if(c=!1,!!o){try{o.deleteTexture(w),o.deleteTexture(F),o.deleteBuffer(A),o.deleteVertexArray(_),o.deleteProgram(m),o.getExtension("WEBGL_lose_context")?.loseContext()}catch{}o=null}}}}function jt(e,a,o){let m=e&&e[a];typeof m=="function"&&m.apply(e,o||[])}var Ht=null,Nt=null,dt=null;async function Ae(){let e=typeof globalThis>"u"?null:globalThis,a=e&&e.__mrdWasmResp;if(!a)return null;e.__mrdWasmResp=null;try{let o=await a;return o&&o.ok?o:null}catch{return null}}function Re(){if(typeof WorkerGlobalScope>"u"||!self.location?.search)return null;let e=a=>new URL(a+self.location.search,self.location.href).href;return{glue:e("./chart_engine.js"),wasm:e("./chart_engine_bg.wasm")}}async function Zt(){return Ht?{module:Ht,memory:Nt}:dt||(dt=(async()=>{let e=Re(),a=e?await import(e.glue):await import("./chart_engine.js"),o=e?null:await Ae(),m=e?e.wasm:o;try{await(m?a.default({module_or_path:m}):a.default())}catch(_){console.warn("[mrd-chart] WebAssembly streaming init failed, retrying with arrayBuffer fallback:",_);let A=e?e.wasm:new URL("../wasm/chart_engine_bg.wasm",import.meta.url),w=await fetch(A);if(!w.ok)throw new Error(`WASM fetch failed (${w.status} ${w.statusText}) at ${A}`);let F=await w.arrayBuffer();await a.default(F)}return Nt=a.wasm_memory(),Ht=a,{module:a,memory:Nt}})(),dt.catch(()=>{dt=null}),dt)}async function te(e){if(!Array.isArray(e)||!e.length||typeof FontFace!="function"||!self.fonts)return;let a=!1,o=new Promise(_=>setTimeout(()=>{a=!0,_()},1500)),m=e.map(async _=>{try{let A={weight:_.weight,style:_.style};_.unicodeRange&&(A.unicodeRange=_.unicodeRange);let w=new FontFace(_.family,`url(${_.src})`,A);await w.load(),a||self.fonts.add(w)}catch{}});await Promise.race([Promise.all(m),o])}function ee(e){let a=e.get_hover_price();return a>0?{price:a,qty:e.get_hover_qty(),side:e.get_hover_side()===2?"ask":"bid"}:null}var Le={OrderbookEngine:{gl:!0,readState(e){let a=ee(e),o=null;if(e.is_range_active()){let m=e.get_range_low_price(),_=e.get_range_high_price();_>m&&(o={low:m,high:_,delta:_-m,deltaPct:m>0?(_-m)/m*100:0,bidQty:e.get_range_bid_qty(),askQty:e.get_range_ask_qty(),ticks:e.get_range_tick_count(),mode:e.get_range_mode()===2?"hover":"measure"})}return{hover:a,range:o}},sig:e=>`${e.hover?`${e.hover.price}|${e.hover.qty}|${e.hover.side}`:""}#${e.range?`${e.range.low}|${e.range.high}|${e.range.bidQty}|${e.range.askQty}|${e.range.mode}`:""}`,clickHover:ee},DomLadderEngine:{gl:!0,lut:"dom",readState(e){let a=e.get_hover_price();return{hover:a>0?{price:a,side:e.get_hover_side()===1?"ask":"bid"}:null,viewLocked:e.is_view_locked(),m1:e.momentum_1m(),buy1m:e.buy_volume_1m(),sell1m:e.sell_volume_1m(),map:typeof e.ladder_view_map=="function"?e.ladder_view_map():null,lanes:typeof e.ladder_lane_map=="function"?e.ladder_lane_map():null,colScroll:typeof e.column_scroll_state=="function"?e.column_scroll_state():null}},sig:e=>{let a=e.map,o=a&&a.length?`${a.length}|${a[0]}|${a[1]}|${a[2]}|${a[3]}|${a[a.length-1]}`:"",m=e.lanes,_=m&&m.length?m.join("|"):"",A=e.colScroll,w=A&&A.length>=2?`${A[0]}|${A[1]}`:"";return`${e.hover?`${e.hover.price}|${e.hover.side}`:""}#${e.viewLocked}#${e.m1}|${e.buy1m}|${e.sell1m}#${o}#${_}#${w}`},clickHover(e){let a=e.get_hover_price();return a>0?{price:a,side:e.get_hover_side()===1?"ask":"bid"}:null}}},G=null,st=null,Gt=null,q=null,K=null,Z=null,rt=null,nt=1,pt=!1,xt=!1,et=!1,Tt=!1,wt=!1,ut=!0,ft=null,oe="",re=typeof self.requestAnimationFrame=="function",$t=!0,Wt=0,Me=1500;function ht(e,a,o){try{self.postMessage({t:e,...a},o||[])}catch{}}function Ft(e){et||(et=!0,vt(),ht("dead",{err:String(e&&e.message?e.message:e)}))}function yt(){xt||!pt||et||wt||ft!==null||$t&&(ft=re?self.requestAnimationFrame(ne):setTimeout(ne,16))}function vt(){ft!==null&&(re?self.cancelAnimationFrame(ft):clearTimeout(ft),ft=null)}function _t(){ut=!0,yt()}function ae(){try{K.save(),K.setTransform(1,0,0,1,0,0),K.clearRect(0,0,q.width,q.height),K.fillStyle="#787b86",K.font=`600 ${Math.round(12*nt)}px ui-monospace, 'SF Mono', Menlo, Consolas, monospace`,K.textAlign="center",K.textBaseline="middle",K.fillText("Enterprise license required",q.width/2,q.height/2),K.restore()}catch{}}function Xt(){if(et)return!1;if(wt)return ae(),!0;try{if(G.render()>0){let a=G.get_command_buffer_ptr(),o=G.get_command_buffer_len();K.save(),K.setTransform(1,0,0,1,0,0),K.clearRect(0,0,q.width,q.height),K.restore(),Kt(K,Gt,a,o,null,null,null,rt)}return!0}catch(e){return e instanceof WebAssembly.RuntimeError&&Ft(e),!1}}function ne(){if(ft=null,xt||!pt||et||wt)return;let e=!1;try{G.set_now_ms?.(Date.now()),e=ut||G.is_dirty()}catch(m){Ft(m);return}let a=Tt&&Wt>0&&Date.now()-Wt>Me;e&&Tt&&!a?ut=!0:e&&(ut=!Xt(),ke());let o=ut;if(!o)try{o=G.is_dirty()}catch{o=!1}o&&yt()}function ve(){if(et||!q)return null;let e=q.width,a=q.height;if(!(e>0)||!(a>0)||!Xt())return null;try{let o=new OffscreenCanvas(e,a),m=o.getContext("2d");return m?(rt&&Z&&m.drawImage(Z,0,0),m.drawImage(q,0,0),o.transferToImageBitmap()):null}catch{return null}}function ke(){if(et||!st)return;let e=null;try{e=st.readState(G)}catch{return}let a=st.sig(e);a!==oe&&(oe=a,ht("state",e,e.map&&e.map.buffer?[e.map.buffer]:[]))}function Se(){try{G?.set_render_backend(0)}catch{}try{rt?.destroy()}catch{}rt=null,_t()}function Ce(){if(!(!Z||!st.gl||typeof G.set_render_backend!="function"))try{let e=st.lut==="dom"?()=>G.dom_lut_rgba():()=>G.ob_lut_rgba(),a=Qt(Z,{fetchLut:e,onContextLost:Se});if(!a.available){a.destroy();return}a.resize(Z.width,Z.height,nt),rt=a,G.set_render_backend(1)}catch(e){console.warn("[PanelEngine/worker] GL band unavailable, using Canvas2D:",e?.message||e),rt=null}}function kt(e,a){if(!(et||!G))try{jt(G,e,a),ut=!0}catch(o){o instanceof WebAssembly.RuntimeError&&Ft(o)}}async function De(e){if(st=Le[e.engine],!st)throw new Error(`unknown panel engine: ${e.engine}`);q=e.canvas,Z=e.gl||null,nt=e.dpr||1,wt=!!e.blocked;let[{module:a,memory:o}]=await Promise.all([Zt(),te(e.opts.fonts)]);Gt=o,K=q.getContext("2d"),K.setTransform(nt,0,0,nt,0,0);let m=a[e.engine];if(typeof m!="function")throw new Error(`engine missing in wasm: ${e.engine}`);G=new m(e.cssW,e.cssH);try{G.set_license_token?.(e.opts.licenseToken||"",Date.now(),e.opts.host||"")}catch(_){console.warn("[PanelEngine/worker] set_license_token failed:",_.message)}for(let[_,A]of e.opts.boot||[])kt(_,A);Ce(),wt&&ae(),ht("ready",{})}self.onmessage=e=>{let a=e.data;if(a)switch(a.t){case"init":De(a).catch(o=>Ft(o));return;case"call":kt(a.m,a.a),yt();return;case"calls":for(let[o,m]of a.batch)kt(o,m);yt();return;case"query":{let o=null;try{let m=G&&G[a.m];typeof m=="function"&&(o=m.apply(G,a.a||[]))}catch{o=null}ht("reply",{id:a.id,v:o});return}case"capture":{let o=ve();ht("reply",{id:a.id,v:o},o?[o]:[]);return}case"domVol":{let o=a.flat;if(!o)return;for(let m=0;m+2<o.length;m+=3)kt("push_dom_vol",[o[m],o[m+1],o[m+2]]);yt();return}case"click":{if(et||!G)return;let o=null;try{G.set_hover(a.x,a.y),o=st.clickHover(G)}catch{return}ut=!0,yt(),o&&ht("click",{hover:o});return}case"resize":{if(et||!G)return;nt=a.dpr||1;let o=q.width!==a.bufW||q.height!==a.bufH;q.width!==a.bufW&&(q.width=a.bufW),q.height!==a.bufH&&(q.height=a.bufH),K.setTransform(nt,0,0,nt,0,0),Z&&(Z.width!==a.bufW&&(Z.width=a.bufW),Z.height!==a.bufH&&(Z.height=a.bufH),rt?.resize(a.bufW,a.bufH,nt));try{G.resize(a.cssW,a.cssH)}catch(m){Ft(m);return}o&&Xt(),_t();return}case"life":if(a.v==="start")pt=!0,_t();else if(a.v==="stop")pt=!1,vt();else if(a.v==="destroy"){xt=!0,pt=!1,vt();try{rt?.destroy()}catch{}rt=null;try{G?.free()}catch{}G=null}return;case"vis":$t=!!a.v,$t?_t():vt();return;case"busy":Tt=!!a.v,Wt=Tt?Date.now():0,Tt||_t();return;case"heap":ht("heap",{bytes:Gt?.buffer?.byteLength||0});return;default:}};