<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="cv" width="500px" height="500px" style="border: 1px solid red;"></canvas>
<script>
let canvas=document.getElementById('cv')
let ctx = canvas.getContext('2d')
let canMove = false
canvas.onmousedown = function(e){
canMove = true
ctx.moveTo(e.pageX - canvas.offsetLeft,e.pageY - canvas.offsetTop)
ctx.beginPath()
}
window.onmouseup = function(){
canMove = false
ctx.closePath()
}
canvas.onmousemove = function(e){
if (canMove) {
ctx.lineTo(e.pageX - canvas.offsetLeft,e.pageY - canvas.offsetTop)
ctx.stroke()
}
}
</script>
</body>
</html>
Canvas简单的写字板
最新推荐文章于 2024-07-26 15:07:33 发布