实现一个简单的画布拖动功能

实现一个简单的画布拖动功能

请添加图片描述

  • 主要原理:计算鼠标移动的距离并作出距离限制,设置元素的 translateX translateY 属性

直接贴源码

<!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>
    <style></style>
  </head>
  <body style="overflow: hidden; margin: 0; height: 100vh; width: 100vw">
    <div
      id="myCanvas"
      style="
        width: 3200px;
        height: 2000px;
        background-color: antiquewhite;
        position: relative;
      "
    >
      <div
        id="box"
        style="
          width: 192px;
          height: 340px;
          background-color: aqua;
          position: absolute;
          top: 500px;
          left: 600px;
        "
      ></div>
      <div
        id="box"
        style="
          width: 192px;
          height: 340px;
          background-color: aqua;
          position: absolute;
          top: 0;
          left: 0;
        "
      ></div>
      <div
        id="box"
        style="
          width: 192px;
          height: 340px;
          background-color: aqua;
          position: absolute;
          bottom: 20px;
          right: 0;
        "
      ></div>
    </div>
    <script>
      var divScreenX = 0; // 盒子到屏幕 左侧的距离
      var divScreenY = 0; // 盒子到屏幕 顶部的距离
      var mouseScreenX = 0; //鼠标到屏幕左边的距离
      var mouseScreenY = 0; // 鼠标到屏幕顶部的距离
      var div = document.getElementById("myCanvas");
      var clientWidth = document.body.clientWidth; // 可见区域宽度
      var clientHeight = document.body.clientHeight; // 可见区域高度
      div.onmousedown = function (e) {
        divScreenX = div.getBoundingClientRect().left;
        divScreenY = div.getBoundingClientRect().top;

        mouseScreenX = e.offsetX;
        mouseScreenY = e.offsetY;
        document.onmousemove = function (e) {
          div.style.transition = "all ease .2s"; // 丝滑

          let moveNumX = divScreenX - (mouseScreenX - e.offsetX); // 鼠标移动的距离 X

          let moveNumY = divScreenY - (mouseScreenY - e.offsetY); // 鼠标移动的距离 Y

          // moveNumX = moveNumX < -1280 ? -1280 : moveNumX; //3200-1920 画布宽度减去当前屏幕可见区域宽度
          moveNumX =
            moveNumX < -(3200 - clientWidth) ? -(3200 - clientWidth) : moveNumX; //3200-1920 画布宽度减去当前屏幕可见区域宽度
          moveNumX = moveNumX > 0 ? 0 : moveNumX;

          // moveNumY = moveNumY < -1024 ? -1024 : moveNumY; // 2000-1024 画布高度减去当前屏幕可见区域高度
          moveNumY =
            moveNumY < -(2000 - clientHeight)
              ? -(2000 - clientHeight)
              : moveNumY; // 2000-1024 画布高度减去当前屏幕可见区域高度

          moveNumY = moveNumY > 0 ? 0 : moveNumY;

          div.style.transform = `translateX(${moveNumX}px) translateY(${moveNumY}px) translateZ(1px)`;

          return false;
        };
        document.onmouseup = function () { // 清除事件
          document.onmouseup = null; 
          document.onmousemove = null;
        };
      };
    </script>
  </body>
</html>


不是很完善,有兴趣的大佬可以自行修改完善
也请各位大佬不要吝啬完善后的代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现鼠标左键拖动画布功能,你可以使用Tkinter库来创建GUI界面,并利用Canvas组件来绘制画布。 首先,我们需要创建一个Tkinter窗口和一个Canvas组件: ```python from tkinter import * # 创建窗口 window = Tk() # 创建画布 canvas = Canvas(window, width=500, height=500) canvas.pack() ``` 接下来,我们需要定义鼠标按下和释放时的事件处理函数。在鼠标按下时,记录当前鼠标的位置,并将画布绑定到鼠标移动事件。在鼠标释放时,取消绑定的鼠标移动事件。 ```python def on_mouse_press(event): # 记录鼠标按下时的位置 canvas.scan_mark(event.x, event.y) canvas.bind("<B1-Motion>", on_mouse_drag) def on_mouse_release(event): # 取消鼠标移动事件绑定 canvas.unbind("<B1-Motion>") def on_mouse_drag(event): # 计算鼠标移动的距离 canvas.scan_dragto(event.x, event.y, gain=1) ``` 最后,我们需要将这些事件处理函数绑定到画布的鼠标事件上: ```python # 绑定鼠标按下和释放事件 canvas.bind("<ButtonPress-1>", on_mouse_press) canvas.bind("<ButtonRelease-1>", on_mouse_release) # 进入主循环 window.mainloop() ``` 这样,当你在画布上按下鼠标左键并移动时,画布会跟随鼠标的移动而进行拖动。 完整的示例代码如下: ```python from tkinter import * # 创建窗口 window = Tk() # 创建画布 canvas = Canvas(window, width=500, height=500) canvas.pack() def on_mouse_press(event): # 记录鼠标按下时的位置 canvas.scan_mark(event.x, event.y) canvas.bind("<B1-Motion>", on_mouse_drag) def on_mouse_release(event): # 取消鼠标移动事件绑定 canvas.unbind("<B1-Motion>") def on_mouse_drag(event): # 计算鼠标移动的距离 canvas.scan_dragto(event.x, event.y, gain=1) # 绑定鼠标按下和释放事件 canvas.bind("<ButtonPress-1>", on_mouse_press) canvas.bind("<ButtonRelease-1>", on_mouse_release) # 进入主循环 window.mainloop() ``` 希望对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值