Fabric js:使用鼠标调整大小时增加字体大小而不是缩放

时间:2015-11-13 12:26:25

标签: javascript html5 html5-canvas fabricjs

我正在研究fabric js应用程序&当我们用鼠标调整字体大小时,我需要增大/减小字体大小 我试过的代码



var canvas = new fabric.Canvas('canvas');
$(document).ready(function() {
$('#text-font-size').keyup(function() {
        var val = $(this).val();
        if (isNaN(val)) {
            alert('please enter number');
            $(this).val('');
        }
        var activeObject = canvas.getActiveObject();
        activeObject.fontSize = val;
        canvas.renderAll();
    });
    $('#add-text-btn').click(function() {
        if ($('#text-font-size').val()) {
            var txtfontsize = $('#text-font-size').val();
        } else {
            var txtfontsize = 40;
        }
        var message = $('#add-text-value').val();
        //var txtfontfamily = $('#font-family').val();
        var new_text = new fabric.IText(message, {
            left: 100,
            top: 100,
            fontSize: txtfontsize,
            //fontFamily: txtfontfamily,
            fill: '#000'
        });
        canvas.add(new_text);
        canvas.setActiveObject(new_text);
    });

    canvas.on('object:selected', function(options) {
        if (options.target) {
            $("textarea#add-text-value").val(options.target.text);
            $("#text-font-size").val(options.target.fontSize);
        }
    });

    canvas.on('object:scaling', function(options) {
        if (options.target) {
            $("textarea#add-text-value").val(options.target.text);
            $("#text-font-size").val(options.target.fontSize);
        }
    });

    canvas.on('object:modified', function(options) {
        if (options.target) {
            $("textarea#add-text-value").val(options.target.text);
            $("#text-font-size").val(options.target.fontSize);
        }
    });

});

<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="add-text-btn">Add text</button><br><br>
<textarea rows="7" id="add-text-value">Your Text Here</textarea>
<br>
<input id="text-font-size" type="text" class="form-control">
<canvas id='canvas' width="500" height="400" style="border:#000 1px solid;"></canvas>
&#13;
&#13;
&#13;

但这只是重新缩放文本而不是增加/减少字体大小,但我必须调整字体大小,如this

2 个答案:

答案 0 :(得分:11)

这个想法是使用canvas.on(&#34; object:modified&#34;)事件将缩放重置为1并增加fontSize以适应新的大小。

fabric.Object.lockUniScaling是建议的补充。

&#13;
&#13;
var canvas = new fabric.Canvas('canvas');
$(document).ready(function() {
  $('#text-font-size').keyup(function() {
    var val = $(this).val();
    if (isNaN(val)) {
      alert('please enter number');
      $(this).val('');
    }
    var activeObject = canvas.getActiveObject();
    activeObject.fontSize = val;
    canvas.renderAll();
  });
  $('#add-text-btn').click(function() {
    if ($('#text-font-size').val()) {
      var txtfontsize = $('#text-font-size').val();
    } else {
      var txtfontsize = 40;
    }
    var message = $('#add-text-value').val();
    //var txtfontfamily = $('#font-family').val();
    var new_text = new fabric.IText(message, {
      left: 100,
      top: 100,
      fontSize: txtfontsize,
      lockUniScaling: true,
      //fontFamily: txtfontfamily,
      fill: '#000'
    });
    canvas.add(new_text);
    canvas.setActiveObject(new_text);
  });

  canvas.on('object:selected', function(options) {
    if (options.target) {
      $("textarea#add-text-value").val(options.target.text);
      $("#text-font-size").val(options.target.fontSize);
    }
  });

  canvas.on('object:scaling', function(event) {
    if (event.target) {
      $("textarea#add-text-value").val(event.target.text);
      $("#text-font-size").val((event.target.fontSize * event.target.scaleX).toFixed(0));
    }
  });

  canvas.on('object:modified', function(event) {
    if (event.target) {
      event.target.fontSize *= event.target.scaleX;
      event.target.fontSize = event.target.fontSize.toFixed(0);
      event.target.scaleX = 1;
      event.target.scaleY = 1;
      event.target._clearCache();
      $("textarea#add-text-value").val(event.target.text);
      $("#text-font-size").val(event.target.fontSize);
    }
  });

});
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="add-text-btn">Add text</button>
<br>
<br>
<textarea rows="7" id="add-text-value">Your Text Here</textarea>
<br>
<input id="text-font-size" placeholder="fontsize" type="number" class="form-control">
<canvas id='canvas' width="500" height="400" style="border:#000 1px solid;"></canvas>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

虽然这可能不是最好的方法(我是 Fabric 的新手),但这对我来说适用于 4.3.0 版:

canvas.on('text:changed', function (e) {
  console.log('Text:', e.target.text, e.target.text.length)
  let objectSelection = canvas.getActiveObject()
  switch (e.target.text.length) {
    case 10:
      console.log(`Text is ${e.target.text.length} characters in length.`)
      objectSelection.set('fontSize', 100)
      break
    case 15:
      console.log(`Text is ${e.target.text.length} characters in length.`)
      objectSelection.set('fontSize', 85)
      break
    case 25:
      console.log(`Text is ${e.target.text.length} characters in length.`)
      objectSelection.set('fontSize', 60)
      break
  }
})
canvas.on('text:editing:exited', function (e) {
  console.log('Text edited!')
})

我选择了 inspiration from another answer,它可以完成这项工作。