【发布时间】:2020-01-19 02:56:00
【问题描述】:
我正在尝试使用ClipPath 剪裁一条带圆圈的线,并通过拖动成功完成。这是我的工作代码;
function clipData(svg, cx, cy) {
var clip = svg.append("defs")
.append("clipPath") // define a clip path
.attr("id", "clip") // give the clipPath an ID
.append("circle") // shape it as an ellipse
.attr("id", "my-circle")
.attr("cx", 100) // position the x-centre
.attr("cy", 80) // position the y-centre
.attr("r", 80) // set the x radius
.attr("fill", "yellow")
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", "red")
var g = svg.append("g")
.datum({
x: 0,
y: 0
})
.attr("id", "child")
//.attr("transform", function(d) { return 'translate(' + d.x + ' '+ d.y + ')'; })
.call(d3.drag()
.on("start", function(d) {
d3.select(this).raise().classed("active", true);
})
.on("drag", function(d) {
d3.select(this).attr("transform", "translate(" + (d3.event.x) + "," + (d3.event.y) + ")");
d3.select("#svgGreen").selectAll("*").remove();
clipData(svg, d3.event.x + cx, d3.event.y + cy);
})
.on("end", function(d) {
d3.select(this).classed("active", false);
}));
g.append("line")
.attr("clip-path", "url(#clip)")
.attr("x1", cx)
.attr("y1", cy)
.attr("x2", cx + 100)
.attr("y2", cy + 100)
.style("stroke", "purple")
.style("stroke-width", 12)
}
var svg = d3.select("#svgGreen");
var cx = 100,
cy = 80,
x = 0,
y = 0;
clipData(svg, cx, cy);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" className="clip-path" id="svgGreen">
</svg>
现在我试图在剪辑后获得length of Visible part of Line。但是当我尝试从线的 x-y 点获取它时,我总是得到与剪裁前相同的长度,
有什么方法可以让我在之后只获得可见的线长 剪辑??
【问题讨论】:
-
您能否进一步说明您想要实现的目标?
-
最简单的事情是..我有一条线,正在被一个圆圈剪裁,所需要的只是剪裁后线条可见区域的长度..我现在无法得到..我没有找到任何其他解决方案
-
如果你需要上面代码sn-p上的线的长度,那么你可以得到那个圆的半径,它会是一样的:)
-
但它并不总是圆形......它也可以是多边形或任何其他弯曲路径
标签: javascript reactjs d3.js clip-path