【发布时间】:2020-02-26 16:54:21
【问题描述】:
我想使用 for 循环在圆形中绘制没有贝塞尔曲线。 到目前为止,我无法在圆形模式下放置任何贝塞尔曲线,但这些曲线不是弯曲的,它们只是直线,因为我无法正确更改手柄,这将使这些曲线弯曲。你可以在这里看到我的代码,请让我知道我的错误。
我也尝试将颜色填充到曲线中,但没有发生这种情况,我不知道为什么。
<html>
<body>
<style>
*{
margin: 0px;
}
body{
background-color: aqua;
}
canvas
{
background-color: white;
}
</style>
<canvas id="mycanvas"></canvas>
<script>
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
x = window.innerWidth/2;
y = window.innerHeight/2;
r = window.innerWidth;
theta = 0.1;
for(theta=0.1; theta<12.6; theta+=0.1) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.bezierCurveTo(x,y,x,y, x+ r * Math.cos(theta),y + r * Math.sin(theta));
ctx.lineWidth = 1;
ctx.strokeStyle = "black";
ctx.stroke();
ctx.closePath();
ctx.fill();
}
for ( i=r/16; i < r; i=i+r/12 ) {
ctx.beginPath();
ctx.arc(x, y, i, 0, 2 * Math.PI, false);
ctx.stroke();
}
</script>
</body>
</html>
【问题讨论】:
-
仅供参考:如果您设置
canvas { display: block; },您将停止滚动
标签: javascript html for-loop canvas html5-canvas