【发布时间】:2020-09-12 00:10:31
【问题描述】:
以下相当愚蠢的代码在 Firefox 中运行良好,但在 chrome 中会发生 cmets 中指示的情况。想要检查 SVG 功能,所以我正在检查椭圆的中心是否是该椭圆的填充点,这显然是正确的。令人费解的事实是前两个控制台日志。 Dom 点输出为“Dom 点”,然后其 typeof 为 object,这自然会破坏接下来的两条 console.log 语句。
let ellipseFirstStop = document.getElementById("Sation_1_circle"); //Sation_1_circle is an SVG ellipse.
let centroFirstStop = new DOMPoint(+ellipseFirstStop.cx.animVal.value, +ellipseFirstStop.cy.animVal.value);
console.log(centroFirstStop);
//outputs DOMPoint {x: 268.2749938964844, y: 183.63299560546875, z: 0, w: 1}, all fine.
console.log(typeof centroFirstStop);
//outputs "object", what?
console.log("is point in fill test: "+ellipseFirstStop.isPointInFill(centroFirstStop));
//causes: Uncaught TypeError: Failed to execute 'isPointInFill' on 'SVGGeometryElement' parameter 1 is not of type 'SVGPoint'.
console.log("is point in fill test: "+ellipseFirstStop.isPointInFill(new DOMPoint(+ellipseFirstStop.cx.animVal.value, +ellipseFirstStop.cy.animVal.value)));
//causes: Uncaught TypeError: Failed to execute 'isPointInFill' on 'SVGGeometryElement': parameter 1 is not of type 'SVGPoint'.
【问题讨论】:
-
我理解你的意思,但我明确地创建了一个 DOM 点,为其提供从 SVG 检索的值,第一个 console.log 确认这是一个 DOM 点:s
-
我不认为你明白我的意思。您正在创建一个
DOMPoint对象。然后你将它传递给一个只接受SVGPoint对象的方法,并通过非常清晰的错误消息Uncaught TypeError: Failed to execute 'isPointInFill' on 'SVGGeometryElement' parameter 1 is not of type 'SVGPoint'.告诉你这一点。x和y的来源没有任何区别。这是您传递给它的对象类型错误。 Firefox 不在乎这很好,但 Chrome 显然在乎,并且正在告诉你究竟出了什么问题。改为创建SVGPoint。 -
对不起。读得太快了。现在尝试:让 mySVGPoint = ellipseFirstStop.createSVGPoint();给我 createSVGPoint() 不是一个函数 :'(
-
你需要一个
标签: javascript svg dom