用canvas寫一個簡易畫圖工具

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <canvas id="myCanvas" width="800" height="500"
    style="border:1px solid #000000;">您的瀏覽器不支持canvas
</canvas>
<script type="text/javascript">
    var c =document.querySelector('#myCanvas');
    // 標識畫布並指明上下文
    var ctx = c.getContext('2d');

    ctx.strokeStyle = '#000';
    ctx.lineWidth = 3;
    c.onmousedown = function (ev){
        ctx.beginPath();
        ctx.moveTo(ev.clientX,ev.clientY)
        c.onmousemove = function (ev) {
            ctx.lineTo(ev.clientX,ev.clientY);
            ctx.stroke()
        }
        c.onmouseup = function () {
            c.onmousemove = null;
        }
    }
</script>

</body>
</html>

 

相關文章
相關標籤/搜索