Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw Grid / Table on Canvas 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.

like image 685
Jay Mayu Avatar asked Jul 31 '12 08:07

Jay Mayu


People also ask

How do you draw a grid on canvas?

In a nutshell, the grid method involves drawing a grid over your reference photo, and then drawing a grid of equal ratio on your work surface (paper, canvas, wood panel, etc). Then you draw the image on your canvas, focusing on one square at a time, until the entire image has been transferred.

How do you make a grid canvas in HTML?

The HTML canvas is a two-dimensional grid. In the previous chapter, you saw this method used: fillRect(0,0,150,75). This means: Start at the upper-left corner (0,0) and draw a 150x75 pixels rectangle.

Does HTML5 support canvas?

The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images.


2 Answers

The answer is taken from here Grid drawn using a <canvas> element looking stretched

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>
like image 117
John Mayer Avatar answered Sep 28 '22 04:09

John Mayer


This can be also be written as:

<html>
<head>

</head>
<body style=" background: lightblue;">
    <canvas id="canvas" width="420px" height="420px" style="background: #fff;     magrin:20px;"></canvas>
    <script type="text/javascript" language="javascript">
    var bw = 400;
    var bh = 400;
    var p = 10;
    var cw = bw + (p*2) + 1;
    var ch = bh + (p*2) + 1;

    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>
</body>
</html>
like image 31
fmt.Fprint Avatar answered Sep 28 '22 05:09

fmt.Fprint