简体   繁体   English

如何用SVG路径绘制字母S

[英]How to draw letter S with SVG path

This is probably SVG 101 for most, however, I need to draw a letter 'S' using a path. 大多数情况下,这可能是SVG 101,但是,我需要使用路径绘制字母“ S”。 I can make a bunch of lines, but it seems two arcs would be the way to get it done smoothly. 我可以画几条线,但是看来两条弧线是顺利完成工作的方法。

This is what I have done so far.. 这是我到目前为止所做的。

<svg width="100" height="100">
  <path d="M20 100 L20 80 L60 80 L60 60 L20 60 L20 0 L80 0 L80 20 L40 20 L40 40 L80 40 L80 100 Z" stroke-linejoin="round" stroke="#808600" stroke-width="0" stroke-linecap="round" fill="#1EB287" transform="translate(0,0)">
</svg>

Yes, you can draw a simple S using two arcs the way you suggested. 是的,您可以按照建议的方式使用两个弧线绘制一个简单的S。 Here's one simple possibility: 这是一种简单的可能性:

<path stroke-width="4" fill="none" stroke="black"
  d="M70 30 A20 20 0 1 0 50 50 A20 20 0 1 1 30 70" />

The following is a fuller explanation. 以下是更完整的说明。

There are several different ways in svg to draw a curved line. svg有几种不同的绘制曲线的方法。 For this use case, ie making a letter S, one of the simplest ways is to use two portions of circles, specifically two 3/4-circles. 对于这种使用情况,即制作字母S,最简单的方法之一是使用两部分圆,特别是两个3/4圆。 You would use the arc (A/a) command in the 'd' string of the path. 您可以在路径的“ d”字符串中使用arc(A / a)命令。 See the MDN documentation and the SVG spec for more information about how to create arcs. 有关如何创建圆弧的更多信息,请参见MDN文档SVG规范

In short, after providing the starting coordinates of your arc (eg with a preceding 'M', for 'move', command) use 'A' (or 'a') to indicate an arc segment using absolute (or relative) coordinates. 简而言之,在提供圆弧的起始坐标(例如,在前面带有“ M”,表示“移动”,命令)之后,使用“ A”(或“ a”)使用绝对(或相对)坐标表示圆弧段。 (I chose absolute, 'A'.) Then supply the following, in order: (我选择了绝对值'A'。)然后按顺序提供以下内容:

  • the x arc radius in pixels x弧半径(以像素为单位)
  • the y arc radius in pixels: I've made these first two the same as each other so that the arcs are portions of circles not ellipses. y弧半径(以像素为单位): 我已经使前两个半径相同,因此弧是圆的一部分,而不是椭圆。
  • the x-axis rotation in degrees: This is only useful for elliptal arcs and is irrelevant for circular arcs because rotating a circle is unnoticeable. x轴以度为单位的旋转: 这仅对椭圆弧有用,而与圆弧无关,因为旋转圆是不明显的。 Thus leave this at 0. 因此,将其保留为0。
  • the 0-or-1 large-arc-flag: On a clock face you can go from 3 o'clock to 6 o'clock (or from 12 o'clock to 9 o'clock, which have identical relative positions) two different ways, the short way (flag=0) ie through 4 & 5 o'clock, or the long way (flag=1) ie through 2, 1, 12, 11, 10, 9, 8, and 7 o'clock. 0或1大弧标记: 在时钟面上,您可以从3点到6点(或从12点到9点,它们具有相同的相对位置)两个不同短途(flag = 0),即4点和5点,或长途(flag = 1),即2、1、12、11、10、9、8和7点。 See my diagram below. 参见下面的图表。
  • the 0-or-1 sweep flag: You can draw an arc between two points moving in the negative-angle direction, ie counter-clockwise (flag=0) or the positive-angle direction, ie clockwise (flag=1). 0或1扫描标志: 您可以在沿负角方向(即逆时针(标志= 0))或正角方向(即顺时针(标志= 1))移动的两个点之间绘制弧。 See my diagram below. 参见下面的图表。
  • the final x-coordinate in pixels and 最终的x坐标(以像素为单位)和
  • the final y-coordinate in pixels 最终的y坐标(以像素为单位)

I made the following diagram to better explain the two flags, ie the large-arc-flag and the sweep-flag, in the arc command. 我制作了下图,以更好地解释arc命令中的两个标志,即arc-arc标志和sweep标志。 In each of the four scenarios, the start and end coordinates and the circle radius are always the same. 在这四个方案的每一个中,起点和终点坐标以及圆半径始终是相同的。 The only things that change are those two flags. 唯一改变的是这两个标志。 You can see how changing those flags changes the resulting arc. 您可以看到更改这些标志如何更改生成的弧。

在此处输入图片说明

The example below shows a simple S drawn in red using a path that includes a starting coordinate followed by two such arcs. 下面的示例显示了一个简单的用红色绘制的S,使用的路径包括一个起始坐标,后跟两个这样的弧。 To make it obvious where those arcs are, I have also superimposed on top of that full 'S' path two other individual arcs, thinner and colored blue and green, which correspond to the first and second arc parts of the full red 'S'. 为了清楚显示这些弧的位置,我还在该完整的“ S”路径上叠加了另外两个单独的弧,分别是较细的蓝色和绿色,分别对应于完整的红色“ S”的第一和第二弧部分。 With respect to the values of the two flags the blue arc corresponds to the one labeled "C" in the diagram above while the green arc corresponds to the one labeled "D". 关于两个标志的值,蓝色弧形对应于上图中的标记为“ C”的一个,而绿色弧形对应于标记为“ D”的一个。

 <svg width="400" height="300"> <g transform="translate(20,20)"> <g stroke-width="40" fill="none"> <path d="M100 50 A50 50 0 1 0 50 100 A50 50 0 1 1 0 150" stroke="red" /> </g> <g stroke-width="8" fill="none"> <path d="M100 50 A50 50 0 1 0 50 100 " stroke="blue" /> <path d=" M50 100 A50 50 0 1 1 0 150" stroke="green" /> </g> </g> </svg> 

Using the 'arc' (ie 'A'/'a') command to draw curves in SVG can be challenging, but not impossible. 使用“弧”(即“A” /“A”)命令来绘制在SVG曲线可以是具有挑战性的,但并非不可能。 This is, however, the simplest way to implement the type of curve you were requesting. 但是,这是实现所请求曲线类型的最简单方法。

Note that I've also simplified the path by programming the trajectory of the centre line of the 'S' curve and just made the stroke 20 pixels wide. 请注意,我还通过对“ S”曲线的中心线的轨迹进行编程来简化了路径,并使笔划宽度为20像素。 This contrasts with what you tried to do which was to draw an outline, ie provide coordinates for all the edge points. 这与您试图绘制轮廓相反,即为所有边缘点提供坐标。 Even your block-y squar-ish 'S' could have been simpler if you used a stroke of 20 pixels and then just drew the centre line. 如果您使用20像素的笔触,然后绘制中心线,则即使是块状的方形“ S”也可能更简单。 You can draw an outline if you want to, and sometimes you'll want to, eg to give you the power to draw the shape any way you want. 可以根据需要绘制轮廓,有时也可以,例如,使您能够以任意方式绘制形状。 However, if you expect the thickness of your path to remain constant, then thicken your stroke and draw just the centre line. 但是,如果您希望路径的厚度保持不变,请加粗笔触并仅绘制中心线。

I've been working on a private (but public) javascript library to script the path you described above. 我一直在研究私有(但公共)JavaScript库,以编写上述路径的脚本。 You can see it in action at www.stretchsketch.com and the library is on github 您可以在www.stretchsketch.com上看到它的运行情况 ,该库位于github上。

Not sure what you're asking but I think the shortest way to create an S in SVG's path d-attribute would be to use bezier curves . 不知道您要问什么,但我认为在SVG的d属性中创建S的最短方法是使用贝塞尔曲线

Typically arcs are 101 x 101 very powerful but way too hard for 99% of its usecases. 通常, 是101 x 101非常强大,但是对于99%的用例来说,弧度太高了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM