【发布时间】:2015-09-05 13:58:34
【问题描述】:
我有一张 .svg 格式的图片,如下图所示。
我想制作一个网页,用户可以在其中与这样的图像进行交互,但具有更多节点。该结构将与一棵树相似。
是否可以使用 javascript/html/css 直接与此 .svg 图片进行交互?
如果是,怎么做?
Note: By interact I mean being able to click on the nodes -and the webpage recognizing it- and when one node is selected the color of the other nodes change.
注意 2:我只有 .svg 文件,我不知道是否可以将其定义为 html 上的内联 svg。
注意3:这张图片会有很多节点(80+),所以我宁愿不必为每个节点定义一个可点击区域等等......但如果这是唯一的解决方案,没问题。
编辑: 这是我的 .svg 文件的内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
-->
<!-- Title: g Pages: 1 -->
<svg width="134pt" height="116pt"
viewBox="0.00 0.00 134.00 116.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 112)">
<title>g</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-112 130,-112 130,4 -4,4"/>
<!-- a -->
<g id="node1" class="node"><title>a</title>
<ellipse fill="none" stroke="black" cx="27" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-86.3" font-family="Times New Roman,serif" font-size="14.00">a</text>
</g>
<!-- b -->
<g id="node2" class="node"><title>b</title>
<ellipse fill="none" stroke="black" cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.3" font-family="Times New Roman,serif" font-size="14.00">b</text>
</g>
<!-- a->b -->
<g id="edge1" class="edge"><title>a->b</title>
<path fill="none" stroke="black" d="M27,-71.6966C27,-63.9827 27,-54.7125 27,-46.1124"/>
<polygon fill="black" stroke="black" points="30.5001,-46.1043 27,-36.1043 23.5001,-46.1044 30.5001,-46.1043"/>
</g>
<!-- c -->
<g id="node3" class="node"><title>c</title>
<ellipse fill="none" stroke="black" cx="99" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="99" y="-14.3" font-family="Times New Roman,serif" font-size="14.00">c</text>
</g>
<!-- b->c -->
<g id="edge2" class="edge"><title>b->c</title>
<path fill="none" stroke="black" d="M54,-18C56.6147,-18 59.2295,-18 61.8442,-18"/>
<polygon fill="black" stroke="black" points="61.9297,-21.5001 71.9297,-18 61.9297,-14.5001 61.9297,-21.5001"/>
</g>
</g>
</svg>
【问题讨论】:
-
如果您一无所知,您可能应该从一本书开始,而不是一个真正笼统的问题。试试这个w3.org/Graphics/SVG/IG/resources/svgprimer.html
-
感谢您的建议。到目前为止,我只是在研究与 svg 图像交互的复杂程度。我什至不确定我是否真的会按照我的想法去做。另外,我打算做的并不是很复杂,我只需要以某种方式知道选择了哪些节点,并据此更改某些节点的颜色。查看@Paulie_D 代码后,我可能会在本周末尝试一下。 :)
标签: javascript html css svg