[html] Draw on HTML5 Canvas using a mouse

Here is a working sample.

_x000D_
_x000D_
 <html>_x000D_
    <script type="text/javascript">_x000D_
    var canvas, ctx, flag = false,_x000D_
        prevX = 0,_x000D_
        currX = 0,_x000D_
        prevY = 0,_x000D_
        currY = 0,_x000D_
        dot_flag = false;_x000D_
_x000D_
    var x = "black",_x000D_
        y = 2;_x000D_
    _x000D_
    function init() {_x000D_
        canvas = document.getElementById('can');_x000D_
        ctx = canvas.getContext("2d");_x000D_
        w = canvas.width;_x000D_
        h = canvas.height;_x000D_
    _x000D_
        canvas.addEventListener("mousemove", function (e) {_x000D_
            findxy('move', e)_x000D_
        }, false);_x000D_
        canvas.addEventListener("mousedown", function (e) {_x000D_
            findxy('down', e)_x000D_
        }, false);_x000D_
        canvas.addEventListener("mouseup", function (e) {_x000D_
            findxy('up', e)_x000D_
        }, false);_x000D_
        canvas.addEventListener("mouseout", function (e) {_x000D_
            findxy('out', e)_x000D_
        }, false);_x000D_
    }_x000D_
    _x000D_
    function color(obj) {_x000D_
        switch (obj.id) {_x000D_
            case "green":_x000D_
                x = "green";_x000D_
                break;_x000D_
            case "blue":_x000D_
                x = "blue";_x000D_
                break;_x000D_
            case "red":_x000D_
                x = "red";_x000D_
                break;_x000D_
            case "yellow":_x000D_
                x = "yellow";_x000D_
                break;_x000D_
            case "orange":_x000D_
                x = "orange";_x000D_
                break;_x000D_
            case "black":_x000D_
                x = "black";_x000D_
                break;_x000D_
            case "white":_x000D_
                x = "white";_x000D_
                break;_x000D_
        }_x000D_
        if (x == "white") y = 14;_x000D_
        else y = 2;_x000D_
    _x000D_
    }_x000D_
    _x000D_
    function draw() {_x000D_
        ctx.beginPath();_x000D_
        ctx.moveTo(prevX, prevY);_x000D_
        ctx.lineTo(currX, currY);_x000D_
        ctx.strokeStyle = x;_x000D_
        ctx.lineWidth = y;_x000D_
        ctx.stroke();_x000D_
        ctx.closePath();_x000D_
    }_x000D_
    _x000D_
    function erase() {_x000D_
        var m = confirm("Want to clear");_x000D_
        if (m) {_x000D_
            ctx.clearRect(0, 0, w, h);_x000D_
            document.getElementById("canvasimg").style.display = "none";_x000D_
        }_x000D_
    }_x000D_
    _x000D_
    function save() {_x000D_
        document.getElementById("canvasimg").style.border = "2px solid";_x000D_
        var dataURL = canvas.toDataURL();_x000D_
        document.getElementById("canvasimg").src = dataURL;_x000D_
        document.getElementById("canvasimg").style.display = "inline";_x000D_
    }_x000D_
    _x000D_
    function findxy(res, e) {_x000D_
        if (res == 'down') {_x000D_
            prevX = currX;_x000D_
            prevY = currY;_x000D_
            currX = e.clientX - canvas.offsetLeft;_x000D_
            currY = e.clientY - canvas.offsetTop;_x000D_
    _x000D_
            flag = true;_x000D_
            dot_flag = true;_x000D_
            if (dot_flag) {_x000D_
                ctx.beginPath();_x000D_
                ctx.fillStyle = x;_x000D_
                ctx.fillRect(currX, currY, 2, 2);_x000D_
                ctx.closePath();_x000D_
                dot_flag = false;_x000D_
            }_x000D_
        }_x000D_
        if (res == 'up' || res == "out") {_x000D_
            flag = false;_x000D_
        }_x000D_
        if (res == 'move') {_x000D_
            if (flag) {_x000D_
                prevX = currX;_x000D_
                prevY = currY;_x000D_
                currX = e.clientX - canvas.offsetLeft;_x000D_
                currY = e.clientY - canvas.offsetTop;_x000D_
                draw();_x000D_
            }_x000D_
        }_x000D_
    }_x000D_
    </script>_x000D_
    <body onload="init()">_x000D_
        <canvas id="can" width="400" height="400" style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>_x000D_
        <div style="position:absolute;top:12%;left:43%;">Choose Color</div>_x000D_
        <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" id="green" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" id="blue" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" id="red" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" id="yellow" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" id="orange" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" id="black" onclick="color(this)"></div>_x000D_
        <div style="position:absolute;top:20%;left:43%;">Eraser</div>_x000D_
        <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div>_x000D_
        <img id="canvasimg" style="position:absolute;top:10%;left:52%;" style="display:none;">_x000D_
        <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">_x000D_
        <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">_x000D_
    </body>_x000D_
    </html>
_x000D_
_x000D_
_x000D_