Hylograph.ForceEngine.Core
- Package
- purescript-hylograph-simulation
- Repository
- afcondon/purescript-hylograph-simulation
Pure Force Engine Core
This module provides the core force simulation loop. We use D3's force calculation algorithms but manage the simulation ourselves.
Key principle: Forces are just functions that mutate vx/vy on nodes. We control when they run and how alpha decays.
#ForceHandle Source
data ForceHandle#createManyBody Source
createManyBody :: ManyBodyConfig -> ForceHandleCreate a many-body (charge) force
#createCollide Source
createCollide :: CollideConfig -> ForceHandleCreate a collision force
#createLink Source
createLink :: LinkConfig -> ForceHandleCreate a link force
#createCenter Source
createCenter :: CenterConfig -> ForceHandleCreate a centering force
#createForceX Source
createForceX :: ForceXConfig -> ForceHandleCreate an X positioning force
#createForceY Source
createForceY :: ForceYConfig -> ForceHandleCreate a Y positioning force
#createRadial Source
createRadial :: RadialConfig -> ForceHandleCreate a radial force
#createManyBodyFiltered Source
createManyBodyFiltered :: forall node. ManyBodyFilteredConfig node -> ForceHandleCreate a many-body force that only applies to nodes matching a predicate Useful for applying charge only to certain node types (e.g., tree parents)
#createRadialFiltered Source
createRadialFiltered :: forall node. RadialFilteredConfig node -> ForceHandleCreate a radial force that only applies to nodes matching a predicate
#createCollideDynamic Source
createCollideDynamic :: forall node. CollideDynamicConfig node -> ForceHandleCreate a collision force with dynamic radius per-node
The radiusAccessor function is called for each node to determine collision radius
Example: { radiusAccessor: \n -> n.r + 5.0, strength: 1.0, iterations: 1 }
#createForceXDynamic Source
createForceXDynamic :: forall node. ForceXDynamicConfig node -> ForceHandleCreate an X positioning force with dynamic target per-node The xAccessor function is called for each node to determine target X Useful for clustering (e.g., modules toward their parent package's X)
#createForceYDynamic Source
createForceYDynamic :: forall node. ForceYDynamicConfig node -> ForceHandleCreate a Y positioning force with dynamic target per-node
#createLinkDynamic Source
createLinkDynamic :: forall link. LinkDynamicConfig link -> ForceHandleCreate a link force with dynamic strength per-link
The strengthAccessor function is called for each link to determine strength
Useful for encoding link weight as force strength
Example: { distance: 30.0, strengthAccessor: \link -> link.weight, iterations: 1 }
#createForceXGrid Source
createForceXGrid :: Number -> ForceHandleCreate an X positioning force that reads node.gridX directly
Much faster than createForceXDynamic because it avoids FFI callbacks.
Nodes must have a gridX :: Number field.
#createForceYGrid Source
createForceYGrid :: Number -> ForceHandleCreate a Y positioning force that reads node.gridY directly
Much faster than createForceYDynamic because it avoids FFI callbacks.
Nodes must have a gridY :: Number field.
#createCollideGrid Source
createCollideGrid :: Number -> Number -> Int -> ForceHandleCreate a collision force that reads node.r directly
Much faster than createCollideDynamic because it avoids FFI callbacks.
Nodes must have an r :: Number field (radius).
Parameters: padding, strength, iterations
#initializeNodes Source
initializeNodes :: forall r. Array (Record r) -> Effect UnitInitialize nodes with indices and default velocities This mutates the nodes array to add index, vx, vy if missing
#initializeForce Source
initializeForce :: forall r. ForceHandle -> Array (Record r) -> Effect ForceHandleInitialize a force with nodes Must be called before applying the force
#initializeLinkForce Source
initializeLinkForce :: forall nodeRow linkRow. ForceHandle -> Array (Record nodeRow) -> Array (Record linkRow) -> Effect ForceHandleInitialize a link force with nodes and links Link forces need both nodes and links
#applyForce Source
applyForce :: ForceHandle -> Number -> Effect UnitApply a single force This mutates vx/vy on the nodes the force was initialized with
#applyForces Source
applyForces :: Array ForceHandle -> Number -> Effect UnitApply multiple forces in sequence
#AnimationHandle Source
type AnimationHandle = Effect UnitHandle to a running animation (can be used to stop it)
#startAnimation Source
startAnimation :: (Number -> Effect Boolean) -> Effect AnimationHandleStart an animation loop The callback receives the current timestamp and should return whether to continue
#stopAnimation Source
stopAnimation :: AnimationHandle -> Effect UnitStop a running animation
#attachGroupDragWithReheat Source
attachGroupDragWithReheat :: Array Element -> String -> Effect Unit -> Effect UnitAttach drag to transformed group elements (like bubble packs)
Unlike attachDragWithReheat, this version takes a container selector to
get pointer coordinates in the correct coordinate space. Use this when
dragging <g> elements that have transform attributes.
Example:
-- For bubble packs inside a zoom group
attachGroupDragWithReheat packElements "#zoom-group" (Sim.reheat sim)
#attachPinningDrag Source
attachPinningDrag :: Array Element -> Effect Unit -> Effect UnitAttach drag with toggle-pinning behavior
Unlike attachDragWithReheat, this version keeps nodes pinned after drag ends.
- First drag: pins the node where you drop it
- Subsequent drags: if you barely move (< 3px), unpins the node
This is useful for allowing users to "fix" certain nodes in place while letting others float freely in the simulation.
Example:
nodeElements <- getElementsByClassName "node"
attachPinningDrag nodeElements (Sim.reheat sim)
#querySelectorElements Source
querySelectorElements :: String -> Effect (Array Element)Query DOM elements by CSS selector
Returns all elements matching the given CSS selector. Useful for getting element references for drag behavior attachment.
Example:
nodeCircles <- querySelectorElements "#my-graph circle.node"
attachPinningDrag nodeCircles (Sim.reheat sim)
- Modules
- Hylograph.
Config. Apply - Hylograph.
Config. Force - Hylograph.
Config. Scene - Hylograph.
ForceEngine - Hylograph.
ForceEngine. Core - Hylograph.
ForceEngine. Demo - Hylograph.
ForceEngine. Events - Hylograph.
ForceEngine. Links - Hylograph.
ForceEngine. Registry - Hylograph.
ForceEngine. Render - Hylograph.
ForceEngine. Setup - Hylograph.
ForceEngine. Setup. WASM - Hylograph.
ForceEngine. Simulation - Hylograph.
ForceEngine. Types - Hylograph.
ForceEngine. WASM - Hylograph.
ForceEngine. WASMEngine - Hylograph.
Scene. Engine - Hylograph.
Scene. Handle - Hylograph.
Scene. Rules - Hylograph.
Scene. Types - Hylograph.
Simulation - Hylograph.
Simulation. Emitter - Hylograph.
Simulation. HATS - Hylograph.
Simulation. Scene - Hylograph.
Transition. Consumers - Hylograph.
Transition. Example