Search code examples
javascriptfabricjs

Rounded control-knobs in fabric.js


Consider this example:

 var canvas = new fabric.Canvas('c');
  canvas.add(new fabric.Circle({ radius: 30, fill: '#f55', top: 100, left: 100 }));

  canvas.item(0).set({
    borderColor: 'red',
    cornerColor: 'green',
    cornerSize: 10,
    cornerRadius: 10,
    transparentCorners: false
  });
  canvas.setActiveObject(canvas.item(0));
  this.__canvases.push(canvas);

The fiddle is on http://jsfiddle.net/SsCh6/

Is it possible to make the control knobs circular? Setting the cornerRadius doesn't work. And by the way, why is the color of the controls changing when moving the object? How can that be adjusted?


Solution

  • the controls have an opacity when moving which by default is set to 0.4. To prevent that you can do this:

    canvas.item(0).set({
     borderOpacityWhenMoving: 1
    });
    

    As far as I know the control knobs cannot be changed. You'd have to change the function that actually draws the controls. This is done in the function drawControls which either uses strokeRect or fillRect depending on the settings to draw the controls. You should be able to change the function to draw a circle.

    hope this helps.