展示效果
整个代码
<!doctype html>
<html lang="zh-zn">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
background: #eeeeee;
margin: 0;
padding: 0;
}
#canvas {
background: #ffffff;
border: 1px solid #ccc;
box-sizing: border-box;
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="canvas" width="1900" height="800"
style="border:1px solid #000000;">
</canvas>
<script>
var canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
function drawGrid(color, stepx, stepy) {
context.save()
context.fillStyle = 'white';
console.log(context);
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.lineWidth = 0.5;
context.strokeStyle = color;
for (var i = stepx; i < context.canvas.width; i += stepx) {
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, context.canvas.height);
context.closePath();
context.stroke();
}
for (var j = stepy; j < context.canvas.height; j += stepy) {
context.beginPath();
context.moveTo(0, j);
context.lineTo(context.canvas.width, j);
context.closePath();
context.stroke();
}
context.restore();
}
drawGrid('#ccc', 20, 20);
</script>
</body>
</html>