1

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: image

However, if I generate directly the diagram (without Reanimate):

main :: IO ()
main = mainWith $ bg white $ dia

I get this: image

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"

Gives: enter image description here

cdupont
  • 1,138
  • 10
  • 17
  • You should avoid using `lw`. Use `lwL` / `lwO` / etc. depending on what you want. – leftaroundabout Nov 15 '21 at 10:57
  • thanks @leftaroundabout. I think there is a problem with the overall diagram size/canvas size. Why is it so big? Why the figures are of-center and small? I think this messes with the calculation of the line widths and arrow widths. – cdupont Nov 15 '21 at 15:39
  • Yeah... I don't know why, haven't used Reanimate yet. In [`dynamic-plot`](https://hackage.haskell.org/package/dynamic-plot) your example shows up as you'd expect, but I remember well that it was bit of a pain to get the different measures right, y-orientation, and I never really felt confident I understood why which system was needed in what situation. – leftaroundabout Nov 15 '21 at 15:56

0 Answers0