GP
Size: a a a
GP
ЗП
ЗП
GP
GP
ЗП
ЗП
ЗП
ЗП
zipEvent :: forall a b. E.Event a -> E.Event b -> E.Event (Tuple a b)
zipEvent e1 e2 = E.makeEvent \o -> do
latestA <- ST.liftST $ STArray.empty
latestB <- ST.liftST $ STArray.empty
emittedA <- Ref.new false
emittedB <- Ref.new false
c1 <- E.subscribe e1 \v1 -> do
_ <- ST.liftST $ STArray.push v1 latestA
Ref.write true emittedA
tryEmit o emittedA emittedB latestA latestB
c2 <- E.subscribe e2 \v2 -> do
_ <- ST.liftST $ STArray.push v2 latestB
Ref.write true emittedB
tryEmit o emittedA emittedB latestA latestB
pure (c1 *> c2)
where
tryEmit o emittedA emittedB latestA latestB = do
ev1 <- Ref.read emittedA
ev2 <- Ref.read emittedB
if ev1 && ev2
then do
mv1 <- ST.liftST $ STArray.shift latestA
mv2 <- ST.liftST $ STArray.shift latestB
case mv1, mv2 of
Just v1, Just v2 -> do
o (Tuple v1 v2)
Ref.write false emittedA
Ref.write false emittedB
_, _ -> pure unit
else
pure unit
GP
GP
GP
ЗП
ЗП
GP
eachFrame :: C.Context2D -> C.CanvasImageSource -> Effect Unit
eachFrame ctx img = E.subscribe event draw *> pure unit
where
event = EA.animationFrame *> updateScene
draw d = drawCharacter ctx img d
withImage :: String -> (C.CanvasImageSource -> Effect Unit) -> Effect Unit
withImage path f = C.tryLoadImage path $ \mimg -> case mimg of
Just img -> f img
Nothing -> throwException $ error ("Could not load image from path: " <> path)
initWithCanvas :: C.CanvasElement -> Effect Unit
initWithCanvas c = do
ctx <- C.getContext2D c
withImage character (eachFrame ctx)
pure unit
GP
GP
ЗП