gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'createElementNS' of undefined

转载 作者:行者123 更新时间:2023-12-02 17:01:56 24 4
gpt4 key购买 nike

我正在尝试运行此函数,该函数采用路径并将其转换为多边形:

function polygonSampledFromPath(path,samples){
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');

var points = [];
var len = path.getTotalLength();
var step = step=len/samples;
for (var i=0;i<=len;i+=step){
var p = path.getPointAtLength(i);
points.push( p.x+','+p.y );
}
poly.setAttribute('points',points.join(' '));
return poly;
}


var path = "m247.96255,140.77197c28.82227,-82.68753 141.75,0 0,106.31247c-141.75005,-106.31247 -28.82213,-189 0,-106.31247z"

polygonSampledFromPath(path,30);

但是,我不断收到此错误,这在某种程度上导致我假设我在 path 变量中传递了错误的路径格式。

Uncaught TypeError: Cannot read property 'createElementNS' of undefined

有什么想法吗?

最佳答案

替换

var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');

var poly = document.createElementNS('http://www.w3.org/2000/svg','polygon');

关于javascript - 未捕获的类型错误 : Cannot read property 'createElementNS' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25610637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com