canvas实现在直角坐标系中画角度

根据输入的角度动态变化

html代码

<div>
   <input id="input" type="text" placeholder="输入任意角度" />
   <button>确定</button>
</div>
<canvas id="css"></canvas>

1. 画直角坐标系

window.onload = (event) => {
        // console.log(event);
        main();
      };
      let ccs;
      let ccsContext;
      function main() {
        ccs = document.querySelector("#css");
        // 设置画布的宽高
        ccs.width = 200;
        ccs.height = 200;
        ccsContext = ccs.getContext("2d");
        ccsContext.translate(ccs.width / 2, ccs.height / 2); // 使画布的起点向左移动 ccs.width / 2 像素,向下移动 ccs.height / 2 像素,使其起点位于画布中心
        // 画直线
        drawLine(ccsContext, 0, 0, ccs.width / 2, 0); // x轴正半轴
        drawLine(ccsContext, 0, 0, -ccs.width / 2, 0); // x轴负半轴
        drawLine(ccsContext, 0, 0, 0, ccs.height / 2); // y轴正半轴
        drawLine(ccsContext, 0, 0, 0, -ccs.height / 2); // y轴负半轴
        fillTriangle(
          ccsContext,
          ccs.width / 2 - 10,
          5,
          ccs.width / 2,
          0,
          ccs.width / 2 - 10,
          -5
        ); // x轴箭头
        fillTriangle(
          ccsContext,
          5,
          ccs.height / 2 - 10,
          0,
          ccs.height / 2,
          -5,
          ccs.height / 2 - 10
        ); // y轴箭头
      }

画直线和画小三角方法

function drawLine(canvasContext, x0, y0, x1, y1, color) {
        canvasContext.beginPath();
        canvasContext.moveTo(x0, -y0);
        canvasContext.lineTo(x1, -y1); // 画笔的终点,第一个参数表示向左移动 xx 像素,,第二个参数表示向下移动 yy 像素
        canvasContext.strokeStyle = color; // 该样式需写在.stroke前面
        canvasContext.stroke();
      }

      function fillTriangle(canvasContext, x0, y0, x1, y1, x2, y2, color) {
        canvasContext.beginPath();
        canvasContext.moveTo(x0, -y0);
        canvasContext.lineTo(x1, -y1);
        canvasContext.lineTo(x2, -y2);
        canvasContext.closePath();
        canvasContext.fillStyle = color;
        canvasContext.fill();
      }

2. 画角度

function drawAngle(angle = 30, r = 30) {
        // 画角度线
        drawLine(
          ccsContext,
          0,
          0,
          (ccs.height / 2) * Math.sin(getHud(angle)),
          (ccs.width / 2) * Math.cos(getHud(angle)),
          "red"
        );

        let l = Math.sqrt(25 + (ccs.height / 2 - 10) * (ccs.height / 2 - 10));
        let pAngle = 3;

        fillTriangle(
          ccsContext,
          l * Math.sin(getHud(angle - pAngle)),
          l * Math.cos(getHud(angle - pAngle)),
          l * Math.sin(getHud(angle + pAngle)),
          l * Math.cos(getHud(angle + pAngle)),
          (ccs.height / 2) * Math.sin(getHud(angle)),
          (ccs.width / 2) * Math.cos(getHud(angle)),
          "red"
        ); // 角度线箭头

        // 画弧度
        ccsContext.beginPath();
        ccsContext.arc(
          0,
          0,
          20,
          hypotenuse(ccs.width / 2, 90 - angle),
          1.5 * Math.PI,
          true
        );
        //画文字
        ccsContext.font = "10px Arial";
        //计算文字位置
        radians = angle * (Math.PI / 180); //角度转弧度
        ccsContext.fillText(
          angle + "°",
          r * Math.sin(radians / 2) - 5,
          -(r * Math.cos(radians / 2) - 5)
        );
        ccsContext.stroke();
      }
      //换算小弧度的大小
      function hypotenuse(r, angle) {
        return (2 - angle / 180) * Math.PI;
      }
      //换算角度对应的弧度
      function getHud(angle) {
        return (angle / 180) * Math.PI;
      }
document.querySelector("button").onclick = doit; //点击事件
function doit() {
   ccsContext.clearRect(0, 0, ccs.width, ccs.height); //清除整个画布
   main(); // 重新绘制直角坐标系
   let value = document.getElementById("input").value * 1;
   drawAngle(value); // 画角度
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,它展示了如何使用canvas和动画来展示极坐标转换为直角坐标系中的坐标的过程。 HTML: ``` <canvas id="canvas"></canvas> ``` CSS: ``` canvas { border: 1px solid black; } ``` JavaScript: ``` // 获取canvas元素和上下文 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 设置canvas的宽度和高度 canvas.width = 500; canvas.height = 500; // 定义极坐标直角坐标系中的坐标 var polarCoord = {r: 100, theta: 0}; var cartesianCoord = {x: 0, y: 0}; // 定义动画的变量 var animationFrameId; var animationStartTime; var animationDuration = 1000; // 毫秒 // 开始动画 function startAnimation() { animationStartTime = new Date().getTime(); animationFrameId = requestAnimationFrame(updateAnimation); } // 更新动画 function updateAnimation() { // 计算动画的进度 var progress = Math.min(1, (new Date().getTime() - animationStartTime) / animationDuration); // 计算当前的极坐标直角坐标系中的坐标 polarCoord.theta = Math.PI * 2 * progress; cartesianCoord.x = polarCoord.r * Math.cos(polarCoord.theta); cartesianCoord.y = polarCoord.r * Math.sin(polarCoord.theta); // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制极坐标中的点 ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2, polarCoord.r, 0, Math.PI * 2); ctx.stroke(); // 绘制直角坐标系中的点 ctx.beginPath(); ctx.arc(canvas.width / 2 + cartesianCoord.x, canvas.height / 2 - cartesianCoord.y, 5, 0, Math.PI * 2); ctx.fill(); // 如果动画未完成,则继续更新动画 if (progress < 1) { animationFrameId = requestAnimationFrame(updateAnimation); } } // 停止动画 function stopAnimation() { cancelAnimationFrame(animationFrameId); } // 注册事件监听器 document.getElementById("startButton").addEventListener("click", startAnimation); document.getElementById("stopButton").addEventListener("click", stopAnimation); ``` 该示例代码使用canvas绘制两个圆,一个表示极坐标中的点,另一个表示直角坐标系中的点。它使用动画来逐步更新极坐标直角坐标系中的坐标,从而展示极坐标转换为直角坐标系中的坐标的过程。您可以点击“开始”按钮来启动动画,点击“停止”按钮来停止动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值