SVG Colors, Patterns, and Gradients
by Amelia Bellamy-Royds
Copyright 2015 Amelia Bellamy-Royds. All rights reserved.
Printed in the United States of America.
Published by O'Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Meg Foley
- Production Editor: FILL IN PRODUCTION EDITOR
- Copyeditor: FILL IN COPYEDITOR
- Proofreader: FILL IN PROOFREADER
- Indexer: FILL IN INDEXER
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- January -4712: First Edition
Revision History for the First Edition
See http://oreilly.com/catalog/errata.csp?isbn=9781491933671 for release details.
The O'Reilly logo is a registered trademark of O'Reilly Media, Inc. SVG Colors, Patterns, and Gradients, the cover image, and related trade dress are trademarks of O'Reilly Media, Inc.
While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-93367-1
[FILL IN]
Preface
This book takes a deep dive into a specific aspect of SVG, painting. Painting not with oils or watercolor, but with graphical instructions that a computer can transform into colored pixels. The book explores the creative possibilities, and also the potential pitfalls. It describes the basics, but also suggests how you can mix and match the tools at your disposal to generate complex effects.
This book was born from another project, an introduction to using SVG on the web. In order to keep that book a manageable lengthand keep it suitable for introductory audiencesmany details and complexities had to be skimmed over. But those details and complexities add up to the full, wonderful potential of SVG as a graphics format. Once you understand the basics of SVG, you can start thinking about creating more intricate drawings and more nuanced effects.
What Well Cover
If youre reading this, hopefully youre already familiar with the basics of SVGhow to define a graphic as a set of shapes, and how to use that graphic either as a stand-alone image file or as markup in an HTML page. If youre not sure if youre ready, reviews the basic concepts well expect you to know.
The rest of the book focuses on the Colors, Patterns, and Gradients described in the title:
discusses the rendering model used to convert SVG code into visual graphics, and introduces the basic properties you can set on your shapes and text to control how they are painted to the screen.
discusses color: how it works in nature, how it works on the computer, and the many different ways it can be specified within your SVG code.
discusses transparency, or more specifically, opacity; it introduces the many ways you can control the opacity of your graphics, and how these affect the end result.
introduces the concept of a paint server: complex graphics content that defines how other SVG shapes and text should be painted to the screen. It also introduces the solid color paint server, which is actually more useful than it first sounds.
looks at gradients with a particular focus on the different color transition effects you can achieve by adjusting color stop positions and properties.
explores the ways in which you can manipulate a linear gradient to move it within the shape being painted.
covers repeating linear gradients and some of the effects you can create with them.
looks at radial gradients, including repeated radial gradients, and concludes with some examples of creating complex effects with multiple gradients.
introduces the element, using it to define a single block of graphics that can be used to fill or stroke shapes or text.
explores the more conventional usage of patterns, to create repeating tiles and textures.
looks at some of the issues that come into play when using paint servers to paint strokes instead of fill regions.
gives some examples of animated paint servers and discusses the benefits and limitations of the different animation methods available in SVG.
At the end of the book, two appendices provide a quick reference for the basic syntax youll need to put this all to use:
recaps all the ways you can define colors, including all the pre-defined color keywords.
summarizes all the paint server elements, their attributes, and the related style properties.
About This Book
Whether youre casually flipping through the book, or reading it meticulously cover-to-cover, you can get more from it by understanding the following little extras used to provide additional information.
Conventions Used in This Book
(OReilly boilerplate on code & term formatting)
Tip
Tips like this will be used to highlight particularly tricky aspects of SVG, or simple shortcuts that might not be obvious at first glance.
Note
Notes like this will be used for more general asides and interesting background information.
Warning
Warnings like this will highlight combatibility problems between different web browsers (or other software), or between SVG as an XML file versus SVG in HTML pages.
About the Examples
(Where to download sample files or view online, compatibility info)
(OReilly boilerplate on copyright & permissions)
How to Contact Us
(OReilly boilerplate)
Acknowledgements
(Thank you, thank you very much)
Chapter 1. Things You Should Already Know
The rest of this book is written with the assumption that you already know something about SVG, web design in general, and maybe even a little JavaScript programming.
However, there are always little quirks of a language that some people think are straightforward and other, equally talented, developers have never heard of. So this chapter gives a quick review of topics that you might want to brush up onif you dont already know them.
SVG is Drawing with Code
An SVG is an image file. It is perfectly possible to only use it as an image file, the same way you would use other image formats such as PNG or JPEG. You can create and edit an SVG in a visual editor. You can embed it in web pages as an image.
But SVG is more than an image. It is a structured document containing markup elements, text, and style instructions. While other image formats tell the computer which color to draw at which point on the screen, SVG tells the computer how to rebuild the graphic from its component parts. That has two main consequences: