From 06456c44a058363f2c00caa0ec095d92de3b7908 Mon Sep 17 00:00:00 2001 From: Alexander Gudev Date: Sat, 18 Jan 2025 15:34:53 +0200 Subject: [PATCH] demo_stars: use fold, give names --- examples/demo_stars.hs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/demo_stars.hs b/examples/demo_stars.hs index 6cc6bca..1293af4 100755 --- a/examples/demo_stars.hs +++ b/examples/demo_stars.hs @@ -30,12 +30,12 @@ main = reanimate $ scene $ do starAnimation :: Animation starAnimation = mkAnimation 10 $ \t -> let camZ = t * 4 - in withStrokeWidth 0 $ rotate (t * 360) $ mkGroup - [ translate (x / newZ) (y / newZ) $ dot (1 - newZ) - | (x, y, z) <- - reverse $ take nStars $ dropWhile (\(_, _, z) -> z < camZ) allStars - , let newZ = z - camZ - ] + dropInvisible = dropWhile $ \(_, _, z) -> z < camZ + placeDot (x, y, z) = + let newZ = z - camZ + in translate (x / newZ) (y / newZ) $ dot (1 - newZ) + starSVGs = map placeDot . reverse . take nStars . dropInvisible $ allStars + in withStrokeWidth 0 $ rotate (t * 360) $ mkGroup starSVGs where black = PixelRGB8 0x0 0x0 0x0 dot o = @@ -52,15 +52,15 @@ trails trailDur raw = mkAnimation (duration raw) $ \t -> in construct $ reverse [idx - trailFrames .. idx] where fps = 200 - construct [] = mkGroup [] - construct (x : xs) = mkGroup - [ withGroupOpacity (fromIntegral trailFrames / fromIntegral (trailFrames + 1)) - $ construct xs - , getFrame x - ] + + construct :: [Int] -> SVG + construct = foldl' go (mkGroup []) where + go acc idx = mkGroup $ [reduceOpacity acc, getFrame idx] + reduceOpacity = withGroupOpacity (fromIntegral trailFrames / fromIntegral (trailFrames + 1)) + getFrame idx = frames V.! (idx `mod` nFrames) + trailFrames = round (trailDur * fps) nFrames = round (duration raw * fps) - getFrame idx = frames V.! (idx `mod` nFrames) frames = V.fromList [ frameAt (fromIntegral i / fromIntegral nFrames * duration raw) raw | i <- [0 .. nFrames]