fabricJS文本,每行都有统一的线高

时间:2017-07-28 19:45:25

标签: html5 html5-canvas fabricjs

我希望文本行完全具有相同的lineHeights,但第一行没有行高

请检查这个小提琴 https://jsfiddle.net/fahadnabbasi/zukc6pvc/1/

var canvas = new fabric.Canvas('c');

var text_temp = new fabric.IText(" One Two Three \ nFour Five Six \ nSeven Eight Nine",{             左:20,             上:20,             fontSize:48,

        fill: "#000000",
        lineHeight : 2
    });

    canvas.add(text_temp)

我想要这样的附加图像

enter image description here

1 个答案:

答案 0 :(得分:0)



var canvas = new fabric.Canvas('c');

// override this to get always full line height
fabric.Text.prototype.calcTextHeight = function() {
      var lineHeight, height = 0;
      for (var i = 0, len = this._textLines.length; i < len; i++) {
        lineHeight = this.getHeightOfLine(i);
        height += lineHeight;
      }
      return height;
    };
    
fabric.Text.prototype._getTopOffset = function() {
  return -this.height / 2 + this.getHeightOfLine(0) / this.lineHeight;
};

var text_temp = new fabric.IText("One Two Three\nFour Five Six\nSeven Eight Nine", {
            left: 20,
            top: 20,
            fontSize: 48,
            textBackgroundColor: 'red',
            backgroundColor: 'yellow',
            fill: "#000000",
            lineHeight : 2
        });
        
        canvas.add(text_temp)
        
        
&#13;
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="600" height="600"></canvas>
&#13;
&#13;
&#13;

我不确定这是一个完整的解决方案,也不想更深入地修改库的核心行为。此代码段可以帮助您,但在更高级的用例中也会失败。