OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/animation/ElementAnimation.h" | 32 #include "core/animation/ElementAnimation.h" |
33 | 33 |
34 #include "bindings/v8/Dictionary.h" | 34 #include "bindings/v8/Dictionary.h" |
35 #include "bindings/v8/ScriptValue.h" | |
35 #include "core/animation/DocumentTimeline.h" | 36 #include "core/animation/DocumentTimeline.h" |
36 #include "core/css/parser/BisonCSSParser.h" | 37 #include "core/css/parser/BisonCSSParser.h" |
37 #include "core/css/RuntimeCSSEnabled.h" | |
38 #include "core/css/resolver/StyleResolver.h" | 38 #include "core/css/resolver/StyleResolver.h" |
39 #include "wtf/text/StringBuilder.h" | 39 #include "wtf/text/StringBuilder.h" |
40 #include <algorithm> | 40 #include <algorithm> |
41 | 41 |
42 namespace WebCore { | 42 namespace WebCore { |
43 | 43 |
44 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& prope rtyName) | 44 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& prope rtyName) |
45 { | 45 { |
46 if (propertyName.find('-') != kNotFound) | 46 if (propertyName.find('-') != kNotFound) |
47 return CSSPropertyInvalid; | 47 return CSSPropertyInvalid; |
48 | 48 |
49 StringBuilder builder; | 49 StringBuilder builder; |
50 size_t position = 0; | 50 size_t position = 0; |
51 size_t end; | 51 size_t end; |
52 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) { | 52 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) { |
53 builder.append(propertyName.substring(position, end - position) + "-" + toASCIILower((propertyName)[end])); | 53 builder.append(propertyName.substring(position, end - position) + "-" + toASCIILower((propertyName)[end])); |
54 position = end + 1; | 54 position = end + 1; |
55 } | 55 } |
56 builder.append(propertyName.substring(position)); | 56 builder.append(propertyName.substring(position)); |
57 // Doesn't handle prefixed properties. | 57 // Doesn't handle prefixed properties. |
58 CSSPropertyID id = cssPropertyID(builder.toString()); | 58 CSSPropertyID id = cssPropertyID(builder.toString()); |
59 return id; | 59 return id; |
60 } | 60 } |
61 | 61 |
62 void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDict ionaryVector, double duration) | 62 void ElementAnimation::populateTiming(Timing& timing, Dictionary timingInputDict ionary) |
63 { | |
64 // FIXME: This method needs to be refactored to handle invalid | |
65 // null, NaN, Infinity values better. | |
66 // See: http://www.w3.org/TR/WebIDL/#es-double | |
67 double startDelay = 0; | |
68 timingInputDictionary.get("delay", startDelay); | |
69 if (!isnan(startDelay) && !isinf(startDelay)) | |
70 timing.startDelay = startDelay; | |
71 | |
72 String fillMode; | |
73 timingInputDictionary.get("fill", fillMode); | |
74 // FIXME: This will need to be changed to "forwards" when | |
75 // Timing.h implements the spec change that makes default | |
76 // fill mode "none". | |
dstockwell
2014/01/21 20:47:29
The default in the spec seems to be "auto"
rjwright
2014/01/22 03:56:08
Done.
| |
77 if (fillMode == "none") { | |
78 timing.fillMode = Timing::FillModeNone; | |
79 } else if (fillMode == "backwards") { | |
80 timing.fillMode = Timing::FillModeBackwards; | |
81 } else if (fillMode == "both") { | |
82 timing.fillMode = Timing::FillModeBoth; | |
83 } | |
84 | |
85 double iterationStart = 0; | |
86 timingInputDictionary.get("iterationStart", iterationStart); | |
87 if (!isnan(iterationStart) && !isinf(iterationStart)) | |
88 timing.iterationStart = std::max<double>(iterationStart, 0); | |
89 | |
90 double iterationCount = 1; | |
91 timingInputDictionary.get("iterations", iterationCount); | |
92 if (!isnan(iterationCount)) | |
93 timing.iterationCount = std::max<double>(iterationCount, 0); | |
94 | |
95 v8::Local<v8::Value> iterationDurationValue; | |
dstockwell
2014/01/21 20:47:29
We shouldn't be using v8::Local in core/, but ther
rjwright
2014/01/22 03:56:08
Done.
| |
96 bool hasIterationDurationValue = timingInputDictionary.get("duration", itera tionDurationValue); | |
97 if (hasIterationDurationValue) { | |
98 if (iterationDurationValue->IsString()) { | |
99 // All strings are treated as 'auto' except strings that are numbers , e.g. '1', 'Infinity'. | |
dstockwell
2014/01/21 20:47:29
Looks like it should actually be that numbers less
rjwright
2014/01/22 03:56:08
Done.
| |
100 double iterationDuration = iterationDurationValue->NumberValue(); | |
101 if (!isnan(iterationDuration)) | |
102 timing.iterationDuration = std::max<double>(iterationDuration, 0 ); | |
103 timing.hasIterationDuration = true; | |
104 } else if (iterationDurationValue->IsNumber()) { | |
105 double iterationDuration = iterationDurationValue->NumberValue(); | |
106 if (!isnan(iterationDuration)) { | |
107 timing.iterationDuration = std::max<double>(iterationDuration, 0 ); | |
108 timing.hasIterationDuration = true; | |
109 } | |
110 } | |
111 } | |
112 | |
113 double playbackRate = 1; | |
114 timingInputDictionary.get("playbackRate", playbackRate); | |
115 if (!isnan(playbackRate) && !isinf(playbackRate)) | |
116 timing.playbackRate = playbackRate; | |
117 | |
118 String direction; | |
119 timingInputDictionary.get("direction", direction); | |
120 if (direction == "reverse") { | |
121 timing.direction = Timing::PlaybackDirectionReverse; | |
122 } else if (direction == "alternate") { | |
123 timing.direction = Timing::PlaybackDirectionAlternate; | |
124 } else if (direction == "alternate-reverse") { | |
125 timing.direction = Timing::PlaybackDirectionAlternateReverse; | |
126 } | |
127 | |
128 timing.assertValid(); | |
129 } | |
130 | |
131 static bool checkDocumentAndRenderer(Element* element) | |
132 { | |
133 if (!element->inActiveDocument()) | |
134 return false; | |
135 element->document().updateStyleIfNeeded(); | |
136 if (!element->renderer()) | |
137 return false; | |
138 return true; | |
139 } | |
140 | |
141 void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDict ionaryVector, Dictionary timingInput) | |
63 { | 142 { |
64 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); | 143 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); |
65 | 144 |
66 // FIXME: This test will not be neccessary once resolution of keyframe value s occurs at | 145 // FIXME: This test will not be neccessary once resolution of keyframe value s occurs at |
67 // animation application time. | 146 // animation application time. |
68 if (!element->inActiveDocument()) | 147 if (!checkDocumentAndRenderer(element)) |
69 return; | |
70 element->document().updateStyleIfNeeded(); | |
71 if (!element->renderer()) | |
72 return; | 148 return; |
73 | 149 |
74 startAnimation(element, keyframeDictionaryVector, duration); | 150 startAnimation(element, keyframeDictionaryVector, timingInput); |
75 } | 151 } |
76 | 152 |
77 void ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyfr ameDictionaryVector, double duration) | 153 void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDict ionaryVector, double timingInput) |
154 { | |
155 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); | |
156 | |
157 // FIXME: This test will not be neccessary once resolution of keyframe value s occurs at | |
158 // animation application time. | |
159 if (!checkDocumentAndRenderer(element)) | |
160 return; | |
161 | |
162 startAnimation(element, keyframeDictionaryVector, timingInput); | |
163 } | |
164 | |
165 void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDict ionaryVector) | |
166 { | |
167 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); | |
168 | |
169 // FIXME: This test will not be neccessary once resolution of keyframe value s occurs at | |
170 // animation application time. | |
171 if (!checkDocumentAndRenderer(element)) | |
172 return; | |
173 | |
174 startAnimation(element, keyframeDictionaryVector); | |
175 } | |
176 | |
177 static PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element* elemen t, Vector<Dictionary> keyframeDictionaryVector) | |
78 { | 178 { |
79 KeyframeEffectModel::KeyframeVector keyframes; | 179 KeyframeEffectModel::KeyframeVector keyframes; |
80 Vector<RefPtr<MutableStylePropertySet> > propertySetVector; | 180 Vector<RefPtr<MutableStylePropertySet> > propertySetVector; |
81 | 181 |
82 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) { | 182 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) { |
83 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::c reate(); | 183 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::c reate(); |
84 propertySetVector.append(propertySet); | 184 propertySetVector.append(propertySet); |
85 | 185 |
86 RefPtr<Keyframe> keyframe = Keyframe::create(); | 186 RefPtr<Keyframe> keyframe = Keyframe::create(); |
87 keyframes.append(keyframe); | 187 keyframes.append(keyframe); |
88 | 188 |
89 double offset; | 189 double offset; |
90 if (keyframeDictionaryVector[i].get("offset", offset)) { | 190 if (keyframeDictionaryVector[i].get("offset", offset)) { |
91 keyframe->setOffset(offset); | 191 keyframe->setOffset(offset); |
92 } else { | 192 } else { |
93 // FIXME: Web Animations CSS engine does not yet implement handling of | 193 // FIXME: Web Animations CSS engine does not yet implement handling of |
94 // keyframes without specified offsets. This check can be removed wh en | 194 // keyframes without specified offsets. This check can be removed wh en |
95 // that funcitonality is implemented. | 195 // that funcitonality is implemented. |
96 ASSERT_NOT_REACHED(); | 196 ASSERT_NOT_REACHED(); |
97 return; | 197 return 0; |
98 } | 198 } |
99 | 199 |
100 String compositeString; | 200 String compositeString; |
101 keyframeDictionaryVector[i].get("composite", compositeString); | 201 keyframeDictionaryVector[i].get("composite", compositeString); |
102 if (compositeString == "add") | 202 if (compositeString == "add") |
103 keyframe->setComposite(AnimationEffect::CompositeAdd); | 203 keyframe->setComposite(AnimationEffect::CompositeAdd); |
104 | 204 |
105 Vector<String> keyframeProperties; | 205 Vector<String> keyframeProperties; |
106 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); | 206 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); |
107 | 207 |
108 for (size_t j = 0; j < keyframeProperties.size(); ++j) { | 208 for (size_t j = 0; j < keyframeProperties.size(); ++j) { |
109 String property = keyframeProperties[j]; | 209 String property = keyframeProperties[j]; |
110 CSSPropertyID id = camelCaseCSSPropertyNameToID(property); | 210 CSSPropertyID id = ElementAnimation::camelCaseCSSPropertyNameToID(pr operty); |
111 | 211 |
112 // FIXME: There is no way to store invalid properties or invalid val ues | 212 // FIXME: There is no way to store invalid properties or invalid val ues |
113 // in a Keyframe object, so for now I just skip over them. Eventuall y we | 213 // in a Keyframe object, so for now I just skip over them. Eventuall y we |
114 // will need to support getFrames(), which should return exactly the | 214 // will need to support getFrames(), which should return exactly the |
115 // keyframes that were input through the API. We will add a layer to wrap | 215 // keyframes that were input through the API. We will add a layer to wrap |
116 // KeyframeEffectModel, store input keyframes and implement getFrame s. | 216 // KeyframeEffectModel, store input keyframes and implement getFrame s. |
117 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty (id)) | 217 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty (id)) |
118 continue; | 218 continue; |
119 | 219 |
120 String value; | 220 String value; |
121 keyframeDictionaryVector[i].get(property, value); | 221 keyframeDictionaryVector[i].get(property, value); |
122 propertySet->setProperty(id, value); | 222 propertySet->setProperty(id, value); |
123 } | 223 } |
124 } | 224 } |
125 | 225 |
126 // FIXME: Replace this with code that just parses, when that code is availab le. | 226 // FIXME: Replace this with code that just parses, when that code is availab le. |
127 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode l(*element, propertySetVector, keyframes); | 227 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode l(*element, propertySetVector, keyframes); |
228 return effect; | |
229 } | |
128 | 230 |
129 // FIXME: Totally hardcoded Timing for now. Will handle timing parameters la ter. | 231 void ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyfr ameDictionaryVector, Dictionary timingInput) |
232 { | |
233 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf rameDictionaryVector); | |
234 | |
130 Timing timing; | 235 Timing timing; |
131 // FIXME: Currently there is no way to tell whether or not an iterationDurat ion | 236 populateTiming(timing, timingInput); |
132 // has been specified (becauser the default argument is 0). So any animation | |
133 // created using Element.animate() will have a timing with hasIterationDurat ion() | |
134 // == true. | |
135 timing.hasIterationDuration = true; | |
136 timing.iterationDuration = std::max<double>(duration, 0); | |
137 | 237 |
138 RefPtr<Animation> animation = Animation::create(element, effect, timing); | 238 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
139 DocumentTimeline* timeline = element->document().timeline(); | 239 DocumentTimeline* timeline = element->document().timeline(); |
240 ASSERT(timeline); | |
241 timeline->play(animation.get()); | |
242 } | |
243 | |
244 void ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyfr ameDictionaryVector, double timingInput) | |
245 { | |
246 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf rameDictionaryVector); | |
247 | |
248 Timing timing; | |
249 if (!isnan(timingInput)) { | |
250 timing.hasIterationDuration = true; | |
251 timing.iterationDuration = std::max<double>(timingInput, 0); | |
252 } | |
253 | |
254 RefPtr<Animation> animation = Animation::create(element, effect, timing); | |
255 DocumentTimeline* timeline = element->document().timeline(); | |
256 ASSERT(timeline); | |
257 timeline->play(animation.get()); | |
258 } | |
259 | |
260 void ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyfr ameDictionaryVector) | |
261 { | |
262 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf rameDictionaryVector); | |
263 | |
264 Timing timing; | |
265 | |
266 RefPtr<Animation> animation = Animation::create(element, effect, timing); | |
267 DocumentTimeline* timeline = element->document().timeline(); | |
140 ASSERT(timeline); | 268 ASSERT(timeline); |
141 timeline->play(animation.get()); | 269 timeline->play(animation.get()); |
142 } | 270 } |
143 | 271 |
144 } // namespace WebCore | 272 } // namespace WebCore |
OLD | NEW |