Reputation: 297
I want to know is there a way to change the bounding box icon, I read source code in fabric.js, it generates square box for bounding box, but I want to change it to circle or change to my custom appearance. could you advise me?
Upvotes: 7
Views: 5763
Reputation: 101
This works for the latest version of fabricjs (4.4 as of now).
You have an example online, http://fabricjs.com/controls-customization, that allows you to perform basic customization on the controls globally.
Its as simple as:
fabric.Object.prototype.borderColor = 'blue';
to change border color to blue for example.
Another demo, that is dependent on fabricjs 4, http://fabricjs.com/custom-control-render, has examples that allows you to add custom controls. Just leaving this here for anyone interested.
Upvotes: 1
Reputation: 14731
Fastest way to customize controls is to write your own _drawControl
function and make it compatible with fabricjs standard to override it.
Remember that this function is called 9 times for every render, so try to stay low with code and drawings. Also if you modify the context ( ctx ) remember to use .save
and .restore
to do not mess up rendering pipeline.
FabricJs will call the function with top
and left
ready for a rectangle, so the corner will be at top + size/2
and left + size/2
, where size is this.cornerSize
function newControls(control, ctx, methodName, left, top) {
if (!this.isControlVisible(control)) {
return;
}
var size = this.cornerSize;
isVML() || this.transparentCorners || ctx.clearRect(left, top, size, size);
ctx.arc(left + size/2, top + size/2, size/2, 0, 2 * Math.PI, false);
ctx.stroke();
}
fabric.Object.prototype._drawControl = newControls;
function newControls(control, ctx, methodName, left, top) {
if (!this.isControlVisible(control)) {
return;
}
var size = this.cornerSize;
this.transparentCorners || ctx.clearRect(left, top, size, size);
ctx.beginPath();
ctx.arc(left + size/2, top + size/2, size/2, 0, 2 * Math.PI, false);
ctx.stroke();
}
fabric.Object.prototype._drawControl = newControls;
fabric.Object.prototype.cornerSize = 15;
var canvas = new fabric.Canvas('c');
canvas.add(new fabric.Rect({width:100, height:100, top:50, left:50}));
canvas.setActiveObject(canvas.getObjects()[0]);
<script type ="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="c" width="400" height="400" style="border:1px solid #ccc"></canvas>
Upvotes: 9
Reputation: 2247
Check these examples:
http://fabricjs.com/customization/
Example 2:
var canvas = new fabric.Canvas('c3');
canvas.add(new fabric.Circle({ radius: 30, fill: '#f55', top: 100, left: 100 }));
canvas.item(0).set({
borderColor: 'red',
cornerColor: 'green',
cornerSize: 6,
transparentCorners: false
});
canvas.setActiveObject(canvas.item(0));
Upvotes: 4