Hylograph.ForceEngine.WASM
- Package
- purescript-hylograph-simulation
- Repository
- afcondon/purescript-hylograph-simulation
WASM Force Engine
High-performance force simulation using Rust/WebAssembly. Provides 3-4x speedup over D3.js for large graphs (10,000+ nodes).
Usage:
import Hylograph.ForceEngine.WASM as WASM
main = do
-- Initialize WASM module (once at app startup)
WASM.initWasm "./pkg/force_kernel.js"
-- Create simulation
sim <- WASM.create 1000
-- Configure forces
WASM.configureManyBody sim { strength: -30.0, theta: 0.9, distanceMin: 1.0, distanceMax: 10000.0 }
WASM.configureLinks sim { distance: 30.0, strength: 1.0, iterations: 1 }
WASM.configureCenter sim { x: 0.0, y: 0.0, strength: 1.0 }
-- Set links
WASM.setLinks sim [0, 1, 1, 2, 2, 3] -- [source, target, source, target, ...]
-- Run simulation
WASM.tick sim
-- Get results
positions <- WASM.getPositions sim -- [x0, y0, x1, y1, ...]
#WASMSimulation Source
data WASMSimulationOpaque type for WASM simulation handle
#ManyBodyConfig Source
type ManyBodyConfig = { distanceMax :: Number, distanceMin :: Number, strength :: Number, theta :: Number }Many-body force configuration
#LinkConfig Source
type LinkConfig = { distance :: Number, iterations :: Int, strength :: Number }Link force configuration
#CenterConfig Source
type CenterConfig = { strength :: Number, x :: Number, y :: Number }Center force configuration
#isWasmReady Source
isWasmReady :: Effect BooleanCheck if WASM is initialized and ready
#create Source
create :: Int -> Effect WASMSimulationCreate a new WASM simulation with the given number of nodes. Nodes are initialized in a phyllotaxis (sunflower) pattern.
#free Source
free :: WASMSimulation -> Effect UnitFree WASM simulation memory. Call when done with simulation.
#setNodeCount Source
setNodeCount :: WASMSimulation -> Int -> Effect UnitResize simulation to new node count
#setPosition Source
setPosition :: WASMSimulation -> Int -> Number -> Number -> Effect UnitSet a single node's position
#setAllPositions Source
setAllPositions :: WASMSimulation -> Array Number -> Effect UnitSet all positions from flat array [x0, y0, x1, y1, ...]
#getPositions Source
getPositions :: WASMSimulation -> Effect (Array Number)Get all positions as flat array [x0, y0, x1, y1, ...]
#getPositionsAsObjects Source
getPositionsAsObjects :: WASMSimulation -> Effect (Array { x :: Number, y :: Number })Get positions as array of {x, y} objects
#setLinksFromRecords Source
setLinksFromRecords :: forall r. WASMSimulation -> Array { source :: Int, target :: Int | r } -> Effect UnitSet links from array of records with source/target fields
#configureManyBody Source
configureManyBody :: WASMSimulation -> ManyBodyConfig -> Effect UnitConfigure many-body (charge) force
#configureLinks Source
configureLinks :: WASMSimulation -> LinkConfig -> Effect UnitConfigure link (spring) force
#configureCenter Source
configureCenter :: WASMSimulation -> CenterConfig -> Effect UnitConfigure center force
#enableForces Source
enableForces :: WASMSimulation -> { center :: Boolean, links :: Boolean, manyBody :: Boolean } -> Effect UnitEnable/disable forces individually
#setAlpha Source
setAlpha :: WASMSimulation -> Number -> Effect UnitSet alpha (temperature). Higher = more movement.
#setAlphaDecay Source
setAlphaDecay :: WASMSimulation -> Number -> Effect UnitSet alpha decay rate
#setVelocityDecay Source
setVelocityDecay :: WASMSimulation -> Number -> Effect UnitSet velocity decay (friction). 0.6 means 40% decay per tick.
#tick Source
tick :: WASMSimulation -> Effect NumberRun a single simulation tick. Returns new alpha value.
#tickN Source
tickN :: WASMSimulation -> Int -> Effect NumberRun multiple ticks. Stops early if alpha < alphaMin.
#isRunning Source
isRunning :: WASMSimulation -> Effect BooleanCheck if simulation is still running (alpha >= alphaMin)
#syncPositionsToWasm Source
syncPositionsToWasm :: forall r. WASMSimulation -> Array { x :: Number, y :: Number | r } -> Effect UnitCopy positions from PureScript node array to WASM
- 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