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

标签 javascript python 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()
注意:我不确切知道的是如何确定 dot 是否在里面我的形状。我知道如何选择所有的点并删除它们

这是生成的 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>

最佳答案

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

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

关于javascript - 仅使用 getBoundingClientRect 查找 svg 特定区域中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68518234/

相关文章:

javascript - 我怎样才能干燥这个 Controller ( Angular 1.5)

python - Python 中的 ZMQ - PULL 端进程能否知道 PUSH 端进程是否已关闭?

javascript - 从 html img 标签更改一个 svg 元素的颜色 - css

css - SVG url 的 currentColor 继承

graph - d3.js 中的网络多路由正交图

javascript - 使用 puppeteer 从未知的 li 中检索数据

javascript - 如何使用简单的推送通知唤醒应用程序?

javascript - Owl Carousel 外箭头导航

python - 如何模拟 Selenium 中的触摸屏?

python - django modeladmin list_display