canvas绘制表格

课程目标:

1.绘制表格需要什么核心方法?
2.步骤是怎么样的?
3.源代码

1.绘制表格需要什么核心方法?

Math.floor  beginPath     moveTo  lineTo  strokeStyle  stroke  getContext

2.步骤是怎么样的?

let oCanvas = document.querySelector("canvas");

第一步:拿到canvas对象

 let oCtx = oCanvas.getContext("2d");

第二步:通过canvas对象里面的方法拿到绘图工具.

let gridSize = 50;

第三步:设置个变量代表单元格的宽高.

 let canvasWidth = oCtx.canvas.width;
    let canvasHeight = oCtx.canvas.height;

第四步:通过绘图工具获取canvas对象里面的宽高。

let row = Math.floor(canvasHeight / gridSize);
    let col = Math.floor(canvasWidth / gridSize);

第五步:问题是如果单元格的宽高与canvas的宽高除不尽怎么办?
向下取余.

// 6.绘制垂直方向的横线
    for(let i = 0; i < row; i++){
        oCtx.beginPath();
        oCtx.moveTo(0, i * gridSize - 0.5);
        oCtx.lineTo(canvasWidth, i * gridSize - 0.5);
        oCtx.strokeStyle = "#ccc";
        oCtx.stroke();
    }
    // 7.绘制水平方向的横线
    for(let i = 0; i < col; i++){
        oCtx.beginPath();
        oCtx.moveTo(i * gridSize - 0.5, 0);
        oCtx.lineTo(i * gridSize - 0.5, canvasHeight);
        oCtx.strokeStyle = "#ccc";
        oCtx.stroke();
    }

第六步:用for循环绘制表格。
问题?怎么绘制?
第一步:每一次的循环都开启一个新的路径。根据xy坐标绘制就行了.(默认canvas左上角开始).
为什么-0.5,因为默认情况下线条的中心点和像素的底部对齐所以会2显示,所以显示非纯黑色问题。所以-0.5,代表0.52=1

3.源代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		*{padding: 0px;margin: 0px;}
		canvas{
			display: block;
			margin: 0px auto;
			background-color: red;
		}
	</style>
</head>
<body>
	<canvas width="500" height="500">
		
	</canvas>
	<script>
		let q=document.querySelector("canvas");
		let w=q.getContext("2d");
		let gridSize=50;
		//console.log(q.offsetWidth);
		//console.log(q.offsetHeight);
		//console.log(w.canvas.width);
		//console.log(w.canvas.height);
		let canvasWidth=w.canvas.width;
		let canvasHeight=w.canvas.height;
		let row=Math.floor(canvasWidth/gridSize);
		let col = Math.floor(canvasWidth / gridSize);
		for(let i=0;i<row;i++)
		{
			w.beginPath();
			w.moveTo(0,i*gridSize-0.5);
			w.lineTo(canvasWidth,i*gridSize-0.5);
			w.strokeStyle="#ccc";
			w.stroke();
		}
		for(let i=0;i<col;i++)
		{
			w.beginPath();
			w.moveTo(i*gridSize-0.5,0);
			w.lineTo(i*gridSize-0.5,canvasHeight);
			w.strokeStyle="#ccc";
			w.stroke();
		}
	</script>
</body>
</html>

在这里插入图片描述

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的uniapp canvas表格的示例代码: ```html <template> <view class="canvas-container"> <canvas canvas-id="myCanvas" class="canvas"></canvas> </view> </template> <script> export default { onReady() { const context = uni.createCanvasContext('myCanvas', this); const width = uni.upx2px(300); // 表格宽度 const height = uni.upx2px(200); // 表格高度 const padding = uni.upx2px(10); // 表格与画布边缘的距离 const rowHeight = uni.upx2px(30); // 行高 const colWidth = uni.upx2px(80); // 列宽 const rowCount = 4; // 行数 const colCount = 3; // 列数 // 绘制表格边框 context.strokeRect(padding, padding, width, height); // 绘制表格行 for(let i = 1; i < rowCount; i++) { context.beginPath(); context.moveTo(padding, padding + rowHeight * i); context.lineTo(padding + width, padding + rowHeight * i); context.stroke(); } // 绘制表格列 for(let i = 1; i < colCount; i++) { context.beginPath(); context.moveTo(padding + colWidth * i, padding); context.lineTo(padding + colWidth * i, padding + height); context.stroke(); } // 填充表格内容 const data = [ ['姓名', '年龄', '性别'], ['张三', '18', '男'], ['李四', '20', '女'], ['王五', '22', '男'] ]; context.font = '14px sans-serif'; context.textAlign = 'center'; context.textBaseline = 'middle'; for(let i = 0; i < rowCount; i++) { for(let j = 0; j < colCount; j++) { context.fillText(data[i][j], padding + colWidth * (j + 0.5), padding + rowHeight * (i + 0.5)); } } context.draw(); } } </script> <style> .canvas-container { width: 300upx; height: 200upx; } .canvas { width: 100%; height: 100%; } </style> ``` 这段代码绘制了一个4行3列的表格表格宽度为300upx,高度为200upx。你可以根据实际需要进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贵哥的编程之路(热爱分享)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值