I'm trying to create an animation out of a Haskell Diagram. I'm using Reanimate for this. I tried this code:
{-# LANGUAGE OverloadedStrings #-}
module Reanimate.Diagrams
( renderDiagram
) where
import Data.Text.Lazy (toStrict)
import Diagrams.Backend.SVG (SVG (SVG), Options (SVGOptions))
import Diagrams.Core.Types (Diagram)
import Diagrams.Core.Compile (renderDia)
import Diagrams.Size (absolute)
import Graphics.Svg.Core (renderText)
import Graphics.SvgTree (Tree, parseSvgFile)
import Reanimate.Svg.Unuse (unbox)
import Data.Maybe (maybe)
renderDiagram :: Diagram SVG -> Tree
renderDiagram d =
let opts = SVGOptions absolute Nothing "" [] False
e = renderDia SVG opts d
t = toStrict $ renderText e
err = error "Malformed SVG"
in maybe err unbox (parseSvgFile "" t)
and:
module Main where
import Diagrams.Core.Types (Diagram)
import Diagrams.Backend.SVG (B)
import Diagrams.TwoD.Ellipse (circle)
import Reanimate (addStatic, mkBackground, reanimate, staticFrame)
import Reanimate.Diagrams (renderDiagram)
main :: IO ()
main = reanimate $ addStatic (mkBackground "cyan")
$ staticFrame 1 $ renderDiagram dia
dia :: Diagram B
dia = circle 1 # lw 0.1 <> square 1 # lw 0.1 # (moveTo $ p2 (1, 1))
I get this:
However, if I generate directly the diagram (without Reanimate):
main :: IO ()
main = mainWith $ bg white $ dia
I get this:
There is still a problem of line width... The diagram also seems to be flipped vertically. Thanks
EDIT: adding arrows also gives problems.
dia :: Diagram B
dia = (circle 1 # lw 0.01 # named "circle"
<> square 1 # lw 0.01 # (moveTo $ p2 (1, 1)) # named "square")
# connect "circle" "square"