admin管理员组文章数量:1419633
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 badges1 Answer
Reset to default 4Not 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();
本文标签: javascriptfabricjsHow to set text widthStack Overflow
版权声明:本文标题:javascript - fabric.js - How to set text width? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745311918a2652972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论