javascript - fabric.js - How to set text width? - Stack Overflow

I find that setting textAlign to a fabric.js Text object is useless because the width of the textfield

I find that setting textAlign to a fabric.js Text object is useless because the width of the textfield is automatically assigned to perfectly fit the width of the text.

I can't find anything in the API that lets me set another width. I tried scaleToWidth, but it didn't to anything either.

this.text = new fabric.Text(this.name, 
{
        fontFamily: 'Calibri Light',
        fontSize: 17,
        backgroundColor: 'blue', // using this to confirm that the width is actually just set to the text width
        textAlign: 'center',
        top: 15,
        scaleToWidth: 1.1,
})

I've made it appear in the desired way using a pretty hacky solution.

Quoting from the fabric.js website:

textAlign

Text align es useful when working with multiline text. With one-line text, the width of bounding box is always exactly matching that line's width, so there's nothing to align. Allowed values are "left", "center", and "right".

Does anyone know how to get around this?

I find that setting textAlign to a fabric.js Text object is useless because the width of the textfield is automatically assigned to perfectly fit the width of the text.

I can't find anything in the API that lets me set another width. I tried scaleToWidth, but it didn't to anything either.

this.text = new fabric.Text(this.name, 
{
        fontFamily: 'Calibri Light',
        fontSize: 17,
        backgroundColor: 'blue', // using this to confirm that the width is actually just set to the text width
        textAlign: 'center',
        top: 15,
        scaleToWidth: 1.1,
})

I've made it appear in the desired way using a pretty hacky solution.

Quoting from the fabric.js website:

textAlign

Text align es useful when working with multiline text. With one-line text, the width of bounding box is always exactly matching that line's width, so there's nothing to align. Allowed values are "left", "center", and "right".

Does anyone know how to get around this?

Share Improve this question edited Aug 16, 2015 at 8:44 user3262713 asked Jul 15, 2015 at 11:01 user3262713user3262713 3799 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Not sure if you ever found an answer in the two years from when you first asked but I had the same problem and was able to find a solve by adding the text object to the canvas then setting it's width and then firing a renderAll() on the canvas.

var text = new fabric.Text('sample text', {fill: '#fff', textAlign: 'center'});
canvas.add(text);
text.width = 180;
canvas.renderAll();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745311918a4622037.html

相关推荐

  • javascript - fabric.js - How to set text width? - Stack Overflow

    I find that setting textAlign to a fabric.js Text object is useless because the width of the textfield

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信