never executed always true always false
1 {-| Effects represent modifications applied to frames of the 'Animation'.
2 Effects can (and usually do) depend on time.
3 One or more effects can be applied over the entire duration of animation, or modified to affect
4 only a specific portion at the beginning \/ middle \/ end of the animation.
5 -}
6 module Reanimate.Effect
7 ( -- * Primitive Effects
8 Effect
9 , fadeInE
10 , fadeOutE
11 , fadeLineInE
12 , fadeLineOutE
13 , fillInE
14 , drawInE
15 , drawOutE
16 , translateE
17 , scaleE
18 , constE
19 -- * Modifying Effects
20 , overBeginning
21 , overEnding
22 , overInterval
23 , reverseE
24 , delayE
25 , aroundCenterE
26 -- * Applying Effects to Animations
27 , applyE
28 ) where
29
30 import Graphics.SvgTree (Tree)
31 import Reanimate.Animation
32 import Reanimate.Svg
33
34 -- | An Effect represents a modification of a SVG 'Tree' that can vary with time.
35 type Effect = Duration -- ^ Duration of the effect (in seconds)
36 -> Time -- ^ Time elapsed from when the effect started (in seconds)
37 -> Tree -- ^ Image to be modified
38 -> Tree -- ^ Image after modification
39
40 -- | Modify the effect so that it only applies to the initial part of the animation.
41 overBeginning :: Duration -- ^ Duration of the initial segment of the animation over which the Effect should be applied
42 -> Effect -- ^ The Effect to modify
43 -> Effect -- ^ Effect which will only affect the initial segment of the animation
44 overBeginning maxT effect _d t =
45 if t < maxT
46 then effect maxT t
47 else id
48
49 -- | Modify the effect so that it only applies to the ending part of the animation.
50 overEnding :: Duration -- ^ Duration of the ending segment of the animation over which the Effect should be applied
51 -> Effect -- ^ The Effect to modify
52 -> Effect -- ^ Effect which will only affect the ending segment of the animation
53 overEnding minT effect d t =
54 if t >= blankDur
55 then effect minT (t-blankDur)
56 else id
57 where
58 blankDur = d-minT
59
60 -- | Modify the effect so that it only applies within given interval of animation's running time.
61 overInterval :: Time -- ^ time after start of animation when the effect should start
62 -> Time -- ^ time after start of the animation when the effect should finish
63 -> Effect -- ^ The Effect to modify
64 -> Effect -- ^ Effect which will only affect the specified interval within the animation
65 overInterval start end effect _d t =
66 if start <= t && t <= end
67 then effect dur ((t - start) / dur)
68 else id
69 where
70 dur = end - start
71
72 -- | @reverseE effect@ starts where the @effect@ ends and vice versa.
73 reverseE :: Effect -> Effect
74 reverseE fn d t = fn d (d-t)
75
76 -- | Delay the effect so that it only starts after specified duration and then runs till the end of animation.
77 delayE :: Duration -> Effect -> Effect
78 delayE delayT fn d = overEnding (d-delayT) fn d
79
80 -- | Modify the animation by applying the effect. If desired, you can apply multiple effects to single animation by calling this function multiple times.
81 applyE :: Effect -> Animation -> Animation
82 applyE fn (Animation d genFrame) = Animation d $ \t -> fn d (d*t) $ genFrame t
83
84 -- | Build an effect from an image-modifying function. This effect does not change as time passes.
85 constE :: (Tree -> Tree) -> Effect
86 constE fn _d _t = fn
87
88 -- | Change image opacity from 0 to 1.
89 fadeInE :: Effect
90 fadeInE d t = withGroupOpacity (t/d)
91
92 -- | Change image opacity from 1 to 0. Reverse of 'fadeInE'.
93 fadeOutE :: Effect
94 fadeOutE = reverseE fadeInE
95
96 -- | Change stroke width from 0 to given value.
97 fadeLineInE :: Double -> Effect
98 fadeLineInE w d t = withStrokeWidth (w*(t/d))
99
100 -- | Change stroke width from given value to 0. Reverse of 'fadeLineInE'.
101 fadeLineOutE :: Double -> Effect
102 fadeLineOutE = reverseE . fadeLineInE
103
104 -- | Effect of progressively drawing the image. Note that this will only affect primitive shapes (see 'pathify').
105 drawInE :: Effect
106 drawInE d t = withFillOpacity 0 . partialSvg (t/d) . pathify
107
108 -- | Reverse of 'drawInE'.
109 drawOutE :: Effect
110 drawOutE = reverseE drawInE
111
112 -- | Change fill opacity from 0 to 1.
113 fillInE :: Effect
114 fillInE d t = withFillOpacity f
115 where
116 f = t/d
117
118 -- | Change scale from 1 to given value.
119 scaleE :: Double -> Effect
120 scaleE target d t = scale (1 + (target-1) * t/d)
121
122 -- | Move the image from its current position to the target x y coordinates.
123 translateE :: Double -> Double -> Effect
124 translateE x y d t = translate (x * t/d) (y * t/d)
125
126 -- | Transform the effect so that the image passed to the effect's image-modifying
127 -- function has coordinates (0, 0) shifted to the center of its bounding box.
128 -- Also see 'aroundCenter'.
129 aroundCenterE :: Effect -> Effect
130 aroundCenterE e d t = aroundCenter (e d t)