2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 #include "core/animation/ElementAnimation.h"
34 #include "bindings/v8/Dictionary.h"
35 #include "bindings/v8/ScriptValue.h"
36 #include "core/animation/DocumentTimeline.h"
37 #include "core/animation/css/CSSAnimations.h"
38 #include "core/css/parser/BisonCSSParser.h"
39 #include "core/css/resolver/StyleResolver.h"
40 #include "wtf/text/StringBuilder.h"
45 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& propertyName)
47 if (propertyName.find('-') != kNotFound)
48 return CSSPropertyInvalid;
50 StringBuilder builder;
53 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) {
54 builder.append(propertyName.substring(position, end - position) + "-" + toASCIILower((propertyName)[end]));
57 builder.append(propertyName.substring(position));
58 // Doesn't handle prefixed properties.
59 CSSPropertyID id = cssPropertyID(builder.toString());
63 void ElementAnimation::populateTiming(Timing& timing, Dictionary timingInputDictionary)
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;
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;
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);
90 double iterationCount = 1;
91 timingInputDictionary.get("iterations", iterationCount);
92 if (!std::isnan(iterationCount))
93 timing.iterationCount = std::max<double>(iterationCount, 0);
95 v8::Local<v8::Value> iterationDurationValue;
96 bool hasIterationDurationValue = timingInputDictionary.get("duration", iterationDurationValue);
97 if (hasIterationDurationValue) {
98 double iterationDuration = iterationDurationValue->NumberValue();
99 if (!std::isnan(iterationDuration) && iterationDuration >= 0) {
100 timing.iterationDuration = iterationDuration;
101 timing.hasIterationDuration = true;
105 double playbackRate = 1;
106 timingInputDictionary.get("playbackRate", playbackRate);
107 if (!std::isnan(playbackRate) && !std::isinf(playbackRate))
108 timing.playbackRate = playbackRate;
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;
120 timing.assertValid();
123 static bool checkDocumentAndRenderer(Element* element)
125 if (!element->inActiveDocument())
127 element->document().updateStyleIfNeeded();
128 if (!element->renderer())
133 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector, Dictionary timingInput)
135 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
137 // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
138 // animation application time.
139 if (!checkDocumentAndRenderer(element))
142 return startAnimation(element, keyframeDictionaryVector, timingInput);
145 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector, double timingInput)
147 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
149 // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
150 // animation application time.
151 if (!checkDocumentAndRenderer(element))
154 return startAnimation(element, keyframeDictionaryVector, timingInput);
157 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector)
159 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
161 // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
162 // animation application time.
163 if (!checkDocumentAndRenderer(element))
166 return startAnimation(element, keyframeDictionaryVector);
169 static PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element* element, Vector<Dictionary> keyframeDictionaryVector)
171 KeyframeEffectModel::KeyframeVector keyframes;
172 Vector<RefPtr<MutableStylePropertySet> > propertySetVector;
174 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
175 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create();
176 propertySetVector.append(propertySet);
178 RefPtr<Keyframe> keyframe = Keyframe::create();
179 keyframes.append(keyframe);
182 if (keyframeDictionaryVector[i].get("offset", offset)) {
183 keyframe->setOffset(offset);
186 String compositeString;
187 keyframeDictionaryVector[i].get("composite", compositeString);
188 if (compositeString == "add")
189 keyframe->setComposite(AnimationEffect::CompositeAdd);
191 Vector<String> keyframeProperties;
192 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties);
194 for (size_t j = 0; j < keyframeProperties.size(); ++j) {
195 String property = keyframeProperties[j];
196 CSSPropertyID id = ElementAnimation::camelCaseCSSPropertyNameToID(property);
198 // FIXME: There is no way to store invalid properties or invalid values
199 // in a Keyframe object, so for now I just skip over them. Eventually we
200 // will need to support getFrames(), which should return exactly the
201 // keyframes that were input through the API. We will add a layer to wrap
202 // KeyframeEffectModel, store input keyframes and implement getFrames.
203 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty(id))
207 keyframeDictionaryVector[i].get(property, value);
208 propertySet->setProperty(id, value);
212 // FIXME: Replace this with code that just parses, when that code is available.
213 RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectModel(*element, propertySetVector, keyframes);
217 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector, Dictionary timingInput)
219 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
222 populateTiming(timing, timingInput);
224 RefPtr<Animation> animation = Animation::create(element, effect, timing);
225 DocumentTimeline* timeline = element->document().timeline();
227 timeline->play(animation.get());
229 return animation.get();
232 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector, double timingInput)
234 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
237 if (!std::isnan(timingInput)) {
238 timing.hasIterationDuration = true;
239 timing.iterationDuration = std::max<double>(timingInput, 0);
242 RefPtr<Animation> animation = Animation::create(element, effect, timing);
243 DocumentTimeline* timeline = element->document().timeline();
245 timeline->play(animation.get());
247 return animation.get();
250 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector)
252 RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
256 RefPtr<Animation> animation = Animation::create(element, effect, timing);
257 DocumentTimeline* timeline = element->document().timeline();
259 timeline->play(animation.get());
261 return animation.get();
264 } // namespace WebCore