如何在fabricJS中的圆形对象中添加文本?

时间:2016-04-11 15:09:28

标签: jquery canvas frontend fabricjs web-frontend

我在这里搜索fabricJS-circle,但没有找到添加文字的方法,如数字1或2或3 ...在画布上的圆圈内?

这是我在画布上的圆形对象:

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;
}

1 个答案:

答案 0 :(得分:4)

我认为您正在寻找的是一个结构组。

此处的教程:http://fabricjs.com/fabric-intro-part-3#groups

文档:http://fabricjs.com/docs/fabric.Group.html

尝试这样的事情:

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;