canvas鼠標點擊划線


  今天學習了canvas,打算寫一個鼠標划線的效果。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        body{
            background-color: black;
        }
        canvas{
            background-color: white;
        }
    </style>
    <script type="text/javascript">
        window.onload= function(){
            var oc= document.getElementById('c1');
            var ogc=oc.getContext('2d');
            oc.onmousedown = function(ev){
                var ev=ev || window.event;
                ogc.moveTo(ev.clientX-oc.offsetLeft,ev.clientY-oc.offsetTop);
                document.onmousemove = function(ev){
                    var ev = ev || window.event;
                    ogc.lineTo(ev.clientX-oc.offsetLeft,ev.clientY-oc.offsetTop);
                    ogc.stroke();
                };
                document.onmouseup = function(){
                    document.onmousemove = null;
                    document.onmouseup = null;
                };

            }
        }
    </script>
</head>
<body>
    <canvas id="c1" width="400" height="400">
        
    </canvas>
    
</body>
</html>

如上通過簡單的moveTo lineTo功能實現效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM