Hylograph.ForceEngine.Core
- Package
- purescript-hylograph-simulation
- Repository
- afcondon/purescript-hylograph-simulation
Force Engine Core
The simulation loop's parts: forces, the position update, cooling, the animation frame and dragging.
The forces are hylograph-forces: d3-force's seven and the Rust
kernel's additions, in PureScript, with no D3 anywhere. A force is made in
two steps, because a simulation is usually described before its nodes
exist:
- a
ForceRecipesays what the force is (createManyBody config, orHylograph.ForceEngine.Setup.recipefor anything aSetupcan say); initializeForcegives it the nodes and links and returns aForceHandle, whichapplyForceapplies each tick.
Per-node and per-link values are read when the force is initialized, so
a force has to be initialized again (reinitializeForce) when the nodes,
the links, or anything those values are computed from have changed.
Coincident nodes are separated by a seeded generator (RandomSource),
one per simulation, so the same nodes, links and forces give the same
layout every time.
#ForceRecipe Source
newtype ForceRecipe :: Row Type -> Typenewtype ForceRecipe row
What a force is, before it has met its nodes: given the simulation's
generator, nodes and links, the force itself, or why it cannot be made
(a link naming a node that is not there: node not found: <id>).
Constructors
ForceRecipe (RandomSource -> Array (Node row) -> Links -> Either String (Force (NodeRow row)))
#ForceHandle Source
newtype ForceHandle :: Row Type -> Typenewtype ForceHandle row
A force that has its nodes, and the recipe it was made from, so that it can be made again when they change.
#RandomSource Source
type RandomSource = SourceThe seeded generator that separates coincident nodes: d3's own, started where d3 starts it.
#randomSource Source
randomSource :: Effect RandomSourceA fresh generator, for a new simulation.
#createManyBody Source
createManyBody :: forall row. ManyBodyConfig -> ForceRecipe rowA many-body (charge) force with one strength for every node.
#createCollide Source
createCollide :: forall row. CollideConfig -> ForceRecipe rowA collision force with one radius for every node.
#createLink Source
createLink :: forall row. LinkConfig -> ForceRecipe rowA link force with one distance and one strength for every link.
#createCenter Source
createCenter :: forall row. CenterConfig -> ForceRecipe rowA centering force.
#createForceX Source
createForceX :: forall row. ForceXConfig -> ForceRecipe rowA force pulling every node toward one x.
#createForceY Source
createForceY :: forall row. ForceYConfig -> ForceRecipe rowA force pulling every node toward one y.
#createRadial Source
createRadial :: forall row. RadialConfig -> ForceRecipe rowA force pulling every node toward one circle.
#createForceXGrid Source
createForceXGrid :: forall row. Number -> ForceRecipe (gridX :: Number | row)A force pulling each node toward its own gridX, read off the node when
the force is initialized.
#createForceYGrid Source
createForceYGrid :: forall row. Number -> ForceRecipe (gridY :: Number | row)A force pulling each node toward its own gridY.
#createCollideGrid Source
createCollideGrid :: forall row. Number -> Number -> Int -> ForceRecipe (r :: Number | row)A collision force whose radius is each node's own r plus a padding.
Arguments: padding, strength, iterations.
#initializeNodes Source
initializeNodes :: forall row. Array (Node row) -> Effect Unitd3's initializeNodes: give every node its index, put a pinned node on
its pin, stop a node with no velocity, and give a node with no position
a place on d3's phyllotaxis spiral. (Until 2026-11-18 a node with no
position was put somewhere at random.)
#initializeForce Source
initializeForce :: forall row. RandomSource -> Array (Node row) -> Links -> ForceRecipe row -> Either String (ForceHandle row)Make a force for these nodes and links.
#reinitializeForce Source
reinitializeForce :: forall row. RandomSource -> Array (Node row) -> Links -> ForceHandle row -> Either String (ForceHandle row)Make a force again, from the recipe it was first made from, for nodes or links that have changed.
#applyForce Source
applyForce :: forall row. ForceHandle row -> Number -> Effect UnitApply a single force: it changes the velocities (or, for centering, the positions) of the nodes it was initialized with.
#applyForces Source
applyForces :: forall row. Array (ForceHandle row) -> Number -> Effect UnitApply forces in order, each seeing what the ones before it did.
#simulationTick Source
simulationTick :: forall row. { alpha :: Number, alphaDecay :: Number, alphaMin :: Number, alphaTarget :: Number, forces :: Array (ForceHandle row), nodes :: Array (Node row), velocityRetention :: Number } -> Effect NumberA complete tick: apply the forces at the current alpha, move the nodes, apply hard containment, and return the cooled alpha.
#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 UnitDrag transformed group elements (like bubble packs)
Unlike attachDragWithReheat, this takes a container selector and reads
the pointer in that container's coordinates. Use it when dragging <g>
elements that have transform attributes. The node is the datum of the
nearest ancestor of what was pressed that has the class module-pack.
- Modules
- 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.
Simulation. Stepped - Hylograph.
Transition. Consumers - Hylograph.
Transition. Example