smilingbuddha
smilingbuddha

Reputation: 14660

How to create this simple gif animation with Diagrams

I am trying to understand how to use Diagrams library in Haskell.

Here is my attempt at making a simple gif animation which switches between two circles of radii 1 and 2. I tried to mimic what I saw on

Here is the code, I tried.

{-# LANGUAGE NoMonomorphismRestriction #-}
import Diagrams.Backend.SVG.CmdLine
import Diagrams.Prelude

delays = take 2 (repeat 3)

gif :: [(Diagram B, Int)]
gif = zip [circle 1, circle 2] delays

main = mainWith $ gif

But this fails to compile! The errors I get are

[1 of 1] Compiling Main             ( maze.hs, maze.o )

maze.hs:10:8:
    No instance for (Diagrams.Backend.CmdLine.Parseable
                       (Diagrams.Backend.CmdLine.MainOpts
                          [(QDiagram B V2 Double Any, Int)]))
      arising from a use of ‘mainWith’
    In the expression: mainWith
    In the expression: mainWith $ gif
    In an equation for ‘main’: main = mainWith $ gif

Where am I going wrong in the code above?

Upvotes: 2

Views: 914

Answers (2)

Johannes Riecken
Johannes Riecken

Reputation: 2515

I'll explain the other steps necessary, since this is one of the first search results for doing animations in diagrams. Diagrams rescales the viewport to the image size for each circle, so that circle 1 and circle 2 are equivalent. To solve this you can lay the circles on invisible squares of the same size as circle 2. Also the delays are given in 1/100 seconds. The correct code would then be:

{-# LANGUAGE NoMonomorphismRestriction #-}
import Diagrams.Backend.Cairo.CmdLine
import Diagrams.Prelude

gif :: [(Diagram B, Int)]
gif = map (\x -> (x # lc white <> square 4 # lw none,300)) [circle 1,circle 2]

main = mainWith gif

Upvotes: 3

madjar
madjar

Reputation: 12951

It took me a moment to figure it out, because the error message is not obvious at all, but it's a backend problem. The documentation states that the cairo backend can create animated gif. However, you are using the svg backend (which is unable to understand what [(Diagram B, Int)] is.

To solve this, make sure you have the diagrams-cairo package installed, and change the line

import Diagrams.Backend.SVG.CmdLine

to

import Diagrams.Backend.Cairo.CmdLine

Upvotes: 3

Related Questions