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 ani = let d = duration ani
83 in mkAnimation d $ \t -> fn d (d*t) $ frameAt (d*t) ani
84
85 -- | Build an effect from an image-modifying function. This effect does not change as time passes.
86 constE :: (Tree -> Tree) -> Effect
87 constE fn _d _t = fn
88
89 -- | Change image opacity from 0 to 1.
90 fadeInE :: Effect
91 fadeInE d t = withGroupOpacity (t/d)
92
93 -- | Change image opacity from 1 to 0. Reverse of 'fadeInE'.
94 fadeOutE :: Effect
95 fadeOutE = reverseE fadeInE
96
97 -- | Change stroke width from 0 to given value.
98 fadeLineInE :: Double -> Effect
99 fadeLineInE w d t = withStrokeWidth (w*(t/d))
100
101 -- | Change stroke width from given value to 0. Reverse of 'fadeLineInE'.
102 fadeLineOutE :: Double -> Effect
103 fadeLineOutE = reverseE . fadeLineInE
104
105 -- | Effect of progressively drawing the image. Note that this will only affect primitive shapes (see 'pathify').
106 drawInE :: Effect
107 drawInE d t = withFillOpacity 0 . partialSvg (t/d) . pathify
108
109 -- | Reverse of 'drawInE'.
110 drawOutE :: Effect
111 drawOutE = reverseE drawInE
112
113 -- | Change fill opacity from 0 to 1.
114 fillInE :: Effect
115 fillInE d t = withFillOpacity f
116 where
117 f = t/d
118
119 -- | Change scale from 1 to given value.
120 scaleE :: Double -> Effect
121 scaleE target d t = scale (1 + (target-1) * t/d)
122
123 -- | Move the image from its current position to the target x y coordinates.
124 translateE :: Double -> Double -> Effect
125 translateE x y d t = translate (x * t/d) (y * t/d)
126
127 -- | Transform the effect so that the image passed to the effect's image-modifying
128 -- function has coordinates (0, 0) shifted to the center of its bounding box.
129 -- Also see 'aroundCenter'.
130 aroundCenterE :: Effect -> Effect
131 aroundCenterE e d t = aroundCenter (e d t)