当前位置: 首页 > article >正文

Canvas鼠标画线

鼠标按下开始画线,鼠标移动根据鼠标的轨迹去画,鼠标抬起停止画线
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas Drawing</title>
  <style>
    canvas {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="500" height="500"></canvas>

  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const canvas = document.getElementById('myCanvas')
      const ctx = canvas.getContext('2d')
      let isDrawing = false

      function startDrawing (e) {
        isDrawing = true
        draw(e)
      }

      function stopDrawing () {
        isDrawing = false
        ctx.beginPath() // Reset the path for the next draw
      }

      function draw (e) {
        if (!isDrawing) return

        ctx.lineWidth = 5
        ctx.lineCap = 'round'
        ctx.strokeStyle = '#000'

        ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop)
        ctx.stroke()
        ctx.beginPath()
        ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop)
      }

      canvas.addEventListener('mousedown', startDrawing)
      canvas.addEventListener('mousemove', draw)
      canvas.addEventListener('mouseup', stopDrawing)
      canvas.addEventListener('mouseout', stopDrawing)
    });
  </script>
</body>

</html>

效果图
在这里插入图片描述

和上面效果相同,只是轨迹线换成了直线(鼠标移动始终显示两点直线,只能绘制有一条线)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas Drawing</title>
  <style>
    canvas {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="500" height="500"></canvas>

  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const canvas = document.getElementById('myCanvas')
      const ctx = canvas.getContext('2d')
      let isDrawing = false
      let startX, startY

      function startDrawing (e) {
        isDrawing = true
        startX = e.clientX - canvas.offsetLeft
        startY = e.clientY - canvas.offsetTop
      }

      function drawLine (e) {
        if (!isDrawing) return

        const endX = e.clientX - canvas.offsetLeft
        const endY = e.clientY - canvas.offsetTop

        ctx.clearRect(0, 0, canvas.width, canvas.height) // Clear the canvas
        ctx.lineWidth = 5
        ctx.lineCap = 'round'
        ctx.strokeStyle = '#000'

        ctx.beginPath()
        ctx.moveTo(startX, startY)
        ctx.lineTo(endX, endY)
        ctx.stroke()
      }

      function stopDrawing () {
        isDrawing = false
      }

      canvas.addEventListener('mousedown', startDrawing)
      canvas.addEventListener('mousemove', drawLine)
      canvas.addEventListener('mouseup', stopDrawing)
      canvas.addEventListener('mouseout', stopDrawing)
    });
  </script>
</body>

</html>

效果图
在这里插入图片描述

效果图和上面相同(可以画多条直线,点击确定按钮会保留最后画的一条线)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas Drawing</title>
  <style>
    canvas {
      border: 1px solid #000;
      margin-bottom: 20px;
    }

    button {
      display: block;
      margin-top: 10px;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="500" height="500"></canvas>
  <button id="confirmButton">Confirm</button>

  <script>
    document.addEventListener('DOMContentLoaded', function () {
      const canvas = document.getElementById('myCanvas')
      const ctx = canvas.getContext('2d')
      let lines = [] // 画线的数组
      let lines2 = [] // 点击确定后存放的按钮
      let num = 0
      let isDrawing = false
      let startPoint = {}
      let endPoint = {}

      function startDrawing (e) {
        isDrawing = true
        startPoint = {
          x: e.clientX - canvas.offsetLeft,
          y: e.clientY - canvas.offsetTop
        }
      }

      function drawLine (e) {
        if (!isDrawing) return

        endPoint = {
          x: e.clientX - canvas.offsetLeft,
          y: e.clientY - canvas.offsetTop
        }

        ctx.clearRect(0, 0, canvas.width, canvas.height) // Clear the canvas

        ctx.lineWidth = 5
        ctx.lineCap = 'round'
        ctx.strokeStyle = '#000'

        lines.forEach(line => {
          ctx.beginPath()
          ctx.moveTo(line.start.x, line.start.y)
          ctx.lineTo(line.end.x, line.end.y)
          ctx.stroke()
        })

        ctx.beginPath()
        ctx.moveTo(startPoint.x, startPoint.y)
        ctx.lineTo(endPoint.x, endPoint.y)
        ctx.stroke()
      }

      function stopDrawing () {
        if (isDrawing) {
          isDrawing = false
          lines.push({ start: { ...startPoint }, end: { ...endPoint } })
        }
      }

      function confirmLines () {
        lines2.push(lines[lines.length - 1])
        lines = [...lines2]
        ctx.clearRect(0, 0, canvas.width, canvas.height) // Clear the canvas
        if (lines2.length > 0) {
          lines2.forEach((item) => {
            const lastLine = item
            ctx.beginPath()
            ctx.moveTo(lastLine.start.x, lastLine.start.y)
            ctx.lineTo(lastLine.end.x, lastLine.end.y)
            ctx.stroke()
          })
        }
      }

      canvas.addEventListener('mousedown', startDrawing)
      canvas.addEventListener('mousemove', drawLine)
      canvas.addEventListener('mouseup', stopDrawing)
      canvas.addEventListener('mouseout', stopDrawing)

      const confirmButton = document.getElementById('confirmButton')
      confirmButton.addEventListener('click', confirmLines)
    });
  </script>
</body>

</html>

http://www.kler.cn/a/161279.html

相关文章:

  • vue css box-shadow transition实现类似游戏中的模糊圈游走的感觉
  • 【vue2父组件调用子组件方法之slot的使用】
  • 深入探索仓颉编程语言:函数与结构类型的终极指南
  • 医疗平板与普通平板对比:优势尽显
  • FastAPI 与 SQLModel 分页功能实现指南
  • adb无法连接到安卓设备【解决方案】报错:adb server version (40) doesn‘t match this client (41);
  • java字符串String类的常用方法
  • Swift 中 User Defaults 的读取和写入
  • 商家门店小程序怎么做?门店小程序的优势和好处
  • Docker 一些设置
  • zabbix配置snmp trap--使用snmptrapd和Bash接收器--图文教程
  • Android启动界面之isTaskRoot的妙用及Deeplink的处理
  • 从文字到使用,一文读懂Kafka服务使用
  • macOS 13.6上Sublime无法使用Package Control问题
  • Vue3 Element-Plus 一站式生成动态表单:简化前端开发流程
  • 浅谈https
  • jQuery的入口函数
  • Java毕业设计源码—vue+SpringBoot图书借阅管理图书馆管理系统
  • 10_企业架构NOSQL数据库之MongoDB
  • [ffmpeg] find 编码器
  • 最新GM/T 0126-2023《HTML密码应用置标语法》等25项密码行业标准
  • QML优化,当列表数据过多时,切换tab可能会导致卡顿的情况。
  • StarRocks 存算分离最佳实践,让降本增效更简单
  • Tomcat的初步学习
  • OPC UA客户端工具UaExpert使用
  • Qt 输入一组数,排序后用柱状图显示