<!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();