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

Animating SVG

There is no better way to wrap up our section on SVG by seeing how we can animate the art we created so far. By the end of this lesson you will know how to work with the animate and animateTransform nodes of SVG.

Keynotes:

  • Creating an animation in SVG.
  • Transforming animations with AnimateTransform.
  • Wrapping up the section.

Video Transcript

00:01In the last lecture of this section about SVG,
00:04our last lecture is animating SVG.
00:07In it, we're going to learn
00:08how to create an animation in SVG.
00:11We're going to do that using transforming animation.
00:13Well, it will be transforming animation.
00:15It's kind of similar to what we did in CSS3
00:18using the property animate transform.
00:21And we'll wrap up this section that way
00:24once we got all of our SVG moving around on the stage.
00:28So let's jump right into it and let's start animating.
00:31All right, I have to give you a notice and a warning.
00:33This probably won't work on every browser,
00:36but it is on Firefox.
00:37Currently, this is very cutting edge.
00:39We're going to animate.
00:40No better way to wrap up this section.
00:43So let's go ahead and go into our SVG
00:45and I'm going to show it to you in action.
00:47And I'm going to show you two types of animations
00:49and they're more.
00:50So make sure to check in your documentation
00:52in our references to find out more things that you can do.
00:56So I'm going to start with our polygon
00:58and I'm going to go ahead.
00:59Let's start with the text, which is more simple.
01:02So I'm going to go into my text and just go inside of it
01:04and we're going to create a new property
01:07and we're going to call it animate.
01:09And the animate property basically enables us
01:12to give us different information and rules
01:14to tell the browser and SVG what to do.
01:18And we're going to start off by defining
01:20what type of thing do we want to animate?
01:22Do we want to animate CSS
01:24or do we want to animate the XML itself?
01:28So I'm going to set an attribute type
01:30and I can set here or as CSS or as XML.
01:33In this case, I want to animate the XML,
01:35the X and Y coordinates of our text.
01:38Beautiful.
01:39So now that we configured the type,
01:41it's time for us to define an attribute name.
01:45Now the attribute name is what do we want to actually animate?
01:48In this case, I want to animate the X coordinate.
01:51And the next item is the from.
01:53From where do I want to animate it?
01:54Well, I want to animate it from a negative 500 pixels
01:58of where it's at.
01:59And I want it to go to where technically
02:02to where it is already right now.
02:04How long do I want it to be?
02:06That's going to be our duration.
02:07I want it to be two seconds.
02:09How many times do I want it to repeat?
02:11Well, I kind of want it to repeat twice.
02:14And literally that's all I had to do
02:15to create a very simple animation.
02:18If I go back into my browser and I click here on refresh,
02:20you'll see that the text itself is going to animate
02:23into its position twice until it locks into its position.
02:27Beautiful, now that we configured that
02:29and now that we set that up,
02:31let's go ahead and take a look at how to animate a polygon
02:34or really how to animate transforms,
02:36which is a bit more complicated,
02:38but don't worry, not much more complicated.
02:41I'm going to go ahead and create a polygon,
02:44basically create a self-enclosed polygon,
02:46open it up from being self-enclosed
02:48to basically being an object.
02:50So I could put inside of a new animate object
02:52that will define the type of animations
02:54that would go in it.
02:56Now I'm just duplicating that animate,
02:57but instead of animate,
02:58I actually want it to be an animate transform.
03:01That's a little bit different,
03:02although it has very similar attributes.
03:05The big difference is that now it will know how to refer to
03:08and work with these methods
03:10that live inside of a transform.
03:12The attribute name that I want to control is the rotation.
03:16So I'm going to go ahead and set the rotation.
03:18We still want the attribute type of XML.
03:20From where do I want to go?
03:21Well, let's go a full 360 degree circle.
03:26And how long do we want it to be?
03:28Why not?
03:29Let's keep it at two seconds,
03:30but let's set the repeat count.
03:32Instead of sending it to two,
03:33I'm going to set it to be indefinite,
03:36making it sure that it will run forever.
03:39If I go back into my application and I click here
03:41on refresh, notice what's going to happen.
03:44That didn't work.
03:45Let me just go ahead and save this,
03:46make sure that animate transform.
03:51Attribute type XML, that's okay.
03:58Attribute name, oh, not rotate.
04:01I actually wanted to do a transform.
04:02That's the name, my silliness.
04:06Now, the next step here is what I want to do
04:07is I want to set a type for it.
04:09And in the type,
04:10that's where I'm going to set the rotate.
04:12Okay, so apologies for that mistake.
04:15So I'm setting the attribute name to transform
04:18because that is the attribute.
04:19And it has a type which is rotate,
04:20which will enable us to approach the rotate
04:23and control the rotate.
04:24Phew, that was close.
04:25Okay, let's go ahead and click here on refresh
04:27and it should now work.
04:28And now we're going to see our triangle flying around,
04:31but its center point is right now position
04:33at that zero zero coordinate.
04:35We want to change that.
04:36To change that,
04:37I'm going to set another two parameters for our rotate
04:40that are acceptable for the rotate
04:42that will define where is its center point.
04:44And in this case,
04:45I'm going to define it to be in the point
04:47where we translated our object.
04:48So it's going to be roughly at 360 by 20.
04:52And I'm going to make sure that in the from
04:54and also in the to the center point will be the same.
04:57And if I go ahead and save this,
04:58this is for the X and this is for the Y.
05:00And if I go ahead and click here on refresh,
05:02I should have the center point
05:03basically animating around our center point.
05:07Beautiful.
05:07And that's exactly what I wanted to happen.
05:09And we just created two really cool animations
05:12that might not work on your browser,
05:13but should be working at some point on most browsers.
05:19This section was all about SVG
05:21and we just wrapped it up with an animation
05:24that was also built natively inside of SVG.
05:27The next section is all about the HTML5 Canvas.
05:31So we'll see in the next lecture,
05:33which will be in the next section
05:34that will be dedicated to that.

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!