Module

Hylograph.Transition.Consumers

Package
purescript-hylograph-simulation
Repository
afcondon/purescript-hylograph-simulation

Tick Consumers

Adapters that wrap various animation sources as TickConsumers for use with the Coordinator.

Supported:

  • TransitionState from Engine.purs
  • TransitionGroup from Engine.purs
  • D3 force simulation (via ForceEngine)

Documented (requires local FFI):

  • WASM force kernel (see pattern at bottom of file)

#transitionConsumer Source

transitionConsumer :: forall a. Ref (TransitionState a) -> (a -> Effect Unit) -> Milliseconds -> Effect TickResult

Create a tick function for a single transition.

Takes a Ref to the transition state and an effect to run on each tick with the current value. Returns Completed when the transition finishes.

Usage:

stateRef <- Ref.new (Engine.start mySpec)
let consumer = transitionConsumer stateRef \value -> setOpacity element value
C.register coord { tick: consumer, onComplete: log "done" }

#transitionGroupConsumer Source

transitionGroupConsumer :: Ref TransitionGroup -> (Array Number -> Effect Unit) -> Milliseconds -> Effect TickResult

Create a tick function for a group of coordinated transitions.

Takes a Ref to the group and an effect to run on each tick with all current values. Returns Completed when all transitions finish.

Usage:

groupRef <- Ref.new (Engine.group [spec1, spec2, spec3])
let consumer = transitionGroupConsumer groupRef \[x, y, opacity] -> do
      setPosition element x y
      setOpacity element opacity
C.register coord { tick: consumer, onComplete: log "all done" }

#simulationConsumer Source

simulationConsumer :: forall row linkRow. Simulation row linkRow -> Number -> (Effect Unit) -> Milliseconds -> Effect TickResult

Create a tick function for a D3 force simulation.

The simulation's tick function is called on each frame. Returns Converged with the alpha value, or Completed when alpha drops below threshold.

Important: Do NOT call FE.start on the simulation - the Coordinator owns the RAF loop. Just call FE.create, FE.setNodes, FE.addForce, etc.

Usage:

sim <- FE.create FE.defaultConfig
FE.setNodes myNodes sim
FE.addForce (FE.ManyBody "charge" FE.defaultManyBody) sim

C.register coord
  { tick: simulationConsumer sim 0.001 \nodes -> renderNodes nodes
  , onComplete: log "Simulation converged!"
  }

Re-exports from Hylograph.Transition.Coordinator

#TickResult Source

data TickResult

Result of a tick - tells coordinator what to do next

Constructors

#Milliseconds Source

type Milliseconds = Number

Time delta in milliseconds

Re-exports from Hylograph.Transition.Engine

#tick Source

tick :: forall a. Milliseconds -> TransitionState a -> TransitionState a

Advance a transition by a time delta (in milliseconds)

#start Source

start :: forall a. TransitionSpec a -> TransitionState a

Start a transition from the beginning

#isComplete Source

isComplete :: forall a. TransitionState a -> Boolean

Check if the transition is complete

#groupValues Source

groupValues :: TransitionGroup -> Array Number

Get current values from all transitions in group

#groupTick Source

groupTick :: Milliseconds -> TransitionGroup -> TransitionGroup

Tick all transitions in a group

#groupComplete Source

groupComplete :: TransitionGroup -> Boolean

Check if all transitions in group are complete

#group Source

group :: Array (TransitionSpec Number) -> TransitionGroup

Create a group from multiple transition specs

#currentValue Source

currentValue :: forall a. TransitionState a -> a

Get the current interpolated value