javascript - ClipPath 隐藏原始形状 - d3 ReactJS
问题描述
我是 D3 和 ReactJS 的新手,试图用直线剪圆,但不知道它是如何工作的。我有这个 HTML 代码来绘制图像;
<div >
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" className="clip-path" ref={this.attachCircle.bind(this)}>
</svg>
</div>
这是我绘制图像的功能;
attachCircle = (svgRef:SVGElement) => {
const svg = d3.select(svgRef);
// draw a circle
svg.append("circle") // shape it as an ellipse
.attr("cx", 100) // position the x-centre
.attr("cy", 80) // position the y-centre
.attr("r", 80) // set the x radius
.attr("fill", "SteelBlue")
}
结果是这样的;
但是一旦我加入clipPath
圈子;
svg.append("clipPath") // define a clip path
.attr("id", "clip") // give the clipPath an ID
.append("circle") // shape it as an ellipse
.attr("cx", 100) // position the x-centre
.attr("cy", 80) // position the y-centre
.attr("r", 80) // set the x radius
.attr("fill", "SteelBlue")
它在屏幕上什么也不显示。它没有画任何圆圈或任何东西。
谁能解释为什么会这样??或者我错过了什么?
解决方案
你需要使用use
<use clip-path="url(#clip)" xlink:href="#clip" fill="red" />
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/clipPath
推荐阅读
- angular - 在应用程序中禁用 Angular AuthGuard 和拦截器安全性,切换功能?
- sql - 带有分片(Vitess)的 mysql 会比任何没有 SQL 数据库都快吗?
- reactjs - RadioButton 一个接一个(进入一个空间)
- python - 如何在 for 循环中将“无”变量更改为“不适用”
- r - 传单地图标记问题 - R Shiny
- java - 新的 Runnable 导致胶子应用程序崩溃
- c++ - 在 Windows 驱动程序中使用 ZwNotifyChangeKey
- json - XQuery 如何计算 JSON 中的所有“咒语”
- bash - 使用 fzf 从目录中查找并执行程序
- python - Tensorflow-gpu 仅使用 1.14 版本训练自定义对象检测模型