<!DOCTYPE html>
<html lang=
"en"
>
<head>
<meta charset=
"UTF-8"
>
<title>网格</title>
<style>
#c1{
border:1px solid black;
}
</style>
<script>
window.onload =
function
(){
var
oCanvas = document.getElementById(
'c1'
)
var
gd = oCanvas.getContext(
'2d'
)
var
space = 20
var
cWidth = gd.canvas.width;
var
cHeight = gd.canvas.height;
var
lines = Math.floor(cHeight/space)
var
cols = Math.floor(cWidth/space)
for
(let i = 0;i<lines;i++){
gd.beginPath()
gd.moveTo(0,space*i-0.5)
gd.lineTo(cWidth,space*i-0.5)
gd.strokeStyle=
'#aaa'
gd.stroke();
}
for
(let j = 0; j<cols;j++){
gd.beginPath();
gd.moveTo(space*j-0.5,0)
gd.lineTo(space*j-0.5,cHeight)
gd.strokeStyle=
"#aaa"
gd.stroke()
}
var
everPadding = 40
var
x0 = everPadding;
var
yo = cHeight -everPadding
var
x1 = cWidth-everPadding;
gd.beginPath();
gd.moveTo(x0,yo)
gd.lineTo(x1,yo)
gd.lineTo(x1-space,yo-space)
gd.lineTo(x1-space,yo+space)
gd.lineTo(x1,yo)
gd.strokeStyle=
"red"
gd.fillStyle=
"red"
gd.stroke()
gd.fill()
gd.beginPath()
gd.moveTo(x0,yo)
gd.lineTo(x0,everPadding)
gd.lineTo(x0-space,everPadding+space)
gd.lineTo(x0+space,everPadding+space)
gd.lineTo(x0,everPadding)
gd.strokeStyle=
"red"
gd.fillStyle=
"red"
gd.stroke()
gd.fill()
}
</script>
</head>
<body>
<canvas id=
"c1"
width=
"500"
height=
"500"
></canvas>
</body>
</html>