【发布时间】:2021-11-09 17:24:14
【问题描述】:
如何将单个 SVG 元素(而不是整个 SVG 文档或其中的一部分)渲染到图像或画布上?
我有一个包含很多节点的 SVG 文档。如果我渲染整个 SVG 或其中的一部分,生成的图像将包含我不感兴趣的其他图形。我对一个特定的 SVG 元素感兴趣,并希望在没有其他任何内容的情况下渲染所有元素。
例子:
<!DOCTYPE html>
<html>
<body>
<svg height="150" width="150">
<circle cx="50" cy="50" r="25" stroke="green" stroke-width="3" fill="gray" />
<circle id="IWantToRenderThis" cx="75" cy="75" r="25" stroke="red" stroke-width="3" fill="white" />
<circle cx="100" cy="100" r="25" stroke="blue" stroke-width="3" fill="black" />
</svg>
<script>
const target = document.getElementById("IWantToRenderThis");
// how can I render *target* into a texture?
</script>
</body>
</html>
如何单独渲染target?我想获得一张没有其他两个圆圈痕迹、透明背景和适合target 的大小的图像。
【问题讨论】:
-
将其他内容设置为 display:none 或完全从 DOM 中删除
标签: javascript html svg browser render