From d4480bc12c8bce2548f03289a8e0bd12d9a279d1 Mon Sep 17 00:00:00 2001 From: kolaflash Date: Tue, 6 Jan 2026 18:29:18 +0100 Subject: [PATCH] Good news is it works, bad news is its costy --- dev/GraphBrowser.tsx | 50 ++++++++++++++++++++++++++------------------ engine/Scene.ts | 3 +++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/dev/GraphBrowser.tsx b/dev/GraphBrowser.tsx index 260e54d..f450267 100644 --- a/dev/GraphBrowser.tsx +++ b/dev/GraphBrowser.tsx @@ -47,7 +47,7 @@ function drawEdge( } function applyRepulsion(nodes: Array): void { - const strength = 8; + const strength = 4; nodes.forEach((node, index) => { for (let i = 0; i < nodes.length; i++) { @@ -75,7 +75,7 @@ function applyRepulsion(nodes: Array): void { function hookEdge([a, b]: Edge): void { const restLength = 48; - const stiffness = 0.1; + const stiffness = 0.2; const direction = createVector(); setDistanceNormal(direction, a.position, b.position); @@ -100,23 +100,20 @@ export const GraphBrowser: Component<{ const [width] = createSignal(300); const [height] = createSignal(300); - onMount(() => { - const ctx: CanvasRenderingContext2D = canvasRef.getContext("2d", { - alpha: false, - })!; - - const damping = 0.8; - - const nodes: Array> = [ - ...props.graph.keys(), - ].map((node: string) => ({ + const enhanceNode = (node: string) => { + return { name: node, position: createVector(Math.random(), Math.random()), velocity: createVector(), acceleration: createVector(), - })); + }; + }; - const edges: Array> = props.graph.entries().reduce( + const nodes: Array> = []; + + let edges: Array> = []; + const updateEdges = () => { + edges = props.graph.entries().reduce( (acc, [name, neighbours]) => { const node = nodes.find((n) => n.name === name)!; for (const neighbour of neighbours) { @@ -129,14 +126,27 @@ export const GraphBrowser: Component<{ }, [] as Array>, ); + }; - const activeNode = props.graph - .keys() - .find((name) => name === currentScene.data.name); - - const activeNeighbours = getNeighbours(props.graph, activeNode!); + onMount(() => { + const ctx: CanvasRenderingContext2D = canvasRef.getContext("2d", { + alpha: false, + })!; + const damping = 0.8; const animate = (): void => { + for (const node of props.graph.keys()) { + if (nodes.find((n) => n.name === node) === undefined) { + nodes.push(enhanceNode(node)); + updateEdges(); + } + } + + const activeNode = props.graph + .keys() + .find((name) => name === currentScene.data.name); + const activeNeighbours = getNeighbours(props.graph, activeNode!); + applyRepulsion(nodes); for (const edge of edges) { hookEdge(edge); @@ -177,7 +187,7 @@ export const GraphBrowser: Component<{ } } - requestAnimationFrame(animate); + setTimeout(animate, 100); }; animate(); diff --git a/engine/Scene.ts b/engine/Scene.ts index 4c2e59c..bc03167 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -149,6 +149,9 @@ export async function setScene(name: string): Promise { const entityInstanceMap = sceneEntiyMap.get(name)!; currentScene.data = data; + self.dispatchEvent(new Event("resize")); + currentScene.process = process; + currentScene.entityInstanceMap = entityInstanceMap; }