html5-canvasfabricjsfabricjs2

How to show object bounding box when mouse hover objects in Fabricjs?



I want to show object bounding box when mouse hover over objects like this video, how to do that ?
enter image description here

I'm using canvas.on('mouse:over') with selectedObj.drawBorders function. However, outline box is drawn in not correct position. And I don't know how to clear that outline box when mouse moves out of the object.

Here is my code:

$(function() {
		
		var canvasObject = document.getElementById("editorCanvas");

		// set canvas equal size with div
		$(canvasObject).width($("#canvasContainer").width());
		$(canvasObject).height($("#canvasContainer").height());

		var canvas = new fabric.Canvas('editorCanvas', {
			backgroundColor: 'white',
			selectionLineWidth: 2,
			width: $("#canvasContainer").width(),
			height: $("#canvasContainer").height()
		});
		
		canvas.viewportTransform[4] = 20;
    	canvas.viewportTransform[5] = 40;
		
		canvas.on('mouse:over', function(opts) {
			var selectedObj = opts.target;
			if (selectedObj != null) {
				selectedObj.drawBorders(canvas.getContext())
			}
		});
		
		var text = new fabric.Text('hello world', { left: 50, top: 50 });
		canvas.add(text);
		
		setObjectCoords();
		
		function setObjectCoords() {
		  canvas.forEachObject(function(object) {
			object.setCoords();
		  });
		}
		
	});
<style>
  #canvasContainer {
    width: 100%;
    height: 100vh;
    background-color: gray;
  }
</style>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
  
<div id="canvasContainer">
  <canvas id="editorCanvas"></canvas>
</div>

Please help me resolve this problem!
Thank you!


Solution

  • Use method _renderControls and in styleOverride set hasControls : false to draw only borders.

    DEMO

    $(function() {
      var canvas = new fabric.Canvas('editorCanvas', {
        backgroundColor: 'white',
        selectionLineWidth: 2,
        width: $("#canvasContainer").width(),
        height: $("#canvasContainer").height()
      });
    
      var text = new fabric.IText('hello world', {
        left: 50,
        top: 50
      });
      canvas.add(text);
      canvas
      text.on('mouseover', function() {
        this._renderControls(this.canvas.contextTop, {
          hasControls: false
        })
      })
      text.on('mousedown', function() {
        this.canvas.clearContext(this.canvas.contextTop);
      })
      text.on('mouseout', function() {
        this.canvas.clearContext(this.canvas.contextTop);
      })
    });
    <style>
      #canvasContainer {
        width: 100%;
        height: 100vh;
        background-color: gray;
      }
    </style>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
      
    <div id="canvasContainer">
      <canvas id="editorCanvas"></canvas>
    </div>