Good news is it works, bad news is its costy

This commit is contained in:
kolaflash
2026-01-06 18:29:18 +01:00
parent c52b62cdaf
commit d4480bc12c
2 changed files with 33 additions and 20 deletions
+30 -20
View File
@@ -47,7 +47,7 @@ function drawEdge(
} }
function applyRepulsion(nodes: Array<PhysicsPartial>): void { function applyRepulsion(nodes: Array<PhysicsPartial>): void {
const strength = 8; const strength = 4;
nodes.forEach((node, index) => { nodes.forEach((node, index) => {
for (let i = 0; i < nodes.length; i++) { for (let i = 0; i < nodes.length; i++) {
@@ -75,7 +75,7 @@ function applyRepulsion(nodes: Array<PhysicsPartial>): void {
function hookEdge<T extends PhysicsPartial>([a, b]: Edge<T>): void { function hookEdge<T extends PhysicsPartial>([a, b]: Edge<T>): void {
const restLength = 48; const restLength = 48;
const stiffness = 0.1; const stiffness = 0.2;
const direction = createVector(); const direction = createVector();
setDistanceNormal(direction, a.position, b.position); setDistanceNormal(direction, a.position, b.position);
@@ -100,23 +100,20 @@ export const GraphBrowser: Component<{
const [width] = createSignal(300); const [width] = createSignal(300);
const [height] = createSignal(300); const [height] = createSignal(300);
onMount(() => { const enhanceNode = (node: string) => {
const ctx: CanvasRenderingContext2D = canvasRef.getContext("2d", { return {
alpha: false,
})!;
const damping = 0.8;
const nodes: Array<Node<PhysicsPartial & { name: string }>> = [
...props.graph.keys(),
].map((node: string) => ({
name: node, name: node,
position: createVector(Math.random(), Math.random()), position: createVector(Math.random(), Math.random()),
velocity: createVector(), velocity: createVector(),
acceleration: createVector(), acceleration: createVector(),
})); };
};
const edges: Array<Edge<PhysicsPartial>> = props.graph.entries().reduce( const nodes: Array<Node<PhysicsPartial & { name: string }>> = [];
let edges: Array<Edge<PhysicsPartial>> = [];
const updateEdges = () => {
edges = props.graph.entries().reduce(
(acc, [name, neighbours]) => { (acc, [name, neighbours]) => {
const node = nodes.find((n) => n.name === name)!; const node = nodes.find((n) => n.name === name)!;
for (const neighbour of neighbours) { for (const neighbour of neighbours) {
@@ -129,14 +126,27 @@ export const GraphBrowser: Component<{
}, },
[] as Array<Edge<PhysicsPartial>>, [] as Array<Edge<PhysicsPartial>>,
); );
};
const activeNode = props.graph onMount(() => {
.keys() const ctx: CanvasRenderingContext2D = canvasRef.getContext("2d", {
.find((name) => name === currentScene.data.name); alpha: false,
})!;
const activeNeighbours = getNeighbours(props.graph, activeNode!);
const damping = 0.8;
const animate = (): void => { 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); applyRepulsion(nodes);
for (const edge of edges) { for (const edge of edges) {
hookEdge(edge); hookEdge(edge);
@@ -177,7 +187,7 @@ export const GraphBrowser: Component<{
} }
} }
requestAnimationFrame(animate); setTimeout(animate, 100);
}; };
animate(); animate();
+3
View File
@@ -149,6 +149,9 @@ export async function setScene(name: string): Promise<void> {
const entityInstanceMap = sceneEntiyMap.get(name)!; const entityInstanceMap = sceneEntiyMap.get(name)!;
currentScene.data = data; currentScene.data = data;
self.dispatchEvent(new Event("resize"));
currentScene.process = process; currentScene.process = process;
currentScene.entityInstanceMap = entityInstanceMap; currentScene.entityInstanceMap = entityInstanceMap;
} }