Browse Source

moved drag handlers to top level

master
limina1 8 months ago
parent
commit
2410ea3656
  1. 76
      src/lib/navigator/EventNetwork/utils/starForceSimulation.ts
  2. 2
      src/lib/navigator/EventNetwork/utils/starNetworkBuilder.ts

76
src/lib/navigator/EventNetwork/utils/starForceSimulation.ts

@ -226,35 +226,63 @@ export function applyInitialStarPositions(
} }
/** /**
* Custom drag handler for star networks * Handler for the start of a drag event in the star network simulation.
* Sets the fixed position of the node to its current position.
* @param event - The drag event from d3
* @param d - The node being dragged
* @param simulation - The d3 force simulation instance
*/ */
export function createStarDragHandler( function dragstarted(event: any, d: NetworkNode, simulation: Simulation<NetworkNode, NetworkLink>) {
simulation: Simulation<NetworkNode, NetworkLink> // If no other drag is active, set a low alpha target to keep the simulation running smoothly
): any { if (!event.active) {
function dragstarted(event: any, d: NetworkNode) { simulation.alphaTarget(0.1).restart();
if (!event.active) simulation.alphaTarget(0.1).restart(); // Lower target for smoother dragging
// For all nodes, set their fixed position at start
d.fx = d.x;
d.fy = d.y;
} }
// Set the node's fixed position to its current position
d.fx = d.x;
d.fy = d.y;
}
function dragged(event: any, d: NetworkNode) { /**
d.fx = event.x; * Handler for the drag event in the star network simulation.
d.fy = event.y; * Updates the node's fixed position to follow the mouse.
} * @param event - The drag event from d3
* @param d - The node being dragged
*/
function dragged(event: any, d: NetworkNode) {
// Update the node's fixed position to the current mouse position
d.fx = event.x;
d.fy = event.y;
}
function dragended(event: any, d: NetworkNode) { /**
if (!event.active) simulation.alphaTarget(0); * Handler for the end of a drag event in the star network simulation.
* Keeps the node fixed at its new position after dragging.
// Keep all nodes fixed after dragging * @param event - The drag event from d3
// This allows users to manually position any node type * @param d - The node being dragged
d.fx = event.x; * @param simulation - The d3 force simulation instance
d.fy = event.y; */
function dragended(event: any, d: NetworkNode, simulation: Simulation<NetworkNode, NetworkLink>) {
// If no other drag is active, lower the alpha target to let the simulation cool down
if (!event.active) {
simulation.alphaTarget(0);
} }
// Keep the node fixed at its new position
d.fx = event.x;
d.fy = event.y;
}
/**
* Custom drag handler for star networks
* @param simulation - The d3 force simulation instance
* @returns The d3 drag behavior
*/
export function createStarDragHandler(
simulation: Simulation<NetworkNode, NetworkLink>
): any {
// These handlers are now top-level functions, so we use closures to pass simulation to them.
// This is a common pattern in JavaScript/TypeScript when you need to pass extra arguments to event handlers.
return d3.drag() return d3.drag()
.on("start", dragstarted) .on('start', function(event: any, d: NetworkNode) { dragstarted(event, d, simulation); })
.on("drag", dragged) .on('drag', dragged)
.on("end", dragended); .on('end', function(event: any, d: NetworkNode) { dragended(event, d, simulation); });
} }

2
src/lib/navigator/EventNetwork/utils/starNetworkBuilder.ts

@ -66,7 +66,7 @@ export function createStarNetwork(
const node = state.nodeMap.get(id); const node = state.nodeMap.get(id);
if (node) { if (node) {
// Set the peripheral node level // Set the peripheral node level
node.level = level + 1; node.level += 1;
peripheralNodes.push(node); peripheralNodes.push(node);
// Create link from center to peripheral node // Create link from center to peripheral node

Loading…
Cancel
Save