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

Manipulating SVG with jQuery

The first library we'll look at is jQuery. jQuery isn't as hot as it once was, but it's still the most popular JavaScript library on the planet, and understanding the quirks of using SVG with jQuery is still potentially useful.

Since jQuery functions as a friendly replacement for common DOM interactions, this section will feature a jQuery-based rewrite of the DOM manipulator demo we worked on in Chapter 6, JavaScript and SVG.

It uses the exact same markup, so the only place we need to look at in this chapter is the script block at the bottom. 



This code will be written in idiomatic jQuery/ES5.

The first thing we'll look at is the function we'll fire on jQuery's equivalent of the DOMContentLoaded event, $(document).ready()$(document).ready() takes a function as an argument. As the name implies, that function will be executed when the document's DOM content has finished loading. 

While you could pass in a function expression, we're going to define a traditionally...