【问题标题】:Is there a way to increase and decrease font-size in fabricjs onclick有没有办法在Fabricjs onclick中增加和减少字体大小
【发布时间】:2019-10-26 19:12:09
【问题描述】:

这些是我的步骤:

  1. 添加虚拟文本
  2. 单击按钮时增大/减小字体大小

我正在使用 Vue、Javascript 和 jQuery。 现在我得到了正确的字体大小,但只有当我添加新文本时。但它应该是动态的。

谢谢帮助

 <input type='button' value='-' class='qtyminus' field='quantity' />
 <input type='text' name='quantity' value='0' class='qty' />
 <input type='button' value='+' class='qtyplus' field='quantity' />

 var currentVal = 12;


      $('.qtyplus').click(function(e){

        e.preventDefault();

        currentVal = parseInt($('input[name=quantity]').val());

        if (!isNaN(currentVal)) {

          $('input[name=quantity]').val(currentVal + 1);
        } else {

          $('input[name=quantity]').val(0);
        }
      });
      $(".qtyminus").click(function(e) {

        e.preventDefault();


        currentVal = parseInt($('input[name=quantity]').val());
        if (!isNaN(currentVal) && currentVal > 0) {

          $('input[name=quantity]').val(currentVal - 1);
        } else {

          $('input[name=quantity]').val(0);
        }
      });

      document.getElementById('buttonFabric').addEventListener("change", function (e) {
        canvas.add(new fabric.IText('Tap and Type', {
          left: 0,
          top:   0,
          fontFamily: 'arial black',
          fill: '#333',
          fontSize: currentVal,

        }));
      });

【问题讨论】:

    标签: vue.js fabricjs font-size


    【解决方案1】:

    知道了

         $('.qtyplus').click(function(e){
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
    
            // Get its current value
            currentVal = parseInt($('input[name=quantity]').val());
            // If is not undefined
            if (!isNaN(currentVal)) {
              // Increment
              $('input[name=quantity]').val(currentVal + 1);
    
              if(currentVal!='')
              {
                var activeObj = canvas.getActiveObject();
                //Check that text is selected
                if(activeObj==undefined)
                {
                  alert('Please select a Text');
                  return false;
                }
                activeObj.set({
                  scaleX:1,
                  scaleY:1,
                  fontSize: currentVal
                });
                canvas.renderAll();
              }
    
            } else {
              // Otherwise put a 0 there
              $('input[name=quantity]').val(0);
            }
          });
          // This button will decrement the value till 0
          $(".qtyminus").click(function(e) {
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
    
            // Get its current value
            currentVal = parseInt($('input[name=quantity]').val());
            // If it isn't undefined or its greater than 0
            if (!isNaN(currentVal) && currentVal > 0) {
              // Decrement one
              $('input[name=quantity]').val(currentVal - 1);
    
              if(currentVal!='')
              {
                var activeObj = canvas.getActiveObject();
                //Check that text is selected
                if(activeObj==undefined)
                {
                  alert('Please select a Text');
                  return false;
                }
                activeObj.set({
                  scaleX:1,
                  scaleY:1,
                  fontSize: currentVal
                });
                canvas.renderAll();
              }
    
            } else {
              // Otherwise put a 0 there
              $('input[name=quantity]').val(0);
            }
          });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 2015-06-11
      • 2021-03-13
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多