首頁 > web前端 > js教程 > 如何使用canvas來製作好用的塗鴉畫板

如何使用canvas來製作好用的塗鴉畫板

php中世界最好的语言
發布: 2018-03-12 14:50:17
原創
2592 人瀏覽過

這次帶給大家如何使用canvas來製作好用的塗鴉畫板,使用canvas來製作好用的塗鴉畫板的注意事項有哪些,下面就是實戰案例,一起來看一下。在canvas中取得遊標座標

取得座標的程式碼很簡單:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <style>
        *{margin: 0;padding: 0}    </style></head><body>
    <canvas id="board" style="border: 1px #ccc solid;"></canvas>
    <span id="point"></span>
    <script>
        var canvas = document.getElementById(&#39;board&#39;);        var context = canvas.getContext(&#39;2d&#39;);        var current = {            color: &#39;black&#39;,//<===画笔颜色配置
            width: 1//线条宽度 
        };        //获取点坐标
        function getPoint(e) {            if (e.touches && e.touches.length > 0) {                var touch = e.touches[0];                return { x: touch.pageX, y: touch.pageY };
            }            return { x: e.clientX, y: e.clientY };
        }        //鼠标移动
        function onMouseMove(e) {            var p = getPoint(e);            document.getElementById("point").innerHTML=p.x+"-"+p.y;
        }
        canvas.width = 600;
        canvas.height = 300; 
        canvas.addEventListener(&#39;mousemove&#39;, onMouseMove, false); //<==兼容PC
        canvas.addEventListener(&#39;touchmove&#39;, onMouseMove, false);//<===兼容安卓或其他系统
    </script></body></html>
登入後複製

注意:因為滑鼠與觸控螢幕的事件是不一樣的,滑鼠只要懸浮與canvas上就可以取得到了,而觸控螢幕是需要按下的,並且所回傳的Event 物件也是不一樣的。

2.控制是否繪製

控制是否繪製其實很簡單,就是在不同事件時判斷自定義變數drawing的值來控制

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        *{margin: 0;padding: 0}    </style></head><body>
    <canvas id="board" style="border: 1px #ccc solid;"></canvas>
    <span id="point"></span>
    <script>
        var canvas = document.getElementById(&#39;board&#39;);        var context = canvas.getContext(&#39;2d&#39;);        var current = {            color: &#39;black&#39;,//<===画笔颜色配置
            width: 1//线条宽度 
        };        var drawing = false;//<===是否绘制
        //获取点坐标
        function getPoint(e) {            if (e.touches && e.touches.length > 0) {                var touch = e.touches[0];                return { x: touch.pageX, y: touch.pageY };
            }            return { x: e.clientX, y: e.clientY };
        }         //鼠标按下
         function onMouseDown(e) {
                drawing = true; 
            }            //鼠标弹起
            function onMouseUp(e) {                if (!drawing) { return; }
                drawing = false; 
            }        //鼠标移动
        function onMouseMove(e) {            if (!drawing) { return; }            var p = getPoint(e);            document.getElementById("point").innerHTML=p.x+"-"+p.y;
        }
        canvas.width = 600;
        canvas.height = 300; 
        canvas.addEventListener(&#39;mousedown&#39;, onMouseDown, false);
        canvas.addEventListener(&#39;mouseup&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;mouseout&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;mousemove&#39;, onMouseMove, false);
        canvas.addEventListener(&#39;touchstart&#39;, onMouseDown, false);
        canvas.addEventListener(&#39;touchend&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;touchmove&#39;, onMouseMove, false);    </script></body></html>
登入後複製

3.線條繪製

線條繪製的程式碼也是很簡單的

....//线条绘制function drawLine(x0, y0, x1, y1, color, width) {
    context.beginPath();
    context.moveTo(x0, y0);
    context.lineTo(x1, y1);
    context.strokeStyle = color;
    context.lineWidth = width; 
    context.stroke();
    context.closePath();
}
....
登入後複製

將繪製線條程式碼整合到事件中:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title></head><body>
    <canvas id="board" style="border: 1px #ccc solid;"></canvas>
    <span id="point"></span>
    <script>
        var canvas = document.getElementById(&#39;board&#39;);        var context = canvas.getContext(&#39;2d&#39;);        var current = {            color: &#39;black&#39;,//<===画笔颜色配置
            width: 1//线条宽度 
        };        var drawing = false;//<===是否绘制
        //获取点坐标
        function getPoint(e) {            if (e.touches && e.touches.length > 0) {                var touch = e.touches[0];                return { x: touch.pageX, y: touch.pageY };
            }            return { x: e.clientX, y: e.clientY };
        }        //线条绘制
        function drawLine(x0, y0, x1, y1, color, width) {
            context.beginPath();
            context.moveTo(x0, y0);
            context.lineTo(x1, y1);
            context.strokeStyle = color;
            context.lineWidth = width; 
            context.stroke();
            context.closePath();
        }        //鼠标按下
        function onMouseDown(e) {
            drawing = true;            //记录按下点
            var p = getPoint(e);
            current.x = p.x;
            current.y = p.y;
        }        //鼠标弹起
        function onMouseUp(e) {            if (!drawing) { return; }
            drawing = false;            //绘制结束点
            var p = getPoint(e);
            drawLine(current.x, current.y, p.x, p.y, current.color, current.width);
        }        //鼠标移动
        function onMouseMove(e) {            if (!drawing) { return; }            var p = getPoint(e);            document.getElementById("point").innerHTML = p.x + "-" + p.y;            //移动绘制
            drawLine(current.x, current.y, p.x, p.y, current.color, current.width);
            current.x = p.x;
            current.y = p.y;
        }
        canvas.width = 600;
        canvas.height = 300;
        canvas.addEventListener(&#39;mousedown&#39;, onMouseDown, false);
        canvas.addEventListener(&#39;mouseup&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;mouseout&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;mousemove&#39;, onMouseMove, false);
        canvas.addEventListener(&#39;touchstart&#39;, onMouseDown, false);
        canvas.addEventListener(&#39;touchend&#39;, onMouseUp, false);
        canvas.addEventListener(&#39;touchmove&#39;, onMouseMove, false);    </script></body></html>
登入後複製

4.繪製線條最佳化

當繪製線條寬度比較小的時候還好,一旦比較粗就會有寫問題:

這時只要稍微改一下繪製的程式碼就行了

....//线条绘制function drawLine(x0, y0, x1, y1, color, width) {
    context.beginPath();
    context.moveTo(x0, y0);
    context.lineTo(x1, y1);
    context.strokeStyle = color;
    context.lineWidth = width; 
    //-----加入-----
    context.lineCap = "round";
    context.lineJoin = "round";    //-----加入-----
    context.stroke();
    context.closePath();
}
....
登入後複製

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

相關閱讀:

如何使用s-xlsx之合併單元格

js-xlsx讀取xlsx檔的非同步詳解

如何使用s-xlsx實作Excel 檔案匯入與匯出(下)

以上是如何使用canvas來製作好用的塗鴉畫板的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板