<!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>
<style>
#huaban{
border: 1px solid pink;
}
</style>
</head>
<body>
<canvas id="huaban" width="500" height="500"></canvas>
</body>
<script>
let huaban = document.getElementById('huaban')
let ctx = huaban.getContext('2d')
huaban.onmousedown = function(e){
// console.log(e.offsetX,e.offsetY)
ctx.moveTo(e.offsetX,e.offsetY)
huaban.οnmοusemοve=function(e){
ctx.lineTo(e.offsetX,e.offsetY)
ctx.stroke()
}
}
document.onmouseup = function(){
// 鼠标松开 解绑事件
huaban.οnmοusemοve=null
}
</script>
</html>