1.画正方形
代码如下
<style>
canvas {
margin: 0 auto;
border: 2px solid #aaa;
display: block; /*画布居中*/
}
</style>
<body>
<canvas id="cont" width="500px" height="500px">Hello Canvas</canvas>
<script>
//获取画布
var canvas = document.querySelector("#cont");
//获取画布上下文
var ctx = canvas.getContext('2d');
/*
//开启一条路径
ctx.beginPath();
//确定起始点
ctx.moveTo(100, 100);
//确定结束点
ctx.lineTo(400, 100);
//着色之前设置颜色和线宽
ctx.strokeStyle = 'green';
ctx.lineWidth = 5;
//着色
ctx.stroke();
//结束路径
ctx.closePath();
*/
function drawLine(x1, y1, x2, y2, color, width) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.lineWidth