Book Image

Mastering SVG

By : Rob Larsen
Book Image

Mastering SVG

By: Rob Larsen

Overview of this book

SVG is the most powerful image format in use on the web. In addition to producing resolution-independent images for today's multi-device world, SVG allows you to create animations and visualizations to add to your sites and applications. The simplicity of cross-platform markup, mixed with familiar modern web languages, such as CSS and JavaScript, creates a winning combination for designers and developers alike. In this book, you will learn how to author an SVG document using common SVG features, such as elements and attributes, and serve SVG on the web using simple configuration tips for common web servers. You will also use SVG elements and images in HTML documents. Further, you will use SVG images for a variety of common tasks, such as manipulating SVG elements, adding animations using CSS, mastering the basic JavaScript SVG (API) using Document Object Model (DOM) methods, and interfacing SVG with common libraries and frameworks, such as React, jQuery, and Angular. You will then build an understanding of the Snap.svg and SVG.js APIs, along with the basics of D3, and take a look at how to implement interesting visualizations using the library. By the end of the book, you will have mastered creating animations with SVG.
Table of Contents (17 chapters)
Title Page
PacktPub.com
Contributors
Preface
Index

Positioning in SVG


As you saw in Chapter 1, Introducing Scalable Vector Graphics, SVG elements use a coordinate plane positioning system. Elements in an SVG document are located using x and y coordinates. This should be familiar to you from your geometry class or, more specifically to the web, if you're used to working with CSS, absolutely positioned elements. The following code shows two variations on the positioning scheme that you've already seen with both a circle element, which uses (cx, center x), and (cy, center y), attributes to place the circle element based on the center of the circle and the rect element, which will use the x and y attributes to place the upper left-hand corner of the square on the coordinate plane:

     <svg xmlns="http://www.w3.org/2000/svg" width="350" height="150"
       viewBox="0 0 350 150" version="1.1"> 
        <circle cx="100" cy="75" r="50" fill="rgba(255,0,0,1)"/> 
        <rect x="200" y="25" width="100" height="100" 
         fill=...