【问题标题】:Allow mouse events to pass through a Fabrixjs Canvas instance?允许鼠标事件通过 Fabrixjs Canvas 实例?
【发布时间】:2020-12-17 05:06:38
【问题描述】:

我已经有几年没有使用Fabricjs了,如果这是一个基本问题,请原谅我

我在一些按钮元素上添加了一个窗口大小的Canvas 元素,我还希望用户能够点击这些元素。

Fabricjs 中是否有允许鼠标事件传递到底层元素的属性?我一直在查看文档,但没有找到任何东西

【问题讨论】:

    标签: javascript html5-canvas fabricjs


    【解决方案1】:

    我最终使用“pointer-events: none”作为内联样式应用于 2 个画布元素(第一个类名为“lower-canvas”,第二个类名为“upper-canvas”)和包装器 div从我提供的 canvas 元素创建的 fabric.Canvas(...) 构造函数。

    这让鼠标事件传递到下面的元素。

    // Setting up the vanilla canvas to pass to FabricJS
    const canvasEl = document.createElement("canvas");
    ...
    canvasEl.style.pointerEvents = "none"; // This gets copied onto the 2 canvas elements that FabricJS will create below
    document.body.appendChild(canvasEl);
    
    // Start of FabricJS part 
    const canvas = new fabric.Canvas(canvasEl); // Creates the div and 2 canvas elements and replaces what was passed in with them (in the DOM)
    canvas.wrapperEl.style.pointerEvents = "none"; //"wrapperEl" refers to the div. I found it by looking through https://github.com/fabricjs/fabric.js/blob/master/dist/fabric.js to see where the upperCanvasEl & lowerCanvasEl were attached
    

    【讨论】:

      【解决方案2】:

      您可以使用document.elementsFromPoint。点击左上角:

      let doc, html, bod, M, I, mobile, S, Q, aC, rC; // for use on other loads
      addEventListener('load', ()=>{
      doc = document; html = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);
      mobile = nav.userAgent.match(/Mobi/i) ? true : false;
      S = (selector, within)=>{
        var w = within || doc;
        return w.querySelector(selector);
      }
      Q = (selector, within)=>{
        var w = within || doc;
        return w.querySelectorAll(selector);
      }
      aC = function(){
        const a = [].slice.call(arguments);
        a.shift().classList.add(...a);
        return aC;
      }
      rC = function(){
        const a = [].slice.call(arguments);
        a.shift().classList.remove(...a);
        return rC;
      }
      // can do below on another page except end load
      const canvas = I('canvas'), ctx = canvas.getContext('2d'), test = I('test');
      ctx.fillStyle = '#070'; ctx.fillRect(0, 0, canvas.width, canvas.height);
      test.onclick = ()=>{
        console.log('test worked');
      }
      canvas.onclick = function(e){
        const all = doc.elementsFromPoint(e.clientX, e.clientY);
        for(let q of all){
          switch(q){
            case test:
              test.click();
              break;
          }
        }
      }
      canvas.onmousemove = function(e){
        const all = doc.elementsFromPoint(e.clientX, e.clientY);
        for(let q of all){
          switch(q){
            case test:
              aC(this, 'pointer');
              break;
            case this:
              rC(this, 'pointer');
              break;
          }
        }
      }
      }); // end load
      *{
        box-sizing:border-box;
      }
      html,body{
       padding:0; margin:0;
      }
      html,body,#outer,#canvas{
        width:100%; height:100%;
      }
      #outer{
        position:relative;
      }
      #outer>*{
        position:absolute;
      }
      .pointer{
        cursor:pointer;
      }
      <div id='outer'>
        <input id='test' type='button' value='test' />
        <canvas id='canvas'></canvas>
      </div>

      我希望你也可以看看你将如何测试其他情况。

      【讨论】:

      • 谢谢。 . .我在想可能有一个属性我忽略了allowClickThru = true
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多