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!

This talk has been presented at React Summit 2020, check out the latest edition of this React Conference.

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 '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.

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.

Monica Wojciechowska
Monica Wojciechowska
32 min
17 Jun, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The video explores the SVG path element, a powerful tool in Scalable Vector Graphics (SVG) for creating both simple and complex shapes. It introduces various commands like 'M' for moving to a location, 'L' for drawing lines, 'H' for horizontal lines, 'V' for vertical lines, and 'C' for creating cubic Bezier curves. The speaker explains how the 'D' attribute in an SVG path specifies the path data and how all shapes can be expressed as cubic Bezier curves. For those interested in 'svg bezier curve' or 'svg cubic bezier', the video discusses the importance of control points in defining the slope of curves. The 'svg path curve' and 'cubic bezier svg' topics are also covered, emphasizing the flexibility and efficiency of these commands. Additionally, the video provides insights into resources like the MDN documentation and tools such as SVGOMG and FlubberJS for optimizing and morphing SVG paths. The speaker also introduces a custom tool for editing SVG paths, showcasing its click-and-drag functionality for real-time visualization and modification of path data.

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
Today's Talk is about using SVGs to enhance blog design, including transforming SVGs into JSX, animating elements, and using React Spring for interactivity. The speaker also demonstrates the use of SVGs in SharkUI and showcases an interactive love button. The benefits of using SVGs in React components are discussed, as well as implementing SVGs as responsive components. Performance considerations and the pros and cons of CSS vs SVG are also touched upon.
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
Watch video: Hacking an e-Reader to Show My Tea Menu With JSX
React can be used to create custom menus for e-readers, and the process involves creating an image and e-book with React and loading them onto the e-reader. Writing an EPUB e-book for e-readers involves converting an SVG file into a PNG image and writing the e-book in EPUB format using HTML, CSS, and images. EPUB generators like Pandoc and Dino simplify the process of generating EPUBs from markdown and running JavaScript on the desktop, respectively.
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)
Anil Durman, engineer at the New York Times, discusses the role of graphics in the context of state. He highlights the work done by his team in helping people understand the news through new formats and pages. The team is hiring for various tech roles. By night, Anil is a creative coder exploring the intersection of creativity and code, and his latest project, Good Graphics, focuses on UI as a function of state, React components, and SVGs. He demonstrates how components can be updated based on state, allowing for declarative graphics. Anil also showcases how nested circles and grids can be used to create complex systems in React, enabling dynamic and reactive graphic designs.