Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取由defs元素使用snap.svg定义的use元素的属性_Svg_Snap.svg - Fatal编程技术网

获取由defs元素使用snap.svg定义的use元素的属性

获取由defs元素使用snap.svg定义的use元素的属性,svg,snap.svg,Svg,Snap.svg,我是svg的新手,也是snap.svg的新手。我正在SVG中生成元素,需要处理以下内容: <svg width="600" height="400" style="shape-rendering: geometricPrecision; position: absolute; left: 0; top: 0;"> <defs> ... <circle id="dot" r="10" st

我是svg的新手,也是snap.svg的新手。我正在SVG中生成元素,需要处理以下内容:

<svg width="600" height="400" style="shape-rendering: geometricPrecision; position: absolute;
        left: 0; top: 0;">
        <defs>
            ...
            <circle id="dot" r="10" stroke-width="2"></circle>
        </defs>
    </svg>

但是我得到的半径值,
r
,是空的。如何访问描述圆的值,如r、宽度、高度、填充颜色等?

SVG中使用元素的方式是它们基本上只是指向原始对象的指针。如果使用use将该点的克隆放置在画布上,则该克隆没有定义的半径。它指向圆点,圆点有一个定义的半径

我并不完全清楚你需要做什么,但我认为正确的方法是获得一个对dot的引用,然后你可以使用它来达到这个目的。您可以克隆点并在以后向其添加其他属性

除此之外,你只是缺少了很多你需要的快照,但可能是因为你只是给了我们一个片段

下面是一些代码:

<svg id="svg" width="600" height="400" style="shape-rendering: geometricPrecision; position:absolute;left: 0; top: 0;">
<defs>
  <circle id="dot" r="10" stroke-width="2"></circle>
</defs>
</svg>
在这里拉小提琴:

<svg id="svg" width="600" height="400" style="shape-rendering: geometricPrecision; position:absolute;left: 0; top: 0;">
<defs>
  <circle id="dot" r="10" stroke-width="2"></circle>
</defs>
</svg>
// reference to svg
var svg=Snap('#svg')

// reference to dot, stored in a
var a=svg.select('[id="dot"]')

// what's the radius of a?
var r=a.attr('r')
alert('The radius is '+r)

// clone a and add it to the svg
b=a.use()
svg.append(b)

// give b some attributes
b.attr({x:100,y:50})
console.log(b.attr())