使用canvas制作画板

//获取canvas标签
var canvas = document.getElementById("canvas");
//获得绘图的对象
var ctx = canvas.getContext("2d");
canvas.onmousedown = function(ev) {
    var ev = ev || window.event;
    ctx.moveTo(ev.clientX - canvas.offsetLeft, ev.clientY - canvas.offsetTop);
    document.onmousemove = function(ev) {
        var ev = ev || window.event;
        ctx.lineTo(ev.clientX - canvas.offsetLeft, ev.clientY - canvas.offsetTop);
        ctx.stroke();
    }
    document.onmouseup = function() {
        document.onmousemove = null;
        document.onmouseup = null;
    }
}

你可能感兴趣的:(html5)