@@ -28,6 +28,8 @@ type Option
2828 = Iteration Iteration
2929 | Delay Millis
3030 | Ease Ease
31+ | Yoyo
32+ | Reverse
3133
3234
3335type Frame
@@ -47,6 +49,21 @@ type alias Millis =
4749 Unit . Millis
4850
4951
52+ type alias Options =
53+ { delay : Maybe Millis
54+ , timingFunction : Maybe Ease
55+ , iteration : Maybe Iteration
56+ , isYoyo : Bool
57+ , reversed : Bool
58+ }
59+
60+
61+ type Direction
62+ = Alternate_
63+ | Reverse_
64+ | AlternateReverse_
65+
66+
5067
5168-- Render
5269
@@ -92,25 +109,54 @@ renderFrame (Frame percent properties) =
92109
93110renderOptions : Animation -> List String
94111renderOptions =
95- options_ >> List . concatMap renderOption
112+ options_ >> renderOptions_ >> List . filterMap identity
96113
97114
98115animationDuration : Animation -> String
99116animationDuration anim =
100117 Unit . ms ( duration_ anim)
101118
102119
103- renderOption : Option -> List String
104- renderOption o =
105- case o of
106- Delay n ->
107- [ " animation-delay: " ++ Unit . ms n ]
120+ renderOptions_ : Options -> List (Maybe String )
121+ renderOptions_ opts =
122+ [ renderOption " animation-delay" Unit . ms opts. delay
123+ , renderOption " animation-timing-function" Ease . toString opts. timingFunction
124+ , renderOption " animation-iteration-count" renderIteration opts. iteration
125+ , renderOption " animation-direction" renderDirection ( toDirection opts)
126+ ]
108127
109- Ease e ->
110- [ " animation-timing-function: " ++ Ease . toString e ]
111128
112- Iteration i ->
113- [ " animation-iteration-count: " ++ renderIteration i ]
129+ toDirection : Options -> Maybe Direction
130+ toDirection opts =
131+ if opts. isYoyo && opts. reversed then
132+ Just AlternateReverse_
133+
134+ else if opts. reversed then
135+ Just Reverse_
136+
137+ else if opts. isYoyo then
138+ Just Alternate_
139+
140+ else
141+ Nothing
142+
143+
144+ renderOption : String -> (a -> String ) -> Maybe a -> Maybe String
145+ renderOption name toProp =
146+ Maybe . map ( \ x -> name ++ " : " ++ toProp x)
147+
148+
149+ renderDirection : Direction -> String
150+ renderDirection d =
151+ case d of
152+ Alternate_ ->
153+ " alternate"
154+
155+ Reverse_ ->
156+ " reverse"
157+
158+ AlternateReverse_ ->
159+ " alternate-reverse"
114160
115161
116162renderIteration : Iteration -> String
@@ -154,6 +200,12 @@ optionName o =
154200 Iteration i ->
155201 iterationName i
156202
203+ Yoyo ->
204+ " yoyo"
205+
206+ Reverse ->
207+ " rev"
208+
157209
158210frameName : Frame -> String
159211frameName ( Frame dur props) =
@@ -179,8 +231,66 @@ iterationName i =
179231-- Helpers
180232
181233
182- options_ : Animation -> List Option
183- options_ ( Animation _ o _) =
234+ options_ : Animation -> Options
235+ options_ =
236+ rawOptions_
237+ >> List . foldl collectOption defaults
238+ >> normalise
239+
240+
241+ normalise : Options -> Options
242+ normalise opts =
243+ if opts. isYoyo then
244+ { opts | iteration = Just ( iterationForYoyo opts) }
245+
246+ else
247+ opts
248+
249+
250+ iterationForYoyo : Options -> Iteration
251+ iterationForYoyo opts =
252+ case opts. iteration of
253+ Just Loop ->
254+ Loop
255+
256+ Just ( Count n) ->
257+ Count ( n * 2 )
258+
259+ Nothing ->
260+ Count 2
261+
262+
263+ collectOption : Option -> Options -> Options
264+ collectOption o opts =
265+ case o of
266+ Delay ms ->
267+ { opts | delay = Just ms }
268+
269+ Iteration i ->
270+ { opts | iteration = Just i }
271+
272+ Ease e ->
273+ { opts | timingFunction = Just e }
274+
275+ Yoyo ->
276+ { opts | isYoyo = True }
277+
278+ Reverse ->
279+ { opts | reversed = True }
280+
281+
282+ defaults : Options
283+ defaults =
284+ { delay = Nothing
285+ , timingFunction = Nothing
286+ , iteration = Nothing
287+ , isYoyo = False
288+ , reversed = False
289+ }
290+
291+
292+ rawOptions_ : Animation -> List Option
293+ rawOptions_ ( Animation _ o _) =
184294 o
185295
186296
0 commit comments