Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding padding and border radius in background text in Fabricjs

Is there any chance to add padding and border radius on Fabric JS background text i want to look like call to action button

enter image description here

JS

canvas = new fabric.Canvas('mainCanvas',
    backgroundColor: 'green'
)


text = new fabric.IText('hello world', 
    left: 200
    top: 100
    backgroundColor: "pink",
)
canvas.add(text)

HTML

<canvas id="mainCanvas" style="border:1px solid #aaa" width="600" height="300"></canvas> 

JSFIDDLE

like image 603
Edin Puzic Avatar asked Feb 08 '23 13:02

Edin Puzic


1 Answers

I solved the problem by simply grouping the elements and in fabric.Rect I added rx: 5 and ry: 5 for border radius.

There is all the code JSFIDDLE

var canvas = window._canvas = new fabric.Canvas('c1');




var bg = new fabric.Rect({
  fill: '#32b775',
  scaleY: 0.5,
  originX: 'center',
  originY: 'center',
  rx: 5, 
  ry: 5,
  width: 90,
  height:80
});

var text = new fabric.Text('Done', {
  fontSize: 18,
  originX: 'center',
  originY: 'center',
  fill: '#FFF'
});

var group = new fabric.Group([ bg, text ], {
  left: 50,
  top: 100
});

canvas.add(group);
canvas {
    border: 1px solid #999;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>

<canvas id="c1" width="300" height="300"></canvas>
like image 169
Edin Puzic Avatar answered Feb 11 '23 01:02

Edin Puzic