【发布时间】:2021-11-21 12:01:49
【问题描述】:
我想获取一些给定坐标的 SVG 元素。
我尝试使用document.elementsFromPoint(x,y)。但是,它只返回主 svg 元素本身,而不是 svg 中的子元素(圆、路径等)。
=>如何找到给定坐标的 SVG 元素?
我想通过按箭头键在绿色路径上移动红色圆圈的示例 html 文件。只有当圆圈保持在绿色路径上时才允许移动。
截图:
演示:
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
function onClick() {
alert('You have clicked the circle.')
}
function onKeyPress(event) {
switch (event.keyCode) {
case 37:
moveLeft();
break;
case 38:
moveUp();
break;
case 39:
moveRight();
break;
case 40:
moveDown();
break;
default:
}
}
function moveDown() {
console.log('down');
var path = d3.select('#path');
var robot = d3.select('#robot');
var cx = Number(robot.attr('cx'));
var cy = Number(robot.attr('cy'));
var newcy = cy + 10;
var elements = document.elementsFromPoint(cx, newcy)
if (path in elements) {
robot.attr('cy', cy + 10);
}
}
function moveUp() {
console.log('up');
var robot = d3.select('#robot');
var cy = Number(robot.attr('cy'));
robot.attr('cy', cy - 10);
}
function moveLeft() {
console.log('left')
var robot = d3.select('#robot');
var cx = Number(robot.attr('cx'));
robot.attr('cx', cx - 10);
}
function moveRight() {
console.log('right');
var robot = d3.select('#robot');
var cx = Number(robot.attr('cx'));
robot.attr('cx', cx + 10);
}
function onLoad() {
console.log('onload')
this.addEventListener('keydown', event => onKeyPress(event));
}
</script>
<svg width='500px' height='500px' focusable onload="onLoad()">
<text x='0' y='20' fill='blue'>Hello world from within svg! Press arrow keys to move the circle:</text>
<path id="path" d="M100 100 L 100 200 L 200 200" stroke='green' fill="transparent"/>
<circle id="robot" cx="100" cy="100" r="5" fill='red' onclick="onClick()" />
</svg>
</body>
</html>
【问题讨论】:
-
document.elementsFromPoint 也适用于 SVG 元素,但它是相对于视口的,而 cx 和 cy 是相对于 svg 的 viewBox 的。在这里,您需要添加身体的填充。此外,
elements将是一个数组,您不会使用in检查数组是否包含元素。
标签: javascript svg