Native JS-canvas realizes drawing board/signature board function

table of Contents:


Preface

An electronic blackboard in a common electronic classroom.

Features of this article:

  • PrimitiveJS
  • Packaged module

Simplest code example

<!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>

Result display

Insert picture description here

Code explanation

Ideas

  1. Press the mouse to start drawing. Mouse down event.
  2. The mouse bounces up to end the drawing. The mouse up event.
  3. The mouse is pressed and moved, and the path is drawn. Mouse movement event.

Code explanation

The overall idea: press the mouse to trigger the switch to move, and start recording the line after moving (using the coordinate after the movement-the coordinate before the movement, and then drawing the line), and the old coordinates will be updated every time you move. After releasing the mouse, release the move switch.

  1. Only when the mouse is pressed, will the effect of moving the drawing be triggered, so a state judgment needs to be added.
  2. Because there is an offset between the mouse pointer and the actual position, when positioning the coordinate, it needs to be increased pagex-10so that the coordinate is at the tip of the pointer.
  3. The coordinate position must be updated every time you move, and a small line segment is used to simulate an irregular line.

Package module

<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>

Guess you like

Origin blog.csdn.net/u013362192/article/details/113934773