【canvas】使用canvas画出坐标和线

具体代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>在指定位置画多个点</title>
    <style>
        canvas{
            border: 1px dashed gray;
        }
    </style>
</head>
<body>
    <canvas id="cvs" width="500" height="500"></canvas>
</body>
</html>

js代码:

<script>
    var cvs = document.getElementById('cvs');
    var ctx = cvs.getContext('2d');

    // 坐标轴距离画布上右下左的边距
    var padding = {
        top:20,
        right:20,
        bottom:20,
        left:20
    }
    // 坐标轴中箭头的宽和高
    var arrow = {
        width:12,
        height:20
    }
    // 求坐标轴上顶点的坐标
    var vertexTop = {
        x:padding.left,
        y:padding.top
    }
    // 求坐标轴原点的坐标
    var origin = {
        x:padding.left,
        y:cvs.height - padding.bottom
    }
    // 求坐标轴右顶点的坐标
    var vertexRight = {
        x:cvs.width - padding.left,
        y:cvs.height - padding.bottom
    }

    //设置线宽
    ctx.lineWidth = 2;
    //画坐标轴的两条线
    ctx.beginPath();
    ctx.moveTo(vertexTop.x,vertexTop.y);
    ctx.lineTo(origin.x,origin.y);
    ctx.lineTo(vertexRight.x,vertexRight.y);
    ctx.stroke();

    //如何画箭头
    //画顶上箭头
    // ^
    // |
    // |
    ctx.beginPath();
    ctx.moveTo(vertexTop.x,vertexTop.y);
    ctx.lineTo(vertexTop.x - arrow.width/2,vertexTop.y + arrow.height);
    ctx.lineTo(vertexTop.x,vertexTop.y + arrow.height/2);
    ctx.lineTo(vertexTop.x + arrow.width/2,vertexTop.y + arrow.height);
    ctx.fill();

    //画右边的箭头
    // --->
    ctx.beginPath();
    ctx.moveTo(vertexRight.x,vertexRight.y);
    ctx.lineTo(vertexRight.x - arrow.height,vertexRight.y - arrow.width);
    ctx.lineTo(vertexRight.x - arrow.height/2,vertexRight.y);
    ctx.lineTo(vertexRight.x - arrow.height,vertexRight.y + arrow.width);
    ctx.fill();

    /*
     * 在坐标轴中指定位置画点,坐标算法:
     * 点的x轴:原点x坐标 + 点到原点的水平距离
     * 点的y轴:原点y坐标 - 点到原点的垂直距离
     */
    //定义点的坐标
    var points = [[10,10],[50,50],[90,90],[130,130],[170,170],[200,200]];
    //在坐标中画点 使用循环遍历数组中的坐标
    //设置颜色
    ctx.fillStyle = "green";
    points.forEach(function(arr){
        ctx.fillRect(origin.x + arr[0],origin.y - arr[1],5,5);
    });
    //根据点连线
    //防止重绘
    ctx.beginPath();
    ctx.lineWidth = 2;
    ctx.strokeStyle = "yellow";
    points.forEach(function (arr) {
        ctx.lineTo(origin.x + arr[0] + 1.8,origin.y - arr[1] + 1.8);
    });
    //描边
    ctx.stroke();
</script>


效果如下:





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Canvas 中画坐标轴的基本步骤如下: 1. 首先,确定坐标轴的位置和大小。可以使用 Canvas 的属性来设置画布的宽度和高度,然后使用变量来记录坐标轴的位置。 2. 绘制坐标轴的线条。可以使用 Canvas 的 moveTo 和 lineTo 方法来绘制线条,例如绘制 x 轴可以先 moveTo(xStart, yStart),再 lineTo(xEnd, yEnd)。 3. 绘制坐标轴的刻度和文字。可以使用 Canvas 的 fillText 和 strokeText 方法来绘制文字,使用 for 循环来绘制坐标轴上的刻度。 4. 绘制坐标轴的箭头。可以使用 Canvas 的 moveTo 和 lineTo 方法来绘制箭头,例如绘制 x 轴箭头可以先 moveTo(xEnd, yEnd),再 lineTo(xEnd-10, yEnd+5) 和 lineTo(xEnd-10, yEnd-5)。 下面是一个简单的示例代码,绘制一个带有刻度和箭头的 x 轴坐标轴: ```javascript const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const width = canvas.width; const height = canvas.height; const xStart = 50; // x 轴起点 const xEnd = width - 50; // x 轴终点 const yStart = height / 2; // y 轴起点 const yEnd = height / 2; // y 轴终点 // 绘制 x 轴线条 ctx.beginPath(); ctx.moveTo(xStart, yStart); ctx.lineTo(xEnd, yEnd); ctx.stroke(); // 绘制 x 轴刻度和文字 const step = 50; // 刻度间隔 for (let i = xStart + step; i < xEnd; i += step) { ctx.beginPath(); ctx.moveTo(i, yStart - 5); ctx.lineTo(i, yStart + 5); ctx.stroke(); ctx.fillText(i - xStart, i, yStart + 20); } // 绘制 x 轴箭头 ctx.beginPath(); ctx.moveTo(xEnd, yEnd); ctx.lineTo(xEnd - 10, yEnd + 5); ctx.lineTo(xEnd - 10, yEnd - 5); ctx.closePath(); ctx.fill(); ``` 希望这个示例可以帮助您了解如何在 Canvas 中绘制坐标轴。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值