drawing_animation
a flutter library for gradually painting svg path objects on canvas (drawing line animation).
from static svg assets | see more examples in the showcasing app. | |
---|---|---|
dynamically created from path objects which are animated over time | ||
more coming soon … |
the rendering library exposes a central widget called animateddrawing
which allows to render svg paths (via animateddrawing.svg
) or flutter path objects (via animateddrawing.paths
) in a drawing like fashion.
getting started – animateddrawing.svg
to get started with the drawing_animation
package you need a valid svg file.
currently only simple path elements without transforms are supported (see supported svg specifications)
- add dependency in your
pubspec.yaml
dependencies:
drawing_animation: ^0.1.1
- add the svg asset
assets:
- assets/my_drawing.svg
- use the widget
an animateddrawing widget can be initiated in two ways:
- simplified – without animation controller (see example_01)
by default every animation repeats infinitely. for running an animation only once you can use a callback to set
run
to false after the first animation cycle completed (see fieldonfinish
).animateddrawing.svg( "assets/my_drawing.svg", run: this.run, duration: new duration(seconds: 3), onfinish: () => setstate(() { this.run = false; }), )
- standard – with animation controller (see example_02)
the simplified version will be sufficient in most of the use cases. if you wish to controll the animation furthermore or you want to syncronize it with other existing animations, you might consider using an custom animation controller:
animateddrawing.svg( "assets/test.svg", controller: this.controller, )
- simplified – without animation controller (see example_01)
- check out examples in the
examples
folder. it seems that antialising for the paint/canvas is switched off when using debug mode. for pretty results useflutter run --release
.
getting started – animateddrawing.paths (still experimental)
by providing path objects directly to the widget, elements can be changed dynamically, even during the animation. the internal data structure is rebuild every time the state changes, therefore the animation performance might suffer if the amount of elements in paths
is very high (see limitations). more examples will be provided soon (for now see example_01 and example_04).
animateddrawing.paths(
[
///path objects
],
paints:[
///paint objects (optional), specifies a [paint] object for each [path] element in `paths`.
],
run: this.run,
duration: new duration(seconds: 3),
onfinish: () => setstate(() {
this.run = false;
}),
)
current limitations:
as stated, for every state change of the widget, the internal data structure for the path objects is rebuilt. when the amount of provided path objects is high and a custom animationorder
is defined (which triggers a sorting operation over the data structure) it can result in lags. this becomes especially apparent when the state is rebuild at 60fps by another animation (e.g. rotating the path objects at every frame). any suggestions on how to elegantly solve this are very welcome 🙂
option list
here is increasingly growing list with all available parameters and their visual effect.
field | type |
example |
---|---|---|
lineanimation
specifies in which way the path elements are drawn to the canvas. when |
lineanimation.onebyone |
|
lineanimation.allatonce |
||
animationorder
denotes the order in which the path elements are drawn to canvas when |
pathorders.original |
|
pathorders.bottomtotop |
||
pathorders.decreasinglength |
||
pathorders.increasinglength |
||
pathorders.lefttoright |
||
pathorders.righttoleft |
||
pathorders.toptobottom |
||
animationcurve
easing curves are used to adjust the rate of change of an animation over time, allowing them to speed up and slow down, rather than moving at a constant rate. see flutter docs. |
curves.linear |
|
curves.elasticout |
||
curves.bounceinout |
||
curves.decelerate |
||
other | ||
onfinish
callback when one animation cycle is finished. by default every animation repeats infinitely. |
||
onpaint
callback when a complete path is painted to the canvas. returns with the relative index and the path element itself. |
||
range
start and stop a animation from a certain moment in time by defining a |
||
scaletoviewport
path objects are scaled to the available viewport while maintaining the aspect ratio. defaults to true. |
supported svg specifications
- only path elements (
<path d="m3m1....">
) are supported for now. i’m currently considering to add flutter_svg as dependency for more complete svg parsing. - attributes
- stroke, only hex-color without alpha for now
- stroke-width
- style, but only the both fields above
- no transforms are supported, yet.
how can i use my own svg files?
a lot of tools can convert existing svg files to the supported format.
for example with inkscape:
- select all objects and ungroup till there is no group left (ctrl+u)
- convert selection to paths:
path>>object to path
and hit save - afterwards remove transforms with svgo or the webversion svgomg.
- now it should work, if not feel free to write an issue!
examples:
example_01
: set up simplfied animateddrawing with animateddrawing.svg and animateddrawing.pathsexample_02
: set up animateddrawing with an custom animation controllerexample_03
: small artistic showcasing app with vectorizied drawings of old book scans provided by the british libraryexample_04
: show how to create gifs with high resolution using thedebug
field.
todo
- better test coverage
- improve svg parsing capabilities
- circles, rect etc.
- better color parsing incl. alpha for hex code and rgb(a)
- subsitute svg parsing logic with an mature parsering library as flutter_svg
- provide a way to overwrite color/brush etc. for
animateddrawing.svg
– maybe also overpaints
object? - define a [pathorder] which maintains each path and only sorts them relative to each other
- improve performance animateddrawing.paths, for every rebuild all provided paths have to be parsed again. is there a way to check path-objects for equality like keys for widget? idea: implementing a proxy for path which creates a unique hash when command evoked
- showcase: write “drawing_animation” in different ways + 3 cirlcles + color it and one gif and put it at the top
- showcase: create fractals with l-systems
- animateddrawing.paths:
- provide some kind of fixed boundingbox since paths and the overall bounding box can dynamically change (e.g. rotating circle pulses in size)
- also custom viewport
Comments are closed.