【es6】原生js在页面上画矩形层级等问题的优化(二)

接上篇的业务,实现了基础的画图,选中与拖动的效果,还有点细节问题,多个元素重叠在一起,选中的不能在最上面显示,以及最后绘制的元素要能覆盖前面绘制的,

优化如下

优化后效果

  • 在后面绘制的矩形要能覆盖前面的,这个比较简单,改变每个divz-index就可以
  • 代码调整
js 复制代码
drawRect() {
    const div = document.createElement("div");
    div.className = "draw-rect";
    div.style = `width: ${this.disX}px;height: ${
      this.disY
    }px;border:1px solid #ccc;position: absolute;left: ${this.startX}px;top: ${
      this.startY
    }px;z-index:${this.zIndex++};background:greenyellow`;
    div.appendChild(this.addDeleteBtn());
    document.body.appendChild(div);
    this.allRect.push(div);
    this.setCurrentBorderColor(div);
  }
  • 多个图形重叠在一起的时候,选择的元素在最顶层,也比较简单
js 复制代码
changeBorderColor(target) {
  this.nowMoveTarget = target;
  this.setCurrentBorderColor(target);
  // 改变鼠标指针
  target.style.cursor = "move";
  target.style.zIndex = ++this.zIndex;
}
  • 完整代码
js 复制代码
class Draw {
  constructor() {
    this.x = 0;
    this.y = 0;
    this.disX = 0;
    this.disY = 0;
    this.startX = 0;
    this.startY = 0;
    this.offsetX = 0;
    this.offsetY = 0;
    this.nowMoveTarget = null;
    this.mouseDown = this.mouseDown.bind(this);
    this.mouseMove = this.mouseMove.bind(this);
    this.mouseUp = this.mouseUp.bind(this);
    this.handleRectMove = this.handleRectMove.bind(this);
    this.handleRectUp = this.handleRectUp.bind(this);
    this.zIndex = 0;
    this.allRect = [];
    this.shadowBox = document.createElement("div");
    this.init();
  }

  init() {
    this.draw();
  }
  draw() {
    document.addEventListener("mousedown", this.mouseDown, false);
  }
  mouseDown(e) {
    console.log("🚀 ~ Draw ~ mouseDown ~ e:", e);
    if (e.target.className == "delete-btn") return;
    // 校验点击的是不是画的的元素
    if (e.target.className == "draw-rect") {
      // 改变边框颜色
      this.changeBorderColor(e.target);
      this.handleRectDown(e);
      return false;
    } else {
      this.x = e.clientX;
      this.y = e.clientY;
      document.addEventListener("mousemove", this.mouseMove);
      document.addEventListener("mouseup", this.mouseUp);
    }
  }
  mouseMove(e) {
    // 不要选中文字
    e.preventDefault();
    // this.disX = e.clientX - this.x
    // this.disY = e.clientY - this.y
    // const startX = e.clientX < this.x ? e.clientX : this.x
    // const startY = e.clientY < this.y ? e.clientY : this.y
    // this.disX = e.clientX > this.x ? e.clientX - this.x : this.x - e.clientX
    // this.disY = e.clientY > this.y ? e.clientY - this.y : this.y - e.clientY
    this.startX = Math.min(e.clientX, this.x);
    this.startY = Math.min(e.clientY, this.y);
    this.disX = Math.abs(e.clientX - this.x);
    this.disY = Math.abs(e.clientY - this.y);

    // console.log('🚀 ~ Draw ~ mouseMove ~ e:', this.disX, this.disY)
    this.drawShadeRect();
  }
  mouseUp(e) {
    document.removeEventListener("mousemove", this.mouseMove);
    document.removeEventListener("mouseup", this.mouseUp);
    this.drawRect();
    this.shadowBox && this.shadowBox.remove();
  }
  drawShadeRect(startX, startY) {
    this.shadowBox.style = `width: ${this.disX}px;height: ${
      this.disY
    }px;border:1px solid red;background:rgba(94,243,243,.5);position: absolute;left: ${
      this.startX
    }px;top: ${this.startY}px;z-index:${this.zIndex++}`;
    document.body.appendChild(this.shadowBox);
  }
  drawRect() {
    const div = document.createElement("div");
    div.className = "draw-rect";
    div.style = `width: ${this.disX}px;height: ${
      this.disY
    }px;border:1px solid #ccc;position: absolute;left: ${this.startX}px;top: ${
      this.startY
    }px;z-index:${this.zIndex++};background:greenyellow`;
    div.appendChild(this.addDeleteBtn());
    document.body.appendChild(div);
    this.allRect.push(div);
    this.setCurrentBorderColor(div);
  }
  handleRectDown(e) {
    this.startX = e.clientX;
    this.startY = e.clientY;
    this.offsetX = e.clientX - this.nowMoveTarget.offsetLeft;
    this.offsetY = e.clientY - this.nowMoveTarget.offsetTop;
    document.addEventListener("mousemove", this.handleRectMove);
    document.addEventListener("mouseup", this.handleRectUp);
  }
  handleRectMove(e) {
    this.disX = e.clientX - this.offsetX;
    this.disY = e.clientY - this.offsetY;
    this.nowMoveTarget.style.left = `${this.disX}px`;
    this.nowMoveTarget.style.top = `${this.disY}px`;
  }
  handleRectUp() {
    document.removeEventListener("mousemove", this.handleRectMove);
    document.removeEventListener("mouseup", this.handleRectUp);
  }
  changeBorderColor(target) {
    this.nowMoveTarget = target;
    this.setCurrentBorderColor(target);
    // 改变鼠标指针
    target.style.cursor = "move";
    target.style.zIndex = ++this.zIndex;
  }
  // 动态添加一个删除按钮
  addDeleteBtn() {
    const btn = document.createElement("button");
    btn.innerHTML = "删除";
    btn.className = "delete-btn";
    btn.style = `position: absolute;right: 0px;bottom: -25px`;
    // 绑定事件
    btn.onclick = function () {
      this.parentElement.remove();
    };
    return btn;
  }

  setCurrentBorderColor(target) {
    // 改变边框颜色,当前选中的高亮
    this.allRect.forEach((item) => {
      if (item != target) {
        item.style.border = "1px solid #ccc";
      }
    });
    target.style.border = "1px solid blue";
  }
}

const d = new Draw();
d.init();

这样就对我们的画图的层级问题优化完成了。

总结

  • 层级问题多半是css的z-index问题,需要我们手动去更新
  • 还需要实现拖拉边框实现调整矩形大小的业务
相关推荐
mldong4 小时前
你的 Vue3 项目也能有钉钉同款审批流设计器:npm 装包,10 分钟画出第一条审批流
前端·vue.js
2分钟速写快排4 小时前
什么是 RAG?如何用 RAG 实现一个用户记忆?
前端·后端·ai编程
passerby60615 小时前
如何自己造一个时间处理库
前端·javascript·github
走到天涯海角6 小时前
react里面的长列表渲染优化
前端·react.js·前端框架
小羊没烦恼!6 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
北岛贰6 小时前
迷茫焦虑期,我做了一个带支付带官网的 AI 聊天虚拟恋人 App
前端·人工智能·后端
mayaairi8 小时前
Vue2 组件通讯(三):全局事件总线、PubSub、插槽与组件实例属性
前端·javascript·vue.js
kyriewen8 小时前
面试官问我:AI 都能写代码了,前端凭什么还值 25K
前端·javascript·人工智能
风骏时光牛马9 小时前
AI源码分析:拆解模型底层实现逻辑
前端
IT_陈寒9 小时前
React子组件莫名其妙重渲染?你可能漏了这个Hook
前端·人工智能·后端