<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>签名案例</title>
<style>
canvas {
border: 1px solid red;
}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
<script>
window.onload = function() {
let canvas = document.querySelector('#canvas');
if (!canvas.getContext) return;
let ctx = canvas.getContext('2d');
canvas.onmousedown = function(e) {
e = e || window.event;
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
if (canvas.setCapture) { // 捕获鼠标
canvas.setCapture();
}
document.onmousemove = function(evt) {
evt = evt || window.event;
ctx.lineJoin = "round";
ctx.save();
ctx.lineTo(evt.clientX - canvas.offsetLeft, evt.clientY - canvas.offsetTop);
ctx.lineWidth = 10;
ctx.stroke();
ctx.restore();
}
document.onmouseup = function() {
document.onmousemove = document.onmouseup = null;
if (document.releaseCapture) {
document.releaseCapture();
}
return false;
}
}
}
</script>
</body>
</html>
记录canvas学习--签名
最新推荐文章于 2024-07-23 17:10:08 发布
1320

被折叠的 条评论
为什么被折叠?



