<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>面试canvas</title>
</head>
<body>
<canvas id='canvas' width="500" height="500" style="background-color: black;"></canvas>
</body>
<script>
function draw(){
var canvas=document.getElementById('canvas')
if(!canvas.getContext) return
//1.getContext返回一个在画布上绘图的环境,返回绘制时的方法和属性
var ctx=canvas.getContext('2d')
// 2.开始一条路径
ctx.beginPath()
// 3.移动到位置(0,0)
ctx.moveTo(0,0)
// 4.创建到达位置的线
ctx.lineTo(250,250)
ctx.lineTo(500,0)
// ctx.closePath() //和stroke空心的一起使用
ctx.fillStyle='#ffffff'
// ctx.strokeStyle="white";
// ctx.stroke()
ctx.fill()
}
draw()
</script>
</html>
显示出来的图形如下: