| 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 (!std::isnan(startDelay) && !std::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 (!std::isnan(iterationStart) && !std::isinf(iterationStart)) |
| 88 timing.iterationStart = std::max<double>(iterationStart, 0); |
| 89 |
| 90 double iterationCount = 1; |
| 91 timingInputDictionary.get("iterations", iterationCount); |
| 92 if (!std::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 (!std::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 (!std::isnan(playbackRate) && !std::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); |
| 89 | 180 |
| 90 double offset; | 181 double offset; |
| 91 if (keyframeDictionaryVector[i].get("offset", offset)) { | 182 if (keyframeDictionaryVector[i].get("offset", offset)) { |
| 92 keyframe->setOffset(offset); | 183 keyframe->setOffset(offset); |
| 93 } | 184 } |
| 94 | 185 |
| 95 String compositeString; | 186 String compositeString; |
| 96 keyframeDictionaryVector[i].get("composite", compositeString); | 187 keyframeDictionaryVector[i].get("composite", compositeString); |
| 97 if (compositeString == "add") | 188 if (compositeString == "add") |
| 98 keyframe->setComposite(AnimationEffect::CompositeAdd); | 189 keyframe->setComposite(AnimationEffect::CompositeAdd); |
| 99 | 190 |
| 100 Vector<String> keyframeProperties; | 191 Vector<String> keyframeProperties; |
| 101 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); | 192 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); |
| 102 | 193 |
| 103 for (size_t j = 0; j < keyframeProperties.size(); ++j) { | 194 for (size_t j = 0; j < keyframeProperties.size(); ++j) { |
| 104 String property = keyframeProperties[j]; | 195 String property = keyframeProperties[j]; |
| 105 CSSPropertyID id = camelCaseCSSPropertyNameToID(property); | 196 CSSPropertyID id = ElementAnimation::camelCaseCSSPropertyNameToID(pr
operty); |
| 106 | 197 |
| 107 // FIXME: There is no way to store invalid properties or invalid val
ues | 198 // FIXME: There is no way to store invalid properties or invalid val
ues |
| 108 // in a Keyframe object, so for now I just skip over them. Eventuall
y we | 199 // in a Keyframe object, so for now I just skip over them. Eventuall
y we |
| 109 // will need to support getFrames(), which should return exactly the | 200 // will need to support getFrames(), which should return exactly the |
| 110 // keyframes that were input through the API. We will add a layer to
wrap | 201 // keyframes that were input through the API. We will add a layer to
wrap |
| 111 // KeyframeEffectModel, store input keyframes and implement getFrame
s. | 202 // KeyframeEffectModel, store input keyframes and implement getFrame
s. |
| 112 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty
(id)) | 203 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty
(id)) |
| 113 continue; | 204 continue; |
| 114 | 205 |
| 115 String value; | 206 String value; |
| 116 keyframeDictionaryVector[i].get(property, value); | 207 keyframeDictionaryVector[i].get(property, value); |
| 117 propertySet->setProperty(id, value); | 208 propertySet->setProperty(id, value); |
| 118 } | 209 } |
| 119 } | 210 } |
| 120 | 211 |
| 121 // FIXME: Replace this with code that just parses, when that code is availab
le. | 212 // FIXME: Replace this with code that just parses, when that code is availab
le. |
| 122 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode
l(*element, propertySetVector, keyframes); | 213 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectMode
l(*element, propertySetVector, keyframes); |
| 214 return effect; |
| 215 } |
| 123 | 216 |
| 124 // FIXME: Totally hardcoded Timing for now. Will handle timing parameters la
ter. | 217 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector, Dictionary timingInput) |
| 218 { |
| 219 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 220 |
| 125 Timing timing; | 221 Timing timing; |
| 126 // FIXME: Currently there is no way to tell whether or not an iterationDurat
ion | 222 populateTiming(timing, timingInput); |
| 127 // has been specified (becauser the default argument is 0). So any animation | |
| 128 // created using Element.animate() will have a timing with hasIterationDurat
ion() | |
| 129 // == true. | |
| 130 timing.hasIterationDuration = true; | |
| 131 timing.iterationDuration = std::max<double>(duration, 0); | |
| 132 | 223 |
| 133 RefPtr<Animation> animation = Animation::create(element, effect, timing); | 224 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
| 134 DocumentTimeline* timeline = element->document().timeline(); | 225 DocumentTimeline* timeline = element->document().timeline(); |
| 226 ASSERT(timeline); |
| 227 timeline->play(animation.get()); |
| 228 |
| 229 return animation.get(); |
| 230 } |
| 231 |
| 232 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector, double timingInput) |
| 233 { |
| 234 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 235 |
| 236 Timing timing; |
| 237 if (!std::isnan(timingInput)) { |
| 238 timing.hasIterationDuration = true; |
| 239 timing.iterationDuration = std::max<double>(timingInput, 0); |
| 240 } |
| 241 |
| 242 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
| 243 DocumentTimeline* timeline = element->document().timeline(); |
| 244 ASSERT(timeline); |
| 245 timeline->play(animation.get()); |
| 246 |
| 247 return animation.get(); |
| 248 } |
| 249 |
| 250 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary>
keyframeDictionaryVector) |
| 251 { |
| 252 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyf
rameDictionaryVector); |
| 253 |
| 254 Timing timing; |
| 255 |
| 256 RefPtr<Animation> animation = Animation::create(element, effect, timing); |
| 257 DocumentTimeline* timeline = element->document().timeline(); |
| 135 ASSERT(timeline); | 258 ASSERT(timeline); |
| 136 timeline->play(animation.get()); | 259 timeline->play(animation.get()); |
| 137 | 260 |
| 138 return animation.get(); | 261 return animation.get(); |
| 139 } | 262 } |
| 140 | 263 |
| 141 } // namespace WebCore | 264 } // namespace WebCore |
| OLD | NEW |