【发布时间】:2011-10-05 05:19:05
【问题描述】:
我在 SVG 中做了一个表格,我想用数据动态填充它。这意味着我不知道文本占用了多少空间,并且我想剪辑或隐藏重叠的文本。如何在 SVG 中做到这一点?
我的带有 SVG 的 HTML 文档如下所示:
<!DOCTYPE html>
<html>
<body>
<svg>
<text x="100" y="100">Orange</text> <text x="160" y="100">12</text>
<text x="100" y="115">Pear</text> <text x="160" y="115">7</text>
<text x="100" y="130">Banana</text> <text x="160" y="130">9</text>
<text x="100" y="145">Pomegranate</text><text x="160" y="145">2</text>
<line x1="157" y1="85" x2="157" y2="155" style="stroke:rgb(100,100,100)"/>
</svg>
</body>
</html>
这将呈现到:
有什么方法可以在我的 SVG-“表格”中剪辑文本吗?
实施的解决方案来自 Erik 的回答:
<!DOCTYPE html>
<html>
<body>
<svg>
<text x="10" y="20" clip-path="url(#clip1)">Orange</text>
<text x="10" y="35" clip-path="url(#clip1)">Pear</text>
<text x="10" y="50" clip-path="url(#clip1)">Banana</text>
<text x="10" y="65" clip-path="url(#clip1)">Pomegranate</text>
<text x="70" y="20">12</text>
<text x="70" y="35">7</text>
<text x="70" y="50">9</text>
<text x="70" y="65">2</text>
<line x1="67" y1="5" x2="67" y2="75" style="stroke:rgb(100,100,100)"/>
<clipPath id="clip1">
<rect x="5" y="5" width="57" height="90"/>
</clipPath>
</svg>
</body>
</html>
【问题讨论】:
-
你可以在上面的图片中看到它夹在字母
a的中间。更喜欢 textPath 而不是 stackoverflow.com/a/9249966/592792