canvas 绘制坐标轴

 

 

结果:

 

 

代码:

 

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    body {
      text-align: center;
    }
    canvas {
      background: #ddd;
    }
  </style>
</head>
<body>
  <h3>绘制路径——直线</h3>
  <canvas id="c13" width="500" height="400"></canvas>
  <script>
    var ctx = c13.getContext('2d');

    //绘制X轴
    ctx.beginPath();
    ctx.moveTo(50, 350);
    ctx.lineTo(450, 350);
    ctx.lineTo(450-20, 350-20);
    ctx.moveTo(450, 350);
    ctx.lineTo(450-20, 350+20);

    //ctx.lineJoin = 'miter'; //线的连接处采用尖角
    //ctx.lineJoin = 'bevel'; //线的连接处采用方角
    ctx.lineJoin = 'round'; //线的连接处采用圆角
    ctx.lineWidth = 5;
    ctx.strokeStyle = '#0a0';
    ctx.stroke();

    //绘制Y轴
    ctx.beginPath();  //必须开始新路径
    ctx.moveTo(50, 350);
    ctx.lineTo(50, 50);
    ctx.lineTo(50-20, 50+20);
    ctx.moveTo(50, 50);
    ctx.lineTo(50+20, 50+20);

    ctx.strokeStyle = '#00f';
    ctx.stroke();

  </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/web-fusheng/p/6906630.html

uniApp的Canvas API允许开发者在Webview组件中创建图形,包括绘制坐标轴。要在uniApp中绘制坐标轴,你可以按照以下步骤操作: 1. **准备环境**: 首先,在uniApp项目中引入`<canvas>`元素,并给它一个id,比如`myCanvas`。 ```html <view class="container"> <canvas id="myCanvas" canvas-id="myCanvas" style="display:none;"></canvas> </view> ``` 2. **获取绘图上下文**: 在JavaScript中,通过`uni.createSelectorQuery()`获取到Canvas节点并取得2D渲染上下文: ```javascript const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); ``` 3. **设置坐标系**: 设置绘图区域大小,并确定原点位置。例如,如果你想一个从0到100的x轴和y轴,可以这样做: ```javascript ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除原有内容 const width = canvas.width; const height = canvas.height; const centerX = width / 2; const centerY = height / 2; // 设置坐标轴范围和间隔 const xAxisMin = 0; const yAxisMin = 0; const xAxisMax = 100; const yAxisMax = 100; const xAxisStep = (xAxisMax - xAxisMin) / 10; // 每个刻度的距离 const yAxisStep = (yAxisMax - yAxisMin) / 10; ``` 4. **绘制坐标线**: 使用`moveTo`和`lineTo`方法绘制x轴和y轴线条: ```javascript // x轴 for (let i = xAxisMin; i <= xAxisMax; i += xAxisStep) { ctx.moveTo(centerX, height); ctx.lineTo(centerX, height - Math.round(i * height / xAxisMax)); } // y轴 for (let j = yAxisMin; j <= yAxisMax; j += yAxisStep) { ctx.moveTo(width, centerY); ctx.lineTo(Math.round(j * width / yAxisMax), centerY); } ``` 5. **添加标签**: 可以利用`fillText`方法在每个刻度上添加数字标签: ```javascript // 添加x轴和y轴标签 const fontSize = 14; for (let i = xAxisMin; i <= xAxisMax; i += xAxisStep) { const labelY = height - Math.round(i * height / xAxisMax) - fontSize / 2; ctx.fillText(`${i}`, centerX, labelY); } for (let j = yAxisMin; j <= yAxisMax; j += yAxisStep) { const labelX = Math.round(j * width / yAxisMax) + fontSize / 2; ctx.fillText(`${j}`, labelX, centerY); } ``` 6. **显示结果**: 最后记得将`canvas`元素显示出来: ```javascript canvas.style.display = 'block'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值