02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Animating with DrawSVGPlugin

In this chapter, we will take a deep dive into the world of DrawSVGPlugin. By the end of this lesson, you will know how work with the DrawSVGPlugin.

Video Transcript

00:00Welcome back. In the last video,
00:02we saw how to load in an SVG file and
00:05then animate it into a specific position.
00:08The next step for us is we want to go back into
00:11the source file and we want to also add the feature and
00:14capability of animating the different strokes of an SVG.
00:20Now, to do that, I already copied the file.
00:23So I'm going to go ahead and I'm just going to paste them right in.
00:26So I'm going to go right underneath somewhere in our script,
00:28in our head, and I'm going to go ahead and just copy
00:32and paste basically the draw SVG plugin,
00:35which you could find inside of
00:37that extra plugins that you got inside of the club membership.
00:40So just make sure that if you haven't already done it already so,
00:44make sure that you drop the gsap files right into
00:47that gsub folder in our JS folder,
00:50and you'll find that plugin inside of the plugins folder.
00:54So next up, now that we've loaded up
00:55our draw SVG plugin and our timeline,
00:58it's time for us to go back into our load SVG,
01:00and I want to add more animations
01:02and more capabilities to our element.
01:04Now, to do that, I'm going to show you something that
01:08won't work because I want you to be aware of it.
01:11So first thing is I want to decide what do I want to
01:13animate and I'm going to animate all the different paths.
01:16There are a lot of paths that are created in general in SVG,
01:19especially when you output it directly out of Illustrator.
01:23Most of the time, you're going to get all these paths.
01:25So what I'm going to do is I'm going to go ahead
01:28and create a new variable, which I'm going to call it paths.
01:31And what I want to do with it is basically ask from jQuery
01:35to return to me all the different paths.
01:38Now, notice how I'm just asking for paths directly
01:41and I'm not defining a name to that SVG
01:44because I only have one SVG.
01:46In any other case, I would probably want to make sure
01:48that after I add the SVG, that I give it an ID
01:51or I wrap it inside of a specific item that is unique
01:54so I could then refer only to the paths that are there.
01:57But in our case, we'll only have paths that come
02:00from this logo.svg so we don't have to worry about it.
02:04The next step that I want to do is I want to create
02:06a new timeline because I want to animate
02:08every single path separately and I'm going to go ahead
02:10here and create a new timeline, max.
02:14And really I'm using timeline max because I want to create
02:16a lot of animations at the same time and take leverage
02:19of that capability inside of timeline that enables us
02:22to stagger animations using the stagger to
02:25or the stagger from to.
02:27In our case, we could use just stagger from
02:30but I'm going to go ahead and use stagger from to
02:32just for the sake of showing the more full version
02:36of the animation itself.
02:39Now, the next step is I want to define what am I animating
02:42which is going to be my paths.
02:44How long do I want each animation to take
02:46and in my case, I want it to take one second.
02:49Next property is from where do I want it to animate
02:52and now I'm going to take advantage of that plugin feature
02:55that was just added in by calling the draw SVG.
02:59And I'm going to define inside of it a value
03:02which could be a hard coded value of a number
03:05or of a percentage.
03:07In my case, I'm going to define a percentage itself
03:11and I'm going to go ahead and put here another comma
03:15and go ahead and also define my ending position
03:18and my ending position in this case is actually
03:20the same as what it was originally
03:22so I could have used just the stagger from
03:25but I'm going to go ahead here and use a stagger from to
03:29and put here 100% basically saying I want to animate
03:32each path from no visibility at all,
03:35from all the way through showing the whole thing.
03:39Last but not least, I want to also define the staggeration
03:41or how much separation is between each animation
03:44and to do that, I'm going to put here 0.3 seconds
03:47making sure that each animation will basically take
03:51just a little bit less than the little 0.3 seconds
03:56in between each animation.
03:58All right, that's all I really had to do
04:00and once I do this, if I run this and again,
04:01I'm going to tell you that it's not going to work
04:03and I'm going to show you why it's not going to work
04:05and then we're going to fix it together.
04:07So if I go ahead and I click on refresh,
04:08we're going to see that we don't really
04:09see anything happening.
04:11We don't see anything unfolding in our logo.
04:14Now the reason why we don't see anything unfolding
04:16in our logo is because technically this logo
04:20only has fills, it does not have any strokes
04:23and this plugin only supports strokes,
04:26it doesn't support fills.
04:28So in this case, because we don't have any strokes,
04:30we're not actually drawing anything in.
04:33To remedy this problem, I've created earlier
04:36onto a second art element which you will find
04:39inside of our art folder, which is called ben.svg
04:43and I'm going to go ahead and just put this right here
04:45so you could see it and ben.svg,
04:47contrary to what we had previously,
04:51is it's all created without fills
04:53but it's created with strokes instead.
04:55I just created an illustrator and an illustrator,
04:59what I did was just draw with the pencil
05:02instead of using the crayon.
05:04So I'm going to go ahead here
05:06and I'm going to run it by just changing
05:08the value here to ben.svg and if I do that
05:11and we want to run our application one more time,
05:14you're going to see that now,
05:16oh, that did not work so let me go ahead here,
05:18I probably have a little typo still.
05:21Draw svg stagger from two paths.
05:29So one thing that I could do
05:31to try to see what's going on here
05:33is I'm just going to do a little bit
05:34of debugging really quickly instead of redoing this video.
05:38I'm going to go to my path
05:38and I'm going to ask for its length
05:41and I just want to see, first of all,
05:43do I have the path, do I have a large amount of items
05:46as I should and if I click on refresh here,
05:49then I'll see, okay, yeah, I am getting 10 elements,
05:51okay, so that's not my problem.
05:53So let's continue, maybe I didn't save this file
05:55after I added it in, there's a probability of that
05:58but then we probably would get an error
05:59for not loading in timeline max.
06:02So that's fine but paths are here,
06:05timeline max is true, stagger from two
06:10seems to be accurate.
06:13The paths is really that item, I want it to be one second.
06:17Draw svg, make sure that I didn't have any typo here.
06:23Everything here seems right,
06:24so let me just give it one more refresh
06:27and there we go, I probably didn't save actually
06:30the initial file and you can see again
06:32that the animation is going on
06:34and it's animating the text element by element.
06:37What probably happened earlier
06:38because I don't think I had any typo
06:40was that I forgot to save the file
06:42which could happen, it could happen to all of us,
06:45even the best of us.
06:46So that's it, we're done exploring draw svg basics
06:51and I'm gonna lead you on to the next video
06:53where you'll see official green sock video
06:56that drives deep into all the different features
06:59of draw svg, so enjoy it
07:01and we'll see you in the next chapter
07:02where we explore another feature of the club members.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!