Creating simple and complex svg. What is SVG? Basics

Happy Victory Day to everyone! And today I am writing the first article in a series in which we will find out what is SVG and how to use it.

SVG(Scalable Vector Graphic) is a language for describing two-dimensional graphics based on XML. This technology is gradually gaining momentum, and this is due to the following:

  • When resizing svg shapes, the shapes are always redrawn, so you will always see a smooth and beautiful image without pixelation
  • The browser does not need to request the image from the server, because he draws it himself, therefore productivity increases
  • You can style objects using CSS, like regular HTML elements, or using JavaScript
  • Can be edited or animated using CSS and JavaScript
  • Smaller size

So, I think you already understand why it is needed and why it is better than a regular image, and now let’s move on to how to use it.

To begin with, it is worth noting that all our figures must be in the tag svg, now let's draw line. A tag is used to display the line line, which has the following attributes:

  • x1- Start x coordinate
  • x2- End x coordinate
  • y1- Start y coordinate
  • y2- End y coordinate
  • stroke-width- Line width
  • stroke- Line color


But what if we need to draw right away several lines? Use multiple tags line it wouldn't be very nice. There is a tag for this polyline.



At the tag polyline there is an attribute points, where we indicate the coordinates x And y separated by a comma, then make a space and write the coordinates of the next point and so on. Notice that there is an attribute here fill, in which we can specify the color in any of the formats css, which will fill the space between the lines. If you want the space between the lines to be empty, then specify the value none.

Let's move on to more complex shapes and draw rectangle.

To draw a rectangle, you need to use the tag rect, where in attributes width And height indicate width And height accordingly, and the remaining attributes are already familiar to you and perform absolutely the same functions as with lines.



To draw a circle, use the tag circle and attributes cx, cy And r to set coordinates center of the circle and him radius.

Another element - ellipse is drawn in exactly the same way as a circle, but there we can set the radii along the axis ourselves x and along the axis y. The tag is used to render it ellipse



If you need to withdraw polyhedral figures, such as a pentagon, then use the tag polygon


Attribute points at the tag polygon works the same as the attribute points at the tag polyline.

If the object is very complex, then you can use any vector graphics editor and draw it there, and then simply insert it like this

SVG supported I.E. c 9 versions, Mozilla Firefox With 1.5 , Google Chrome With 3.0 And opera With 8.0 .

This concludes the article. It turned out big, but simple. In the next one we will continue to study this interesting technology, but that’s all. Good luck!

Today we’ll talk about SVG, what kind of format it is, what it’s used with, and whether it’s worth using. Although the format is not particularly new, it has earned its popularity thanks to its implementation. All latest versions of browsers have already received support for this format.

Why is he so popular? And what exactly makes it different from others? Today we will answer these questions and also tell you what you need to know to get started working with these file types.

What is SVG?

SVG – Scalable Vector Graphics is a vector-based format for web developers. This type of format has become so popular due to the fact that its images can be displayed at high resolutions without loss of quality, since SVG is a vector format.

The impetus for the development of this markup language was first given by the World Wide Web Consortium, which was better known in 1999 as W3C. The W3C gave the concept of SVG - a markup language for creating two-dimensional graphical interfaces and images.

Use in practice

What makes SVG so popular and why are more and more designers starting to use it? It's very simple, it works as it should.

SVG is lightweight compared to other formats. This format has incredible possibilities. Flags, symbols, interface elements. And this is just a small list of how it can be used. The biggest advantage of using it is that it is a vector format, that is, it can be used on any display - whatever their resolution, it will be displayed the same everywhere.

SVG files are quite easy to manage since its files are saved as a static image. You can add interactivity to your website by making very simple but beautiful animations using SVG.

Some examples of where it can be used:

  • Logos
  • Background image
  • Use as a button
  • Cards
  • Diagrams or drawings

As a rule, SVG is most often used when creating responsive websites, animations and other dynamic effects.

Pros of using SVG

So why should we abandon the usual JPG or GIF in favor of SVG? There are quite a few reasons why you should still use it in your project.

  • SVG is a vector format, which is why it is worth using for responsive sites where the size of images depends on the user's display resolution, and SVG fits all requirements flawlessly.
  • SVG images use XML to define their properties, and therefore have the ability to be even more compressed.
  • SVG images are very easy to manipulate, and this opens up even more possibilities for designers who can change colors, add shadows, filters, blurs and many more effects.
  • SVG is pretty easy to understand
  • SVG works with open web standards
  • You can use a regular text code editor to create SVG. This gives you some freedom in your actions, it all depends on your needs and the level of expert knowledge in this area.

Cons of using SVG

You're probably shocked at how many benefits SVG has. And perhaps they thought that it had no downsides. But no, there are definitely a couple, and here are some of them:

  • There is no support for older browsers such as Explorer 8 and below.
  • SVG cannot be used in photographs as it is a vector format and is used to create various shapes and lines.

What's next?

The future of SVG is just beginning. As an accepted image format (and standard) for mobile, SVG will only continue to gain momentum.

SVG files are scalable vector graphics files. To be more precise, it is a language for describing two-dimensional graphics. The basis was the XML markup language.
This format has a number of advantages: being vector images, SVG images maintain quality no matter how they are scaled or resized. SVG images support interactivity and animation. SVG is a W3C standard, which means it has good and high-quality support. You can create SVG files not only in graphic editors, but also in any text editor.
XML-based SVG images are searchable, indexable, and can be scripted and compressed.

Before moving on to drawing without drawing, let's look at the structure of an empty svg file, sufficient for display in any program.

1.
2.
3.
4.
5.

For convenience, the lines are numbered. The first line contains the standard declaration of the XML document.
The second line is an indication of the DTD schema of the document. The third line sets the size of the created document to 600 by 600 pixels. First tag attribute This is the width width, and the second one is the height height. Here the attribute is to indicate the namespace and version. The fourth line will contain everything else.
If you open a drawing created in inkscape in notepad, you can find additional tags that inkscape creates by default. If, when saving a drawing, you select a simple svg, the file structure will be slightly different.
Let's move on to creating the basic shapes. Arm yourself with a notepad and go ahead. We check what we do in inkscape, so we keep it running.

Circle.

To create a circle, add the line to our empty svg file
The attributes are the coordinates of the circle's center (cx and cy) and its radius (r). In this case, the origin of coordinates lies in the upper left corner of the worksheet. Full svg file:





A circle will be created exactly in the center of the document, with a radius of 100 pixels. Since there were no instructions about the fill color, stroke, layers or other effects, the circle is drawn without a stroke and filled with black, which is what we see when opening the document in Inkscape. Please note that there will be no layers in the document.

Attribute designations cx, cy And r is standard. Let's set the stroke and fill of our circle, and also set the stroke thickness to 6 pixels.




fill= "green" stroke= "yellow" stroke-width= "6"/>

The fill is set to green and the stroke is set to yellow.

What’s interesting is that if you create a layer in this document, you can see that the circle drawing will not be on the created layer, but somewhere there? The corresponding attributes are highlighted in red. If you need to create a circle without fill or stroke, set the value of the corresponding attribute to none

stroke= "none"

To change the transparency of the fill, add an attribute opacity, For example opacity= "0.5" reduces the transparency of the fill by 2 times, and to change the transparency of the stroke, add the attribute stroke-opacity, For example stroke-opacity= "0.5" Reduces the transparency of the stroke by 2 times.





Rectangles, squares.

Setting rectangles looks like this





Attributes of a rectangle (square) rect indicate the coordinates of the upper left corner, as well as the width and height of the rectangle. The fill and stroke settings look the same.

Well, in the end there is a circle with a square






To change transparency, we use an already known approach - add an attribute opacity for fill and attribute stroke-opacity for outlining.

To be continued.

Vector graphics are widely used in printing. But we can also use it for websites using SVG ( Scalable Vector Graphic - scalable vector graphics). According to the W3.org specification, SVG is defined as:

A language for describing two-dimensional graphics in XML. SVG allows three types of objects: vector graphics (such as paths made up of straight lines and curves), images, and text.

Despite the fact that SVG has been included in the W3C recommendations since August 2011, this technology is practically not used in web projects, although it has a number of advantages over raster images. In this series of lessons we will introduce how to work with SVG elements on web pages.

Advantages of SVG

Resolution independence

Raster images are resolution dependent. Graphics take on an unpresentable appearance when resizing to a certain scale. With vector graphics, this situation is impossible in principle, since everything is represented by mathematical expressions that are automatically recalculated when the scale is changed, and the quality is maintained in any conditions.

Reducing the number of HTTP requests

SVG can be embedded directly in an HTML document using the svg tag, so the browser doesn't need to make any requests to serve the graphics. This approach has a good effect on the loading characteristics of the website.

Styles and scripts

Embedding with the svg tag also makes it easy to style your graphics using CSS. You can change object properties such as background color, transparency, borders, and so on. In a similar way, you can manipulate graphics using JavaScript.

Easy to edit and animate

SVG objects can be animated using CSS or JavaScript. SVG objects can also be modified using a text editor.

Smaller file size

SVG has a smaller file size compared to raster graphics.

Basic SVG Shapes

According to the specification, we can draw several basic shapes: line, polyline, rectangle, circle, ellipse. All elements must be inserted into the tag ... . Let's look at the basic elements in detail.

Line

To display a line in SVG, use the element . He draws a segment for which two points need to be determined: the beginning and the end.

The start of the segment is determined by the attributes x1 and y1 , and the end point is determined by the coordinates in the attributes x2 and y2 .

There are also two other attributes (stroke and stroke-width) that are used to define the color and width of the line, respectively.

This object is similar to , but using the element You can draw several lines at once.

Element Contains the points attribute, which is used to specify the coordinates of points.

The rectangle is drawn using the element . You need to determine the width and height.

To display a circle we use the element . In the following example, we create a circle with a radius of 100, which is defined in the r attribute:

The first two attributes cx and cy define the coordinates of the center. In the above example we have set the value 102 for both coordinates. The default value is 0.

To display an ellipse we use the element . It works the same as a circle, but we can specifically specify the x and y radii using the rx and ry attributes:

Element Displays polyhedral shapes such as triangle, hexagon, etc. For example:

Using a vector graphics editor

Rendering simple SVG objects to HTML is easy. However, as the complexity of the object increases, this practice can lead to a large amount of work required.

But you can use any vector graphics editor (for example, Adobe Illustrator or Inkscape) to create objects. If you have a tool like this, drawing the necessary objects in them is much easier than encoding graphics in an HTML tag.

You can copy vector graphics commands from a file into an HTML document. Or you can embed the .svg file using one of the following elements: embed , iframe and object .

The result will be the same.

Browser support

SVG has good support in most modern browsers, with the exception of IE version 8 and earlier. But the problem can be solved using the JavaScript library. To make your work easier, you can use the ReadySetRaphael.com tool to convert SVG code to Raphael format.

First, we download and include the library in the HTML document. Then we load the .svg file, copy and paste the resulting code into the function after loading:

In the body tag we place the following div element with the identifier rsr .

And everything is ready.

In the next tutorial in the series, we'll look at how to style SVG objects in CSS.

This post is the first in a series of articles on SVG (Scalable Vector Graphic) graphics, covering the basics of vector graphics on the site.

Vector graphics are widely used in printing. For websites there is SVG, which according to the official specification on w3.org is a language for describing two-dimensional graphics in XML. SVG includes three types of objects: shapes, images, and text. SVG has been around since 1999, and since August 16, 2011 it has been included in the W3C recommendations. SVG is highly underrated by web developers, although it has several important advantages.

broken line

The syntax is similar to the previous one, the tag is used polyline, attribute points sets points:


Rectangle

Called by the rect tag, you can add some attributes:


Circle

Called by tag circle, in the example using the attribute r set the radius, cx And cy specify the coordinates of the center:


Ellipse

Called by tag ellipse, works similarly circle, but you can specify two radii - rx And ry:


Polygon

Called by tag polygon, a polygon can have a different number of sides:


Using editors

As you can see from the examples, drawing basic SVG shapes is very simple, but objects can be much more complex. For these, you need to use vector graphics editors, such as Adobe Illustrator or Inkscape, where you can save files in SVG format and then edit them in a text editor. You can insert SVG into a page using embed, iframe and object:

An example is an image of an iPod from OpenClipArt.org: