就像这样,总是最后渲染的颜色不一样
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
#canvas {
border: 1px solid #ccc;
}
</style>
<body>
<!-- canvas不能使用css来设置宽高,绘制的内容会被拉伸 -->
<!-- 但由于默认情况下 canvas 会将线条的中心点和像素的底部对齐,所以会导致显示效果是 2px 和非纯黑色问题。 -->
<canvas width="500" height="301" id="canvas"></canvas>
<script>
const cxt = document.getElementById('canvas').getContext('2d'); //获取canvas的上下文
cxt.moveTo(20, 80)
cxt.lineTo(200, 80)
cxt.stroke()
cxt.moveTo(210, 80)
cxt.lineTo(310, 80)
cxt.stroke()
cxt.moveTo(320, 80)
cxt.lineTo(400, 80)
cxt.stroke()
</script>
</body>
</html>