在下面給出的範例中,我們首先定義了網格的寬度和高度。然後我們定義了畫布的大小,並將網格繪製到畫布上。
//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 = $('<canvas/>').attr({width: canvas_w, height: canvas_h}).appendTo('body'); var ctx = canvas.get(0).getContext("2d");
這是我們的方法 −
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); }
以上是如何使用HTML5和canvas或SVG繪製網格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!