【他山之石】The SVG path Syntax: An Illustrated Guide:SVG 中的 path 语法图解指南

写在前面
本文为我的自学精译专栏《CSS in Depth 2》第 086 篇文章、在介绍 CSS 的 clip-path 属性的用法时作者提到的一篇延伸阅读材料,以图文并茂的形式系统梳理了 SVG path 属性的方方面面。其中最为精彩的是文中列举的大量使用案例。为了方便查找,特此整理到我的【他山之石】专栏。

The SVG path Syntax: An Illustrated Guide

by Chris Coyier , Feb 18, 2021

from: https://css-tricks.com/svg-path-syntax-illustrated-guide/

The element in SVG is the ultimate drawing element. It can draw anything! I’ve heard that under the hood all the other drawing elements ultimately use path anyway. The path element takes a single attribute to describe what it draws: the d attribute. The value it has is a mini syntax all to itself. It can look pretty indecipherable. It’s a ton of numbers and letters smashed together into a long string. Like anything computers, there is a reason to the rhyme. I’m no expert here, but I thought it would be fun to dig into.

Here’s an example of a medium-complexity path, I’d say:

<path d="M213.1,6.7c-32.4-14.4-73.7,0-88.1,30.6C110.6,4.9,67.5-9.5,36.9,6.7C2.8,22.9-13.4,62.4,13.5,110.9C33.3,145.1,67.5,170.3,125,217c59.3-46.7,93.5-71.9,111.5-106.1C263.4,64.2,247.2,22.9,213.1,6.7z"/>

We could reformat it to start making sense of it (still valid code):

<path d="M 213.1,6.7c -32.4-14.4-73.7,0-88.1,30.6C 110.6,4.9,67.5-9.5,36.9,6.7C 2.8,22.9-13.4,62.4,13.5,110.9C 33.3,145.1,67.5,170.3,125,217c 59.3-46.7,93.5-71.9,111.5-106.1C 263.4,64.2,247.2,22.9,213.1,6.7z" />

The letters are commands. The numbers are passing values to those commands. All the commas are optional (they could be spaces).

For example, that first command is M. M says, in a metaphorical sense, pick up the pen and move it to the exact location 213.1, 6.7. Don’t draw anything just yet, just move the location of the Pen. So that if other commands do drawing, it now starts at this location.

M is just one of many path commands. There are 18 of them by my count.

Many (but not all of them) come in a pair. There is an UPPERCASE and a lowercase version. The UPPERCASE version is the absolute version and the lowercase is the relative version. Let’s keep using M as an example:

  • M 100,100 means “Pick up the pen and move it to the exact coordinates 100,100”
  • m 100,100 means “Move the Pen 100 down and 100 right from wherever you currently are.”

Many commands have that same setup. The lowercase version factors in where the “pen” currently is.

Let’s look at two absolute commands:

img1

Followed by a relative command:

img2

Just like the M and m commands, L and l take two numbers: either absolute or relative coordinates. There are four other commands that are essentially simpler versions of the line commands. They also draw lines, but only take one value: horizontal or vertical. When we used l 25,0 we could have used h 25 which means “from where the pen currently is, draw 25 to the right. More succinct, I suppose. It’s bigger brother H, as we could guess, means to draw to the exact horizontal coordinate 25. V and v move vertically absolutely and relatively as you’d surely guess.

Check out this Chris Nager demo in which he draws a cross in an extremely tiny amount of code, thanks to relative coordinate drawing:

<svg class="icon  icon--plus" viewBox="0 0 5 5" xmlns="http://www.w3.org/2000/svg"><path d="M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z" />
</svg>
.icon {display: inline-block;vertical-align: middle;
}.icon--plus {background-color: cornflowerblue;
}.icon--plus > path {fill: gold;stroke: gold;stroke-width: 0.1;stroke-linejoin: round;
}

img3

See the very last character Chris used there? Z. Z (or z, it doesn’t matter) “closes” the path. Like any other command, it’s optional. It’s a cheap n’ easy way to draw a straight line directly back to the last place the “pen” was set down (probably the last M or m command). It saves you from having to repeat that first location and using a line command to get back there.

img4

Let’s look at the commands we’ve covered so far.

Mx,yMove to the absolute coordinates x,y
mx,yMove to the right x and down y (or left and up if negative values)
Lx,yDraw a straight line to the absolute coordinates x,y
lx,yDraw a straight line to a point that is relatively right x and down y (or left and up if negative values)
HxDraw a line horizontally to the exact coordinate x
hxDraw a line horizontally relatively to the right x (or to the left if a negative value)
VyDraw a line vertically to the exact coordinate y
vyDraw a line vertically relatively down y (or up if a negative value)
Z(orz)Draw a straight line back to the start of the path

So far we’ve looked at only straight lines. Path is a perfectly acceptable element and syntax for that, although it could be argued that elements like ``might have an even easier syntax for straight-line shapes, if slightly more limited.

The superpower of path is curves! There are quite a few different types.

Remember the first bit of example code we looked at used a lot of C and c commands. Those are “Bezier Curves” and require more data do their thing.

The C command takes three points. The first two points define the location of two bezier curve handles. Perhaps that concept is familiar from a tool like the Pen tool in Adobe Illustrator:

img5

The last of the three points is the end of the curve. The point where the curve should finish up. Here’s an illustration:

img6

The lowercase c command is exactly the same, except all three points use relative values.

The S (or s) command is buddies with the C commands in that it only requires two points because it assumes that the first bezier point is a reflection of the last bezier point from the last S or C command.

img7

The Q command is one of the easier ones as it only requires two points. The bezier point it wants is a “Quadratic” curve control point. It’s as if both the starting and ending point share a single point for where their control handle end.

We might as well cover T at the same time. It’s buddies with Q just like S is with C. When T comes after a Q, the control point is assumed to be a reflection of the previous one, so you only need to provide the final point.

img8

The A command is probably the most complicated. Or the require the most data, at least. You give it information defining an oval’s width, height, and how that oval is rotated, along with the end point. Then a bit more information about which path along that oval you expect the path to take. From MDN:

there are two possible ellipses for the path to travel around and two different possible paths on both ellipses, giving four possible paths. The first argument is the large-arc-flag. It simply determines if the arc should be greater than or less than 180 degrees; in the end, this flag determines which direction the arc will travel around a given circle. The second argument is the sweep-flag. It determines if the arc should begin moving at negative angles or positive ones, which essentially picks which of the two circles you will travel around.

Joni Trythall’s graphic explaining A from her article on SVG paths is pretty clear:

img9

Here’s written explanations of those curve commands.

CcX1,cY1 cX2,cY2 eX,eYDraw a bezier curve based on two bezier control points and end at specified coordinates
cSame with all relative values
ScX2,cY2 eX,eYBasically a C command that assumes the first bezier control point is a reflection of the last bezier point used in the previous S or C command
sSame with all relative values
QcX,cY eX,eYDraw a bezier curve based a single bezier control point and end at specified coordinates
qSame with all relative values
TeX,eYBasically a Q command that assumes the first bezier control point is a reflection of the last bezier point used in the previous Q or T command
tSame with all relative values
ArX,rY rotation, arc, sweep, eX,eYDraw an arc that is based on the curve an oval makes. First define the width and height of the oval. Then the rotation of the oval. Along with the end point, this makes two possible ovals. So the arc and sweep are either 0 or 1 and determine which oval and which path it will take.
aSame with relative values for eX,eY

Wanna see a bunch of examples? OK:

<svg viewBox="0 0 10 10" class="svg-1"><path d="M2,2 L8,8" />
</svg><svg viewBox="0 0 10 10" class="svg-2"><path d="M2,8 L5,2 L8,8" />
</svg><svg viewBox="0 0 10 10" class="svg-3"><path d="M2,2 Q8,2 8,8" />
</svg><svg viewBox="0 0 10 10" class="svg-4"><path d="M2,5 C2,8 8,8 8,5" />
</svg><svg viewBox="0 0 10 10" class="svg-5"><path d="M2,2 L8,2 L2,5 L8,5 L2,8 L8,8" />
</svg>
.svg-1:hover path {d: path("M8,2 L2,8");
}
.svg-2:hover path {d: path("M2,2 L5,8 L8,2");
}
.svg-3:hover path {d: path("M2,2 Q2,8 8,8");
}
.svg-4:hover path {d: path("M2,5 C2,2 8,2 8,5");
}
.svg-5:hover path {d: path("M3,3 L8,3 L2,5 L8,5 L3,7 L7,7");
}
.svg-6:hover path {d: path("M2,5 A 5 25 -15 0 1 8 8");
}
.svg-7:hover path {d: path("M2,5 S2,14 4,5 S7,8 8,4")
}
.svg-8:hover path {d: path("M5,2 Q 8,5 5,8")
}
.svg-9:hover path {d: path("M2,2 Q8,2 5,5 T8,8")
}body {display: flex;flex-wrap: wrap;justify-content: space-between;
}
svg {width: 30%;height: 30%;background: #eee;margin-bottom: 5%;
}
svg polyline,
svg line,
svg path {fill: none;stroke: #000;stroke-linecap: round;stroke-linejoin: round;transition: 0.2s;
}

img10

If you’re looking in a recently-released Blink-based browser and you have a mouse, you’ll see some hover animations! Turns out you can set path data right in CSS now. For example…

<svg viewBox="0 0 10 10"><path d="M2,5 C2,8 8,8 8,5" />
</svg>
svg:hover path {transition: d 0.2s;d: path("M2,5 C2,2 8,2 8,5");
}

This SVG Path Visualizer is awfully cool!

img11

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/64821.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小型 Vue 项目,该不该用 Pinia 、Vuex呢?

说到 Vue3 的状态管理&#xff0c;我们会第一时间想到 Pinia、Vuex&#xff0c;但是经过很长一段时间的 Vue3 项目开发&#xff0c;我逐渐发现&#xff0c;我们真的有必要用 Pinia、Vuex 这类的状态管理工具吗&#xff1f; 带着这样的疑惑&#xff0c;我首先是想知道一下 Pini…

c4d动画怎么导出mp4视频,c4d动画视频格式设置

宝子们&#xff0c;今天来给大家讲讲 C4D 咋导出mp4视频的方法。通过用图文教程的形式给大家展示得明明白白的&#xff0c;让你能轻松理解和掌握&#xff0c;不管是理论基础&#xff0c;还是实际操作和技能技巧&#xff0c;都能学到&#xff0c;快速入门然后提升自己哦。 c4d动…

EfficienetAD异常值检测之瓷砖表面缺陷检测(免费下载测试数据集和模型)

背景 当今制造业蓬勃发展&#xff0c;产品质量把控至关重要。从精密电子元件到大型工业板材&#xff0c;表面缺陷哪怕细微&#xff0c;都可能引发性能故障或外观瑕疵。人工目视检测耗时费力且易漏检&#xff0c;已无法适应高速生产线节奏。在此背景下&#xff0c;表面缺陷异常…

将Minio设置为Django的默认Storage(django-storages)

这里写自定义目录标题 前置说明静态文件收集静态文件 使用django-storages来使Django集成Minio安装依赖settings.py测试收集静态文件测试媒体文件 前置说明 静态文件 Django默认的Storage是本地&#xff0c;项目中的CSS、图片、JS都是静态文件。一般会将静态文件放到一个单独…

Redis生产实践中相关疑问记录

1. Redis相关疑问 1.1. redis内存使用率100% 就等同于redis不可用吗&#xff1f; 正常使用情况下&#xff0c;不是。 redis有【缓存淘汰机制】&#xff0c;Redis 在内存使用率达到 100% 时不会直接崩溃。相反&#xff0c;它依赖内存淘汰策略来释放内存&#xff0c;确保系统的…

量化交易——RSI策略(vectorbt实现)

本文为通过vectorbt&#xff08;以下简称vbt&#xff09;实现量化交易系列第一篇文章&#xff0c;通过使用vbt实现RSI策略从而熟悉其代码框架。 关于本文所使用数据的说明 由于vbt官方文档提供的入门案例使用的数据是通过其内置的yfinance包获取&#xff0c;在国内无法直接访…

本地摄像头视频流在html中打开

1.准备ffmpeg 和(rtsp-simple-server srs搭建流媒体服务器)视频服务器. 2.解压视频流服务器修改配置文件mediamtx.yml ,hlsAlwaysRemux: yes 3.双击运行服务器。 4&#xff0c;安装ffmpeg ,添加到环境变量。 5.查询本机设备列表 ffmpeg -list_devices true -f dshow -i d…

unipp中使用阿里图标,以及闭坑指南

-----------------------------------------------------点赞收藏才是更新的动力------------------------------------------------- unipp中使用阿里图标 官网下载图标在项目中引入使用注意事项 官网下载图标 进入阿里图标网站 将需要下载的图标添加到购物车中 2. 直接下载…

设计模式の享元模板代理模式

文章目录 前言一、享元模式二、模板方法模式三、代理模式3.1、静态代理3.2、JDK动态代理3.3、Cglib动态代理3.4、小结 前言 本篇是关于设计模式中享元模式、模板模式、以及代理模式的学习笔记。 一、享元模式 享元模式是一种结构型设计模式&#xff0c;目的是为了相似对象的复用…

flink实现复杂kafka数据读取

接上文&#xff1a;一文说清flink从编码到部署上线 环境说明&#xff1a;MySQL&#xff1a;5.7&#xff1b;flink&#xff1a;1.14.0&#xff1b;hadoop&#xff1a;3.0.0&#xff1b;操作系统&#xff1a;CentOS 7.6&#xff1b;JDK&#xff1a;1.8.0_401。 常见的文章中&…

越疆科技营收增速放缓:毛利率未恢复,持续亏损下销售费用偏高

《港湾商业观察》施子夫 12月13日&#xff0c;深圳市越疆科技股份有限公司&#xff08;以下简称&#xff0c;越疆科技&#xff0c;02432.HK&#xff09;发布全球发售公告&#xff0c;公司计划全球发售4000万股股份&#xff0c;其中3800万股国际发售&#xff0c;200万股香港公开…

datasets 笔记:加载数据集(基本操作)

参考了huggingface的教程 1 了解数据集基本信息&#xff08; load_dataset_builder&#xff09; 在下载数据集之前&#xff0c;通常先快速了解数据集的基本信息会很有帮助。数据集的信息存储在 DatasetInfo 中&#xff0c;可能包括数据集描述、特征和数据集大小等信息。&…

Java图片拼接

最近遇到一个挺离谱的功能&#xff0c;某个表单只让上传一张图&#xff0c;多图上传会使导出失败。跟开发沟通后表示&#xff0c;这个问题处理不了。我... 遂自己思考&#xff0c;能否以曲线救国的方式拯救一下&#xff0c;即不伤及代码之根本&#xff0c;又能解决燃眉之急。灵…

.NET重点

B/S C/S什么语言 B/S&#xff1a; 浏览器端&#xff1a;JavaScript&#xff0c;HTML&#xff0c;CSS 服务器端&#xff1a;ASP&#xff08;.NET&#xff09;PHP/JSP 优势&#xff1a;维护方便&#xff0c;易于升级和扩展 劣势&#xff1a;服务器负担沉重 C/S java/.NET/…

Linux——卷

Linux——卷 介绍 最近做的项目&#xff0c;涉及到对系统的一些维护&#xff0c;有些盘没有使用&#xff0c;需要创建逻辑盘并挂载到指定目录下。有些软件需要依赖空的逻辑盘&#xff08;LVM&#xff09;。 先简单介绍一下卷的一些概念&#xff0c;有分区、物理存储介质、物…

M3D: 基于多模态大模型的新型3D医学影像分析框架,将3D医学图像分析从“看图片“提升到“理解空间“的层次,支持检索、报告生成、问答、定位和分割等8类任务

M3D: 基于多模态大模型的新型3D医学影像分析框架&#xff0c;将3D医学图像分析从“看图片“提升到“理解空间“的层次&#xff0c;支持检索、报告生成、问答、定位和分割等8类任务 论文大纲理解1. 确认目标2. 分析过程&#xff08;目标-手段分析&#xff09;核心问题拆解 3. 实…

clickhouse-副本和分片

1、副本 1.1、概述 集群是副本和分片的基础&#xff0c;它将ClickHouse的服务拓扑由单节点延伸到多个节点&#xff0c;但它并不像Hadoop生态的某些系统那样&#xff0c;要求所有节点组成一个单一的大集群。ClickHouse的集群配置非常灵活&#xff0c;用户既可以将所有节点组成…

Redis 集群实操:强大的数据“分身术”

目录 Redis Cluster集群模式 1、介绍 2、架构设计 3、集群模式实操 4、故障转移 5、常用命令 Redis Cluster集群模式 1、介绍 redis3.0版本推出的Redis Cluster 集群模式&#xff0c;每个节点都可以保存数据和整个集群状态&#xff0c;每个节点都和其他所有节点连接。Cl…

C# 从控制台应用程序入门

总目录 前言 从创建并运行第一个控制台应用程序&#xff0c;快速入门C#。 一、新建一个控制台应用程序 控制台应用程序是C# 入门时&#xff0c;学习基础语法的最佳应用程序。 打开VS2022&#xff0c;选择【创建新项目】 搜索【控制台】&#xff0c;选择控制台应用(.NET Framew…

猫咪睡眠:萌态背后的奥秘与启示

猫咪的睡眠&#xff0c;犹如一本充满趣味与奥秘的小书&#xff0c;每一页都写满了它们独特的习性与本能。 猫咪堪称 “睡眠大师”&#xff0c;睡眠时间之长令人咋舌&#xff0c;一天中大约有 12 - 16 个小时在梦乡中度过&#xff0c;幼猫和老年猫甚至能睡更久。它们似乎深谙放…