1、画矩形:
<style>
canvas{
width: 500px;
height: 300px;
border: 1px solid;
}
</style>
</head>
<body>
<canvas id="can" width="500px" height="300px"></canvas><!-- 只能写行间样式:画布的大小 -->
<script>
var canvas = document.getElementById('can');
var ctx = canvas.getContext("2d");
//画矩形:(线画法)
ctx.lineWidth = 10;
ctx.moveTo(100,100);
ctx.lineTo(300,100);
ctx.lineTo(300,200);
ctx.lineTo(100,200);
ctx.closePath();
ctx.stroke();
//第二种:
ctx.rect(100,100,200,100);//前俩起始位置,第三个长,第四个宽
ctx.stroke();
//第三种:
ctx.strokeRect(100,100,200,100);
//第四种:(直接填充)
ctx.fillRect(100,100,200,100);
</script>
</body>