<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div>
<canvas id="myCanvas" width="600" height="600" style="border: 1px solid blue;"></canvas>
</div>
</body>
<script>
// r=a(1-sinθ)公式
// 获取 canvas 元素
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// 绘制坐标轴 横坐标
ctx.beginPath();
ctx.moveTo(0, 300);
ctx.lineTo(900, 300);
ctx.stroke();
// 绘制坐标轴 纵坐标
ctx.beginPath();
ctx.moveTo(300, 0);
ctx.lineTo(300, 900);
ctx.stroke();
var t0 = 0.1;
// 弧度
var t = t0 / 180 * Math.PI;
// 设置了3600个小圆点
for (var i = 0; i < 3600; i++) {
const pointX = -(100 * (2 * Math.sin(t) - Math.sin(2 * t)) - 300)
const pointY = -(100 * (2 * Math.cos(t) - Math.cos(2 * t)) - 300)
t0 += 0.1;
t = t0 / 180 * Math.PI;
ctx.beginPath();
ctx.arc(pointX, pointY, 1, 0, Math.PI * 2); // 绘制点
ctx.fill();
}
</script>
</html>
【JavaScript】canvas 绘图 r=a(1-sinθ)
于 2024-01-10 17:20:41 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)