• canvas画布绘图


    请添加图片描述

    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>Documenttitle>
        <style>
          .active {
            color: red;
          }
        style>
      head>
    
      <body>
        <canvas id="c1" width="800" height="400">canvas>
        <hr />
        <button id="boldBtn">粗线条button>
        <button id="thinBtn">细线条button>
        <button id="saveBtn">保存签名button>
        <button id="downloadBtn">下载签名button>
        <input type="color" id="color" value="" />
        <button id="clearBtn">橡皮擦button>
        <button id="nullBtn">清空画布button>
    
        <script>
          // 1. 获取canvas画布和绘制的上下文对象
          /** @type {HTMLCanvasElement} */
          var canvas = document.querySelector("#c1");
          var ctx = canvas.getContext("2d");
          // 连接处圆润
          ctx.lineJoin = "round";
          // 开头和结束端圆润
          ctx.lineCap = "round";
          // 2. 获取输入框和按钮
          // 设置画笔的粗细
          var boldBtn = document.querySelector("#boldBtn");
          var thinBtn = document.querySelector("#thinBtn");
          var saveBtn = document.querySelector("#saveBtn");
          var downloadBtn = document.querySelector("#downloadBtn");
          var clearBtn = document.querySelector("#clearBtn");
          var nullBtn = document.querySelector("#nullBtn");
          var inputColor = document.querySelector("#color");
          // 设置允许绘制的变量
          var isDraw = false;
    
          // 鼠标按下
          canvas.onmousedown = function () {
            isDraw = true;
            ctx.beginPath();
            let x = event.pageX - canvas.offsetLeft;
            let y = event.pageY - canvas.offsetTop;
            ctx.moveTo(x, y);
          };
          // 鼠标离开
          canvas.onmouseleave = function () {
            isDraw = false;
            ctx.closePath();
          };
          // 鼠标抬起
          canvas.onmouseup = function () {
            isDraw = false;
            ctx.closePath();
          };
          // 鼠标移动
          canvas.onmousemove = function () {
            if (isDraw) {
              let x = event.pageX - canvas.offsetLeft;
              let y = event.pageY - canvas.offsetTop;
              ctx.lineTo(x, y);
              ctx.stroke();
            }
          };
          boldBtn.onclick = function () {
            ctx.globalCompositeOperation = "source-over";
            ctx.lineWidth = 20;
            boldBtn.classList.add("active");
            thinBtn.classList.remove("active");
            clearBtn.classList.remove("active");
          };
          thinBtn.onclick = function () {
            ctx.globalCompositeOperation = "source-over";
            ctx.lineWidth = 2;
            thinBtn.classList.add("active");
            boldBtn.classList.remove("active");
            clearBtn.classList.remove("active");
          };
          clearBtn.onclick = function () {
            ctx.globalCompositeOperation = "destination-out";
            ctx.lineWidth = 30;
            clearBtn.classList.add("active");
            boldBtn.classList.remove("active");
            thinBtn.classList.remove("active");
          };
          nullBtn.onclick = function () {
            ctx.clearRect(0, 0, 800, 400);
          };
          // 保存生成图片
          saveBtn.onclick = function () {
            let urlData = canvas.toDataURL();
            let img = new Image();
            img.src = urlData;
            document.body.appendChild(img);
          };
          // 下载画布图片
          downloadBtn.onclick = function () {
            let urlData = canvas.toDataURL();
            let downloadA = document.createElement("a");
            downloadA.setAttribute("download", "签名");
            downloadA.href = urlData;
            downloadA.click();
          };
          // 改变颜色
          inputColor.onchange = function () {
            ctx.strokeStyle = inputColor.value;
          };
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
  • 相关阅读:
    LeeCode-使数组中所有元素都等于零(python)
    2023年最全ins商店开通运营攻略
    网络渗透测试:Wireshark抓取qq图片
    听潮汐,筑灯塔,聚千帆:智慧港口全球创新实验室启航时
    一面高频vue面试题
    视频剪辑软件Corel VideoStudio 会声会影2023新功能介绍及安装激活教程
    【校招VIP】前端ES6相关之Symbol
    java计算机毕业设计计算机专业招聘网站源码+mysql数据库+系统+lw文档+部署
    扬帆际海—移动端流量对跨境电商有多重要?
    Spring中shutdown hook作用
  • 原文地址:https://blog.csdn.net/qq_41154522/article/details/127766645