Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Unified Diff: Source/core/animation/ElementAnimation.cpp

Issue 96283002: Web Animations API: Start implementation of Element.animate(). (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/animation/ElementAnimation.cpp
diff --git a/Source/core/animation/ElementAnimation.cpp b/Source/core/animation/ElementAnimation.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d4a2b1f974a22d84c073597774e4901694d9f4b6
--- /dev/null
+++ b/Source/core/animation/ElementAnimation.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "core/animation/ElementAnimation.h"
+
+#include "core/animation/AnimatableLength.h"
+#include "core/animation/AnimationEffect.h"
+#include "core/animation/DocumentTimeline.h"
+#include "core/animation/KeyframeAnimationEffect.h"
+#include "core/animation/css/CSSAnimatableValueFactory.h"
+#include "core/animation/css/CSSAnimations.h"
+#include "core/css/CSSParser.h"
+#include "core/css/resolver/StyleBuilder.h"
+#include "core/css/resolver/StyleResolverState.h"
+#include "core/page/RuntimeCSSEnabled.h"
+#include "core/rendering/style/RenderStyle.h"
+#include "wtf/text/StringBuilder.h"
+
+
+namespace WebCore {
+
+namespace {
+
+static CSSPropertyID camelCasePropertyToCSSPropertyID(String propertyName)
Steve Block 2013/12/06 03:27:27 No need for static now this is in an anonymous nam
rjwright 2013/12/07 11:09:06 Done.
+{
+ StringBuilder builder;
+ size_t position = 0;
+ size_t end;
+ while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) {
+ builder.append(propertyName.substring(position, end) + "-" + toASCIILower(propertyName[end]));
+ position = end + 1;
+ }
+ builder.append(propertyName.substring(position));
+ return cssPropertyID(builder.toString());
Steve Block 2013/12/06 03:27:27 We should check RuntimeCSSEnabled::isCSSPropertyEn
rjwright 2013/12/09 00:29:45 Done.
+}
+
+} // namespace
+
+void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector)
+{
+ ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
+
+ KeyframeAnimationEffect::KeyframeVector keyframes;
+ RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create();
+ for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
+
+ Vector<String> keyframeProperties;
+ keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties);
Steve Block 2013/12/06 03:27:27 Move this down to line 91
rjwright 2013/12/07 11:09:06 Done.
+
+ RefPtr<Keyframe> keyframe = Keyframe::create();
+
+ double offset;
+ if (keyframeDictionaryVector[i].get("offset", offset))
+ keyframe->setOffset(offset);
+ else
+ RELEASE_ASSERT_WITH_MESSAGE(false, "Web Animations not yet implemented: Handling for keyframes without specified offset.");
+
+ String compositeString;
+ keyframeDictionaryVector[i].get("composite", compositeString);
+ if (compositeString == String("add"))
+ keyframe->setComposite(AnimationEffect::CompositeAdd);
+
+ for (size_t j = 0; j < keyframeProperties.size(); ++j) {
+ String property = keyframeProperties[j];
+ String value;
+ keyframeDictionaryVector[i].get(property, value);
+ CSSPropertyID id = camelCasePropertyToCSSPropertyID(property);
+
+ if (!id || !CSSAnimations::isAnimatableProperty(id))
Steve Block 2013/12/06 03:27:27 (id != CSSPropertyInvalid || ...) for clarity
rjwright 2013/12/07 11:09:06 Done.
+ continue;
Steve Block 2013/12/06 03:27:27 There should be a RELEASE_ASSERT or at least a FIX
rjwright 2013/12/07 11:09:06 Done.
+ if (!CSSParser::parseValue(propertySet.get(), id, value, false, element->document()))
+ continue;
Steve Block 2013/12/06 03:27:27 Same thing about a RELEASE_ASSERT or FIXME
+
+ RefPtr<RenderStyle> style = RenderStyle::clone(element->styleForRenderer().get());
esprehn 2013/12/04 06:14:13 Calling styleForRenderer() repeatedly for each pro
rjwright 2013/12/07 11:09:06 Done.
+ StyleResolverState state(element->document(), element);
esprehn 2013/12/04 06:14:13 Constructing states is not cheap, one per property
rjwright 2013/12/07 11:09:06 Done.
+ state.setStyle(style);
+ StyleBuilder::applyProperty(id, state, propertySet->getPropertyCSSValue(id).get());
+ keyframe->setPropertyValue(id, CSSAnimatableValueFactory::create(id, *style.get()).get());
esprehn 2013/12/04 06:14:13 This does not look like the right way to do this.
rjwright 2013/12/07 11:09:06 Done.
+ }
+ keyframes.append(keyframe);
+ }
+
+ RefPtr<KeyframeAnimationEffect> effect = KeyframeAnimationEffect::create(keyframes);
+
+ // FIXME: Totally hardcoded Timing for now. Will handle timing parameters later.
+ Timing timing;
+ timing.hasIterationDuration = true;
+ timing.iterationDuration = 1;
+
+ RefPtr<Animation> animation = Animation::create(element, effect, timing);
+
+ DocumentTimeline* timeline = element->document().timeline();
+ ASSERT(timeline);
+
+ timeline->play(animation.get());
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698