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/animation/css/CSSAnimations.h" | 37 #include "core/animation/css/CSSAnimations.h" |
37 #include "core/css/parser/BisonCSSParser.h" | 38 #include "core/css/parser/BisonCSSParser.h" |
38 #include "core/css/RuntimeCSSEnabled.h" | |
39 #include "core/css/resolver/StyleResolver.h" | 39 #include "core/css/resolver/StyleResolver.h" |
40 #include "wtf/text/StringBuilder.h" | 40 #include "wtf/text/StringBuilder.h" |
41 #include <algorithm> | 41 #include <algorithm> |
42 | 42 |
43 namespace WebCore { | 43 namespace WebCore { |
44 | 44 |
45 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& prope
rtyName) | 45 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& prope
rtyName) |
46 { | 46 { |
47 if (propertyName.find('-') != kNotFound) | 47 if (propertyName.find('-') != kNotFound) |
48 return CSSPropertyInvalid; | 48 return CSSPropertyInvalid; |
49 | 49 |
50 StringBuilder builder; | 50 StringBuilder builder; |
51 size_t position = 0; | 51 size_t position = 0; |
52 size_t end; | 52 size_t end; |
53 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) { | 53 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) { |
54 builder.append(propertyName.substring(position, end - position) + "-" +
toASCIILower((propertyName)[end])); | 54 builder.append(propertyName.substring(position, end - position) + "-" +
toASCIILower((propertyName)[end])); |
55 position = end + 1; | 55 position = end + 1; |
56 } | 56 } |
57 builder.append(propertyName.substring(position)); | 57 builder.append(propertyName.substring(position)); |
58 // Doesn't handle prefixed properties. | 58 // Doesn't handle prefixed properties. |
59 CSSPropertyID id = cssPropertyID(builder.toString()); | 59 CSSPropertyID id = cssPropertyID(builder.toString()); |
60 return id; | 60 return id; |
61 } | 61 } |
62 | 62 |
63 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyfra
meDictionaryVector, double duration) | 63 void ElementAnimation::populateTiming(Timing& timing, Dictionary timingInputDict
ionary) |
| 64 { |
| 65 // FIXME: This method needs to be refactored to handle invalid |
| 66 // null, NaN, Infinity values better. |
| 67 // See: http://www.w3.org/TR/WebIDL/#es-double |
| 68 double startDelay = 0; |
| 69 timingInputDictionary.get("delay", startDelay); |
| 70 if (!isnan(startDelay) && !isinf(startDelay)) |
| 71 timing.startDelay = startDelay; |
| 72 |
| 73 String fillMode; |
| 74 timingInputDictionary.get("fill", fillMode); |
| 75 if (fillMode == "none") { |
| 76 timing.fillMode = Timing::FillModeNone; |
| 77 } else if (fillMode == "backwards") { |
| 78 timing.fillMode = Timing::FillModeBackwards; |
| 79 } else if (fillMode == "both") { |
| 80 timing.fillMode = Timing::FillModeBoth; |
| 81 } else if (fillMode == "forwards") { |
| 82 timing.fillMode = Timing::FillModeForwards; |
| 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; |
| 96 bool hasIterationDurationValue = timingInputDictionary.get("duration", itera
tionDurationValue); |
| 97 if (hasIterationDurationValue) { |
| 98 double iterationDuration = iterationDurationValue->NumberValue(); |
| 99 if (!isnan(iterationDuration) && iterationDuration >= 0) { |
| 100 timing.iterationDuration = iterationDuration; |
| 101 timing.hasIterationDuration = true; |
| 102 } |
| 103 } |
| 104 |
| 105 double playbackRate = 1; |
| 106 timingInputDictionary.get("playbackRate", playbackRate); |
| 107 if (!isnan(playbackRate) && !isinf(playbackRate)) |
| 108 timing.playbackRate = playbackRate; |
| 109 |
| 110 String direction; |
| 111 timingInputDictionary.get("direction", direction); |
| 112 if (direction == "reverse") { |
| 113 timing.direction = Timing::PlaybackDirectionReverse; |
| 114 } else if (direction == "alternate") { |
| 115 timing.direction = Timing::PlaybackDirectionAlternate; |
| 116 } else if (direction == "alternate-reverse") { |
| 117 timing.direction = Timing::PlaybackDirectionAlternateReverse; |
| 118 } |
| 119 |
| 120 timing.assertValid(); |
| 121 } |
| 122 |
| 123 static bool checkDocumentAndRenderer(Element* element) |
| 124 { |
| 125 if (!element->inActiveDocument()) |
| 126 return false; |
| 127 element->document().updateStyleIfNeeded(); |
| 128 if (!element->renderer()) |
| 129 return false; |
| 130 return true; |
| 131 } |
| 132 |
| 133 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyfra
meDictionaryVector, Dictionary timingInput) |
64 { | 134 { |
65 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); | 135 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); |
66 | 136 |
67 // FIXME: This test will not be neccessary once resolution of keyframe value
s occurs at | 137 // FIXME: This test will not be neccessary once resolution of keyframe value
s occurs at |
68 // animation application time. | 138 // animation application time. |
69 if (!element->inActiveDocument()) | 139 if (!checkDocumentAndRenderer(element)) |
70 return 0; | |
71 element->document().updateStyleIfNeeded(); | |
72 if (!element->renderer()) | |
73 return 0; | 140 return 0; |
74 | 141 |
75 return startAnimation(element, keyframeDictionaryVector, duration); | 142 return startAnimation(element, keyframeDictionaryVector, timingInput); |
76 } | 143 } |
77 | 144 |
78 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector, double duration) | 145 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyfra
meDictionaryVector, double timingInput) |
| 146 { |
| 147 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); |
| 148 |
| 149 // FIXME: This test will not be neccessary once resolution of keyframe value
s occurs at |
| 150 // animation application time. |
| 151 if (!checkDocumentAndRenderer(element)) |
| 152 return 0; |
| 153 |
| 154 return startAnimation(element, keyframeDictionaryVector, timingInput); |
| 155 } |
| 156 |
| 157 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyfra
meDictionaryVector) |
| 158 { |
| 159 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); |
| 160 |
| 161 // FIXME: This test will not be neccessary once resolution of keyframe value
s occurs at |
| 162 // animation application time. |
| 163 if (!checkDocumentAndRenderer(element)) |
| 164 return 0; |
| 165 |
| 166 return startAnimation(element, keyframeDictionaryVector); |
| 167 } |
| 168 |
| 169 static PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element* elemen
t, Vector<Dictionary> keyframeDictionaryVector) |
79 { | 170 { |
80 KeyframeEffectModel::KeyframeVector keyframes; | 171 KeyframeEffectModel::KeyframeVector keyframes; |
81 Vector<RefPtr<MutableStylePropertySet> > propertySetVector; | 172 Vector<RefPtr<MutableStylePropertySet> > propertySetVector; |
82 | 173 |
83 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) { | 174 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) { |
84 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::c
reate(); | 175 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::c
reate(); |
85 propertySetVector.append(propertySet); | 176 propertySetVector.append(propertySet); |
86 | 177 |
87 RefPtr<Keyframe> keyframe = Keyframe::create(); | 178 RefPtr<Keyframe> keyframe = Keyframe::create(); |
88 keyframes.append(keyframe); | 179 keyframes.append(keyframe); |
(...skipping 12 matching lines...) Expand all Loading... |
101 String compositeString; | 192 String compositeString; |
102 keyframeDictionaryVector[i].get("composite", compositeString); | 193 keyframeDictionaryVector[i].get("composite", compositeString); |
103 if (compositeString == "add") | 194 if (compositeString == "add") |
104 keyframe->setComposite(AnimationEffect::CompositeAdd); | 195 keyframe->setComposite(AnimationEffect::CompositeAdd); |
105 | 196 |
106 Vector<String> keyframeProperties; | 197 Vector<String> keyframeProperties; |
107 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); | 198 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); |
108 | 199 |
109 for (size_t j = 0; j < keyframeProperties.size(); ++j) { | 200 for (size_t j = 0; j < keyframeProperties.size(); ++j) { |
110 String property = keyframeProperties[j]; | 201 String property = keyframeProperties[j]; |
111 CSSPropertyID id = camelCaseCSSPropertyNameToID(property); | 202 CSSPropertyID id = ElementAnimation::camelCaseCSSPropertyNameToID(pr
operty); |
112 | 203 |
113 // FIXME: There is no way to store invalid properties or invalid val
ues | 204 // FIXME: There is no way to store invalid properties or invalid val
ues |
114 // in a Keyframe object, so for now I just skip over them. Eventuall
y we | 205 // in a Keyframe object, so for now I just skip over them. Eventuall
y we |
115 // will need to support getFrames(), which should return exactly the | 206 // will need to support getFrames(), which should return exactly the |
116 // keyframes that were input through the API. We will add a layer to
wrap | 207 // keyframes that were input through the API. We will add a layer to
wrap |
117 // KeyframeEffectModel, store input keyframes and implement getFrame
s. | 208 // KeyframeEffectModel, store input keyframes and implement getFrame
s. |
118 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty
(id)) | 209 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty
(id)) |
119 continue; | 210 continue; |
120 | 211 |
121 String value; | 212 String value; |
122 keyframeDictionaryVector[i].get(property, value); | 213 keyframeDictionaryVector[i].get(property, value); |
123 propertySet->setProperty(id, value); | 214 propertySet->setProperty(id, value); |
124 } | 215 } |
125 } | 216 } |
126 | 217 |
127 // FIXME: Replace this with code that just parses, when that code is availab
le. | 218 // FIXME: Replace this with code that just parses, when that code is availab
le. |
128 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode
l(*element, propertySetVector, keyframes); | 219 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode
l(*element, propertySetVector, keyframes); |
| 220 return effect; |
| 221 } |
129 | 222 |
130 // FIXME: Totally hardcoded Timing for now. Will handle timing parameters la
ter. | 223 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector, Dictionary timingInput) |
| 224 { |
| 225 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 226 |
131 Timing timing; | 227 Timing timing; |
132 // FIXME: Currently there is no way to tell whether or not an iterationDurat
ion | 228 populateTiming(timing, timingInput); |
133 // has been specified (becauser the default argument is 0). So any animation | |
134 // created using Element.animate() will have a timing with hasIterationDurat
ion() | |
135 // == true. | |
136 timing.hasIterationDuration = true; | |
137 timing.iterationDuration = std::max<double>(duration, 0); | |
138 | 229 |
139 RefPtr<Animation> animation = Animation::create(element, effect, timing); | 230 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
140 DocumentTimeline* timeline = element->document().timeline(); | 231 DocumentTimeline* timeline = element->document().timeline(); |
| 232 ASSERT(timeline); |
| 233 timeline->play(animation.get()); |
| 234 |
| 235 return animation.get(); |
| 236 } |
| 237 |
| 238 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector, double timingInput) |
| 239 { |
| 240 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 241 |
| 242 Timing timing; |
| 243 if (!isnan(timingInput)) { |
| 244 timing.hasIterationDuration = true; |
| 245 timing.iterationDuration = std::max<double>(timingInput, 0); |
| 246 } |
| 247 |
| 248 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
| 249 DocumentTimeline* timeline = element->document().timeline(); |
| 250 ASSERT(timeline); |
| 251 timeline->play(animation.get()); |
| 252 |
| 253 return animation.get(); |
| 254 } |
| 255 |
| 256 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector) |
| 257 { |
| 258 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 259 |
| 260 Timing timing; |
| 261 |
| 262 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
| 263 DocumentTimeline* timeline = element->document().timeline(); |
141 ASSERT(timeline); | 264 ASSERT(timeline); |
142 timeline->play(animation.get()); | 265 timeline->play(animation.get()); |
143 | 266 |
144 return animation.get(); | 267 return animation.get(); |
145 } | 268 } |
146 | 269 |
147 } // namespace WebCore | 270 } // namespace WebCore |
OLD | NEW |