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

Creating a basic SVG image

Before we can learn how to integrate SVG into an HTML document we need to know the basics of how to work with SVG. In this lesson we will learn how to define and create an SVG element.

Keynotes:

  • Declaring the SVG format.
  • Defining a version to our SVG.
  • Drawing out a rectangle with a fill and stroke.

Video Transcript

00:00Welcome back. In our third section our focus is going to be SVG. SVG is a
00:06technology that is really cool that's been around for a while that is based off
00:10of XML that is intended for drawing out vector illustrations. In this section
00:17we're going to learn how to work with it and get introduced to it. We're going
00:20to go ahead and create a basic SVG element and we're going to draw out a
00:26rectangle. We're going to draw out a circle. We're going to also go ahead and
00:30go through more steps involved with the process of defining and creating an SVG
00:37image. Once we know the basic constructs of how to work with SVG we'll go on into
00:42integrating it into HTML5 and we'll see the differences between traditional SVG
00:48and SVG that is integrated with HTML5. Once we're done with that integration
00:55process we're going to then go ahead and create our most complicated visual
00:59element in SVG which is going to be a polygon. Last but not least we'll make
01:05sure to automate it all using native SVG technologies to do that. So let's jump
01:10right into our first lecture which is creating a basic SVG image. In this
01:15lecture we're going to go ahead and actually declare the SVG format and get
01:20introduced to the basics of how it works. We're going to also declare and
01:23define the version of our SVG and understand why that's important. Last but
01:28not least we're going to go out and actually draw out a shape. We're going to
01:32draw out a rectangle and as we do that we'll be introduced to the fill and
01:36stroke which are the two main methods that enable us to fill out content in
01:40an outline that we define. So let's jump right into it and let's start
01:44creating our first SVG image. SVG is an integral part of really the modern
01:52web today in HTML5. So let's go ahead and understand what it is at all. We're
01:56not going to learn everything about SVG but what we're going to do is we're
02:00going to create a new SVG file starting from an image that has nothing in it.
02:04It is a vector based image format. Go ahead into your source files and just
02:09find your sample SVG and open it up on your browser directly and also open it
02:14up in your editor. And we're going to work directly inside of the file
02:18introducing the basic concepts of SVG. Now the first first basic construct just
02:24like in HTML is defining a root element which is the SVG element. We'll open and
02:30close it and that's it. We've created more or less a legal SVG already at this
02:35stage. All that's left for us to do really if I go ahead and save this and
02:38go and click on refresh we're probably not going to get an error although it's
02:41not really going to do what we want it to do. Now this is just a plain old XML
02:45file which is not what we really want. We really want to tell the browser by the
02:49way this SVG file is literally an SVG. It's not just looks like one it actually
02:55is one. To do that we're going to have to create a namespace for our element and
03:00I'm going to do that by setting a XML and S equals and then define inside of
03:06here the definition of the type of file format it is or the type of XML
03:11that it is. To do that I'm going to type ahead HTTP colon colon www.w3.org
03:19slash 2000 slash SVG. By doing this I declared to the browser and whoever is
03:25going to interpret this that this is an SVG file. Beyond that I also want to tell
03:30the browser what version of this file format it is and to do that I'm going to
03:35type here version and set a version type which currently it's standing at 1.2 but
03:41we're going to go for 1.1 which is the most common version currently available
03:45on most browsers as well. If I go ahead and click here on save and go back and
03:49click on refresh we're going to see something a little bit different. Now
03:52we're not going to see the SVG file itself as a source file but the browser
03:56is going to interpret it and display it as the content that's within. Now one of
04:02the first issues that we have right now is that we didn't define the size
04:06of our canvas. When we're talking about canvas I'm really talking about the
04:10area that is going to be visible in the application. To define the visible area
04:15in the application I'm going to add two new properties which will be the width
04:18and I'm going to set a width of let's say 500 and I'm also going to go ahead and
04:24set a height and I'm going to set a height of 450. Now just by doing this
04:28we created our canvas but if we go and click on refresh again we're
04:33not really going to see much but we do have here an element. For us to be
04:37able to see what's going on here and because I'm working with SVG version 1.1
04:41and not 1.2 I can't just go ahead and color the canvas it's transparent.
04:46Instead of that what I'm going to do is I'm going to go inside of my SVG and
04:50introduce to you our first actual visual element our first basic element
04:57and that basic element is a rectangle which is named rect and every element
05:03especially one that is self enclosed I'm just going to close it and I'm going
05:06to define for it a width and I'm going to say that its width is going to be
05:09exactly the same thing and as our total canvas size. I'm going to go ahead and
05:15define a fill color so we'll actually be able to see something and we're going to
05:19draw something out so literally what I did is define the size of my element
05:22and then I set a fill. Now by the way the fill command is one of those basic
05:28attributes that are available to basically every single or almost every
05:32single element in SVG not including the actual SVG canvas itself which is
05:37transparent by default. If I go ahead and click here and save I should now see a
05:41rectangle that is going to take the full space of our 400 by 500 by 450 square
05:48that we've created. All that's left for me to do 500 by 450 all that's left for
05:54me to do is nothing I created a rectangle but I want to talk about a
05:57few more things that are just throughout every SVG element and we're going to
06:02meet this also in canvas in a later section. Every single element could have a
06:08fill or a stroke or it could have both. Now in this case if I set my stroke
06:13size and I define my stroke size to be not sizes I define my stroke color
06:18let's say I decide I want it to be black so I'm going to go ahead and set
06:22it to be black or I could go ahead and put here a hexa de mesle value or
06:26only valid color value that would define the colors in this case it would set it
06:31to be black. Now because my size of my width and height are literally taking the
06:35full area of our application you'll see that it we're only seeing a half a
06:39pixel really of black here. The reason why we're only seeing a half a pixel is
06:44because when we're talking about strokes strokes are and if I'll just go
06:49ahead here and I'm just gonna what I really want to do is I want to just
06:53explain it very very clearly is that when I define this my stroke itself it's
06:58default size if I'm going to go to my stroke width and add that as another
07:03property and I'm going to set it to be zero. What that will do is basically make
07:07it completely go away. Now imagine the stroke itself is actually starting
07:11right here because our rectangle is taking the full visible area of our
07:15canvas. If I go ahead here and I say hey I want it to be 10 I want it to
07:19be a really big 10 pixels. Notice that right now I'm only seeing roughly 5
07:24pixels. The reason why I know that is also if I go ahead here and just make my
07:28width a little bit larger let me make make it just a little wider our full
07:32canvas size a little wider notice the size of my element and notice how it's
07:36doubling in size the width on this area. The reason is is because a line
07:41itself contrary to a stroke starts from the middle so if it's starting from
07:45the middle it's gonna grow 5 pixels to both ends but because our width and
07:49height is this full width and height of our canvas area then we're not seeing the
07:54full spectrum of it. So that's really the basics of fill and stroke and the
08:00basics of really drawing and creating an element in SVG. Congratulations you just
08:06created your first rectangle in SVG. In the next lecture we're gonna go ahead and
08:12move on to creating circles but we're also going to talk about how to
08:16position elements smartly. So we'll talk about that in the next lecture.

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!