手写视频裁剪框

javascript 复制代码
     <!-- 截取框 -->
                <div
                  v-show="isShow"
                  class="crop-box"
                  :style="{
                    width: cropWidth + 'px',
                    height: cropHeight + 'px',
                    left: cropX + 'px',
                    top: cropY + 'px',
                  }"
                  ref="cropBox"
                  @mousedown="startInteraction"
                >
                  <!-- 内容在这里 -->
                  <div class="crop-box-content"></div>
                  <div
                    @mousedown="dd"
                    data-v-23936b6b=""
                    data-action="se"
                    class="east-south-side"
                  ></div>
                </div>

js

javascript 复制代码
  startInteraction(e) {
      const box = this.$refs.cropBox;
      const boxRect = box.getBoundingClientRect();
      const mouseX = e.clientX - boxRect.left;
      const mouseY = e.clientY - boxRect.top;

      if (mouseX <= this.resizeHandleSize && mouseY <= this.resizeHandleSize) {
        this.resizeDirection = "tl";
      } else if (
        mouseX >= boxRect.width - this.resizeHandleSize &&
        mouseY <= this.resizeHandleSize
      ) {
        this.resizeDirection = "tr";
      } else if (
        mouseX >= boxRect.width - this.resizeHandleSize - 20 &&
        mouseY >= boxRect.height - this.resizeHandleSize - 20
      ) {
        this.resizeDirection = "br";
      } else if (
        mouseX <= this.resizeHandleSize &&
        mouseY >= boxRect.height - this.resizeHandleSize
      ) {
        this.resizeDirection = "bl";
      } else {
        this.resizeDirection = null;
        this.startDragging(e);
        return;
      }

      this.startX = e.clientX;
      this.startY = e.clientY;
      this.startWidth = this.cropWidth;
      this.startHeight = this.cropHeight;
      this.startCropX = this.cropX;
      this.startCropY = this.cropY;
      this.isResizing = true;

      document.addEventListener("mousemove", this.handleResize);
      document.addEventListener("mouseup", this.stopInteraction);
    },
    startDragging(e) {
      this.startX = e.clientX;
      this.startY = e.clientY;
      this.startCropX = this.cropX;
      this.startCropY = this.cropY;
      this.isDragging = true;

      document.addEventListener("mousemove", this.handleDrag);
      document.addEventListener("mouseup", this.stopInteraction);
    },
    handleResize(e) {
      if (this.isResizing) {
        const deltaX = e.clientX - this.startX;
        const deltaY = e.clientY - this.startY;
        let newWidth, newHeight;

        switch (this.resizeDirection) {
          case "tl":
            return;
            newWidth = this.startWidth - deltaX;
            newHeight = this.calculateHeight(newWidth);
            this.cropX = this.startCropX + deltaX;
            this.cropY = this.startCropY + deltaY;
            break;
          case "tr":
            return;

            newWidth = this.startWidth + deltaX;
            newHeight = this.calculateHeight(newWidth);
            this.cropY = this.startCropY + deltaY;
            break;
          case "br":
            newWidth = this.startWidth + deltaX;
            // newHeight = this.calculateHeight(newWidth);
            newHeight = (newWidth * 16) / 9;
            break;
          case "bl":
            return;

            newWidth = this.startWidth - deltaX;
            newHeight = this.calculateHeight(newWidth);
            this.cropX = this.startCropX + deltaX;
            break;
          default:
            break;
        }

        this.cropWidth = Math.max(newWidth, 50); // 最小宽度
        this.cropHeight = Math.max(newHeight, 50); // 最小高度

        // 检查是否超出父容器范围
        const cropper = this.$refs.videoAndCropper;
        // console.log(
        // "🚀 ~ file: index02.vue:1687 ~ handleResize ~ cropper:",
        // cropper.offsetHeight
        // );
        const parentRect = this.$el.getBoundingClientRect();
        // // console.log("🚀 ~ file: index02.vue:1687 ~ handleResize ~ parentRect:", parentRect)

        if (this.cropY + this.cropHeight > cropper.offsetHeight) {
          this.cropHeight = cropper.offsetHeight;
        }

        if (this.cropHeight == cropper.offsetHeight) {
          this.cropWidth = (this.cropHeight / 16) * 9;
        }

        //  if (this.cropX + this.cropWidth > parentRect.width) {
        //   this.cropWidth = parentRect.width - this.cropX;
        // }
      }
    },

   handleDrag(e) {
      // 通过$refs获取元素引用
      const element = this.$refs.videoAndCropper;

      // 获取元素的高度和宽度
      const height = element.clientHeight; // 获取元素内部高度,包括内边距,不包括边框
      const width = element.clientWidth; // 获取元素内部宽度,包括内边距,不包括边框

      if (this.isDragging) {
        const deltaX = e.clientX - this.startX;
        const deltaY = e.clientY - this.startY;

        // 计算新的位置
        const newCropX = this.startCropX + deltaX;
        const newCropY = this.startCropY + deltaY;
        // console.log(
        // "🚀 ~ file: index02.vue:1677 ~ handleDrag ~ newCropY:",
        // newCropY + this.cropHeight
        // );
        // 检查是否超出父容器范围
        const parentRect = this.$el.getBoundingClientRect();
        // console.log(
        // "🚀 ~ file: index02.vue:1651 ~ handleResize ~ parentRect:",
        // parentRect
        // );
        // console.log(
        // "🚀 ~ file: index02.vue:1694 ~ handleDrag ~ height:",
        // height
        // );

        if (newCropX >= 0 && newCropX + this.cropWidth <= parentRect.width) {
          this.cropX = newCropX;
        }
        if (newCropY >= 0 && newCropY + this.cropHeight <= parentRect.height) {
          this.cropY = newCropY;
        }

        //    if (newCropY + this.cropHeight >= height) {
        //     console.log(3333);
        //   return;
        // }

        if (newCropX + this.cropWidth >= width) {
          this.cropX = width - this.cropWidth;
        }

        if (newCropY + this.cropHeight >= height) {
          this.cropY = height - this.cropHeight;
        }
      }
    },
    stopInteraction() {
      this.isResizing = false;
      this.isDragging = false;
      this.resizeDirection = null;
      document.removeEventListener("mousemove", this.handleResize);
      document.removeEventListener("mousemove", this.handleDrag);
      document.removeEventListener("mouseup", this.stopInteraction);
    },
相关推荐
anOnion7 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
用户47949283569157 小时前
claude Fable用不了?把Gpt 5.5pro接到你的claude code里
前端·后端
JieE2128 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
冬奇Lab10 小时前
AI Workflow 定义的四次演进:从 Markdown 到 JS 脚本,再到分布式多 Agent
javascript·人工智能·agent
zhangxingchao10 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒12 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic13 小时前
SwiftUI 手势笔记
前端·后端
橙子家13 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端
user205855615181313 小时前
X6 中边悬浮置顶,规避 `mouseleave` 事件丢失问题
前端
李明卫杭州13 小时前
CSS aspect-ratio 属性完全指南
前端