Moran Moran - 7 months ago 182
Javascript Question

How to add text to a circle object in fabricJS?

I search here fabricJS-circle , and not found there is a way to add text like number 1 or 2 or 3... Inside a circle on canvas?

This is my circle object on canvas:

function makeStaion(left, top, stationID) {
var c = new fabric.Circle({
left: left,
top: top,
radius: 2,
fill: '#5afffa',
stroke: '#666',
selectable: true,
centeredScaling:true,
padding:2,
hasRotatingPoint: false,
borderColor: 'black',
cornerColor: 'black'

});
c.hasControls = true;
c.station = true;

c.stationID = stationID;
c.stationName = stations[stationID].name;
c.description = stations[stationID].desc;
c.image = stations[stationID].image;

return c;
}

Answer

I think what you're looking for is a fabric group.

Tutorial here: http://fabricjs.com/fabric-intro-part-3#groups

Docs here: http://fabricjs.com/docs/fabric.Group.html

try something like this:

var c = new fabric.Circle({
        left: left,
        top: top,
        radius: 2,
        fill: '#5afffa',
        stroke: '#666',
        selectable: true,
        centeredScaling:true,
        padding:2,
        hasRotatingPoint: false,
        borderColor: 'black',
        cornerColor: 'black'
    });

var t = new fabric.Text(stationID, {
        fontFamily: 'Calibri',
        fontSize: 1.2,
        textAlign: 'center',
        originX: 'center',
        originY: 'center',
        left: LayoutCoordX(STA),
        top: LayoutCoordY(BL-BLOffset)-radius-.4
    });

var g = new fabric.Group([c, t],{
        // any group attributes here
    });


    g.hasControls = true;
    g.station = true;

    g.stationID = stationID;
    g.stationName = stations[stationID].name;
    g.description = stations[stationID].desc;
    g.image = stations[stationID].image;

    return g;
Comments