好的,我再次重写了代码,这一次只是传递了转换函数的参数,但是它仍然没有得到这个值。
<!DOCTYPE html>
<html lang = "en" class="tas-com">
<head>
<meta charset="utf-8">
<title>Simple Stars</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<h1>Some Stars</h1>
<svg width="200" height="200" style='background: aliceblue'>
</svg>
<script type="text/javascript">
var x;
var y;
function draw(q,r){
var svg1 = d3.select("svg");
svg1.append("polygon")
.attr("points", "0,0.9511, 1.0,0.95111, 1.309,0, 1.618,0.9511, 2.618,0.9511, 1.809,1.5388, 2.118,2.4899, 1.309,1.9021, 0.5,2.4899, 0.809,1.5388")
.attr("fill", "blue")
.attr("transform", "translate(q, r), scale(4)")
.style("fill", "yellow");
}
var svg = d3.select("svg");
svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 200)
.attr("height", 200)
.style("fill", "blue");
for (var i=0;i<150;i+=15){
var o=30
draw(o+i,o+i)
}
</script>
</body>
</html><script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.10/d3.min.js"></script>
嘿,我正试图和d3和javascript混在一起,并试图画出欧洲的旗帜。但是星星根本没有被展示出来。错误可能是什么?我想用这个函数来表示x,y坐标的值。所画的圆圈暂时只是占位符。
谢谢
<!DOCTYPE html>
<html lang = "en" class="tas-com">
<head>
<meta charset="utf-8">
<title>Simple Stars</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<h1>Some Stars</h1>
<svg width="200" height="200" style="background: aliceblue"/>
</svg>
<script type="text/javascript">
var x
var y
function draw(q){
x= 100 + 60*Math.cos(q)
y= 100 + 60*Math.sin(q)
return x,y
}
var svg = d3.select("svg");
svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 200)
.attr("height", 200)
.style("fill", "blue");
{draw(10)
svg.append("polygon")
.attr("points", "x,y, x+1.0,y, x+1.309,y-0.9511, x+1.618,y, x+2.618,y, x+1.809,y+0.5877, x+2.118,y+1.5388, x+1.309,y+0.951, x+0.5,y+1.5388, x+0.809,y+0.5877")
.attr("transform", "translate(30, 30), scale(4)")
.style("fill", "green");
};
{draw(25)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(40)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(55)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(70)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(85)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(100)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(115)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(130)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(145)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(160)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
{draw(175)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red")
};
</script>
</body>
</html><script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
发布于 2018-09-30 13:03:34
你的重构更好但这行
.attr("transform", "translate(q, r), scale(4)")应写为:
.attr("transform", "translate(" + q + "," + r + "), scale(4)")下面是对整个代码进行进一步清理的部分:
<!DOCTYPE html>
<html lang="en" class="tas-com">
<head>
<meta charset="utf-8">
<title>Simple Stars</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<h1>Some Stars</h1>
<svg width="200" height="200" style='background: aliceblue'>
</svg>
<script type="text/javascript">
var svg;
function draw(q, r) {
var x = 100 + 60 * Math.cos(q),
y = 100 + 60 * Math.sin(q)
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", 5)
.style("fill", "red");
svg.append("polygon")
.attr("points", "0,0.9511, 1.0,0.95111, 1.309,0, 1.618,0.9511, 2.618,0.9511, 1.809,1.5388, 2.118,2.4899, 1.309,1.9021, 0.5,2.4899, 0.809,1.5388")
.attr("fill", "blue")
.attr("transform", "translate(" + (x - 5) + "," + (y - 5) + "), scale(4)")
.style("fill", "yellow");
}
svg = d3.select("svg");
svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 200)
.attr("height", 200)
.style("fill", "blue");
for (var i = 0; i < 150; i += 15) {
var o = 30
draw(o + i, o + i)
}
</script>
</body>
</html>
https://stackoverflow.com/questions/52573520
复制相似问题