仅使用getBoundingClientRect查找svg特定区域中的元素



我正在使用python svgwrite创建一个SVG文件,并且在我的绘图中心有一个形状,我创建了一个路径。我想删除不在包装器形状内的元素。

首先,我可以在svgwrite中移除它们吗?如果不是,我怎么能找到所有的元素在前端使用javascript删除他们中的任何不在我的形状?

svgwrite

# dots that are genrated in the svg are like this
image.add(image.circle((x, y), mag, id='dot', stroke="none", fill=color))
# this is my heart shape that should dots go inside of it
image.defs.add(image.path(d="M0 200 v-200 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0z", id="heart_shape", style="rotate: 225deg;scale:1.9;stroke: #fff;", opacity="1"))
image.add(image.use(href="#heart_shape", fill="none", insert=(half_x, str(height-80)+"mm"), id="heart_wrapper"))

我更喜欢在前端使用javascript删除它们。我得到了形状的边界,如下所示:

var heart = document.querySelector("#heart_wrapper")
var {xHeart, yHeart} = heart.getBBox()
下面是生成的svg形状:
<use xmlns="http://www.w3.org/2000/svg" fill="none" id="heart_wrapper" x="377.9527559055118" xlink:href="#heart_shape" xmlns:xlink="http://www.w3.org/1999/xlink" y="195mm">
<path d="M0 200 v-200 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0z" id="heart_shape" opacity="1" style="rotate: 225deg;scale:1.9;stroke: #fff;"></path>
</use>

如果您有svg,heartdot的元素,您可以使用checkIntersection来检查一个元素是否存在于另一个元素的边界内。因此,检查dot是否在heart内:

var intersecting = svg.checkIntersection(dot, heart.getBBox());

最新更新