天天看點

原生JS-canvas實作畫闆/簽字闆功能前言最簡代碼樣例結果展示代碼講解封裝子產品

目錄:

文章目錄

  • 前言
  • 最簡代碼樣例
  • 結果展示
  • 代碼講解
    • 思路
    • 代碼講解
  • 封裝子產品

前言

常見的電子教室裡的電子黑闆。

本文特點:

  • 原生

    JS

  • 封裝好的子產品

最簡代碼樣例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        let c = document.getElementById('canvas');
        c.width = window.innerWidth;
        c.height = window.innerHeight;
        let ctx = c.getContext('2d');

        // draw one black board
        ctx.fillStyle = "black";
        ctx.fillRect(0,0,600,300);

        // 按下标記
        let onoff = false,
            oldx = -10,
            oldy = -10;

        // 設定顔色
        let linecolor = "white";

        // 設定線寬
        let linw = 4;

        // 添加滑鼠事件
        // 按下
        c.addEventListener('mousedown', event => {
            onoff = true;
            // 位置 - 10是為了矯正位置,把繪圖放在滑鼠指針的頂端
            oldx = event.pageX - 10;
            oldy = event.pageY - 10;
        },false);
        // 移動
        c.addEventListener('mousemove', event => {
            if(onoff == true){
                let newx = event.pageX - 10,
                    newy = event.pageY - 10;

                // 繪圖
                ctx.beginPath();
                ctx.moveTo(oldx,oldy);
                ctx.lineTo(newx,newy);
                ctx.strokeStyle = linecolor;
                ctx.lineWidth = linw;
                ctx.lineCap = "round";
                ctx.stroke();
                // 每次移動都要更新坐标位置
                oldx = newx,
                oldy = newy;
            }
        }, true);
        // 彈起
        c.addEventListener('mouseup', ()=> {
            onoff = false;
        },false);
    </script>
</body>
</html>
           

結果展示

原生JS-canvas實作畫闆/簽字闆功能前言最簡代碼樣例結果展示代碼講解封裝子產品

代碼講解

思路

  1. 滑鼠按下,開始描畫。滑鼠按下事件。
  2. 滑鼠彈起,結束描畫。滑鼠彈起事件。
  3. 滑鼠按下移動,路徑畫線。滑鼠移動事件。

代碼講解

整體思路:按下滑鼠,觸發移動的開關,移動後開始記錄線條(用移動後的坐标-移動前的坐标,然後繪線),每次移動都會更新舊坐标。松開滑鼠後,釋放移動開關。

  1. 隻有在滑鼠按下,才會觸發移動繪圖的效果,是以需要增加一個狀态判斷。
  2. 因為滑鼠指針和實際位置有一個偏移量,是以在坐标定位的時候,需要增加

    pagex-10

    進而使坐标位于指針的尖端處。
  3. 每次移動都要更新坐标位置,用小段的線段來模拟不規則的線。

封裝子產品

<canvas id="canvas"></canvas>
<script>
    class Board{
        constructor(canvasName = 'canvas', data = new Map([
            ["onoff", false],
            ["oldx", -10],
            ["oldy", -10],
            ["fillStyle", "black"],
            ["lineColor", "white"],
            ["lineWidth", 4],
            ["lineCap", "round"],
            ["canvasWidth", window.innerWidth],
            ["canvasHeight", window.innerHeight]
        ])){
            // this.data = data;
            this.c = document.getElementById(canvasName);
            this.ctx = this.c.getContext('2d');
            this.onoff = data.get("onoff");
            this.oldx = data.get("oldx");
            this.oldy = data.get("oldy");
            this.lineColor = data.get("lineColor");
            this.lineWidth = data.get("lineWidth");
            this.lineCap = data.get("lineCap");

            this.c.width = data.get("canvasWidth");
            this.c.height = data.get("canvasHeight");

            this.ctx.fillStyle = data.get("fillStyle");
            this.ctx.fillRect(0,0,600,300);
        }

        eventOperation(){
            // 添加滑鼠事件
            // 按下
            this.c.addEventListener('mousedown', event => {
                this.onoff = true;
                // 位置 - 10是為了矯正位置,把繪圖放在滑鼠指針的頂端
                this.oldx = event.pageX - 10;
                this.oldy = event.pageY - 10;
            },false);
            // 移動
            this.c.addEventListener('mousemove', event => {
                if(this.onoff == true){
                    let newx = event.pageX - 10,
                        newy = event.pageY - 10;

                    // 繪圖
                    this.ctx.beginPath();
                    this.ctx.moveTo(this.oldx,this.oldy);
                    this.ctx.lineTo(newx,newy);

                    this.ctx.strokeStyle = this.lineColor;
                    this.ctx.lineWidth = this.lineWidth;
                    this.ctx.lineCap = this.lineCap;
                    
                    this.ctx.stroke();
                    // 每次移動都要更新坐标位置
                    this.oldx = newx,
                    this.oldy = newy;
                }
            }, true);
            // 彈起
            this.c.addEventListener('mouseup', ()=> {
                this.onoff = false;
            },false);
        }

    }

    let board = new Board();
    board.eventOperation();
</script>
           

繼續閱讀