Jump to content
Search Community

DrawSVGPlugin + SVG in canvas

Spacefuel test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

GSAP draws paths by animating stroke-dashoffset and stroke-dasharray which are CSS properties. Which you can do on canvas too.


See the Pen ZjxOVR?editors=0110 by Sahil89 (@Sahil89) on CodePen


But SVG elements have a method called getTotalLength. Canvas has Path2D method that lets you print a path on canvas but it doesn't have method like getTotalLength. But you can create a path element and use it's getTotalLength method to construct your path and animation. Check the following example using this approach.


See the Pen xJWOmB?editors=0010 by Sahil89 (@Sahil89) on CodePen


  • Like 6
Link to comment
Share on other sites

In addition to Sahil's sweet demo and advice its worth noting that @OSUblake used MorphSVG to generate a canvas rendering.

I suspect you could take a similar approach with an off-canvas SVG that you animate with DrawSVG and then query its path data and use an onUpdate to do some conversions and draw it on canvas. Haven't tried it myself but might be worth giving it a shot. That way you will be able to play(), pause(), reverse() or nest the animation in a timeline.


See the Pen ?editors=0010 by osublake (@osublake) on CodePen


  • Like 4
Link to comment
Share on other sites

That won't work for DrawSVG as path data doesn't change. You can use modifiers plugin instead of onUpdate to retrieve stroke-dashoffset and stroke-dasharray but you can instead create a class that will work just fine in this scenario.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...