Drive declarative SVG animation from a host clock.
#include <cstddef>
#include <iostream>
#include <optional>
#include <string_view>
#include <utility>
#include <vector>
constexpr std::string_view kSvg = R"(
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="32">
<rect width="32" height="32" fill="red">
<animate id="fade" attributeName="fill" from="red" to="blue" dur="2s" fill="freeze" />
</rect>
<rect id="button" x="32" width="32" height="32" fill="gray">
<set attributeName="fill" to="green" begin="button.click" dur="1s" />
</rect>
</svg>)";
std::cerr <<
"Parse Error: " << maybeDocument.
error() <<
"\n";
return 1;
}
for (const double seconds : {0.0, 1.0, 3.0}) {
}
std::optional<donner::svg::SVGElement> button = document.
querySelector(
"#button");
std::cerr << "The click did not start an animation\n";
return 1;
}
: "end";
std::cout << type << " at " << event.time << "s\n";
}
const std::vector<std::pair<donner::svg::SVGAnimationEvent::Type, double>> expected = {
};
bool matches = batch.
complete && batch.
events.size() == expected.size();
for (std::size_t i = 0; matches && i < expected.size(); ++i) {
matches =
batch.
events[i].type == expected[i].first && batch.
events[i].time == expected[i].second;
}
if (!matches) {
std::cerr << "Unexpected animation lifecycle boundaries\n";
return 1;
}
std::cerr << "Restart did not return to the authored timeline\n";
return 1;
}
return 0;
}
A parser result, which may contain a result of type T, or an error, or both.
Definition ParseResult.h:17
bool hasError() const noexcept
Returns true if this ParseResult contains an error.
Definition ParseResult.h:110
T & result() &
Returns the contained result.
Definition ParseResult.h:51
ParseDiagnostic & error() &
Returns the contained error.
Definition ParseResult.h:81
Collects parse warnings during parsing.
Definition ParseWarningSink.h:29
Backend-agnostic renderer that resolves to the active build backend (tiny-skia by default,...
Definition Renderer.h:82
RendererBitmap takeSnapshot() const override
Captures a CPU-readable snapshot of the current frame.
void draw(SVGDocument &document) override
Draws the SVG document using the active backend.
Represents a parsed SVG document containing a tree of SVGElement nodes.
Definition SVGDocument.h:106
bool dispatchMouseEvent(EventType type, const SVGElement &target)
Deliver a mouse event to declarative SVG animation begin/end triggers.
std::optional< SVGElement > querySelector(std::string_view selector)
Find the first descendant of the root <svg> element, in document order, that matches the given CSS se...
double currentTime() const
Get the current document time in seconds.
void restartAnimations()
Start a fresh animation session at time zero, including animations with restart="never".
SVGAnimationEventBatch animationEvents(double from, double to)
Query animation lifecycle boundaries in the half-open interval (from, to].
void setTime(double seconds)
Set the current document time for animations, in seconds from the document start.
static ParseResult< SVGDocument > ParseSVG(std::string_view source, ParseWarningSink &warningSink, Options options={}, SVGDocument::Settings settings={}) noexcept
Parses an SVG XML document from a string (typically the contents of a .svg file).
std::vector< SVGAnimationEvent > events
Boundaries sorted by document time.
Definition SVGDocument.h:55
bool complete
False for invalid bounds or budget exhaustion.
Definition SVGDocument.h:56
@ Click
A click (mousedown + mouseup on same element).
Definition Event.h:19
Bounded lifecycle query result. An incomplete query returns no partial events.
Definition SVGDocument.h:54
int main()
Run the rendering example and report its result.
Definition render_test.cc:34
T y
The y component of the vector.
Definition Vector2.h:18
T x
The x component of the vector.
Definition Vector2.h:17
CPU-readable bitmap produced by a renderer snapshot.
Definition RendererInterface.h:67
Vector2i dimensions
Pixel dimensions of the bitmap in device pixels.
Definition RendererInterface.h:69
A declarative animation boundary observed over a time interval.
Definition SVGDocument.h:40
@ Begin
The animation's active interval began.
Definition SVGDocument.h:43
@ Repeat
The animation started a new repeat iteration.
Definition SVGDocument.h:44
@ End
The animation's active interval ended.
Definition SVGDocument.h:45