Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fabric.js : How to make an image in the shape of a circle with white borders

I am using Fabric.js to create some animations on HTML canvas. I need to make this :

enter image description here

If I use image element I cannot make it circular and not put white border.
If I use circle object I cannot put an image in it.

Any ideas will be appreciated. TIA

like image 885
Abhinav Avatar asked May 13 '15 14:05

Abhinav


1 Answers

Try someting like this with clipTo function:

fabric.Image.fromURL(imageURL, function(oImg) {
    oImg.scale(1.0).set({
        left: 10,
        top: 10,
        stroke : 'white',
        strokeWidth : 100,
        clipTo: function (ctx) {
            ctx.arc(0, 0, radius, 0, Math.PI * 2, true);
          }
    });
    canvas.add(oImg).setActiveObject(oImg);
    canvas.renderAll();
});

Fiddle: http://jsfiddle.net/q6Y6k/8/

like image 96
ptCoder Avatar answered Nov 15 '22 07:11

ptCoder