Update tutorial.

Former-commit-id: f26d6e4164a5baf82671b8079981bfddc247a17c
This commit is contained in:
David Himmelstrup 2019-12-04 16:28:04 +08:00
commit e90f93a0e2
2 changed files with 115 additions and 7 deletions

View file

@ -82,9 +82,7 @@ The following examples shows how something as seemingly complicated as fourier s
<source src="https://github.com/Lemmih/reanimate/raw/master/docs/rendered/tut_glue_fourier.mp4">
</video>
TODO: Write about how lots of libraries are available for Haskell. Use chiphunk
as an example. Show SVG primitives with 2D physics.
Scripting in Haskell also gives access to the extensive body of code libraries. There are Haskell libraries for syntax highlighting, font manipulation, and much, much more. In the spirit of being a batteries-included framework, Reanimate ships with a built-in 2D physics library, called Chipmunk2D. The example below demonstrates how SVG shapes can be generated with LaTeX and then nearly effortlessly used in a physics simulation.
<details>
<summary>Toggle source code.</summary>
@ -101,10 +99,12 @@ TODO: Write about how lots of libraries are available for Haskell. Use chiphunk
## Pillar II: LaTeX
TODO: Show that LaTeX is a provider of SVG graphics. It has the type
'latex :: Text -> SVG'. Caching is automatic and it plays well with
other SVG functions (partialSvg, center, etc).
LaTeX is a widely used system for typesetting equations and documents. It is most commonly used by writing TeX documents which are then converted to pdfs. However, since the output of LaTeX is natively vector graphics, it is trivial to get SVG documents instead of pdfs. Armed with this knowledge, Reanimate offers a simple yet powerful function: `latex :: Text -> SVG`
The `latex` function takes a snippet of TeX code, passes it through the LaTeX system, and converts the result to an SVG image. Furthermore, since the result is entirely determined by the TeX code, caching is used to hide the overhead of invoking LaTeX.
The resulting SVGs can be manipulated just like any other. The below examples illustrates how different effects can be applied to different glyphs in the equation.
<details>
<summary>Toggle source code.</summary>
@ -120,6 +120,13 @@ TODO: Show that LaTeX is a provider of SVG graphics. It has the type
## Pillar III: potrace
## Pillar IV: Povray
## Pillar IV: povray
TODO: Draw text and animate shapes, zoom out so it looks like a piece of paper (still animated), draw circle, fade in rotating sphere.
## Pillar V: Blender
TODO: Crumble SVG animation.
TODO: Morph SVG animation into sphere.

101
examples/tut_glue_povray.hs Executable file
View file

@ -0,0 +1,101 @@
#!/usr/bin/env stack
-- stack runghc --package reanimate
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main (main) where
import Reanimate
import Reanimate.Povray
import Reanimate.Raster
import Data.String.Here
import Data.Text (Text)
main :: IO ()
main = reanimate $ mkAnimation 5 $ \t ->
let s = fromToS 1 4 t in
mkGroup
[ mkBackground "black"
, povray [] (script (svgAsPngFile texture) s) ]
where
texture :: SVG
texture = checker 10 10
script :: FilePath -> Double -> Text
script png s = [iTrim|
//EXAMPLE OF SPHERE
//Files with predefined colors and textures
#include "colors.inc"
#include "glass.inc"
#include "golds.inc"
#include "metals.inc"
#include "stones.inc"
#include "woods.inc"
#include "shapes3.inc"
//Place the camera
camera {
//orthographic
perspective
// angle 50
location <0,-${max 0 (((s**1.5)-1)/16)},-3>
look_at <0,0,0>
//right x*image_width/image_height
up <0,9,0>
right <16,0,0>
}
//Ambient light to "brighten up" darker pictures
global_settings { ambient_light White*3 }
//Set a background color
//background { color White }
//background { color rgbt <0.1, 0, 0, 0> } // red
background { color rgbt <0, 0, 0, 1> } // transparent
polygon {
4,
//<-9, -4.5>, <-9, 4.5>, <9, 4.5>, <9, -4.5>
<0, 0>, <0, 1>, <1.777, 1>, <1.777, 0>
texture {
//pigment{ color rgb <0,0,1> }
pigment{
image_map{ png ${png} }
}
}
translate <-1.777/2,-0.5>
scale 9
}
|]
checker :: Int -> Int -> SVG
checker w h =
withFillColor "white" $
withStrokeColor "white" $
withStrokeWidth 0.1 $
mkGroup
[ withStrokeWidth 0 $
withFillOpacity 1 $ mkBackground "blue"
, mkGroup
[ translate (stepX*x-offsetX + stepX/2) 0 $
mkLine (0, -screenHeight/2*0.9) (0, screenHeight/2*0.9)
| x <- map fromIntegral [0..w-1]
]
,
mkGroup
[ translate 0 (stepY*y-offsetY) $
mkLine (-screenWidth/2, 0) (screenWidth/2, 0)
| y <- map fromIntegral [0..h]
]
]
where
stepX = screenWidth/fromIntegral w
stepY = screenHeight/fromIntegral h
offsetX = screenWidth/2
offsetY = screenHeight/2