Svg features. SVG graphic format and where it is used. What's next

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. Graphics can be manipulated in a similar way 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 set the value to 102 for both coordinates. The default value is 0.

To display an ellipse we use the element . It works the same as 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

Outputting 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.

Let's leave aside the question of the advisability of using SVG on a website. Everyone must determine for themselves the usefulness of this technology. Moreover, this topic has been raised several times already.

Now we will look at SVG embedding methods, their pros and cons, as well as the possibilities for manipulating SVG elements.

The article is intended primarily for those who still do not use vector graphics on their sites, but really want to have one foot in the future and present.
For those curious, I’ll immediately give you a summary table:
1 You can change the color, size, alignment, and other styles of plain text
2 Styles must be written either in the SVG file itself, or connected with an external style in SVG at the beginning of the file:


In truth, styles written inside SVG will also work when using an IMG or background-image tag, but this makes no sense.

Yes, you can create an icon font from SVG, and there are freely available icon fonts. But there are a number of serious limitations. Like any SVG font symbol, icons in a font cannot have more than one color.
Here are several services where you can download ready-made icon sets, or upload your own and create your own icon font:

Please note that when creating your own font, you need to convert all objects into paths. Tags and attributes that will be skipped: circle, rect, stroke, stroke-width, fill, fill-rule.
When using an icon font, all elements of an SVG object are combined into one symbol, and you can interact with it via CSS and JS only as a font symbol: change the size using font-size, change the color using color, animate using CSS animation or JS and other.


+ the icon behaves like a font symbol, and all parameters are configured in the same way via CSS (size, color, alignment, etc.);
+ the only way that works in IE 8 without additional manipulations;
all elements of an SVG file are combined into one symbol, so it can only be manipulated using CSS or JS as a single unit;
Only single-color icons are supported;
if the font download fails, the user will either not see the icons at all, or, if the icon codes match the Unicode characters, the corresponding symbols will be displayed.

SVG as OBJECT



Unfortunately (or fortunately) codepen and jsfiddle block the loading of external objects for security reasons.
The embed looks like this:


The object embeds a data attribute element like an iframe, adding the markup of the included file inside it, so the elements can be accessed using JS, but not in a very usual way:

Var object = document.getElementById("'object'"); //get the element object var svgDocument = object.contentDocument; //get the svg element inside object var svgElement = svgDocument.getElementById("some_id_in_svg"); //get any element inside the svg svgElement.setAttribute("fill", "black"); //change the attributes of the selected element
It is worth noting that in CSS styles for SVG elements differ from the standard ones; the full list of styles supported by SVG can be viewed.
SVG does not behave like a regular image; it cannot be disproportionately transformed by setting the width and height. The object inside will occupy the maximum area and be centered in the container:

But the object can be transformed using CSS like this:

Transform: scale(2, 1);
IE 8 and below do not support SVG from the word “at all,” so if you have this specific audience among your site’s users, you should take care of checking and replacing the svg with a raster image. There are many ways to do this, for example using Modernizr to add a .no-svg class for body:

If (!Modernizr.svg) ( $(body).addClass(“no-svg”); )
.no-svg .icon ( width: 100px; height: 100px; background-image: url(“icon.png”); )
Pros and cons of this approach:
+ you can use an external CSS file to manage styles;
+
+ Interactive animations are supported;

Both embedding methods are somewhat similar to embedding using the object tag, for example you cannot change the proportions by changing the width and height of the container, but they have more restrictions.
External styles included in SVG will not work, and accessing elements via JS will also not work. Interactive animations in SVG won't work either. But problems with IE 8 and below still remain.
But SVG animations will work in both cases.
In the case of IMG, embedding looks like a regular picture:


In the case of background-image as a regular block:


.icon ( background-image: url("icon.svg"); width: 90px; height: 150px; )
Also, using background-image, you can use sprites, as with png images, and you can change the size using background-size:

Background-size: 90px 150px;
Considering that the percentage of people with device-pixel-ratio screens higher than 1 and their devices do not support svg tends to zero (if there are any), then you can use media expressions to connect svg, only for them, and for the rest use the png version:

Icon ( background-image: url("icon.png"); ) @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min--moz-device-pixel-ratio : 1.5), only screen and (-o-device-pixel-ratio: 3/2), only screen and (min-device-pixel-ratio: 1.5) ( .icon ( background-image: url("icon.svg "); ) )
Pros and cons of these approaches:
+ SVG animations and filters are supported;
+ in the case of background-image, you can use SVG sprites;
You cannot change the properties of SVG elements via CSS or JS;
Interactive animations are not supported;
For IE 8 and below, it needs to be replaced with a bitmap.

In this version, the SVG code, which can be obtained by opening any SVG file with a text document, is embedded directly into the page code.
Undoubtedly, this design impairs the readability of the code and increases its size, but it opens up new possibilities.

For example, having a set of icons in an SVG file, you can reuse them with a simple design like:


Where some_svg_element_id id of the element inside the original SVG file.

For example, you can apply SVG transformations to a single element:


But if an interactive animation was applied to an element inside the original SVG, for example on a click, as in the demo above, then when duplicating an object, the animation will work on all elements at the same time.
SVG animations and filters are a topic for a separate article, so I’ll limit myself to

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 has been included in recommendations since August 16, 2011 W3C. 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 it in a text or graphics 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:

Browser support

SVG is supported by almost all modern browsers except Internet Explorer 8 and below. But this can also be fixed using the javascript library Raphael.js. You can convert an SVG file to this library format at ReadySetRaphael.com.

First you need to connect the Raphael.js library to the desired page, then download the SVG file, copy and paste the generated code into function:

Window.onload=function() ( //insert Raphael code here)

Insert on page div with attribute rsr.

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 all modern browsers for PCs and mobile phones.

Some features, such as SMIL animations and SVG Fonts, are not as widespread.

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 images.
  • The size of a vector image is usually smaller than comparable images in JPEG, GIF or PNG formats.
  • SVG graphics have a text format that can be edited in a notepad or drawn in the vector graphic editors Adobe Illustrator and 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 CSS style sheets to the SVG format, 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 And object;
  • 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 an 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)
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 of the end point of the arc.
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:



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. This results in 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