Skip to content

Truly independent web browser

LibWeb: Move <animateMotion> targets along its path attribute

Jelle Raaijmakersa8a84d1ad83c
diff --git a/Libraries/LibWeb/SVG/AttributeNames.h b/Libraries/LibWeb/SVG/AttributeNames.h
index eabb2bc6531..26b531129d0 100644
--- a/Libraries/LibWeb/SVG/AttributeNames.h
+++ b/Libraries/LibWeb/SVG/AttributeNames.h
@@ -67,6 +67,7 @@ namespace Web::SVG::AttributeNames {
__ENUMERATE_SVG_ATTRIBUTE(offset, "offset") \
__ENUMERATE_SVG_ATTRIBUTE(opacity, "opacity") \
__ENUMERATE_SVG_ATTRIBUTE(operator_, "operator") \
+ __ENUMERATE_SVG_ATTRIBUTE(path, "path") \
__ENUMERATE_SVG_ATTRIBUTE(pathLength, "pathLength") \
__ENUMERATE_SVG_ATTRIBUTE(patternContentUnits, "patternContentUnits") \
__ENUMERATE_SVG_ATTRIBUTE(patternTransform, "patternTransform") \
diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
index 565dda19d99..72697975c5f 100644
--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
@@ -5,8 +5,12 @@
*/
#include <AK/CharacterTypes.h>
+#include <LibGfx/Path.h>
+#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
+#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
#include <LibWeb/SVG/AttributeNames.h>
+#include <LibWeb/SVG/Path.h>
#include <LibWeb/SVG/SVGAnimateMotionElement.h>
namespace Web::SVG {
@@ -21,7 +25,7 @@ SVGAnimateMotionElement::SVGAnimateMotionElement(DOM::Document& document, DOM::Q
void SVGAnimateMotionElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
{
Base::attribute_changed(name, old_value, value, namespace_);
- if (first_is_one_of(name, AttributeNames::from, AttributeNames::to) && !namespace_.has_value())
+ if (first_is_one_of(name, AttributeNames::from, AttributeNames::path, AttributeNames::to) && !namespace_.has_value())
update_animation();
}
@@ -64,11 +68,56 @@ static RefPtr<CSS::StyleValue const> parse_coordinate_pair_as_translation(SVGEle
return target.parse_presentation_attribute(CSS::PropertyID::Translate, Utf16String::formatted("{} {}", x, y));
}
+static NonnullRefPtr<CSS::StyleValue const> translation(Gfx::FloatPoint point)
+{
+ return CSS::TransformationStyleValue::create(CSS::PropertyID::Translate, CSS::TransformFunction::Translate,
+ { CSS::LengthStyleValue::create(CSS::Length::make_px(point.x())), CSS::LengthStyleValue::create(CSS::Length::make_px(point.y())) });
+}
+
+// https://svgwg.org/specs/animations/#AnimateMotionElement
+Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes_along_motion_path(Gfx::Path const& motion_path, CSS::PropertyID property_id)
+{
+ // NB: Follow polylines through the motion path, with enough points along its curves to stay as close to them as the
+ // length of the motion path is measured.
+ // FIXME: Animate a position along the motion path rather than points along it, so that the target follows its curves
+ // exactly, as other engines do.
+ auto vertices = motion_path.vertices();
+ if (vertices.is_empty())
+ return {};
+
+ // At any time t within a motion path animation of duration dur, the computed coordinate (x,y) along the motion path
+ // is determined by finding the point (x,y) which is t/dur distance along the motion path using the user agent's
+ // distance along the path algorithm.
+ // NB: Each vertex gets the fraction of the length of the motion path it is along as its offset, between which the
+ // target moves in straight lines. A motion path without length keeps the target at its initial point.
+ auto length = vertices.last().distance;
+ if (length == 0) {
+ auto initial_point = translation(vertices.first().point);
+ return { keyframe(0, property_id, initial_point), keyframe(1, property_id, initial_point) };
+ }
+
+ // When a path is combined with a "paced" ‘calcMode’ setting, all "move to" commands are considered to have 0 length
+ // (i.e. they always happen instantaneously), and is not considered in computing the pacing.
+ // NB: The vertices where a subpath ends and where the next one starts are at the same distance, so the target jumps
+ // between them.
+ // FIXME: Divide the duration between the points of the path for the other calcMode values.
+ Vector<Animations::BaseKeyframe> keyframes;
+ keyframes.ensure_capacity(vertices.size());
+ for (auto const& vertex : vertices)
+ keyframes.unchecked_append(keyframe(vertex.distance / length, property_id, translation(vertex.point)));
+ return keyframes;
+}
+
// https://svgwg.org/specs/animations/#AnimateMotionElement
Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes(SVGElement const& target, CSS::PropertyID property_id) const
{
- // FIXME: Take the motion path from the by and values attributes, the path attribute or an mpath child element too.
+ // Regarding the definition of the motion path, the ‘mpath’ element overrides the ‘path’ attribute, which overrides
+ // ‘values’, which overrides ‘from’, ‘by’ and ‘to’.
+ // FIXME: Take the motion path from an mpath child element, and from the values and by attributes.
// FIXME: Apply the keyPoints and rotate attributes.
+ if (auto path = get_attribute(AttributeNames::path); path.has_value())
+ return keyframes_along_motion_path(parse_path_data(*path).to_gfx_path(), property_id);
+
auto to = get_attribute(AttributeNames::to);
auto to_translation = to.has_value() ? parse_coordinate_pair_as_translation(target, *to) : nullptr;
if (!to_translation)
diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
index 39404b9cd0d..ccea0abf288 100644
--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
@@ -6,6 +6,7 @@
#pragma once
+#include <LibGfx/Forward.h>
#include <LibWeb/SVG/SVGAnimationElement.h>
namespace Web::SVG {
@@ -21,6 +22,7 @@ private:
virtual Optional<CSS::PropertyID> target_property() const override;
virtual Vector<Animations::BaseKeyframe> keyframes(SVGElement const& target, CSS::PropertyID) const override;
+ static Vector<Animations::BaseKeyframe> keyframes_along_motion_path(Gfx::Path const&, CSS::PropertyID);
};
}
diff --git a/Tests/LibWeb/Crash/SVG/animate-motion-element-long-curve.html b/Tests/LibWeb/Crash/SVG/animate-motion-element-long-curve.html
new file mode 100644
index 00000000000..4e836bdddf9
--- /dev/null
+++ b/Tests/LibWeb/Crash/SVG/animate-motion-element-long-curve.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<svg>
+ <rect width="10" height="10"><animateMotion path="M0 0 Q10000000 0 20000000 0" dur="1s"/></rect>
+</svg>
diff --git a/Tests/LibWeb/Text/expected/SVG/animate-motion-element-path.txt b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-path.txt
new file mode 100644
index 00000000000..be4a1622529
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-path.txt
@@ -0,0 +1,40 @@
+t=0
+ lines: 0.0, 0.0
+ relative-lines: 10.0, 10.0
+ curve: 0.0, 0.0
+ arc: 100.0, 0.0
+ subpaths: 0.0, 0.0
+ zero-length: 30.0, 40.0
+ move-only: 30.0, 40.0
+ empty: 0.0, 0.0
+ overrides-from-and-to: 0.0, 0.0
+t=0.5
+ lines: 50.0, 0.0
+ relative-lines: 60.0, 10.0
+ curve: 25.0, 0.0
+ arc: 70.7, 70.7
+ subpaths: 50.0, 0.0
+ zero-length: 30.0, 40.0
+ move-only: 30.0, 40.0
+ empty: 0.0, 0.0
+ overrides-from-and-to: 50.0, 0.0
+t=1
+ lines: 100.0, 0.0
+ relative-lines: 110.0, 10.0
+ curve: 50.0, 0.0
+ arc: 0.0, 100.0
+ subpaths: 0.0, 200.0
+ zero-length: 30.0, 40.0
+ move-only: 30.0, 40.0
+ empty: 0.0, 0.0
+ overrides-from-and-to: 100.0, 0.0
+t=1.5
+ lines: 100.0, 50.0
+ relative-lines: 110.0, 60.0
+ curve: 75.0, 0.0
+ arc: -70.7, 70.7
+ subpaths: 50.0, 200.0
+ zero-length: 30.0, 40.0
+ move-only: 30.0, 40.0
+ empty: 0.0, 0.0
+ overrides-from-and-to: 150.0, 0.0
diff --git a/Tests/LibWeb/Text/input/SVG/animate-motion-element-path.html b/Tests/LibWeb/Text/input/SVG/animate-motion-element-path.html
new file mode 100644
index 00000000000..65ba2cfd7da
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/animate-motion-element-path.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg id="svg" width="400" height="400">
+ <rect id="lines" width="10" height="10"><animateMotion path="M0,0 L100,0 L100,100" dur="2s"/></rect>
+ <rect id="relative-lines" width="10" height="10"><animateMotion path="m10,10 h100 v100" dur="2s"/></rect>
+ <rect id="curve" width="10" height="10"><animateMotion path="M0,0 Q50,0 100,0" dur="2s"/></rect>
+ <rect id="arc" width="10" height="10"><animateMotion path="M100,0 A100,100 0 0 1 -100,0" dur="2s"/></rect>
+ <rect id="subpaths" width="10" height="10"><animateMotion path="M0,0 L100,0 M0,200 L100,200" dur="2s"/></rect>
+ <rect id="zero-length" width="10" height="10"><animateMotion path="M30,40 L30,40" dur="2s"/></rect>
+ <rect id="move-only" width="10" height="10"><animateMotion path="M30,40" dur="2s"/></rect>
+ <rect id="empty" width="10" height="10"><animateMotion path="" dur="2s"/></rect>
+ <rect id="overrides-from-and-to" width="10" height="10"><animateMotion path="M0,0 L200,0" from="0,0" to="0,200" dur="2s"/></rect>
+</svg>
+<script>
+ asyncTest(async done => {
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ await new Promise(requestAnimationFrame);
+ svg.pauseAnimations();
+ const origin = svg.getBoundingClientRect();
+ for (const time of [0, 0.5, 1, 1.5]) {
+ svg.setCurrentTime(time);
+ println(`t=${time}`);
+ for (const rect of document.querySelectorAll("rect")) {
+ const box = rect.getBoundingClientRect();
+ println(` ${rect.id}: ${(box.left - origin.left).toFixed(1)}, ${(box.top - origin.top).toFixed(1)}`);
+ }
+ }
+ done();
+ });
+</script>