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

Clipping and masking

Clipping and masking allow you to subtract portions of elements in an SVG document.

Clipping paths, implemented with the clipPath element, use any combination of paths, text elements, and basic shapes to serve as the outline of a simple mask. This means that everything on the inside of the clipPath element's outline is visible and everything on the outside is cropped out. Every pixel in clipPath is either on or off.

Masks, implemented with the mask element, can contain graphics, text, and basic shapes to serve as a semi-transparent mask. With a mask, each pixel value indicates the degree of opacity and can range from being fully transparent to fully opaque.


The clipPath element in SVG allows you to cut a shape out of another shape. Clipping uses the geometry of the shape to define the area that's cut out. It doesn't take into account anything other than the shape, so attributes such as stroke and fill don't change the area being cut out.

The following code sample...