The Language of Shapes: Understanding the SVG Path

Rate this content
Bookmark

The benefits of SVG are vast: style-able, accessible, animatable. And when it comes to SVG, the Path element is the building block of building blocks. During this presentation, we’ll take a deep dive into the language of the SVG path. Not only will we learn to read path data, but we'll also gain the ability to manipulate paths on the fly with the help of React - no Adobe Illustrator or help from a time-strapped designer necessary!

FAQ

SVG stands for Scalable Vector Graphic. It uses shapes, numbers, and coordinates for rendering, making it resolution independent and infinitely scalable. This makes SVGs clearer than raster images such as JPEGs, especially when scaled up. SVGs also support interactivity and accessibility.

The SVG path element is crucial because it can create all basic shapes and many complex ones through a single element using a series of commands. It acts as a versatile tool in SVG for describing detailed and intricate 2D graphics.

The 'D' attribute in an SVG path specifies the path data or instructions for drawing the graphic. It consists of a series of commands and coordinates that define the path's shape, using different commands like 'M' for move-to, 'L' for line-to, and 'C' for cubic Bezier curves.

Yes, SVG paths can be modified after creation. This allows for adjustments and animations by changing the path's data attribute, which can be done programmatically or using tools designed for editing SVG path data.

Drawing arcs with SVG paths can be tricky, especially when trying to form a complete circle or ellipse because defining the start and end at the same point can create ambiguity. A common workaround is to slightly offset the ending point or use multiple commands to form a complete shape.

The MDN (Mozilla Developer Network) documentation is highly recommended for in-depth understanding of SVGs. For practical tools, SVGOMG is popular for optimizing SVG files. These resources are helpful for both beginners and advanced users.

While SVG paths are generally efficient, using a single long path can perform slightly better than multiple shorter paths. However, the difference is minimal, and the choice largely depends on the specific requirements and structure of the graphic.

Monica Wojciechowska
Monica Wojciechowska
32 min
17 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk introduces the SVG Path element, exploring its commands and potential. The speaker simplifies the language of shapes by grouping commands together and finding alternative ways to define arcs. They also present a path editing tool that allows for easy manipulation and visualization of path data. The Talk concludes with discussions on performance, limitations, recommended resources, SVG morphing, and the FlubberJS library for path interpolation.

1. Introduction to SVG Path

Short description:

Hi everyone, my name is Monica Wojcichowska, and I'm excited to be talking to you about the SVGPAP. SVG, as a refresher, stands for Scalable Vector Graphic. It's a language for describing 2D graphics and has elements like circles, rectangles, lines, and the star of today's show, the SVG path element. The path has huge potential as it's not just a visible line, but a set of instructions that can be used in various ways.

♪ Hi everyone, my name is Monica Wojcichowska, and I'm coming to you straight from Warsaw, Poland, where I'm excited to be talking to you a little bit about the SVGPAP. But before we get started, a little bit about myself. I was born and raised in New Jersey, but returned to my roots, and I'm currently living in Poland and eating all of the pierogi I can. If you ask LinkedIn, I'm officially a front-end developer and data visualization engineer, but if you ask my friends, they'll likely tell you that I'm a world traveler and lover of all things nature and sport.

But before I became who, what and where I am today, there were a few other things along the way, such as switching majors in college, switching careers from marketing to programming, switching countries I already mentioned, and of course, all the new things along the way, new passions, new people, new adventures, and the adventure continues. And the reason I'm telling you all of this in a bit more detail than a single slide introduction is to show by example that our paths are rarely straight. They bend, they twist, they turn, and the path rarely looks like this. It looks more like this, actually. And even when it does look like this, there's often some sort of roadblock that makes it inaccessible. But that's the super abstract path of life. And as much as I'd love to talk about that, today we'll focus on a bit less abstract of a path, the SVG path instead.

So what is the SVG? Well SVG, as a refresher for most of you probably, stands for Scalable Vector Graphic, and it uses shapes, numbers and coordinates instead of pixel grid for rendering, which makes it resolution independent and infinitely scalable. So if you want a little graphic, there might not be a huge difference, but when you start increasing the size, the difference between raster, which is the JPEG, PNG, GIF formats, and Vector becomes immediately clear as you can see from these images. This along with interactivity and accessibility are huge pros when it comes to SVG versus raster. But what's key for today's presentation is that it's not just an image format. It's actually a language for describing these 2D graphics. So I like to say that SVG is to graphics what HTML is to text. It's a way to display fill in the blank in a given structure. So where HTML has elements like divs, headers, lists, and paragraph elements, SVG has elements like the group container, circles, rectangle, lines, and even for an object. And of course, the SVG path element, which is the star of today's show. And why exactly is it the star? Well, because all basic shapes can be created with a path element. So the other elements that we mentioned, such as rectangle, such a circle, can be created using a path. So we have some basic shapes and less basic shapes. And then we have the things that you wouldn't even classify as shapes at all. Like this arch that's hopping across the screen, or this interactive drum set to the right here. We also have things such as clip paths, which allow us to basically create this mask and clip whatever is outside of that path. Like this birthday cake with different backgrounds. Or a text path, which allows us to lay out text along a path of our choice. So before we dig deeper, why understand the path? Well, huge potential. A path isn't just a visible line as I just mentioned, but it's a set of instructions that can be later used in a variety of different ways.

2. Exploring the SVG Path Element

Short description:

For pleasure and practical purposes, let's explore the path element in SVG. It is a set of instructions represented by letters and numbers. SVG provides 10 different commands, each with uppercase and lowercase variants. The first command, m, is for moving to a location. Line 2 is for drawing straight lines, while line 3 is for horizontal lines. The v command is for vertical lines. Lastly, the c command is for creating cubic Bezier curves.

For pleasure, of course, because we often tend to enjoy things more as our understanding of them grows. And for practical purposes, such as introducing modifications or animations. So let's take a look at the path element along these lines.

Let's say a designer friend gives you a set of custom icons, and you want to introduce one little edit to one little icon. No big deal, right? Well, so you would think, but behind the scenes, something that looks like this looks more like this. It's a super long string of numbers, letters, and punctuation.

So let's learn to read this language. When we learn to read a book, we don't start with the book entirely, but we start with its building blocks. And the same goes for the path. So we have the path. Each path requires a path data attribute, which is signified by D, which is essentially a set of instructions, and it's chronological. We can read it from left to right, similar to reading a book. And the path data is made up of different commands. Each command is then represented by a set of letters and numbers, which tell us different things about that command.

So let's take a look at those building blocks, those letters and numbers. Well, SVG gives us 10 different commands, 10 different letters, and each command comes in two different variants, uppercase and lowercase. An uppercase letter specifies absolute coordinates on the page, and a lowercase letter specifies relative coordinates. So our first command, m, means move to. Basically pick up a pen and put it down in a certain location, so we just need to specify the x and y location. Line 2. This has to do with drawing straight lines from one point to another. So again, we just need to specify the x and y coordinates of our endpoint. Line 3. Next we have the horizontal line, here we just need to provide an x value because the y is assumed to be what it was. Same thing goes for v, the vertical line, just need to provide a y value. And then come the curves. So there's an infinite number of Bezier curves out there, but only two simple ones that are available in the path element, and that's the quadratic and the cubic. So c stands for the cubic Bezier curve. And it's used to create a smooth curve. So we need to define two points, two control points, and an end point.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

SVGs to Make Your Blog Stand Out
React Summit Remote Edition 2021React Summit Remote Edition 2021
36 min
SVGs to Make Your Blog Stand Out
Are dev blogs dying out? No, they're not! Blogs are making a comeback. But sadly, most of them are lacking personal style. So, what can developers do to make their blogs unique and prettier? In this talk, I’ll show you how SVGs (as React Components) can help you take your blog from dull to awesome.
Hacking an e-Reader to Show My Tea Menu With JSX
React Day Berlin 2023React Day Berlin 2023
7 min
Hacking an e-Reader to Show My Tea Menu With JSX
I wanted to have a tea menu to show guests and for my own reference. Turns out e-Readers use so little power and can render HTML! I'll share how to generate an e-book with all your drinks with React, JSX, and Deno, as well as rendering a custom cover page with SVG. Wow your friends by turning an old device into a smart home tea menu, and learn how to write your next book with React.
Graphics, as a Function of State / Graphic = fn(state)
React Summit Remote Edition 2021React Summit Remote Edition 2021
15 min
Graphics, as a Function of State / Graphic = fn(state)
Applying the React principle of “UI is a function of state” to SVGs and Graphic Design. We will talk about using systems of componentization and state management to create and manage SVGs. Viewers will leave the session with a firm understanding of creative coding practices and a new viewpoint for building systems in JavaScript.