js纯原生实现div画框+多选+框选canvas内容+跨屏选

本文介绍了一种在Canvas上实现框选及多选的方法,通过监听鼠标事件和计算坐标来绘制选择区域,并讨论了如何根据坐标进行数据操作。同时,提到了跨屏选择的技术实现思路。
新需求需要在canvas画布上实现框选及对框选中的数据进项操作
以下代码实现框选及多选
// main画布事件监听,框选
function mainMarqueeMousedown (e) {
    if (e.button !== 0) return
    // 判断是否多选
    if (!e.ctrlKey) {
      // 容纳marqueeDOM的盒子
      marqueeBoxDOM.innerHTML = ''
      // 储存marquee坐标数组
      marqueeAxiss = []
    }
    // 创建新div
    const marquee = this.createMarquee()
    marqueeBoxDOM.appendChild(marquee)

    if (!(e.target.id === 'mainCtxbox' || e.target.id === 'marquee')) return
    let marqueeAxis={}
    marqueeAxis.end.x = marquee.end.y = null
    marqueeAxis.starting = this.getEventPosition(e)
    marquee.style.left = marqueeAxis.starting.x + 'px'
    marquee.style.top = marqueeAxis.starting.y +'px'
    mainCanvas.onmousemove = mainMarqueeMousemove.bind(this, marquee)
    document.onmouseup = mainMarqueeMouseUp.bind(this, marquee)
    mainCanvas.onmouseleave = this.moveSelection.bind(this, marquee)
    marqueeBoxDOM.style.pointerEvents = 'none'
    function mainMarqueeMousemove(marquee, e) {
      const end = marqueeAxis.end = this.getEventPosition(e)
      const starting = marqueeAxis.starting
      // 左上角
      if (end.x > starting.x && end.y > starting.y) {
        marquee.style.top = starting.y + 'px'
        marquee.style.left = starting.x + 'px'
        marquee.style.width = end.x - starting.x + 'px'
        marquee.style.height = end.y - starting.y + 'px'

        // 右下角
      } else if (end.x < starting.x && end.y < starting.y) {
        const x = starting.x - end.x
        const y = starting.y - end.y
        marquee.style.top = starting.y - y + 'px'
        marquee.style.left = starting.x - x + 'px'
        marquee.style.width = x + 'px'
        marquee.style.height = y + 'px'

        // 左下角
      } else if (end.x > starting.x && end.y < starting.y) {
        const y = starting.y - end.y
        marquee.style.top = starting.y - y + 'px'
        marquee.style.left = starting.x + 'px'
        marquee.style.width = end.x - starting.x + 'px'
        marquee.style.height = y + 'px'
        // 右上角
      } else if (end.x < starting.x && end.y > starting.y) {
        const x = starting.x - end.x
        marquee.style.left = starting.x - x + 'px'
        marquee.style.top = starting.y  + 'px'
        marquee.style.width = x + 'px'
        marquee.style.height = end.y - starting.y + 'px'
      }
    }
    function mainMarqueeMouseUp(marquee, e) {
      const end = marqueeAxis.end
      const starting = marqueeAxis.starting
      this.destroy()
      this.boxsDom.marquee.style.pointerEvents = 'auto'
      if (end.y === null || end.x === null) {
        end.y = starting.y + 1
        end.x = starting.x + 1
      }
      // 左上角
      if (end.x > starting.x && end.y > starting.y) {
        marquee.style.width = end.x - starting.x + 'px'
        marquee.style.height = end.y - starting.y + 'px'
        // 右下角
      } else if (end.x < starting.x && end.y < starting.y) {
        marquee.style.top = end.y + 'px'
        marquee.style.left = end.x + 'px'
        marquee.style.width = starting.x - end.x + 'px'
        marquee.style.height = starting.y - end.y + 'px'

        // 左下角
      } else if (end.x > starting.x && end.y < starting.y) {
        marquee.style.top = end.y  + 'px'
        marquee.style.width = end.x - starting.x + 'px'
        marquee.style.height = starting.y - end.y + 'px'

        // 右上角
      } else if (end.x < starting.x && end.y > starting.y) {
        marquee.style.left = end.x + 'px'
        marquee.style.width = starting.x - end.x + 'px'
        marquee.style.height = end.y - starting.y + 'px'
      }
      marqueeAxiss.push(JSON.parse(JSON.stringify(marqueeAxis)))
    }
  }

  // 获取坐标信息
  function getEventPosition(e) {
    let x, y
    if (e.layerX || e.layerX === 0) {
      x = e.layerX
      y = e.layerY
    } else if (e.offsetX || e.offsetX === 0) {
      x = e.offsetX
      y = e.offsetY
    }
    return { x, y }
  }

  // 创建框选div
  function createMarquee() {
    const marquee = document.createElement('div')
    marquee.style.position = 'absolute'
    marquee.style.background = '#034175'
    marquee.style.border = '1px solid #0976d4'
    return marquee
  },

以上代码可以实现画多个框+获取坐标
剩下的框选内容无非就是用坐标和对应的数据计算出所选中的数据
跨屏选无非就是监听鼠标移出事件,然后设置setInterval 每100ms 增加减少scrollTop或scrollLeft的值
欢迎讨论拍砖

CanvasHTML5的一个绘图API,可以用于创建交互式的图形效果,包括实现瀑布图的滚动和框选功能。下面是一个简单的示例,展示了如何使用canvasJavaScript实现瀑布图的框选功能: ```html <!DOCTYPE html> <html lang="en"> <body> <canvas id="myCanvas" width="800" height="600"></canvas> <script> var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); // 假设data是一个包含瀑布图数据的对象数组 var data = [{value: 100}, {value: 200}, ...]; function drawWaterfall() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); var y = canvas.height; for (var i = data.length - 1; i >= 0; i--) { var item = data[i]; var height = item.value; // 计算每个瀑布的位置并绘制 ctx.fillRect(item.position, y - height, 20, height); y -= height; } } // 监听鼠标事件,获取框选区域 canvas.addEventListener('mousedown', function(e) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; // 更新框选区域 drawSelectedArea(x, y); }); function drawSelectedArea(x, y) { ctx.fillStyle = 'red'; ctx.strokeStyle = 'black'; ctx.lineWidth = 2; // 使用矩形路径来模拟框选 ctx.beginPath(); ctx.rect(x, y, 20, canvas.height); ctx.stroke(); ctx.fill(); } drawWaterfall(); </script> </body> </html> ``` 在这个例子中,`drawWaterfall`函数负责渲染瀑布图,而`drawSelectedArea`则是在鼠标按下时开始记录框选范围。请注意,这只是一个基础版本,实际应用可能需要处理更的细节,例如边界检测、连续框选等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值