gpt4 book ai didi

javascript - Circle 未使用 Canvas 正确连接线

转载 作者:行者123 更新时间:2023-12-04 10:29:36 24 4
gpt4 key购买 nike

我正在尝试创建 11 个圆圈,这些圆圈通过线与中间圆圈相连。我正在尝试绘制圆圈。在这里我做了一些研发,但我无法制作线条。请帮我完成这个。

 var canvas, ctx;
var circlePoints = [];
function createCanvasPainting() {

canvas = document.getElementById('myCanvas');
if (!canvas || !canvas.getContext) {
return false;
}
canvas.width = 600;
canvas.height = 600;
ctx = canvas.getContext('2d');
ctx.strokeStyle = '#B8D9FE';
ctx.fillStyle = '#B8D9FE';
ctx.translate(300, 250);
ctx.arc(0, 0, 50, 0, Math.PI * 2); //center circle
ctx.stroke();
ctx.fill();
var angleRotate = 0;
for (var i=0; i<11; i++) {
if (i > 0) {
angleRotate += 32.72;
}
lineToAngle(ctx, 0, 0, 200, angleRotate);
}
}

function lineToAngle(ctx, x1, y1, length, angle) {

angle *= Math.PI / 180;

var x2 = x1 + length * Math.cos(angle),
y2 = y1 + length * Math.sin(angle);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineWidth = 1;
ctx.arc(x2, y2, 40, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();

circlePoints.push({x: x2, y: y2});
// console.log(circlePoints);
}

createCanvasPainting();
<canvas id="myCanvas"></canvas>

Here is my JSFiddle Link

最佳答案

见下文,我从您的代码中删除了所有“噪音”。
只是带有与中间圆圈连接的线条的圆圈。

canvas = document.getElementById('myCanvas');
canvas.width = canvas.height = 200;
ctx = canvas.getContext('2d');
ctx.lineWidth = 1;
ctx.translate(99, 99);
angle = 0;

function draw() {
ctx.clearRect(-99, -99, 200, 200);
ctx.beginPath();
ctx.arc(0, 0, 35 + Math.cos(angle / 3000), 0, Math.PI * 2);
ctx.stroke();
ctx.fill();

for (var i = 0; i < 11; i++) {
a = angle * Math.PI / 180;
x = 80 * Math.cos(a)
y = 80 * Math.sin(a)
ctx.beginPath();
ctx.arc(x, y, 18, 0, Math.PI * 2);
ctx.moveTo(x, y);
ctx.lineTo(0, 0);
ctx.fill();
ctx.stroke();
angle += 32.7;
}
}

setInterval(draw, 10);
<canvas id="myCanvas"></canvas>

关于javascript - Circle 未使用 Canvas 正确连接线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473417/

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