x
 
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>绘制形状</h2>
<canvas id="myCanvas" width="200" height="150" style="border:1px solid grey;"></canvas>
<script>
// 创建画布:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
// 定义新的路径:
ctx.beginPath();
// 定义起点:
ctx.moveTo(20,20);
// 定义点:
ctx.lineTo(100,20);
ctx.lineTo(175,100);
ctx.lineTo(20,100);
ctx.lineTo(20,20);
// 绘制:
ctx.stroke();
</script>
</body>
</html>