简体   繁体   English

重复SVG路径

[英]Repeating a SVG path

Say that I have the following arrow, which I want to repeat several times at different locations within the svg: 假设我有以下箭头,我想在svg内的不同位置重复几次:

<svg width="400" height="400">
<path transform="translate(150,100)" fill="black" d="M 0.5,0.5 l-100,0 -25,20 25,20 100,0 z" />
<path transform="translate(300,200)" fill="black" d="M 0.5,0.5 l-100,0 -25,20 25,20 100,0 z" />
<path transform="translate(150,300)" fill="black" d="M 0.5,0.5 l-100,0 -25,20 25,20 100,0 z" />
</svg>

箭头

I might want to change the path shape in the future, so I would prefer to avoid copy-pasting the d attribute all over the place. 我可能希望将来改变路径形状,所以我宁愿避免在整个地方复制粘贴d属性。

Is it possible to define path's shape somewhere and then place it at different x , y coordinates, without manipulating it from javascript? 是否有可能在某处定义路径的形状,然后将其放置在不同的xy坐标,而不是从javascript操纵它?

Two thoughts: 两个想法:

Is there any way to avoid copy past the entire thing, short of injecting it from javascript? 有没有办法避免复制过去整个事情,没有从javascript注入它?

<defs> does in fact support <path> , since it is a "Shape Element" as listed under the "Permitted Content" section of the documentation. <defs>实际上支持<path> ,因为它是文档“允许的内容”部分下列出的“形状元素”。 However, <defs> by itself doesn't do much, it's just a semantic way to organize your SVG. 但是, <defs>本身并没有太大作用,它只是组织SVG的一种语义方式。 What you're looking for are <symbol> and <use> . 你要找的是<symbol><use>

<symbol> defines a template that you can use and re-use. <symbol>定义了一个可以使用和重用的模板。

<use> creates an instance of a given symbol. <use>创建给定符号的实例。

 <svg width="400" height="400"> <defs> <symbol id="mypath"> <path fill="black" d="M 0,20 l25,20 100,0 0,-40 -100,0 z" /> </symbol> </defs> <use xlink:href="#mypath" x="50" y="100" /> <use xlink:href="#mypath" x="200" y="200" /> <use xlink:href="#mypath" x="50" y="300" /> </svg> 

Your <path> data is kind of weird because it starts on the left side of the element and then proceeds to draw even further to the left, necessitating the transform property. 您的<path>数据有点奇怪,因为它从元素的左侧开始,然后继续向左绘制,需要transform属性。 Note how I do it in the above snippet (starting the line at the left edge of the element and never drawing further left than that) which works a bit better and doesn't need transform to avoid accidental cropping. 请注意我是如何在上面的代码片段中开始的(从元素的左边缘开始,并且从不进一步向左绘制),这样做效果更好,并且不需要transform以避免意外裁剪。

https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol

This answer provided as an extension to JCD's answer in order to show OP what I by the <symbol> not being required here. 这个答案作为JCD答案的扩展提供,以便向OP展示我在这里不需要<symbol> <symbol> elements provide some features that are not needed when you are simply reusing an element. <symbol>元素提供了一些仅在重用元素时不需要的功能。

 <svg width="400" height="400"> <defs> <path id="mypath" fill="black" d="M 0,20 l25,20 100,0 0,-40 -100,0 z" /> </defs> <use xlink:href="#mypath" x="50" y="100" /> <use xlink:href="#mypath" x="200" y="200" /> <use xlink:href="#mypath" x="50" y="300" /> </svg> 

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

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