Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript和SVG:createElement()上的错误_Javascript_Svg - Fatal编程技术网

JavaScript和SVG:createElement()上的错误

JavaScript和SVG:createElement()上的错误,javascript,svg,Javascript,Svg,因此,我拥有的是一个SVG文档,没有HTML,在…-部分中,我有我的JavaScript。其中有一个函数用于创建椭圆弧(尽管我只将其用于圆弧,在本例中,仅用于圆),还有一个函数用于使用前一种方法创建圆。这可能不是目前最优雅的方式,但我会在以后研究 但是现在,当我试图通过createElement()创建新元素时,我在Chrome和Firefox中都发现了一个错误,即SVG DOM对象不包含该方法 代码如下: var SVGObj = document.getElementById('canvas

因此,我拥有的是一个SVG文档,没有HTML,在
-部分中,我有我的JavaScript。其中有一个函数用于创建椭圆弧(尽管我只将其用于圆弧,在本例中,仅用于圆),还有一个函数用于使用前一种方法创建圆。这可能不是目前最优雅的方式,但我会在以后研究

但是现在,当我试图通过
createElement()
创建新元素时,我在Chrome和Firefox中都发现了一个错误,即SVG DOM对象不包含该方法

代码如下:

var SVGObj = document.getElementById('canvas');
function Point(x, y) {
  this.x = x;
  this.y = y;
}

var ids =  new Array();
ids.nextID = nextID;
function nextID() {
  this.push(this.length);
  return this.length;
}

function hypot(pointA, pointB) {
  return Math.sqrt((pointA.x - pointB.x) * (pointA.x - pointB.x) + (pointA.y - pointB.y) * (pointA.y - pointB.y))
}

function arc(center, from, to, fill) {
  if (hypot(center, from) == hypot(center, to))
    radius = hypot(center, from);
  else 
    return false;
  var arch = SVGObj.createElement('path');
  arch.setAttribute('d', 'M ' + center.x + ',' + center.y + //Mittelpunkt
                        ' A ' + radius + ' ' + radius + //Radius
                        ' ' +  (Math.atan((from.y - center.y) / (from.x - center.x)) * 180 / Math.PI) + ' 1 0 ' + //from
                        to.x + ',' + to.y); //to
  arch.setAttribute('id', ids.nextID());
  if(fill) arch.setAttribute('style', 'fill: black');
  SVGObj.appendChild(arch);
  return true;
}

function fullCircle(center, radius, fill) {
  return arc(center, new Point(center.x + radius, center.y + radius), new Point(center.x + radius, center.y + radius), fill);
}


if(!fullCircle(new Point(100, 50), 40, true)) alert("Fehler");

您需要使用
document.createElements('http://www.w3.org/2000/svg“,”path')
在SVG名称空间中创建路径元素。

使用
document.createElement
而不是
SVGObj.createElement
谢谢,这很有效!我现在看不到,但至少错误已经消失了。。。再次感谢!