Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering SVG
  • Table Of Contents Toc
  • Feedback & Rating feedback
Mastering SVG

Mastering SVG

By : Rob Larsen
1 (1)
close
close
Mastering SVG

Mastering SVG

1 (1)
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 (13 chapters)
close
close

Feature detection and Modernizr

While overall support for SVG on the global web (https://caniuse.com/#search=svg) is now very high, it's not uniform and there are still non-supporting browsers out there. This is where Modernizr, the feature detection library, can be useful. If your user base is broad or you're using newer (even experimental) features, you can use Modernizr to detect browser compatibility with your important features and adjust your code accordingly.

There are two ways this works. One is the classes that Modernizr can place on the HTML element. The other is the global Modernizr object that contains results for all the tests as Booleans. Before we move on, I'll show you examples of both tools in action.

The Modernizr project provides hundreds of tests. Since some of the tests are quite expensive (in terms of resources needed to compute, when working with Modernizr, you want to use just the tests you need for your application. In this case, I've created a specific build of Modernizr that tests for multiple SVG features and nothing else. When added to an HTML page, this file will add classes to the HTML element indicating support for various SVG features

Here's the output of the HTML element in Microsoft Edge. The no-smil class indicates that Edge doesn't support Synchronized Multimedia Integration Language (SMIL), but does support everything else we're testing for:

<html class=" svg svgclippaths svgforeignobject svgfilters
no-smil inlinesvg svgasimg" lang="en">

Output from the latest Chrome Version indicates support for all tested features:

<htmlclass=" svg svgclippaths svgforeignobject svgfilters smil 
inlinesvg svgasimg" lang="en" >

And finally, Internet Explorer 8 (IE8), which has no SVG support at all:

<HTML class=" no-svg no-svgclippaths no-svgforeignobject no-svgfilters 
no-smil no-inlinesvg no-svgasimg" lang="en">

Using these classes would allow you to, as a simple example, provide a PNG fallback function for CSS background images in IE8:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mastering SVG- Modernizr</title>
<style type="text/css">
.header {
color: #ffffff;
background: url(1-3-gradient.svg) repeat-x;
width: 500px;
height: 40px;
text-align: center;
}
.no-svg .header {
background: url(1-3-gradient.png) repeat-x;
}
</style>
</head>
<body>
<div class="header"><h1>CSS!</h1></div>
</body>
</html>

As was mentioned, Modernizr also exposes a global Modernizr JavaScript object with each of the tests available as a Boolean. The following example shows how to access that Boolean and using an if statement for the code approximately, depending on whether or not SVG is supported:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mastering SVG- Monderizr JavaScript Object</title>
<script src="modernizr-custom.js"></script>
</head>
<body>
<script>
if (Modernizr.svg){
// do things with SVG
} else {
//create a non-SVG fallback
}
</script>
</body>
</html>

In general, the rest of this book will not focus on fallbacks for older browsers, but it is useful to know that they're available if you're working in an environment where you need to support a broad range of browsers and devices.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY