demo_stars: use fold, give names

This commit is contained in:
Alexander Gudev 2025-01-18 15:34:53 +02:00
commit 06456c44a0

View file

@ -30,12 +30,12 @@ main = reanimate $ scene $ do
starAnimation :: Animation starAnimation :: Animation
starAnimation = mkAnimation 10 $ \t -> starAnimation = mkAnimation 10 $ \t ->
let camZ = t * 4 let camZ = t * 4
in withStrokeWidth 0 $ rotate (t * 360) $ mkGroup dropInvisible = dropWhile $ \(_, _, z) -> z < camZ
[ translate (x / newZ) (y / newZ) $ dot (1 - newZ) placeDot (x, y, z) =
| (x, y, z) <- let newZ = z - camZ
reverse $ take nStars $ dropWhile (\(_, _, z) -> z < camZ) allStars in translate (x / newZ) (y / newZ) $ dot (1 - newZ)
, let newZ = z - camZ starSVGs = map placeDot . reverse . take nStars . dropInvisible $ allStars
] in withStrokeWidth 0 $ rotate (t * 360) $ mkGroup starSVGs
where where
black = PixelRGB8 0x0 0x0 0x0 black = PixelRGB8 0x0 0x0 0x0
dot o = dot o =
@ -52,15 +52,15 @@ trails trailDur raw = mkAnimation (duration raw) $ \t ->
in construct $ reverse [idx - trailFrames .. idx] in construct $ reverse [idx - trailFrames .. idx]
where where
fps = 200 fps = 200
construct [] = mkGroup []
construct (x : xs) = mkGroup construct :: [Int] -> SVG
[ withGroupOpacity (fromIntegral trailFrames / fromIntegral (trailFrames + 1)) construct = foldl' go (mkGroup []) where
$ construct xs go acc idx = mkGroup $ [reduceOpacity acc, getFrame idx]
, getFrame x reduceOpacity = withGroupOpacity (fromIntegral trailFrames / fromIntegral (trailFrames + 1))
] getFrame idx = frames V.! (idx `mod` nFrames)
trailFrames = round (trailDur * fps) trailFrames = round (trailDur * fps)
nFrames = round (duration raw * fps) nFrames = round (duration raw * fps)
getFrame idx = frames V.! (idx `mod` nFrames)
frames = V.fromList frames = V.fromList
[ frameAt (fromIntegral i / fromIntegral nFrames * duration raw) raw [ frameAt (fromIntegral i / fromIntegral nFrames * duration raw) raw
| i <- [0 .. nFrames] | i <- [0 .. nFrames]