-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRaster.hs
115 lines (102 loc) · 3.79 KB
/
Raster.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module Main where
import Codec.FFmpeg
import Codec.Picture
-- import Codec.Picture.Types (dropTransparency)
import Control.Monad (forM_)
import Graphics.Rasterific
import Graphics.Rasterific.Linear
import Graphics.Rasterific.Texture
import Graphics.Rasterific.Transformations
-- | The Rasterific logo sample shape.
logo :: Int -> Bool -> Vector -> [Primitive]
logo size inv offset = map BezierPrim . bezierFromPath . way $ map (^+^ offset)
[ (V2 0 is)
, (V2 0 0)
, (V2 is 0)
, (V2 is2 0)
, (V2 is2 is)
, (V2 is2 is2)
, (V2 is is2)
, (V2 0 is2)
, (V2 0 is)
]
where is = fromIntegral size
is2 = is + is
way | inv = reverse
| otherwise = id
-- | Sample a quadratic bezier curve.
bezierInterp :: Bezier -> [Point]
bezierInterp (Bezier a b c) = go 0
where v1 = b - a
v2 = c - b
go t
| t >= 1 = []
| otherwise = let q0 = a + v1 ^* t
q1 = b + v2 ^* t
vq = q1 - q0
in q0 + vq ^* t : (go $! t + 0.05)
-- | Our animation path.
path :: [Point]
path = concatMap bezierInterp $
bezierFromPath [ (V2 0 is)
, (V2 0 0)
, (V2 (is+5) 0)
, (V2 (is2+10) 0)
, (V2 (is2+10) is)
, (V2 (is2+10) is2)
, (V2 (is+5) is2)
, (V2 0 is2)
, (V2 0 is)
]
where is = 15
is2 = is + is
background, blue :: PixelRGBA8
background = PixelRGBA8 128 128 128 255
blue = PixelRGBA8 0 020 150 255
-- `fgSize` will determine our image size. `bgSize` is smaller so we
-- see the effect of the `SamplerRepeat` sampler.
fgSize, fgScale, bgSize :: Float
fgSize = 350
fgScale = fgSize / 100
bgSize = 57 * fgScale
fgSizei :: Integral a => a
fgSizei = floor fgSize
-- | A ring with a drop-shadow on the inside. The texture is repeated,
-- resulting in concentric rings centered at @(200,200)@.
bgGrad :: Texture PixelRGBA8
bgGrad = withSampler SamplerRepeat $
radialGradientTexture gradDef (V2 bgSize bgSize) (bgSize * 0.5)
where gradDef = [(0 , PixelRGBA8 255 255 255 255)
,(0.5, PixelRGBA8 255 255 255 255)
,(0.5, PixelRGBA8 255 255 255 255)
,(0.525, PixelRGBA8 255 255 255 255)
,(0.675, PixelRGBA8 128 128 128 255)
,(0.75, PixelRGBA8 100 149 237 255)
,(1, PixelRGBA8 100 149 237 255)
]
-- | Adapted from the Rasterific logo example.
logoTest :: Texture PixelRGBA8 -> Vector -> Image PixelRGBA8
logoTest texture insetOrigin =
renderDrawing fgSizei fgSizei background (bg >> drawing)
where
beziers = logo 40 False $ V2 10 10
inverse = logo 20 True $ (V2 20 20 + insetOrigin)
bg = withTexture bgGrad . fill $ rectangle (V2 0 0) fgSize fgSize
drawing = withTexture texture . fill
. transform (applyTransformation $ scale fgScale fgScale)
$ beziers ++ inverse
-- | Animate the logo and write it to a video file!
main :: IO ()
main = do initFFmpeg
-- Change the output file extension to ".gif" and drop
-- transparency to get an animated gif! We can get a small
-- GIF file by setting 'epPixelFormat' to 'avPixFmtRgb8',
-- but it might not look very good.
w <- imageWriter params "logo.mov"
-- w <- (. fmap (pixelMap dropTransparency))
-- `fmap` imageWriter params "logo.gif"
forM_ path $ w . Just . logoTest (uniformTexture blue)
w Nothing
where params = defaultParams fgSizei fgSizei
-- tinyGif = params { epPixelFormat = Just avPixFmtRgb8 }
-- prettyGif = params { epPreset = "dither" }