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

Loading SVG files

In this chapter, we will take a deep dive into the world of DrawSVGPlugin. By the end of this lesson, you will know how to integrate SVG files into your work directly and by loading them with the help of jQuery.

Video Transcript

00:00Welcome back. So in this chapter we're going to take a deep dive into the world
00:05of draw SVG plugin which is a plugin that you could add into the tween
00:10light and tween max and enables you to animate different SVG files. So I wanted
00:18to show you just a couple of files that I pre-created earlier and you'll find
00:23them inside of our art folder where you'll find our logo and you'll find
00:27ben.svg. We're going to talk about the differences between the two of them in
00:31the next video. I just want you to know a couple of things about the different
00:36ways of how could you integrate SVG into an HTML file and there are probably two
00:42main ways. One is by just taking the actual content of the file for example
00:47ben.svg or for example logo.svg and I could go ahead and if I look at this
00:52I'll see that I'll have this SVG file and literally what I could do is just
00:56copy all this and you can see that I've created it using Illustrator and if I
01:01just copy this I could go ahead and just copy all the SVG. If I want to I
01:05could include the XML information as well the browser should know how to deal
01:10with it so if I grab all this element I could literally just paste it
01:14anywhere inside of my body for example let's say right in the top there and
01:18if I save it and go right back into our browser and click on refresh we'll
01:23see that now we should have our logo and it's quite a massive logo before our
01:28hello world. But I want to show you another way of adding SVG into an HTML
01:34file such as if you want to load it in dynamically which is probably going to
01:37be the more common scenario. So I'm just going to delete that SVG from our
01:41HTML and I'm going to go back into my JavaScript where here we're going to
01:46create a new function we'll call this load SVG and I'm just going to create this
01:55load SVG function you know and let me put it in a sequential order so whoever is
02:01following this will be able to find it in the right place so this was chapter
02:05one and load SVG and let's go ahead and create that process of loading an SVG
02:15file now that we've called it and are on ready. So I go ahead here and just save
02:20it for a second and click on refresh in my browser we'll see that there is no
02:23logo which is exactly what we want to have at this stage. The next stage is for
02:28us to actually load the file and we want to decide I'm going to use jQuery
02:31for that so make sure that you have jQuery loaded up because it's going to
02:35be the easiest way for us to load files and what I want to happen is I
02:39want it to basically load into this slide and because there's only one of
02:43them I could just use that dot slide to get access a reference access to it so
02:48I'm going to go ahead there and just access that specific item by calling the
02:52dot slide and the next thing is I'm going to ask jQuery object that I've
02:57just created to load in that element now I already have this locally so I
03:03know it's inside of my art folder so I'm going to go ahead here and just
03:06send the path to be art and our logo dot SVG which you'll find in the source
03:14files that you could download if you downloaded downloaded it earlier and the
03:20second parameter the first parameter is what we want to load the second
03:23parameter is going to be a callback function and this callback function
03:27accepts a few parameters the first parameter is going to be our response
03:32itself the next will be the status and last but not least will be an info
03:39object which will give us more information so the status is a string all
03:43that's really left for us to do really technically is nothing because if I just
03:48say this I don't even have to do anything but if we want to we could
03:51take a peek at that information that comes back from our response status and
03:56info in case we have something that's broken or in case we want to have a
04:00fallback in case the load doesn't work in our case we're not going to dig into
04:03that because we want to stay focused on the GSAP animation library and in our
04:09case in the next video talk about the draw SVG plugin once we have an SVG
04:14element on the stage but if I go back into my application and I click on
04:17refresh we're going to see that our logo is going to be right there we could
04:21see the string that was sent in in the response itself we could see also
04:25the response which was successful as the second parameter and last but not
04:30least is an object with more information about that success but we're not going
04:33to dig into that at all but before we wrap up this video I want to go back
04:38into our tween library because the problem we have now is our logo is
04:42massive so what I want to do is I want to make sure that it's smaller so
04:46we'll be able to see it so I'm just going to go ahead here and call tween
04:49max.to and I'm going to call my SVG file but it's now inside of our
04:55HTML and we only have one SVG element and that SVG element is this one and now
05:01it's baked into our HTML so what I want to do is call that SVG and add a new
05:06property to it but I really want to animate it and what I want to do is I
05:10want to give it one second to animate to the width of a hundred now because
05:16I was playing around with this earlier I really wanted to center it in that blue
05:19area that we have so I'm going to add also a CSS attribute of padding top
05:24and I'm going to make sure that it goes to position 31 I already pre-configured
05:28that and if we go ahead and press and refresh we'll see that our logo will load
05:32in and it will animate into its position although it didn't do our
05:36padding do I have your small little typo padding oh padding tap instead of
05:43padding top that would do it and let's click on a refresh and we should have
05:47our padding right there and more or less in the center where I want it to
05:51so that's it for this video we've seen how to load in an SVG file and we've seen how
05:56to bake it right into an HTML in the next lesson we're going to take
06:00advantage of those club only features of draw SVG plug-in and see how could we
06:06leverage it to create really cool animations in SVG so we'll see you in the
06:11next lesson where we talk about 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!