ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A HTML5 Tutorial: Creating Simple Graphics With SVG, Scalable Vector Graphics

Updated on May 6, 2014

SVG is a Quick Means to Enhance HTML Documents

Scalable Vector Graphics (SVG) is a way to create and incorporate images in a web page. The images are defined in the XML language. SVG is a W3C recommended practice.

SVG is relatively easy to learn and has a number of advantages over using other image formats such as .jpeg and .gif. Some of the advantages are:

  • you have a specific need for a simple two dimensional picture and cannot find anything on the web that matches your specific need.
  • you don't need to worry about copyright infringement because the drawing is your own work.
  • you don't need to acquire expensive software. SVG can be created with any text editor.
  • SVG can be compressed, is searchable, can be scripted
  • SVG images can be zoomed without degradation.
  • and can be embedded in web pages
  • it has support for event handlers.

Of course, nothing really comes without some costs, chiefly is that rendering can be slow for large complex drawings and naturally there is some learning involved.

SVG Images Can Become Quite Complex

With SVG and a lot of patience you can create images like this.
With SVG and a lot of patience you can create images like this.

The <svg> HTML Tags

To start, one must enclose the code between the <svg> and </svg> element tags.

The next thing you will probably want to do is set width and height attributes for the image. Color for an image part is set with the fill attribute.


SVG Predefined Shapes

SGV has a number of predefined shaps which a developer can use:Circle <circle>

  • Circle <circle>
  • Ellipse <ellipse>
  • Line <line>
  • Path <path>
  • Polygon <polygon>
  • Polyline <polyline>
  • Rectangle <rect>


Creating a Simple Circle

The snapshot shows the code and the circle created.The SVG tags needed is <circle> which is self-closing.

The attributes besides width, height, and fill color include the circle attributes:

  • cx and cy - are the coordinates of the center of the circle.By default they are set to (0,0).
  • r is the radius of the circle in pixels.

Creating a Simple Circle

In this example cx and cy are set, otherwise they would default to 0,0 in which case the circle would disappear.
In this example cx and cy are set, otherwise they would default to 0,0 in which case the circle would disappear.

Creating an Ellipse

To create an ellipse one needs to define values for rx and ry that differ from each other unlike the circle where the values are the same

A Simple Ellipse

The only difference between this shape and the circle is the radius values for x and y are not the same.
The only difference between this shape and the circle is the radius values for x and y are not the same.

A Simple Line

The values needed for the line besides stroke at the beginning and ending points defined by (x1,y1) and (x2,y2) as illustrated in the snapshot which follows.

A Simple Line

One needs to define the beginning and ending points of the line x1, y1 an x2 ,y2.
One needs to define the beginning and ending points of the line x1, y1 an x2 ,y2.

Creating a Simple Polygon

The <polygon> element is used to create a graphic that contains at least three sides such as a triangle. In order to create a polygon you need to have at least 3 x,y coordinate pairs.

Polygons are made of straight lines, and the shape is "closed".

A Simple Triangle (Polygon)

A polygon is define bby at least 3 x,y coordinate pairs.
A polygon is define bby at least 3 x,y coordinate pairs.

Creating a Jagged Line With <polyline>

A polyline is just a figure created with line segments.
A polyline is just a figure created with line segments.

Creating a Path

We create a path by specifying the <path> element with any of the following commands. The commands are enclosed in a path data value, "d". The commands when they are specified in uppercase means the positioning is absolute. When they are specified in lowercase positioning is relative.

  • A = elliptical Arc
  • C = curveto
  • H = horizontal lineto
  • L = lineto
  • M = moveto
  • Q = quadratic Bézier curve
  • S = smooth curveto
  • T = smooth quadratic Bézier curveto
  • V = vertical lineto
  • Z = closepath

The <path> element can create complex drawings, however if you are going to be creating many complex drawings you might want to consider a free application such as Inkscape.

Creating a Simple Triangle Using the <path> Element

In this example we created a triangle with the  element even though we could have used the  element.
In this example we created a triangle with the element even though we could have used the element.

Creating a Simple Rectangle

A basic rectangle is shown in the snapshot which follows. The element names are <rect> and </rect> The attributes are relatively simple. One would specify width and height. The style attribute lists the CSS attributes:

  • fill - lists the color of the rectangle
  • stroke - lists the color of the border
  • stroke-width - lists the width of the border.

Two additional attributes are sometimes used fill-opacity and stroke-opacity. Their legal values are in the range 0 to 1.

A Simple Rectangle Defined in SVG

The HTML <text> Elements

Text can be created with the use of <text> and </text> tags. Beause it is an image that you are working with one can apply a number of special effects. The <test> element can be further refined by sing the <tspan> element. We will defer this for now and address it in a tutorial about special effects.

Using the <text> Element

Defining text with the HTML SVG  element opens up a way to added special effects. We will take up that subject in our second SVG tutorial.
Defining text with the HTML SVG element opens up a way to added special effects. We will take up that subject in our second SVG tutorial.

Wrap Up and What's Next

In our next tutorial we will take SVG to a higher level by showing some advanced elements and attributes which results in some very striking web pages.

Please Rate This Tutorial on Its Usefulness

Cast your vote for Was the tutorial easy to follow?

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)