<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas 特效</title>
</head>
<body>
<canvas id="canvas" style="border:1px solid #aaa; display:block; margin:50px auto;" >
当前浏览器不支持Canvas,请更换浏览器后再试
</canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.moveTo(100,100);
context.lineTo(700,700);
context.lineTo(100,700);
context.lineTo(100,100);
context.lineWidth = 10;
context.strokeStyle = "red";
context.stroke();
}
</script>
</body>
</html>
接着往后看,要使用closePath
最后一个context.lineTo(100,100); 可以改为 context.closePath();也可以在其后面加上 context.closePath();