Fabric.js: Is there a way to use padding in fabric.Textbox, as currently text goes in-line with background.

Created on 22 Feb 2017  路  2Comments  路  Source: fabricjs/fabric.js

Version

1.7.6

Test Case

http://jsfiddle.net/fabricjs/Da7SP/

Steps to reproduce

Expected Behavior

Actual Behavior

will be closed not adequate code sample

Most helpful comment

Here is a solution that I implemented -

var TextboxWithPadding = fabric.util.createClass(fabric.Textbox, {
  _renderBackground: function(ctx) {
    if (!this.backgroundColor) {
      return;
    }
    var dim = this._getNonTransformedDimensions();
    ctx.fillStyle = this.backgroundColor;

    ctx.fillRect(
      -dim.x / 2 - this.padding,
      -dim.y / 2 - this.padding,
      dim.x + this.padding * 2,
      dim.y + this.padding * 2
    );
    // if there is background color no other shadows
    // should be casted
    this._removeShadow(ctx);
  }
});

this.left and this.top will also have to be taken into account.
But with new fabric updates, this method will have to be synced with that of fabric.object.

All 2 comments

Here is a solution that I implemented -

var TextboxWithPadding = fabric.util.createClass(fabric.Textbox, {
  _renderBackground: function(ctx) {
    if (!this.backgroundColor) {
      return;
    }
    var dim = this._getNonTransformedDimensions();
    ctx.fillStyle = this.backgroundColor;

    ctx.fillRect(
      -dim.x / 2 - this.padding,
      -dim.y / 2 - this.padding,
      dim.x + this.padding * 2,
      dim.y + this.padding * 2
    );
    // if there is background color no other shadows
    // should be casted
    this._removeShadow(ctx);
  }
});

this.left and this.top will also have to be taken into account.
But with new fabric updates, this method will have to be synced with that of fabric.object.

@brijeshb42 I've been researching on this for a few days... your solution really helps me a lot...!

Thanks so much ... you are the best ...!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amancqlsys picture amancqlsys  路  5Comments

Vivek-KT picture Vivek-KT  路  3Comments

semiadam picture semiadam  路  3Comments

keanass picture keanass  路  5Comments

guettli picture guettli  路  4Comments