pastel.codes/obsidian/lib/scripts/graph-view.js

1 line
15 KiB
JavaScript
Raw Normal View History

2024-04-29 14:52:09 +01:00
var running=!1;let pixiApp,graphRenderer,batchFraction=1,minBatchFraction=.3,dt=1,targetFPS=40,startingCameraRect={minX:-1,minY:-1,maxX:1,maxY:1},mouseWorldPos={x:void 0,y:void 0},scrollVelocity=0,averageFPS=2*targetFPS;class GraphAssembly{static nodeCount=0;static linkCount=0;static hoveredNode=-1;static#e=0;static#r=0;static#t=0;static#a=0;static#s=0;static linkSources=new Int32Array(0);static linkTargets=new Int32Array(0);static radii=new Float32Array(0);static maxRadius=0;static averageRadius=0;static minRadius=0;static init(e){GraphAssembly.nodeCount=e.nodeCount,GraphAssembly.linkCount=e.linkCount;let r=new Float32Array(2*GraphAssembly.nodeCount);GraphAssembly.radii=new Float32Array(e.radii),GraphAssembly.linkSources=new Int32Array(e.linkSources),GraphAssembly.linkTargets=new Int32Array(e.linkTargets),GraphAssembly.#e=Module._malloc(r.byteLength),GraphAssembly.#r=r.byteLength,GraphAssembly.#t=Module._malloc(GraphAssembly.radii.byteLength),GraphAssembly.#a=Module._malloc(GraphAssembly.linkSources.byteLength),GraphAssembly.#s=Module._malloc(GraphAssembly.linkTargets.byteLength),GraphAssembly.maxRadius=GraphAssembly.radii.reduce(((e,r)=>Math.max(e,r))),GraphAssembly.averageRadius=GraphAssembly.radii.reduce(((e,r)=>e+r))/GraphAssembly.radii.length,GraphAssembly.minRadius=GraphAssembly.radii.reduce(((e,r)=>Math.min(e,r))),r=this.loadState(),Module.HEAP32.set(new Int32Array(r.buffer),GraphAssembly.#e/r.BYTES_PER_ELEMENT),Module.HEAP32.set(new Int32Array(GraphAssembly.radii.buffer),GraphAssembly.#t/GraphAssembly.radii.BYTES_PER_ELEMENT),Module.HEAP32.set(new Int32Array(GraphAssembly.linkSources.buffer),GraphAssembly.#a/GraphAssembly.linkSources.BYTES_PER_ELEMENT),Module.HEAP32.set(new Int32Array(GraphAssembly.linkTargets.buffer),GraphAssembly.#s/GraphAssembly.linkTargets.BYTES_PER_ELEMENT),Module._Init(GraphAssembly.#e,GraphAssembly.#t,GraphAssembly.#a,GraphAssembly.#s,GraphAssembly.nodeCount,GraphAssembly.linkCount,batchFraction,dt,e.graphOptions.attractionForce,e.graphOptions.linkLength,e.graphOptions.repulsionForce,e.graphOptions.centralForce)}static get positions(){return Module.HEAP32.buffer.slice(GraphAssembly.#e,GraphAssembly.#e+GraphAssembly.#r)}static saveState(e){localStorage.setItem("positions",JSON.stringify(new Float32Array(GraphAssembly.positions).map((e=>Math.round(e)))))}static loadState(){let e=localStorage.getItem("positions"),r=null;if(e&&(r=new Float32Array(Object.values(JSON.parse(e)))),!r||!e||r.length!=2*GraphAssembly.nodeCount){r=new Float32Array(2*GraphAssembly.nodeCount);let e=GraphAssembly.averageRadius*Math.sqrt(GraphAssembly.nodeCount)*2;for(let t=0;t<GraphAssembly.nodeCount;t++){let a=(1-GraphAssembly.radii[t]/GraphAssembly.maxRadius)*e;r[2*t]=Math.cos(t/GraphAssembly.nodeCount*7.41*2*Math.PI)*a,r[2*t+1]=Math.sin(t/GraphAssembly.nodeCount*7.41*2*Math.PI)*a}}let t=1/0,a=-1/0,s=1/0,i=-1/0;for(let e=0;e<GraphAssembly.nodeCount-1;e+=2){let o={x:r[e],y:r[e+1]};t=Math.min(t,o.x),a=Math.max(a,o.x),s=Math.min(s,o.y),i=Math.max(i,o.y)}return startingCameraRect={minX:t-50,minY:s-50,maxX:a+50,maxY:i+50},r}static update(e,r,t){GraphAssembly.hoveredNode=Module._Update(e.x,e.y,r,t)}static free(){Module._free(GraphAssembly.#e),Module._free(GraphAssembly.#t),Module._free(GraphAssembly.#a),Module._free(GraphAssembly.#s),Module._FreeMemory()}static set batchFraction(e){Module._SetBatchFractionSize(e)}static set attractionForce(e){Module._SetAttractionForce(e)}static set repulsionForce(e){Module._SetRepulsionForce(e)}static set centralForce(e){Module._SetCentralForce(e)}static set linkLength(e){Module._SetLinkLength(e)}static set dt(e){Module._SetDt(e)}}class GraphRenderWorker{#i;#o;#n;#h;#d;#l;#c;constructor(){this.canvas=document.querySelector("#graph-canvas"),this.canvasSidebar=void 0;try{this.canvasSidebar=document.querySelector(".sidebar:has(#graph-canvas)")}catch(e){console.log("Error: "+e+"\n\n Using fallback.");let r=document.querySelector(".sidebar-right"),t=document.querySelector(".sidebar-left");this.canvasSidebar=r.querySelector("#graph-canvas")?r:t}this.view=this.canvas.transferControlToOffscreen()