Search code examples
javascriptcanvashtml5-canvasfabricjs

Change border size of the "hasControls" in fabricJs


I would like to further customize the look and feel of the fabricJs hasControls border size. It currently is drawn on the canvas with a 1px border but a 3px border is what I am trying to accomplish.

the fabricJS docs do not have any custom properties to change this border size.


Solution

  • fabricjs objects with hasControls property set to true also have a borderScaleFactor and a borderColor properties.

    color.addEventListener('change', function(){
        canvas.getActiveObject().borderColor = this.value;
        canvas.renderAll();
      });
    scale.addEventListener('change', function(){
        canvas.getActiveObject().borderScaleFactor = this.value;
        canvas.renderAll();
      });
    
    
    var canvas = new fabric.Canvas('canvas');
    fabric.Image.fromURL('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', function(img) {
      img.scale(0.4).set({
        left: 150,
        top: 150,
        angle: -15
      });
      img.borderColor = color.value;
      canvas.add(img).setActiveObject(img);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.js"></script>
    <input id="color" type="color" value="#FF00FF"/>
    <input id="scale" type="range" max="2" min=".01" step=".01"/>
    <canvas id="canvas" width="800" height="600"></canvas>