SVG graphic format and where it is used. Introduction to SVG graphics Draw a diagram in the browser html svg

Vector graphics are widely used in print media. In the website, we can also add SVG vector graphics (Scalable Vector Graphic). Referring to the official specification on W3.org, SVG is described as a language for describing two-dimensional graphics in XML. SVG allows you to create vector graphic shapes (such as an outline made up of straight lines and curves), images, and text.

Advantages of SVG

SVG offers several advantages over raster graphics, here are some of them:

Scalability

A raster image is made up of pixels and loses quality when scaled, while vector graphics maintain their proportions regardless of scale.

Speeding up an HTTP request

An SVG file can be embedded directly into an HTML document using the svg tag, so the browser doesn't need to make a request. It leads to better performance and less load on the site.

Stylization and scripts

Embedding the svg tag directly into the HTML document also allows us to easily set graphic styles using . We can change object properties such as background color, border transparency, etc. Moreover, we can also manipulate graphics using .

Images can be animated and editable

An SVG object can be animated by using element animation using css or JavaScript (JQuery). An SVG object can be edited with any text editor or vector graphics editor, such as Inkscape or Adobe Illustrator.

Smaller file size

SVG has a smaller file size compared to a raster image.

Creating Simple Shapes with SVG

According to the specification, we can create some basic shapes such as rectangles, circles, lines, ellipses, polylines and polygons using SVG and in order for the browser to display SVG graphics, all of these graphic elements must be inserted into a tag ... ... tag. Let's look at the examples below:

Line

To draw a line in SVG you can use the element . This element is used to draw one line, so it will consist of only two points, Start And end.

"0" y1="0" x2="200" y2="200" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

As you can see above, the origin point of the line is expressed with the first two attributes x1 and x2 , and the end point of the line coordinate is expressed with y1 and y2 .

There are also two other attributes, stroke and stroke-width, which are used to define the color and width of the border. Additionally, we can also define these attributes in an inline style:

Style="stroke-width:1; stroke:rgb(0,0,0);"

she ends up just doing the same thing.

broken line

This element is similar to , but with the help For an element, we can draw several lines instead of one. Here's an example:

"0,0 50,0 150,100 250,100 300,150" fill="rgb(249,249,249)" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

Rectangle

Drawing a rectangle is also easy with this element. We only need to specify the width and height, like this:

width ="200" height ="200" fill="rgb(234,234,234)" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

Circle

We can also draw a circle with element. In the following example we will create a circle with a radius of 100 defined using the r attribute:

"102" cy="102" r="100" fill="rgb(234,234,234)" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

The first two attributes, cx and cy, define the coordinates of the circle's center. In the above example, we have created 102 as both the x and y coordinates, if these attributes are not specified, 0 will be treated as the default value.

Ellipse

We can draw an ellipse using the tag . The creation principle is the same as with the circle, but this time we can control the line's x-radius and y-radius, as well as the rx and ry attributes;

"100" cy="50" rx="100" ry="50" fill="rgb(234,234,234)" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

Polygon

Using the element We can make multi-sided shapes such as triangle, hexagon and even rectangle. Here's an example:

"70.444,218.89 15.444,118.89 70.444,18.89 180.444,18.89 235.444,118.89 180.444,218.89" fill="rgb(234,234,234)" stroke-width ="1" stroke="rgb(0,0,0)" /> < /svg>

Using a vector graphics editor

As you can see, using simple SVG objects in an HTML document is quite easy. However, when the object becomes more complex, this method is no longer ideal.

Fortunately, as we mentioned above, we can use a vector graphics editor, for example Adobe Illustrator or Inkscape .

If you are working with Inkscape, you can save your vector files in SVG format and then open it in a text code editor. Copy all the codes and paste them into your HTML document.

You can embed .svg files using embed , iframe and object tags, for example;

The result will ultimately be the same.

*In this example we are using an image

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 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 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 language markup was first given by the Consortium World Wide Web(World Wide Web Consortium), which was better known in 1999 as W3C. 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 simply incredible opportunities. 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.

A few 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 the bill 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.

This post is the first in a series of articles about SVG (Scalable Vector Graphic), 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.

Advantages of SVG

  • Scaling: Unlike raster graphics, SVG does not lose quality when scaled, so it is convenient to use for retina development.
  • Reducing HTTP requests: when using SVG, the number of calls to the server is reduced, and the site loading speed increases accordingly.
  • Styling and scripting: Using CSS, you can change the graphics settings on the site, such as background, transparency or borders.
  • Animation and editing: using javascript you can animate SVG, as well as edit in text or graphic editor(InkScape or Adobe Illustrator).
  • Small size: SVG objects weigh much less than raster images.

Basic SVG Shapes

According to the official specification, you can draw simple objects using SVG: rectangle, circle, line, ellipse, polyline or polygon using a tag svg.

Simple line using tag line with only two parameters - start points (x1 and x2) and end points (y1 and y2):

You can also add stroke and stroke-width attributes or styles to set the color and width:

Style="stroke-width:1; stroke:rgb(0,0,0);"

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.

SVG (Scalable Vector Graphics) is a vector graphics standard developed by the W3C consortium.

SVG is a markup language for describing two-dimensional graphics applications and images, and is a subset of the Extensible Markup Language XML. This also includes a number of related graphical scripts.

SVG is supported by everyone modern browsers for PC and mobile phones. Some features, such as SMIL animations and SVG Fonts, are not as widespread.

The latest version of the full specification is SVG 1.1.

SVG 2 is under development. It will add new, easy-to-use features for SVG, and is also working on tighter integration with HTML, CSS and DOM.

Advantages of SVG

  • SVG graphics are created using mathematical formulas that can be adjusted when the image size is changed. Thus vector images scale better than raster ones.
  • The size of a vector image is usually smaller than comparable images in JPEG, GIF or PNG formats.
  • SVG graphics have text format, which can be edited in a notepad or drawn in graphic vector editors Adobe Illustrator, CorelDRAW.
  • SVG scripts and animations allow you to create dynamic and interactive graphics.
  • The text in SVG graphics is text, not an image, so it is indexed by search engines.
  • You can add multiple links to an SVG image.
  • You can connect external tables to the SVG format CSS styles, global styles inside the container or add internal styles using the style attribute on shape and path tags.

Inserting SVG into a Web Page

There are several ways to insert an SVG image into a Web page. The first of them is a simple insertion of SVG code into the page (with a large image, the HTML code of the page will become huge and difficult to read). With other methods, you first need to save the SVG code in a file with the extension .svg.

So, here are the ways to insert an SVG image into a Web page:

  • directly inserting code into an HTML document in a container ... ;
  • using an SVG file as a background image;
  • connecting an SVG file to an HTML document using tags img, embed, object And iframe;
  • connecting an SVG file to a PHP document using the function include.
1. Directly inserting SVG code into a Web document
2. Using an SVG file as a background image
3. Connecting an SVG file using the img tag
4. Connecting the SVG file using the embed tag
5. Including an SVG file using the object tag
6. Connecting an SVG file using the iframe tag
7. Including an SVG file using the include function

Coordinate system

Dimensions and coordinates can be specified in various units (px, pt, pc, cm, mm, em, in). If the units are not specified, then these are pixels.

The coordinate origin is the upper left corner of the screen, i.e.

Basic SVG Elements

Example stroke-dasharray Alternating strokes and spaces in a dotted line Example stroke-dashoffset Shift dotted line Example fill Fill color (none – no fill) fill-opacity Fill transparency (from 0 to 1) fill-rule Filling rule.
Possible attribute values: style Element style class Element class

Straight line

Specified by tag .

Tag attributes
Example

RESULT:

broken line

Specified by tag .

Tag attribute
Example

RESULT:

Polygon

Specified by tag . Always displays closed figures, automatically drawing a line from the end of the last segment to the beginning of the first.

Tag attribute
Example

RESULT:

Rectangle

Specified by tag .

Tag attributes
Example

RESULT:

Circle

Specified by tag .

Tag attributes
Example

RESULT:

Complex trajectory

Specified by tag . It is the most versatile of the SVG elements. Allows you to create arbitrary shapes. The shape of a figure is specified by attributes d, the value of which is a set of special commands. These commands can be in both upper and lower case. Upper case indicates that absolute positioning is used, while lower case indicates relative positioning.

Commands that determine the trajectory and direction of a curly line
M,m starting point
Mx,y
L,l Straight segment
Lx,y
H, h Horizontal line
Hx,y or hx
V, v Vertical line
Vx,y or vy
A,a Ellipse arc
Arx,ry x-axis-rotation large-arc-flag,sweep-flag x,y
rx,ry– radii of the ellipse arc;
x-axis-rotation– angle of rotation of the arc relative to the X axis;
large-arc-flag– if (=1), then a larger part of the arc is built, if (=0) – a smaller part;
sweep-flag– if (=1), then the arc is built clockwise, if (=0) – counterclockwise;
x,y– coordinates end point arcs.
C,c Cubic Bezier curve
Cx1,y1 x2,y2 x,y
x1,y1– coordinates of the first control point;
x2,y2
x,y
S, s Smooth cubic bezier curve
Sx2,y2 x,y
x2,y2– coordinates of the second control point;
x,y
The first control point is a mirror image of the second control point.
Q, q Quadratic Bezier curve
Qx1,y1 x,y
x1,y1– coordinates of the control point;
x,y– coordinates of the end point of the curve.
T,t Smooth Quadratic Bezier Curve
Qx1,y1 x,y
x,y– coordinates of the end point of the curve.
This command's checkpoint is a mirror image of the previous command's checkpoint.
Z, z Closing a path
Example

RESULT:



SVG is an image format for vector graphics. Literally, this is nothing more than scalable vector graphics. That is, this is exactly what you work with in Adobe Illustrator. Using SVG in web development is fairly easy, but there are some features worth considering.

What is SVG used for?

  • Small file size that compresses well
  • Scaling to any size without loss of quality
  • Looks great on retina displays
  • Full design control in interactivity and filters

Let's draw something for the upcoming work in Adobe Illustrator. Let's take the Kiwi bird:

Please note that the canvas is trimmed exactly along the edge of the picture. An SVG canvas is constructed absolutely identically to a PNG or JPG. You can save the file directly from Adobe Illustrator as an SVG file.

Once you save the file, another dialog box will appear with SVG options. To be honest, I don't know much about all the settings presented in this window. There is a specification for SVG profiles, so if you are interested you can read it. I find SVG 1.1 works great.

The cool thing here is that you can either click OK and save the file, or click the “SVG Code...” button and a text editor will open with the SVG code.

Both methods can be useful.

Using SVG as an img tag

In Illustrator our canvas turned out to be 612px ✕ 502px.

How large the image will be on the page is up to you. You can resize an image by defining the width and height properties, in exactly the same way as with PNG or JPG. Here's an example:

Check out this Pen!

How to make cross-browser SVG

When using SVG in this way, you need to be aware of different browser support. Essentially, SVG works everywhere except IE8 and below, as well as Android 2.3 and below.

If you need to use SVG, but also need support from the above browsers, there are several options to solve the problem. I have used several techniques in various projects to solve this problem.

One way to check SVG support is Modernizr, with which you change the src path:

If (!Modernizr.svg) ( $(".logo img").attr("src", "images/logo.png"); )

David Bushell came up with a very easy one alternative way but it contains javascript in the markup:

The SVGeezy service can also help. As we continue this article, we will look at many different backup methods for SVG support.

Using SVG as background-image

Similar to the img tag, SVG can be used as a background image:

Kiwi Corp .logo ( display: block; text-indent: -9999px; width: 100px; height: 82px; background: url(kiwi.svg); background-size: 100px 82px; )

Note that we are setting the background-size to be exactly the same as the element's size. This must be done because otherwise we would see a small top part original image. These numbers take into account the original proportions of the image. You can also use background-size as values keywords, for example contain, so that the image completely occupies the entire element.

Using SVG as a background image affects browser support, but in general, everything is the same as when using the img tag.

Modernizr can help us even more here effective way than in the case of img. The point is, if you replace background image with a supported format, then only one HTTP request will be executed instead of two, as is the case with img. Modernizr adds the class name “no-svg” to HTML tag if the browser doesn't have SVG support:

Main-header ( background: url(logo.svg) no-repeat top left; background-size: contain; ) .no-svg .main-header ( background-image: url(logo.png); )

If you're having trouble using the above two ways to use SVG, below are more ways to lay out vector graphics.

Using “inline” SVG

It was mentioned above that when saving a picture in SVG format, using the Illustrator editor you can get the correct SVG code (you can also open the file using text editor and copy this code). You can copy this code into your HTML and the SVG will render exactly the same as using img.

This can be useful since the image image comes along with the code in the document and does not require an HTTP request. In other words, the benefits are the same as using Data URIs. However, there are also disadvantages. By inserting straight SVG code, the document starts to look like a big, bloated piece of crap.

There is also an option to insert SVG on the server side:

SVG optimization

Adobe Illustrator does not perform optimization actions by default on the resulting SVG image. It provides DOCTYPE and notes, and it's all pretty much garbage. SVG is pretty light weight by default, but why not make it even smaller? Peter Collingridge provided online tool to optimize SVG Optimiser. Using this service you can upload old file and get an optimized new one.

If you want more hardcore - no problem, here is a tool for optimizing SVG using server-side javascript Node JS tool https://github.com/svg/svgo

SVG styling

See how SVG is similar to HTML? This is because they are both XML data. In our design there are two elements that form the basis, these are the ellipse and the path. We can easily specify classes for them via HTML code.

Now we can control these elements using custom SVG CSS. This CSS does not have to be directly embedded in the SVG, it can be placed absolutely anywhere. Please note that SVG elements have a special set of styles that are created specifically for working with vector graphics. For example, not the usual background-color is used, but fill. Although some regular styles also work, for example:hover.

Kiwi ( fill: #94d31b; ) .kiwi:hover ( fill: #ace63c; )

SVG has fun filters. For example blur:

...

You can then apply this in css if needed:

Ground:hover ( filter: url(#pictureFilter); )

Here's what happened:

Check out this Pen!

Support for “inline” SVG in browsers

List of browsers that support this mode SVG displays can be viewed here http://caniuse.com/#feat=svg-html5. Again, there is no support in IE8 and Android 2.3.

One of the fallback options for this type of SVG:

...

Then we use Modernizr again:

Logo-fallback ( display: none; /* Make sure it"s the same size as the SVG takes up */ ) .no-svg .logo-fallback ( background-image: url(logo.png); )

Using SVG as an object

If using an “inline” SVG isn't your thing (keep in mind that there are downsides to this option, like lack of caching), you can bind the SVG to an object and then change it using css:

For cross-browser support we use Modernizr:

No-svg .logo ( width: 200px; height: 164px; background-image: url(kiwi.png); )

This option works great with caching and has the most browser support of all of the above. But to act with using CSS for such an object, you will have to write styles directly into the SVG file.

...

Using Data The URI is a way to reduce the weight of the SVG. Mobilefish.com provides an online optimization tool for this purpose. Just paste the contents of your SVG file and fill out the form, then the result will be displayed in a text field that can be copied. It looks like this:

You can use this code anywhere! For example:

Logo ( background: url(data:image/svg+xml;base64,); )

And by the way, if you have an inline style in the SVG that is before base64, it will work if you use it as an object!