在 Canvas HTML5 上绘制网格/表格 [英] Draw Grid / Table on Canvas HTML5

查看:26
本文介绍了在 Canvas HTML5 上绘制网格/表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在到处搜索,但找不到如何在 HTML5 画布上绘制网格/表格.我是 HTML5 和画布的新手.

I've been searching everywhere and couldn't find how to draw a Grid / Table on a HTML5 Canvas. I'm new to HTML5 and canvas.

我知道如何绘制形状,但这个绘图网格需要很长时间才能理解.

I know how to draw shapes but this drawing grid is taking forever to understand.

有人可以帮我解决这个问题吗?非常感谢您的时间.

Can someone help me on this? Your time is greatly appreciated.

推荐答案

答案取自这里使用 <canvas> 绘制的网格看起来被拉伸的元素

稍微修改了一下,希望对你有帮助

Just edited it a little, hope it helps

<html>
<head>
<script type="text/javascript" language="javascript">
// Box width
var bw = 400;
// Box height
var bh = 400;
// Padding
var p = 10;

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function drawBoard(){
    for (var x = 0; x <= bw; x += 40) {
        context.moveTo(0.5 + x + p, p);
        context.lineTo(0.5 + x + p, bh + p);
    }

    for (var x = 0; x <= bh; x += 40) {
        context.moveTo(p, 0.5 + x + p);
        context.lineTo(bw + p, 0.5 + x + p);
    }
    context.strokeStyle = "black";
    context.stroke();
}

drawBoard();
</script>
</head>
<body style=" background: lightblue;">
    <canvas id="canvas" width="420px" height="420px" style="background: #fff; margin:20px"></canvas>
</body>
</html>

这篇关于在 Canvas HTML5 上绘制网格/表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆