gpt4 book ai didi

javascript - 如何计算 HTML Canvas 弧线结束 Angular 的 x 和 y 坐标?

转载 作者:行者123 更新时间:2023-12-03 04:33:26 25 4
gpt4 key购买 nike

我有一个 HTML5 Canvas ,我使用下面的代码绘制圆弧:

c = document.querySelector("#myCanvas");
ctx = c.getContext("2d");
var startAngle = 0;
var endAngle = 0.25;
var xPos = 300;
var yPos = 200;
var radius = 100;

ctx.beginPath();
ctx.arc(xPos, yPos, radius, startAngle*Math.PI, endAngle*Math.PI);
ctx.stroke();
ctx.closePath();

如何计算 endAngle*Math.PI 生成的结束 Angular x 和 y 坐标?

最佳答案

如果我理解正确,你可以通过以下方式实现这一目标......

var c = document.querySelector("#canvas");
var ctx = c.getContext("2d");
var startAngle = 0; //degree
var endAngle = 229; //degree
var xPos = 100;
var yPos = 100;
var radius = 60;

// compute x and y coordinates of the end angle relative to canvas
var x = xPos + Math.cos(endAngle * Math.PI / 180) * radius;
var y = yPos + Math.sin(endAngle * Math.PI / 180) * radius;

ctx.beginPath();
ctx.arc(xPos, yPos, radius, startAngle * Math.PI / 180, endAngle * Math.PI / 180);
ctx.stroke();

console.log('angle ended in position x:', x|0, ', y:', y|0);
canvas {
border: 1px solid lightgrey;
}
<canvas id="canvas" width="200" height="200"></canvas>

关于javascript - 如何计算 HTML Canvas 弧线结束 Angular 的 x 和 y 坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43396822/

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