This commit is contained in:
kolaflash
2026-01-05 21:29:14 +01:00
parent 2f634715c9
commit 5dd5f13c43
2 changed files with 29 additions and 9 deletions
+28 -8
View File
@@ -1,6 +1,11 @@
import "./GraphBrowser.css"; import "./GraphBrowser.css";
import { type Component, createSignal, onMount } from "solid-js"; import { type Component, createSignal, onMount } from "solid-js";
import type { Edge, Graph, Node } from "#/lib/Graph.ts"; import {
type Edge,
type Graph,
getNeighbours,
type Node,
} from "#/lib/Graph.ts";
import { import {
createVector, createVector,
getDistance, getDistance,
@@ -18,7 +23,7 @@ type PhysicsPartial = {
function drawNode( function drawNode(
node: Node<PhysicsPartial>, node: Node<PhysicsPartial>,
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
color: string = "#fff", color: string = "#dbdee1",
): void { ): void {
ctx.lineWidth = 2; ctx.lineWidth = 2;
ctx.strokeStyle = color; ctx.strokeStyle = color;
@@ -30,7 +35,7 @@ function drawNode(
function drawEdge( function drawEdge(
edge: Edge<PhysicsPartial>, edge: Edge<PhysicsPartial>,
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
color: string = "#fff", color: string = "#dbdee1",
): void { ): void {
ctx.lineWidth = 2; ctx.lineWidth = 2;
ctx.strokeStyle = color; ctx.strokeStyle = color;
@@ -77,7 +82,7 @@ function hookEdge<T extends PhysicsPartial>([a, b]: Edge<T>): void {
const distance = getDistance(a.position, b.position); const distance = getDistance(a.position, b.position);
const magnitude = (distance - restLength) * stiffness + Number.EPSILON; const magnitude = (distance - restLength) * stiffness + Number.EPSILON;
scale(direction, magnitude / 2); scale(direction, magnitude);
a.acceleration.x -= direction.x; a.acceleration.x -= direction.x;
a.acceleration.y -= direction.y; a.acceleration.y -= direction.y;
@@ -113,6 +118,9 @@ export const GraphBrowser: Component<{
const damping = 0.8; const damping = 0.8;
const activeNode = props.graph.keys().find((_, i) => i === 3);
const activeNeighbours = getNeighbours(props.graph, activeNode);
const animate = (): void => { const animate = (): void => {
applyRepulsion(nodes); applyRepulsion(nodes);
for (const edge of edges) { for (const edge of edges) {
@@ -138,12 +146,24 @@ export const GraphBrowser: Component<{
ctx.save(); ctx.save();
ctx.translate(width() / 2, height() / 2); ctx.translate(width() / 2, height() / 2);
for (const node of nodes) { for (const edge of edges) {
drawNode(node, ctx); drawEdge(edge, ctx, "#404249");
} }
for (const edge of edges) { for (const node of nodes) {
drawEdge(edge, ctx); if (node === activeNode) {
drawNode(node, ctx, "#ee459e");
} else {
if (
activeNeighbours.includes(
node as PhysicsPartial & { label: string },
)
) {
drawNode(node, ctx, "#6572f5");
} else {
drawNode(node, ctx, "#404249");
}
}
} }
requestAnimationFrame(animate); requestAnimationFrame(animate);
+1 -1
View File
@@ -48,7 +48,7 @@
} }
.item-list-name { .item-list-name {
padding-left: var(--sp-1); padding: 0 var(--sp-1);
cursor: pointer; cursor: pointer;
display: flex; display: flex;
align-items: center; align-items: center;