Skip to content

Commit

Permalink
feat: Use transparent background on animations
Browse files Browse the repository at this point in the history
Signed-off-by: George Thomas <[email protected]>
  • Loading branch information
georgefst committed Oct 26, 2023
1 parent 1e23eca commit ab8ea0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions primer/primer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ library
, exceptions >=0.10.4 && <0.11.0
, extra >=1.7.10 && <1.8.0
, generic-optics >=2.0 && <2.3.0
, JuicyPixels ^>=3.3.8
, list-t >=1.0 && <1.1.0
, logging-effect ^>=1.4
, mmorph ^>=1.2.0
Expand Down
30 changes: 20 additions & 10 deletions primer/src/Primer/Primitives.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@ module Primer.Primitives (

import Foreword hiding (rotate)

import Codec.Picture.ColorQuant (palettizeWithAlpha)
import Codec.Picture.Gif (
GifDisposalMethod (DisposalAny),
GifEncode (GifEncode),
GifLooping (LoopingForever),
encodeComplexGifImage,
)
import Control.Monad.Fresh (MonadFresh)
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.ByteString.Base64 qualified as B64
import Data.Data (Data)
import Data.Map qualified as M
import Diagrams.Backend.Rasterific (
GifLooping (LoopingForever),
defaultPaletteOptions,
rasterGif,
Options (RasterificOptions),
Rasterific (Rasterific),
)
import Diagrams.Prelude (
Diagram,
V2 (..),
circle,
deg,
Expand All @@ -47,6 +54,7 @@ import Diagrams.Prelude (
mkSizeSpec,
rect,
rectEnvelope,
renderDia,
rotate,
sRGB24,
translate,
Expand Down Expand Up @@ -336,7 +344,7 @@ primFunDef def args = case def of
Animate -> case args of
-- Note that `diagramExpr` is an expression of type `Picture` with one free variable, `time`.
[PrimCon () (PrimInt duration), Lam () time diagramExpr]
| Just frames <- traverse diagramAtTime [0 .. (duration * 100) `div` frameLength] ->
| Just (frames :: [Diagram Rasterific]) <- traverse diagramAtTime [0 .. (duration * 100) `div` frameLength] ->
Right
$ prim
$ PrimAnimation
Expand All @@ -347,14 +355,16 @@ primFunDef def args = case def of
-- for unrelated reasons (getting the `Bytestring` without dumping it to a file).
mempty
(decodeUtf8 . B64.encode . toS)
$ rasterGif @Double
(mkSizeSpec $ Just . fromInteger <$> V2 width height)
gifLooping
defaultPaletteOptions
$ encodeComplexGifImage
$ GifEncode (fromInteger width) (fromInteger height) Nothing Nothing gifLooping
$ flip palettizeWithAlpha DisposalAny
$ map
( (,fromInteger frameLength)
( (fromInteger frameLength,)
. renderDia
Rasterific
(RasterificOptions (mkSizeSpec $ Just . fromInteger <$> V2 width height))
. rectEnvelope
(fromInteger <$> mkP2 (width `div` 2) (height `div` 2))
(fromInteger <$> mkP2 (-width `div` 2) (-height `div` 2))
(fromInteger <$> V2 width height)
)
frames
Expand Down

0 comments on commit ab8ea0c

Please sign in to comment.