Home > Web Front-end > HTML Tutorial > How to draw a grid using HTML5 and canvas or SVG?

How to draw a grid using HTML5 and canvas or SVG?

PHPz
Release: 2023-09-01 16:49:06
forward
1144 people have browsed it

How to draw a grid using HTML5 and canvas or SVG?

In the example given below, we first define the width and height of the grid. Then we define the size of the canvas and draw the grid onto the canvas.

//we are setting the grid width and height
var grid_w = 200;
var grid_h = 200;

//we are setting padding around grid
var gridp = 15;

//we are defining size of canvas by defining its width and height
var canvas_w = grid_w + (gridp*2) + 1;
var canvas_h = grid_h + (gridp*2) + 1;
var canvas = $(&#39;<canvas/>&#39;).attr({width: canvas_w, height: canvas_h}).appendTo(&#39;body&#39;);
var ctx = canvas.get(0).getContext("2d");
Copy after login

This is our approach −

function drawBoard(){
   for (var a = 0; a <= grid_w; a += 50) {
      ctx.moveTo(0.5 + a + gridp, gridp);
      ctx.lineTo(0.5 + a+ gridp, grid_h + gridp);
   }
Copy after login

The above is the detailed content of How to draw a grid using HTML5 and canvas or SVG?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template