SVG, graphics in XML for the Web
SVG, Scalable Vector Graphic, is a specification of documents to describe scalable 2D graphics in XML.
It is compatible with XML 1.0.
It is mainly used in replacement to Flash for creating graphics for the Web.
There are frameworks to build Web applications in SVG.
Why use SVG?
SVG allows to put graphics inside a web page, or a desktop document that can interacts with textual data. A SVG page may be built from a database, it may be searched by a search engine. You can turn XML numeric data into graphics by an XSL transformation.
Compared with Canvas, SVG is better suited as a format for graphics produced with a drawing program, to be integrated into a web page, while Canvas is better for graphics dynamically created in the page when it appears.
Compared to a bitmap picture, SVG has the advantage of resizing without loss in definition, and allows interaction with the user.
You can display a city or a building and allow a user to visit it by selecting locations.
SVG is a standard defined by the World Web Consortium and may be freely used at production or end-user level without license to pay.
How to use SVG?
A SVG file is an XML document with graphical tags. It is viewed with a common
browser with a plug-in, or a special viewer.
SVG may be used:
- As a standalone web page, loaded directly or through a link <a> tag.
- Inserted into a HTML page in <svg> tag, see example below.
- Embedded with the include tags: img src, object, applet, iframe.
<embed src="webmasting.svg" type="image/svg+xml" width="200" height="100">
Main functions of SVG
- Colors, opacity, gradients, textures.
- Fill: painting the content of a shape or text.
- Stroke: painting the outline of a shape or text.
- Filter effects.
- Template of objects.
- Inserting symbols or images at coordinates.
- Interactive, event handling.
A SVG file must have the ".svg" extension under Unix and Windows.
A compressed, zipped SVG file must have the ".svgz" extension.
In the same manner you can save an image taken from a web page, you can save SVG either as .svg or .svgz.
A SVG file has no doctype but an informative svg tag instead:
<svg version="1.1" baseprofile="full" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events">
How to program SVG documents?
To build a SVG document you need for a canvas. Batik
provides a such Java canvas named "SVGCanvas2D" that convert your
drawing into SVG.
If you want visually build a SVG document, you need for a component that can display SVG content and let the user interact with that content (insert object, rotate, put text, etc...). JSVGCanvas is a Java component to do that.
You need also to manipulate the SVG document from a scripting language with a DOM API. DOM (Document Object Model) is an object oriented representation of the document. Events may be assigned then to SVG elements.
All these modules are provided by Batik for Java programmers.
Components of SVG
A SVG document is made of:
- Vector graphic shapes.
- Containers of other elements.
- Styling properties.
- Canvas: a surface where to draw.
- Viewport: a rectangular region in a canvas.
- Use: a template.
- Property: a parameter for how to render the document.
- Graphical shapes: rect, circle, ellipse, line, polyline, polygon.
- Symbol: reusable object as flowchart, electronic symbols.
- Container, made of other elements:
- g: a container for grouping graphic elements, used as partial canvas.
- svg: a document.
- switch: a list of alternative elements with conditions for one selected.
- clipPath: masked elements.
- mask: use objects as mask.
- pattern: a texture.
- a: a link.
SVG vs. Canvas
As can be seen in the examples below:
- SVG is a descriptive language, composed of tags that make up a page, or incorporated into a web page.
Or a drawing may be made with Inkscape to produce a file that is embedded in an HTML page.
SVG tags can contain HTML tags. A link tag <a> for example. This is not the case for Canvas.
We can build a game with both SVG Canvas.
Some studies was made to compare SVG and Canvas, see the articles SVG vs. Canvas at decision time.
SVG and XMLXML may be translated into SVG with an XSL definition. This allows documents produced by application, statistics for example, to be turned directly into a graphical view.
It is compatible with HTML 4.0 and XHTML 1.0
The following XML extensions may be used on SVG documents:
- XLink: for referencing.
- XPointer: syntax for referencing element
- Stylesheet: styled either by CSS 2 or XSL.
- DOM: included in SVG and compatible with DOM 2 plus event handling.
- Incorporates and extends SMIL, the animation language.
Converting SVG to GIF, PNG, JPG or other raster image
This is easy, just export the image in the raster format. The result is not perfect.
Make a screen shoot
- Set the screen to a resolution high enough to display the whole image.
- Use a capture tool such as IrfanView or XnView or a specialized tool as MWSnap, and start capturing in background task for the current window and client.
- Display the SVG file with a modern browser and press the capture key.
- Save the capture, as a PNG or JPEG file.
FOP is a java tool from Apache to convert SVG to PDF.
Hello World! in SVG
Example of a minimal program, running on all modern browsers.
<?xml version="1.0" encoding="utf-8"?>
<svg width="220px" height="120px" xmlns="http://www.w3.org/2000/svg">
<text font-size="32" x="25" y="60">
SVG in a HTML page
You can insert the following code into an HTML page, it will appear perfectly ...
<svg width="220px" height="120px">
<text font-size="32" x="25" y="60" fill="green">
Simple demo: Tetris
Load the SVG file to run the demonstration.
More informations and demos
- Mozilla SVG samples.
- Adobe and SVG. Plug-in for your browser and examples.
- SVG Girl. A demo of SVG animation under IE9.
- Invaders. SVG version of a classic game.
- 20 SVG uses that will make your jaw drop. SVG to the limits. (Example with the image on the right).
- Polymaps. Vector based maps library, using the SVG format. Zoom over maps.
- Batik. A set of modules to generate, display and convert SVG (Java).
- SVG Edit. Online editor.
- SVG Web. A plugin to add SVG support to any browser that lacks it, including Internet Explorer and Firefox.
- Ssrc SVG. A plugin for IE to add support for SVG and XUL. Google Chrome frame adds also support to SVG to IE.
- D3JS. A framework to ease data binding in SVG.
- Inkscape. Open source editor.
- Dmoz. The SVG directory at Dmoz.