【问题标题】:How to trigger click on canvas object and DOM element that is placed on top of object at the same time?如何触发同时点击画布对象和放置在对象顶部的DOM元素?
【发布时间】:2021-02-26 09:51:18
【问题描述】:

我有一些可以添加到 fabricjs 画布的圆圈。每个圆圈都是一个对象,而在我的 javascript 代码之外我有一个 DOM 元素,看起来像这样:

<span id="cirkel1" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;z-index:9999999;position:absolute;cursor:pointer;">
  <div class="tooltipcontent darktext tooltippadding" style="position:relative;">
    Testtest
  </div>
</span>

这个元素触发一个带有Tippjs(一个js工具提示包)的工具提示,它具有以下代码(不要介意每个循环,我还应该提到下面的代码在画布函数之外):

$( "#cirkel1" ).each(function( i ) {
  tippy(this, {
    theme: 'blue',
    trigger: 'click',
    allowHTML: true,
    placement: 'right',
    animation: 'scale-subtle',
    interactive: true,
    content: function (reference) {
      return reference.querySelector('.tooltipcontent');
    }
  });
});

在我为画布声明所有内容的函数中,我有以下代码将 DOM 元素放置在画布对象的顶部:

fabric.Canvas.prototype.getAbsoluteCoords = function(object) {
  return {
    left: object.left + this._offset.left,
    top: object.top + this._offset.top
  };
}

var cirkel1tooltip = document.getElementById('cirkel1'),
btnWidth = 40,
btnHeight = 40;

function positionBtn(obj) {
  var absCoords = canvas.getAbsoluteCoords(obj);
  cirkel1tooltip.style.left = (absCoords.left - btnWidth / 10) + 'px';
  cirkel1tooltip.style.top = (absCoords.top - btnHeight / 10) + 'px';
}

这可行,并且工具提示会在单击时显示,但在我的画布功能中,我还有一个单击功能,可以在单击时切换特定圆圈的图像。我需要在单击圆圈时同时触发,现在当我单击一个圆圈时,会出现图像,但只有在我第二次单击后才会出现工具提示,而不是在第一次单击时出现。

在我单击另一个圆圈然后再单击之前单击的圆圈之前,通过第二次单击来删除图像也不起作用。

奇怪的是,当我删除两个功能之一(工具提示单击或图像切换单击)时,它会立即起作用,但一起只有图像切换立即起作用,而工具提示仅在第二次单击后才起作用。这是为什么呢?

完整代码可以看这里(点击小圆圈测试):https://codepen.io/twan2020/pen/jOVaWMm

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <base href="//printzelf.nl/new/">
    <title>Image test</title>
    <link rel="stylesheet" href="https://unpkg.com/tippy.js@6/animations/scale-subtle.css"/>
    <link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
  </head>
  <body>
    <style media="screen">
    .tippy-box {
      width: 100%!important;
      text-align: center;
      background-color: #fff!important;
      color: #fff!important;
      box-shadow: 3px 2px 15px 6px rgb(0 0 0 / 10%);
    }
    .darktext {
      color: #383838;
      font-family: Panton;
      font-size: 15px;
    }
    .tooltippadding {
      padding: 15px;
    }
    body .tippy-arrow {
      color: #fff!important;
    }
    </style>
    <img id="background" src="https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg" alt="" style="display:none;">
    <div class="canvas-container" style="width: 600px; height: 500px; position: relative; user-select: none;">
      <canvas id="c" width="600" height="500" class="lower-canvas" style="border:1px solid red;position: absolute; width: 600px; height: 500px; left: 0px; top: 0px; touch-action: none; user-select: none;"></canvas>
    </div>
  </body>
  <span id="cirkel1" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;z-index:9999999;position:absolute;cursor:pointer;">
    <div class="tooltipcontent darktext tooltippadding" style="position:relative;">
      Testtest
    </div>
  </span>
  <!-- Popper JS -->
  <script src="assets/js/popper.min.js"></script>
  <script src="https://unpkg.com/tippy.js@6"></script>
  <script type="text/javascript" src="assets/js/fabric.js"></script>
  <script type="text/javascript">
  (function() {
    var myImg = document.querySelector("#background");
     var realWidth = myImg.naturalWidth;
     var realHeight = myImg.naturalHeight;
     var source = document.getElementById('background').src;
     var canvas = new fabric.Canvas('c');
     canvas.hoverCursor = 'pointer';
     canvas.selection = false;
     canvas.setDimensions({ width: realWidth, height: realHeight });
     var img = new Image();
     // use a load callback to add image to canvas.
     img.src = 'https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg';
     canvas.setBackgroundImage(source, canvas.renderAll.bind(canvas), {
         backgroundImageOpacity: 0.5,
         backgroundImageStretch: false
     });


     const hotspots = [
      {
        top: 140,
        left: 230,
        radius: 10,
        fill: '#009fe3',
        id: 'cirkel1',
        hoverCursor: 'pointer',
        selectable: false,
        imgtop: 200,
        imgleft: 300,
        imgheight: 200,
        imgwidth: 200,
        tooltipid: 'cirkel1',
        imgUrl: 'https://printzelf.nl/new/assets/images/logo_gewoon.png'
      },
      {
        top: 240,
        left: 530,
        radius: 10,
        fill: '#009fe3',
        id: 'cirkel2',
        hoverCursor: 'pointer',
        selectable: false,
        imgtop: 200,
        imgleft: 700,
        imgheight: 200,
        imgwidth: 200,
        imgUrl: 'https://i1.wp.com/nypost.com/wp-content/uploads/sites/2/2020/04/pugs-coronavirus.jpg'
      },
      {
        top: 240,
        left: 730,
        radius: 10,
        fill: '#009fe3',
        id: 'cirkel2',
        hoverCursor: 'pointer',
        selectable: false,
        imgtop: 200,
        imgleft: 800,
        imgheight: 200,
        imgwidth: 200,
        imgUrl: 'https://i.guim.co.uk/img/media/fe1e34da640c5c56ed16f76ce6f994fa9343d09d/0_174_3408_2046/master/3408.jpg?width=1200&height=900&quality=85&auto=format&fit=crop&s=0d3f33fb6aa6e0154b7713a00454c83d'
      }
    ];

    const loadedImages = [];

    for (let [idx, props] of hotspots.entries()) {
      let c = new fabric.Circle(props);
      c.class = 'hotspot';
      c.name = 'hotspot-' + idx;
      canvas.add(c);
    }

    fabric.Canvas.prototype.getAbsoluteCoords = function(object) {
      return {
        left: object.left + this._offset.left,
        top: object.top + this._offset.top
      };
    }

    var cirkel1tooltip = document.getElementById('cirkel1'),
    btnWidth = 40,
    btnHeight = 40;

    function positionBtn(obj) {
      var absCoords = canvas.getAbsoluteCoords(obj);
      cirkel1tooltip.style.left = (absCoords.left - btnWidth / 10) + 'px';
      cirkel1tooltip.style.top = (absCoords.top - btnHeight / 10) + 'px';
    }

    for (const ho of canvas.getObjects()) {
      // check for 'hotspot' class
      if (ho.class && ho.class === 'hotspot') {
        ho.on('mousedown', () => {
          // check if image was previously loaded
          if (loadedImages.indexOf(ho.name) < 0) {
            // image is not in the array
            // so it needs to be loaded
            // prepare the image properties
            let imgProps = {
              width: ho.imgwidth,
              height: ho.imgheight,
              left: ho.imgleft,
              top: ho.imgtop,
              scaleX: .25,
              scaleY: .25,
              selectable: false,
              id: 'img-' + ho.name,
              hoverCursor: "default"
            };
            var printzelfImg = new Image();
            printzelfImg.onload = function (img) {
              var printzelf = new fabric.Image(printzelfImg, imgProps);
              canvas.add(printzelf);
            };
            printzelfImg.src = ho.imgUrl;
            // update the `loadedImages` array
            loadedImages.push(ho.name);
          } else {
            // image was previously loaded
            for (const o of canvas.getObjects()) {
              // find the correct image on the canvas
              if (o.id && o.id === 'img-' + ho.name) {
                // toggle the visible property
                o.visible = !o.visible;
                break;
              }
            }
          }
          positionBtn(ho);
        });
      }
    }
  })();
  $( "#cirkel1" ).each(function( i ) {
    tippy(this, {
      theme: 'blue',
      trigger: 'click',
      allowHTML: true,
      placement: 'right',
      animation: 'scale-subtle',
      interactive: true,
      content: function (reference) {
        return reference.querySelector('.tooltipcontent');
      }
    });
  });
  </script>
</html>

另外,是否可以为每个点/圆附加不同的工具提示?

【问题讨论】:

  • 你能做一个有效的 sn-p 以便我们看到问题吗?
  • @AHaworth 我已经添加了 sn-p。如您所见,单击一个小圆圈后会出现图像,但没有工具提示,仅在第二次单击后才出现,并且在第二次单击时,图像不会再次被删除,只有在单击另一个圆圈然后再次单击前一个圆圈后才会删除。
  • 您好,当我转到 codepen 并单击蓝色圆圈时没有任何反应。
  • @AHaworth 这只是小圆圈,大圆圈是图像的一部分。
  • @AHaworth 我选择了另一个背景图片以使其更清晰。

标签: javascript html canvas fabricjs tippyjs


【解决方案1】:

它不起作用的原因是您只有 1 个 DIV 热点,并且您在 mousedown 上移动此热点并期望它随后触发 onclick 事件,这不起作用。它在第二次点击时起作用的原因是它现在在那里的热点。

解决方案是拥有与热点相同数量的 DIV。这使您可以拥有独特的弹出消息。目前它为每个热点显示相同的消息。

tippy 属性有一个 onShow(instance) 和 onHide(instance),允许您在单击这些热点时执行额外的功能。在您的情况下,您想要加载与所选热点相关的图像。这消除了设置两个事件。

切换图像时也出现问题。我已修复此问题,但我不能 100% 确定它是否可以按照您希望的方式工作。

您在

标记之外还有 HTML 标记,并且 HTML 内容不应该存在于 body 之外。

我尽可能地保留了你的大部分原始代码。

<html lang="en" dir="ltr">
  <head>
<meta charset="utf-8">
<base href="//printzelf.nl/new/">
<title>Image test</title>
<link rel="stylesheet" href="https://unpkg.com/tippy.js@6/animations/scale-subtle.css"/>
<link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <style media="screen">
.tippy-box {
  width: 100%!important;
  text-align: center;
  background-color: #fff!important;
  color: #fff!important;
  box-shadow: 3px 2px 15px 6px rgb(0 0 0 / 10%);
}
.darktext {
  color: #383838;
  font-family: Panton;
  font-size: 15px;
}
.tooltippadding {
  padding: 15px;
}
body .tippy-arrow {
  color: #fff!important;
}
</style>
  </head>
  <body>

<img id="background" src="https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg" alt="" style="display:none;">
<div class="canvas-container" style="width: 600px; height: 500px; position: relative; user-select: none;">
  <canvas id="c" width="600" height="500" class="lower-canvas" style="border:1px solid red;position: absolute; width: 600px; height: 500px; left: 0px; top: 0px; touch-action: none; user-select: none;"></canvas>
</div>

  <span id="cirkel1" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;position:absolute;cursor:pointer;">
<div class="tooltipcontent1 tooltipcontent darktext tooltippadding" style="position:relative;">
  Message 1
</div>
  </span>
  
  <span id="cirkel2" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;position:absolute;cursor:pointer;">
<div class="tooltipcontent2 tooltipcontent darktext tooltippadding" style="position:relative;">
  Message 2
</div>
  </span>
  
  <span id="cirkel3" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;position:absolute;cursor:pointer;">
<div class="tooltipcontent3 tooltipcontent darktext tooltippadding" style="position:relative;">
  Message 3
</div>
  </span>
  
  
  <!-- Popper JS -->
  <script src="assets/js/popper.min.js"></script>
  <script src="https://unpkg.com/tippy.js@6"></script>
  <script type="text/javascript" src="assets/js/fabric.js"></script>
  <script type="text/javascript">
  (function() {
var myImg = document.querySelector("#background");
 var realWidth = myImg.naturalWidth;
 var realHeight = myImg.naturalHeight;
 var source = document.getElementById('background').src;
 var canvas = new fabric.Canvas('c');
 canvas.hoverCursor = 'pointer';
 canvas.selection = false;
 canvas.setDimensions({ width: realWidth, height: realHeight });
 var img = new Image();
 // use a load callback to add image to canvas.
 img.src = 'https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg';
 canvas.setBackgroundImage(source, canvas.renderAll.bind(canvas), {
     backgroundImageOpacity: 0.5,
     backgroundImageStretch: false
 });


 const hotspots = [
  {
    top: 140,
    left: 230,
    radius: 10,
    fill: '#009fe3',
    id: 'cirkel1',
    hoverCursor: 'pointer',
    selectable: false,
    imgtop: 200,
    imgleft: 300,
    imgheight: 200,
    imgwidth: 200,
    tooltipid: 'cirkel1',
    imgUrl: 'https://printzelf.nl/new/assets/images/logo_gewoon.png'
  },
  {
    top: 240,
    left: 530,
    radius: 10,
    fill: '#009fe3',
    id: 'cirkel2',
    hoverCursor: 'pointer',
    selectable: false,
    imgtop: 200,
    imgleft: 700,
    imgheight: 200,
    imgwidth: 200,
    imgUrl: 'https://i1.wp.com/nypost.com/wp-content/uploads/sites/2/2020/04/pugs-coronavirus.jpg'
  },
  {
    top: 240,
    left: 730,
    radius: 10,
    fill: '#009fe3',
    id: 'cirkel2',
    hoverCursor: 'pointer',
    selectable: false,
    imgtop: 200,
    imgleft: 800,
    imgheight: 200,
    imgwidth: 200,
    imgUrl: 'https://i.guim.co.uk/img/media/fe1e34da640c5c56ed16f76ce6f994fa9343d09d/0_174_3408_2046/master/3408.jpg?width=1200&height=900&quality=85&auto=format&fit=crop&s=0d3f33fb6aa6e0154b7713a00454c83d'
  }
];

const loadedImages = [];

for (let [idx, props] of hotspots.entries()) {
  let c = new fabric.Circle(props);
  c.class = 'hotspot';
  c.name = 'hotspot-' + idx;
  canvas.add(c);
}

fabric.Canvas.prototype.getAbsoluteCoords = function(object) {
  return {
    left: object.left + this._offset.left,
    top: object.top + this._offset.top
  };
}

var cirkel1tooltip = document.getElementById('cirkel1'),
btnWidth = 40,
btnHeight = 40;

function positionBtn(obj, index) {
  var absCoords = canvas.getAbsoluteCoords(obj);
  var element = document.getElementById('cirkel'+index);
  element.style.left = (absCoords.left - btnWidth / 10) + 'px';
  element.style.top = (absCoords.top - btnHeight / 10) + 'px';
}

canvas.getObjects().forEach(function(ho, index) {
  positionBtn(ho, index + 1); 
});
  

  
  $( ".carttip" ).each(function( i ) {
tippy(this, {
  theme: 'blue',
  trigger: 'click',
  allowHTML: true,
  placement: 'right',
  animation: 'scale-subtle',
  interactive: true,
  onShow(instance) {
     canvas.getObjects().forEach(function(ho, index) {
        if (ho.class && ho.class === 'hotspot') {
           if (instance.id == index + 1) {
           // check if image was previously loaded
              if (loadedImages.indexOf(ho.name) < 0) {
                 // image is not in the array
                 // so it needs to be loaded
                 // prepare the image properties
                 let imgProps = {
                    width: ho.imgwidth,
                    height: ho.imgheight,
                    left: ho.imgleft,
                    top: ho.imgtop,
                    scaleX: .25,
                    scaleY: .25,
                    selectable: false,
                    id: 'img-' + ho.name,
                    hoverCursor: "default",
                    
                 };
                 var printzelfImg = new Image();
                 printzelfImg.onload = function (img) {
                    var printzelf = new fabric.Image(printzelfImg, imgProps);
                    printzelf.trippyHotspotImage = true;
                    canvas.add(printzelf);
                 };
                 printzelfImg.src = ho.imgUrl;
                 // update the `loadedImages` array
                 loadedImages.push(ho.name);
              } else {
                 for (const o of canvas.getObjects()) {
                    if (o.id && o.id === 'img-' + ho.name) {
                       o.visible = true;
                       break;
                    }
                 }
                 canvas.renderAll();
              }
           }
        }
     });
  },
  onHide(instance) {
     for (const o of canvas.getObjects()) {
        if (o.trippyHotspotImage) {
           o.visible = false;
        }
     }
     canvas.renderAll();
  },
  content: function (reference) {
    return reference.querySelector('.tooltipcontent' + (i + 1));
  }
});
  });
})();
  </script>
  </body>

  </html>

【讨论】:

    【解决方案2】:

    看起来click 事件第一次没有在mousedown 第一次之后立即触发。您使用的框架似乎可以防止这种情况发生,因为(由侦听器)执行了一个进程。

    (可能与事件传播有关,但目前我还没有弄清楚如何防止click事件在mouseup之后被触发。)

    我称之为解决方法:要在同一次点击中显示工具提示,即 mousedown 事件后跟 mouseup 事件,您可以设置 mouseuptrigger 属性,显示工具提示:

    $( "#cirkel1" ).each(function( i ) {
        tippy(this, {
          theme: 'blue',
          trigger: 'mouseup', /* <-- here */
          allowHTML: true,
          placement: 'right',
          animation: 'scale-subtle',
          interactive: true,
          content: function (reference) {
            return reference.querySelector('.tooltipcontent');
          }
        });
    

    如果mousedown 发生在圈子上,则将触发mouseup 事件。


    工作 sn-p。

    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <base href="//printzelf.nl/new/">
        <title>Image test</title>
        <link rel="stylesheet" href="https://unpkg.com/tippy.js@6/animations/scale-subtle.css"/>
        <link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
        <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
      </head>
      <body>
        <style media="screen">
        .tippy-box {
          width: 100%!important;
          text-align: center;
          background-color: #fff!important;
          color: #fff!important;
          box-shadow: 3px 2px 15px 6px rgb(0 0 0 / 10%);
        }
        .darktext {
          color: #383838;
          font-family: Panton;
          font-size: 15px;
        }
        .tooltippadding {
          padding: 15px;
        }
        body .tippy-arrow {
          color: #fff!important;
        }
        </style>
        <img id="background" src="https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg" alt="" style="display:none;">
        <div class="canvas-container" style="width: 600px; height: 500px; position: relative; user-select: none;">
          <canvas id="c" width="600" height="500" class="lower-canvas" style="border:1px solid red;position: absolute; width: 600px; height: 500px; left: 0px; top: 0px; touch-action: none; user-select: none;"></canvas>
        </div>
      </body>
      <span id="cirkel1" class="carttip inlineflexmenu" style="border-radius:100%;width: 25px;height:25px;z-index:9999999;position:absolute;cursor:pointer;">
        <div class="tooltipcontent darktext tooltippadding" style="position:relative;">
          Testtest
        </div>
      </span>
      <!-- Popper JS -->
      <script src="assets/js/popper.min.js"></script>
      <script src="https://unpkg.com/tippy.js@6"></script>
      <script type="text/javascript" src="assets/js/fabric.js"></script>
      <script type="text/javascript">
      (function() {
        var myImg = document.querySelector("#background");
         var realWidth = myImg.naturalWidth;
         var realHeight = myImg.naturalHeight;
         var source = document.getElementById('background').src;
         var canvas = new fabric.Canvas('c');
         canvas.hoverCursor = 'pointer';
         canvas.selection = false;
         canvas.setDimensions({ width: realWidth, height: realHeight });
         var img = new Image();
         // use a load callback to add image to canvas.
         img.src = 'https://static01.nyt.com/images/2019/05/29/realestate/00skyline-south4/88ce0191bfc249b6aae1b472158cccc4-superJumbo.jpg';
         canvas.setBackgroundImage(source, canvas.renderAll.bind(canvas), {
             backgroundImageOpacity: 0.5,
             backgroundImageStretch: false
         });
    
    
         const hotspots = [
          {
            top: 140,
            left: 230,
            radius: 10,
            fill: '#009fe3',
            id: 'cirkel1',
            hoverCursor: 'pointer',
            selectable: false,
            imgtop: 200,
            imgleft: 300,
            imgheight: 200,
            imgwidth: 200,
            tooltipid: 'cirkel1',
            imgUrl: 'https://printzelf.nl/new/assets/images/logo_gewoon.png'
          },
          {
            top: 240,
            left: 530,
            radius: 10,
            fill: '#009fe3',
            id: 'cirkel2',
            hoverCursor: 'pointer',
            selectable: false,
            imgtop: 200,
            imgleft: 700,
            imgheight: 200,
            imgwidth: 200,
            imgUrl: 'https://i1.wp.com/nypost.com/wp-content/uploads/sites/2/2020/04/pugs-coronavirus.jpg'
          },
          {
            top: 240,
            left: 730,
            radius: 10,
            fill: '#009fe3',
            id: 'cirkel2',
            hoverCursor: 'pointer',
            selectable: false,
            imgtop: 200,
            imgleft: 800,
            imgheight: 200,
            imgwidth: 200,
            imgUrl: 'https://i.guim.co.uk/img/media/fe1e34da640c5c56ed16f76ce6f994fa9343d09d/0_174_3408_2046/master/3408.jpg?width=1200&height=900&quality=85&auto=format&fit=crop&s=0d3f33fb6aa6e0154b7713a00454c83d'
          }
        ];
    
        const loadedImages = [];
    
        for (let [idx, props] of hotspots.entries()) {
          let c = new fabric.Circle(props);
          c.class = 'hotspot';
          c.name = 'hotspot-' + idx;
          canvas.add(c);
        }
    
        fabric.Canvas.prototype.getAbsoluteCoords = function(object) {
          return {
            left: object.left + this._offset.left,
            top: object.top + this._offset.top
          };
        }
    
        var cirkel1tooltip = document.getElementById('cirkel1'),
        btnWidth = 40,
        btnHeight = 40;
    
        function positionBtn(obj) {
          var absCoords = canvas.getAbsoluteCoords(obj);
          cirkel1tooltip.style.left = (absCoords.left - btnWidth / 10) + 'px';
          cirkel1tooltip.style.top = (absCoords.top - btnHeight / 10) + 'px';
        }
    
        for (const ho of canvas.getObjects()) {
          // check for 'hotspot' class
          if (ho.class && ho.class === 'hotspot') {
            ho.on('mousedown', () => {
              // check if image was previously loaded
              if (loadedImages.indexOf(ho.name) < 0) {
                // image is not in the array
                // so it needs to be loaded
                // prepare the image properties
                let imgProps = {
                  width: ho.imgwidth,
                  height: ho.imgheight,
                  left: ho.imgleft,
                  top: ho.imgtop,
                  scaleX: .25,
                  scaleY: .25,
                  selectable: false,
                  id: 'img-' + ho.name,
                  hoverCursor: "default"
                };
                var printzelfImg = new Image();
                printzelfImg.onload = function (img) {
                  var printzelf = new fabric.Image(printzelfImg, imgProps);
                  canvas.add(printzelf);
                };
                printzelfImg.src = ho.imgUrl;
                // update the `loadedImages` array
                loadedImages.push(ho.name);
              } else {
                // image was previously loaded
                for (const o of canvas.getObjects()) {
                  // find the correct image on the canvas
                  if (o.id && o.id === 'img-' + ho.name) {
                    // toggle the visible property
                    o.visible = !o.visible;
                    break;
                  }
                }
              }
              positionBtn(ho);
            });
          }
        }
      })();
      $( "#cirkel1" ).each(function( i ) {
        tippy(this, {
          theme: 'blue',
          trigger: 'mouseup',
          allowHTML: true,
          placement: 'right',
          animation: 'scale-subtle',
          interactive: true,
          content: function (reference) {
            return reference.querySelector('.tooltipcontent');
          }
        });
      });
      </script>
    </html>

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 2011-03-08
      • 1970-01-01
      • 2021-06-18
      • 2016-10-27
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多