Halogen can describe canvas scenes using the same approach as HTML. Halogen.Canvas.Elements (CE) and Halogen.Canvas.Properties (CP) provide shapes, groups and handlers such as CE.rectangle_, CE.circle_, CE.keyedGroup and CP.onClick.
These functions build a VDom tree, reconciled by the same code as HTML. The haskell-halogen-pixi backend turns its nodes into PixiJS display objects.
The scene
scene converts state into a Pixi.View, containing the camera, interaction settings and scene nodes. Pixi.defaultInteraction enables drag-to-pan and scroll-to-zoom. Pixi.defaultCamera places the origin at the canvas centre.
Shapes are positioned by their centres. CE.rectangle_ (Pixi.Point 0 0) (Pixi.Point 320 200) ... creates the background, and the circle's horizontal position comes from st.x.
The child Pixi.component, mounted in the "canvas" slot, receives the scene as input. Updates patch only changed properties. Moving the ball redraws its shape without rebuilding the rest. Pads use their numbers as keys in CE.keyedGroup, preserving their display objects across reordering.
CP.onClick (\_ -> Just i) makes the canvas emit Pixi.Fired i, received by the parent as Canvas (Pixi.Fired i). Clicking a pad moves the ball to it. Camera changes also produce outputs, which this example ignores.
PixiJS loads from a CDN when the first canvas mounts, so the scene may appear shortly after compilation finishes.
Exercise
Make the ball glide instead of jumping:
Change the click handler to update only target.
Add startFrames to H.onInitialize. Subscribe with void (H.subscribe (Frame <$ Window.animationFrame)), importing Halogen.Query.Event.Window qualified as Window.
On each Frame, move x six pixels toward target, without overshooting. Read state with get and call put only when (st.x /= st.target). Once the ball arrives, unchanged frames no longer trigger renders.
Lesson.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
module Lesson (component) where
import Clay qualified as C
import Control.Monad.State (modify)
import Control.Monad.UUID (MonadUUID)
import Data.Row (type (.==))
import Data.Text qualified as T
import Halogen qualified as H
import Halogen.Canvas.Elements qualified as CE
import Halogen.Canvas.Pixi qualified as Pixi
import Halogen.Canvas.Properties qualified as CP
import Halogen.HTML qualified as HH
import Halogen.HTML.Properties qualified as HP
import UnliftIO (MonadUnliftIO)
type Slots = "canvas" .== H.Slot H.VoidF (Pixi.CanvasEvent Int) ()
data State = State {x :: Double, target :: Double}
newtype Action = Canvas (Pixi.CanvasEvent Int)
-- Where pad i is: three pads 100 pixels apart, the middle one at the origin.
padX :: Int -> Double
padX i = fromIntegral (i - 1) * 100
-- The nodes are a VDom tree, as a render's HTML is, of Pixi display objects.
scene :: State -> Pixi.View Int
scene st =
Pixi.view
Pixi.defaultCamera
Pixi.defaultInteraction
[ CE.rectangle_ (Pixi.Point 0 0) (Pixi.Point 320 200) (Just (Pixi.FillStyle 0x1e293b 1)) Nothing
, CE.keyedGroup [] [(T.pack (show i), pad i) | i <- [0, 1, 2]]
, CE.circle_ (Pixi.Point st.x (-30)) 18 (Just (Pixi.FillStyle 0xf59e0b 1)) Nothing
]
where
pad i = CE.rectangle (Pixi.Point (padX i) 60) (Pixi.Point 70 30) (Just (Pixi.FillStyle 0x38bdf8 1)) Nothing [CP.onClick (\_ -> Just i)]
component :: forall q o m. (MonadUnliftIO m, MonadUUID m) => H.Component q () o m
component =
H.component initialState render [H.onAction handleAction]
where
initialState _ = pure State {x = padX 1, target = padX 1}
render :: State -> H.ComponentHTML Action Slots m
render st =
HH.div_
[ HH.div
[HP.style (C.position C.relative >> C.width (C.px 320) >> C.height (C.px 200))]
[HH.slot "canvas" () Pixi.component (scene st) Canvas]
, HH.p_ [HH.text ("The ball is at x = " <> T.pack (show st.x) <> ". Click a pad.")]
]
handleAction (Canvas (Pixi.Fired i)) = modify (\st -> st {x = padX i, target = padX i})
handleAction (Canvas _) = pure ()
The compiler loads once the page has started.
Your component will appear here after compilation.