绘制Canvas坐标系

知识要点

canvas中的坐标是从左上角开始的,x轴沿着水平方向(按像素)向右延伸,y轴沿垂直方向向下延伸。左上角坐标为x=0,y=0的点称作原点。在默认坐标系中,每一个点的坐标都是直接映射到一个CSS像素上。

代码

对于canvas的初学者,我们可以在绘制坐标系来辅助canvas设计,以下是一段示例代码:

HTML

<div id="coordinates">X: 0, Y: 0</div>
<canvas id="canvas" width="800" height="600" style="border:1px solid #000;"></canvas>

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const coordinatesDiv = document.getElementById('coordinates');

// 绘制箭头
function drawArrow(fromX, fromY, toX, toY, label) {
    const headlen = 10; // 箭头长度
    const angle = Math.atan2(toY - fromY, toX - fromX);

    ctx.beginPath();
    ctx.moveTo(fromX, fromY);
    ctx.lineTo(toX, toY);
    ctx.lineTo(toX - headlen * Math.cos(angle - Math.PI / 6), toY - headlen * Math.sin(angle - Math.PI / 6));
    ctx.moveTo(toX, toY);
    ctx.lineTo(toX - headlen * Math.cos(angle + Math.PI / 6), toY - headlen * Math.sin(angle + Math.PI / 6));
    ctx.stroke();

    ctx.font = "16px Arial";
    ctx.fillText(label, toX + (label === 'X' ? -20 : 10), toY + (label === 'Y' ? -10 : 20));
}

// 绘制坐标轴
function drawAxes() {
    const width = canvas.width;
    const height = canvas.height;
    
    // X轴
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(width, 0);
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 2;
    ctx.stroke();
    drawArrow(0, 0, width, 0, 'X');

    // Y轴
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(0, height);
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 2;
    ctx.stroke();
    drawArrow(0, 0, 0, height, 'Y');
}

// 绘制坐标刻度
function drawTicks() {
    const width = canvas.width;
    const height = canvas.height;

    // X轴刻度
    for (let i = 0; i < width; i += 50) {
        ctx.beginPath();
        ctx.moveTo(i, -5);
        ctx.lineTo(i, 5);
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 1;
        ctx.fillText(i, i - 8, 20); // X轴刻度数值
        ctx.stroke();
    }

    // Y轴刻度
    for (let i = 0; i < height; i += 50) {
        ctx.beginPath();
        ctx.moveTo(- 5, i);
        ctx.lineTo(+ 5, i);
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 1;
        ctx.fillText(i, 8, i + 8); // Y轴刻度数值
        ctx.stroke();
    }
}

// 绘制坐标系
function drawCoordinateSystem() {
    drawAxes();
    drawTicks();
}

drawCoordinateSystem();

 // 更新坐标显示
function updateCoordinatesDisplay(x, y) {
    coordinatesDiv.textContent = `X: ${x}, Y: ${y}`;
}

// 处理鼠标移动事件
canvas.addEventListener('mousemove', (event) => {
    const rect = canvas.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;
    updateCoordinatesDisplay(x, y);
});

// 以下代码是实例,你可以写自己的代码
function drawHouse() {
  // Set line width
  ctx.lineWidth = 10;

  // Wall
  ctx.strokeRect(75, 140, 150, 110);

  // Door
  ctx.fillRect(130, 190, 45, 65);

  // Roof
  ctx.beginPath();
  ctx.moveTo(50, 140);
  ctx.lineTo(150, 60);
  ctx.lineTo(250, 140);
  ctx.closePath();
  ctx.stroke();
}
drawHouse()

完整的示例请查看Codepen

效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解答这个问题。下面是利用Canvas进行绘制XY坐标系的代码示例: ```html <!DOCTYPE html> <html> <head> <title>Canvas绘制XY坐标系</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> // 获取canvas元素 var canvas = document.getElementById("myCanvas"); // 获取canvas上下文 var ctx = canvas.getContext("2d"); // 绘制X轴 ctx.beginPath(); ctx.moveTo(20, 380); ctx.lineTo(380, 380); ctx.stroke(); // 绘制Y轴 ctx.beginPath(); ctx.moveTo(20, 20); ctx.lineTo(20, 380); ctx.stroke(); // 绘制X轴箭头 ctx.beginPath(); ctx.moveTo(380, 380); ctx.lineTo(370, 370); ctx.moveTo(380, 380); ctx.lineTo(370, 390); ctx.stroke(); // 绘制Y轴箭头 ctx.beginPath(); ctx.moveTo(20, 20); ctx.lineTo(10, 30); ctx.moveTo(20, 20); ctx.lineTo(30, 30); ctx.stroke(); // 绘制X轴刻度 var stepX = 40; for (var i = 0; i < 8; i++) { ctx.beginPath(); ctx.moveTo(20 + stepX * (i + 1), 380); ctx.lineTo(20 + stepX * (i + 1), 370); ctx.stroke(); } // 绘制Y轴刻度 var stepY = 40; for (var i = 0; i < 8; i++) { ctx.beginPath(); ctx.moveTo(20, 380 - stepY * (i + 1)); ctx.lineTo(30, 380 - stepY * (i + 1)); ctx.stroke(); } // 标注X轴坐标 ctx.font = "12px Arial"; ctx.fillText("0", 10, 395); ctx.fillText("1", 50, 395); ctx.fillText("2", 90, 395); ctx.fillText("3", 130, 395); ctx.fillText("4", 170, 395); ctx.fillText("5", 210, 395); ctx.fillText("6", 250, 395); ctx.fillText("7", 290, 395); ctx.fillText("8", 330, 395); // 标注Y轴坐标 ctx.fillText("0", 5, 385); ctx.fillText("1", 5, 345); ctx.fillText("2", 5, 305); ctx.fillText("3", 5, 265); ctx.fillText("4", 5, 225); ctx.fillText("5", 5, 185); ctx.fillText("6", 5, 145); ctx.fillText("7", 5, 105); ctx.fillText("8", 5, 65); </script> </body> </html> ``` 这段代码使用了Canvas的API来绘制XY坐标系,其中包括绘制坐标轴、箭头、刻度和标注坐标等步骤。您可以将代码复制到一个HTML文件中并在浏览器中打开来查看效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值