<canvas id="canvas_name" height="200" width="200" style="border:2px solid #000000;"></canvas>
</canvas> tag or you will be unhappy.
let c1 = document.getElementById("canvas_name")
let width1 = c1.width;
let height1 = c1.height;
console.log("This canvas is ", width1 , " x ", height1);
let ctx1 = c1.getContext("2d");
|
let c2 = document.getElementById("grid");
let w2 = c2.width;
let h2 = c2.height;
let ctx2 = c2.getContext("2d");
let skip = 10;
let color="green";
let width = 2;
function DrawGrid(ctx, w, h, skip, color, width){
let i;
// clear the box
ctx.clearRect(0,0,w,h);
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.beginPath();
for(i =0; i <= w; i+= skip){
ctx.moveTo(i,0);
ctx.lineTo(i,h);
}
for(i =0; i <= h; i+= skip) {
ctx.moveTo(0,i);
ctx.lineTo(w,i);
}
ctx.stroke();
}
DrawGrid(ctx2, w2, h2, skip, color, width);
stroke() or fill to draw the figure.
| canvas |
|
<table border>
<tr>
<td> canvas</td>
<td>
<table border>
<tr> <td> button 1</td></tr>
<tr> <td> button 2</td></tr>
<tr> <td> button 3</td></tr>
<tr> <td> button 4</td></tr>
</table>
</td>
</tr>
</table>
Grid Size <input id="gridSize", type="text" size=4></input>
// set
let skip = 20;
let gridBox = document.getElementById("gridSize");
gridBox.value = skip;
// get
skip = parseInt(gridBox.value);
Grid Color <select id="gridColor"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> <option value="black">Black</option> </select>
// set
let color = "green";
let colorSelect = document.getElementById("gridColor");
colorSelect.value = color;
...
// get
color = colorSelect.value;
Width <input type="range", min='1' max='5' value ='1' id="widthSlider">
// set
let widthSelect = document.getElementById("widthSlider");
widthSelect.value = width;
// get
width = parseInt(widthSelect.value);