gpt4 book ai didi

javascript - SVG HTML 不绘制 SVG 模式

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:33 24 4
gpt4 key购买 nike

我在 HTML 中创建 SVG 模式,用户将能够更改颜色和大小等。但它不起作用。

我得到一个关于 body onload 函数的错误。然后将 SVG 图附加到我拥有的 svg 占位符时。

这是脚本:

<script>
var SVG = document.getElementById("svgArea");
document.content.appendChild(SVG);

function drawCircle()
{


var svgLink = "http://www.w3.org/2000/svg";
var Centre = document.createElementNS(svgLink,"circle");

Centre.setAttributeNodeNS(null,"id","Centre");
Centre.setAttributeNodeNS(null,"cx",230);
Centre.setAttributeNodeNS(null,"cy",0);
Centre.setAttributeNodeNS(null,"r",75);
Centre.setAttributeNodeNS(null,"fill","centreColour");



document.getElementById("svgArea").appendChild(Centre);


var group = document.getElementById("svgArea");
group.setAttribute("transform","translate(230,0)");

}
</script>

然后对于 body 标签,我有以下内容:

<body onload="drawCircle()">

对于页面内容,我有以下代码:

<div class="content">
<!-- SVG DIAGRAM -->

<svg id="SVG" style="background:pink" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="690" height"400">

<g id="svgArea">
</g>
</svg>
</div>

错误:

[Error] TypeError: 'undefined' is not an object (evaluating 'document.content.appendChild')
global code (SVG.html, line 33)

[Error] TypeMismatchError: DOM Exception 17: The type of an object was incompatible with the expected type of the parameter associated to the object.
drawCircle (SVG.html, line 57)
onload (SVG.html, line 107)

我哪里做错了什么?

谢谢

这行不通:

//Drawing Petals
for (var i = 0; i < numberOfPetals; i++)
{
var svgLink = "http://www.w3.org/2000/svg";
var flowerPettle = document.createElementNS(svgLink,"ellipse");


flowerPettle.setAttributeNS(null,"id","flowerPettle");
flowerPettle.setAttributeNS(null,"ry", 230);
flowerPettle.setAttributeNS(null,"rx",0)
flowerPettle.setAttributeNS(null,"fill",petalColour);

var rotate = "rotate(" + (i*(360 / numberOfPetals)) + " " + 300 + "," + 30 + ")";

flowerPettle.setAttribute("transform",rotate);


document.getElementById("FlowerArea").appendChild(flowerPettle);

}

最佳答案

您的代码有一些问题。一个是 createAttributeNode() 调用。另一个是 document.content 引用。此外还有奇怪的颜色值“centreColour”。

我整理了一个可用的 fiddle 。希望它能帮助您在代码中正常工作。

Demo here

关于javascript - SVG HTML 不绘制 SVG 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220849/

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