Fabricjs Textbox make the text shrink to fit

我是研究僧i 提交于 2019-12-20 10:35:04

问题


I would define a text box (single-line)

By default, with a character size of 16 (for example)

When the text gets bigger than the text box, I do not want it to wrap or the text box gets bigger, I want the text size to fit the maximum size of the text box. text. When the text returns to a smaller size, it can resume its initial size (in our example 16). Possibly manage a minimum size

If you have an idea, I take :)

thanks in advance

Test Case : http://jsfiddle.net/Da7SP/273/

// initialize fabric canvas and assign to global windows object for debug
var canvas = window._canvas = new fabric.Canvas('c');

// ADD YOUR CODE HERE
var canvas = window._canvas = new fabric.Canvas('c');
var t1 = new fabric.Textbox('My Text', {
    width: 200,
    top: 5,
    left: 5,
    fontSize: 16,
    textAlign: 'center'
});

var t2 = new fabric.Textbox('My text is longer, but I do not want the box to grow, or the text to wrap. I only want the text to fit the available size', {
    width: 200,
    height: 200,
    top: 250,
    left: 5,
    fontSize: 16,
    textAlign: 'center'
});



canvas.add(t1);
canvas.add(t2);

A small video to explain what I want :

When the text gets bigger than the text box, I want the text size fit the maximum size of the text box.


回答1:


This is a basic fiddle that can replicate your idea. The point is that you have an event that fires on every text change and that can be used to do something before the textbox is rendered.

In this case i m shrinking font size based on a non standard parameter i added to textbox called fixedWidth

// ADD YOUR CODE HERE
var canvas = new fabric.Canvas('c');
var t1 = new fabric.Textbox('MyText', {
    width: 150,
    top: 5,
    left: 5,
    fontSize: 16,
    textAlign: 'center',
    fixedWidth: 150
});

canvas.on('text:changed', function(opt) {
  var t1 = opt.target;
  if (t1.width > t1.fixedWidth) {
    t1.fontSize *= t1.fixedWidth / (t1.width + 1);
    t1.width = t1.fixedWidth;
  }
});

canvas.add(t1);
canvas {
    border: 1px solid #999;
}
<script src="https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="600" height="600"></canvas>



回答2:


neopheus,

Please check this updated fiddle

I defined fabric Text and put there your long text. After that get width of that text and assign that width to the TextBox.

var someText = 'My text is longer, but I do not want the box to grow, or the text to wrap. I only want the text to fit the available size';
var textSize = new fabric.Text(someText,{
                    fontSize: 16
                });
var textBoxWidth = textSize.getWidth();

if (canvas.width < textBoxWidth){
textBoxWidth = canvas.width - 10;
}
var t2 = new fabric.Textbox(someText, {
    width: textBoxWidth,
    height: 200,
    top: 250,
    left: 5,
    fontSize: 16,
    textAlign: 'center'
});


来源:https://stackoverflow.com/questions/41279855/fabricjs-textbox-make-the-text-shrink-to-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!