How to add a text box popup (Jquery tooltip or similar) to a Fabric JS image within a canvas?

前端 未结 2 1431
猫巷女王i
猫巷女王i 2021-02-10 01:29

I\'m working on a Fabric JS project to map a floor with its rooms\' locations.
At each room location I added an icon. I want to have a text box pop up (such as jquery toolt

相关标签:
2条回答
  • 2021-02-10 01:34

    Step 1: Set up your watchers

    Step 2: Load the dialog

    Step 3: Figure out where the bounding rect is on the page and move the dialog.

    canvas.observe('mouse:over', function (e) {
        console.log("Everyday I'm hovering");
        showImageTools(e.target);
    });
    canvas.observe('mouse:out', function (e) {
        $('#imageDialog').remove();
    });
    function showImageTools (e) {
        var url = 'dialog/imageDialog.htm';
        $.get(url, function(data) {
        // Don't add it twice
        if (!$('#imageDialog').length) {
            $(body).append(data);
        }
        moveImageTools();
    });
    
    function moveImageTools () {
        var w = $('#imageDialog').width();
        var h = $('#imageDialog').height();
        var e = canvas.getActiveObject();
        var coords = getObjPosition(e);
        // -1 because we want to be inside the selection body
        var top = coords.bottom - h - 1;
        var left = coords.right - w - 1;
        $('#imageDialog').show();
        $('#imageDialog').css({top: top, left: left});
    }
    function getObjPosition (e) {
        // Get dimensions of object
        var rect = e.getBoundingRect();
        // We have the bounding box for rect... Now to get the canvas position
        var offset = canvas.calcOffset();
        // Do the math - offset is from $(body)
        var bottom = offset._offset.top + rect.top + rect.height;
        var right = offset._offset.left + rect.left + rect.width;
        var left = offset._offset.left + rect.left;
        var top = offset._offset.top + rect.top;
        return {left: left, top: top, right: right, bottom: bottom};
    }
    

    That should be enough to get you started. Let me know if any of this doesn't make sense.

    0 讨论(0)
  • 2021-02-10 01:55

    Add span element below the canvas

    <span ref="toolTip" class="toolTip">ToolTip</span>
    
    

    Add style for span element NB: Visibility is hidden by default

    .toolTip{
      position: absolute;
      z-index: 1;
      background: rgb(119, 128, 0);
      height: 30px;
      width: 120px;
      padding: 8px;
      font-size: 13px;
      color: #fff;
      visibility: hidden;
    }
    

    Add mouse over and mouse out events

     this.$data.canvas.on('mouse:over', function (e) {
            // console.log(e.e.offsetX)
            if (e.target && e.target.feature === 'Seat') {
              self.$refs.toolTip.innerHTML =
                'Seat: ' + e.target.label + '  Row: ' + e.target.rowLabel
              self.$refs.toolTip.style.visibility = 'visible'
              self.$refs.toolTip.style.top = e.e.offsetY + 'px'
              self.$refs.toolTip.style.left = e.e.offsetX + 'px'
            }
          })
          this.$data.canvas.on('mouse:out', function (e) {
            self.$refs.toolTip.style.visibility = 'hidden'
          })
    
    0 讨论(0)
提交回复
热议问题