<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
window.onload=function () {
var canvas=document.getElementById("text");
if (canvas.getContext){
var ctx=canvas.getContext("2d");
}
canvas.onmousedown=function (ev) {
//判断兼容性
ev=ev||event;
if(canvas.setCapture){
canvas.setCapture();
}
ctx.beginPath();
ctx.moveTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
document.onmousemove=function (ev) {
ev=ev||event;
ctx.save();
ctx.strokeStyle="pink";
ctx.lineTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
ctx.stroke();
ctx.restore();
};
document.onmouseup=function () {
document.onmousemove=document.onmouseup=null;
if (document.releaseCapture) {
document.releaseCapture();
}
};
return false;
};
}
</script>
<style>
#text{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background: gray ;
}
</style>
</head>
<body>
<canvas id="text" height="500" width="500"></canvas>
</body>
</html>