How to get itext styles in fabric.js ver. 2?

In previous versions of fabric.js I used the getSelectionStyles

way it was used in a function getStyle

with great success.

In this case: when text is selected, the text fontsize-slider

will change to the current value for the first selected character.

But after upgrading to version 2+, it doesn't work anymore. The function getStyle

returns undefined

.

canvas.on('text:selection:changed', onSelectionChanged);

function onSelectionChanged() {
  var obj = canvas.getActiveObject();
  if (obj.selectionStart>-1) {
    $('#fontSize').val(getStyle(obj,'fontSize'));
  }
}

function getStyle(object, styleName) {
  return (object.getSelectionStyles && object.isEditing)
    ? object.getSelectionStyles()[styleName] : object[styleName];
}

      

Here is a working example using fabric.js version 1.7.22. If you change it to the current version, it won't work. How to use getSelectionStyles

in version 2? What changed?

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Fontsize test'))
canvas.on('text:selection:changed', onSelectionChanged);

$( "#fontSize" ).on("input", function() {
    var obj = canvas.getActiveObject();
    if (obj) {
    setStyle(obj, 'fontSize', $( "#fontSize" ).val());
        canvas.renderAll();
    }
});
    
function setStyle(object, styleName, value) {
    if (object.setSelectionStyles && object.isEditing) {
        var style = { };
        style[styleName] = value;
        object.setSelectionStyles(style);
    }
    else {
        object[styleName] = value;

    }
}

function onSelectionChanged() {
    var obj = canvas.getActiveObject();
    if (obj.selectionStart>-1) {
           $('#fontSize').val(getStyle(obj,'fontSize'));
    }
}

function getStyle(object, styleName) {
    return (object.getSelectionStyles && object.isEditing)
    ? object.getSelectionStyles()[styleName] : object[styleName];
}
      

canvas{
 border:2px solid #000;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>-->
<canvas id='c' width=400 height=120></canvas>
<label for="fontSize" hint="Font size"><a style="font-size:12px;color:#999">T</a><a style="font-size:19px;color:#ccc">T</a></label>
<input type="range" value="" min="1" max="120" step="1" id="fontSize">
      

Run codeHide result


0


source to share


1 answer


getSelectionStyles returns an array of style objects for the current selection.

So it object.getSelectionStyles()[styleName]

always will be undefined

.

DEMO

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Hello'))
canvas.on('text:selection:changed', onSelectionChanged);

function onSelectionChanged() {
  var obj = canvas.getActiveObject();
  if (obj.selectionStart>-1) {
    console.log(getStyle(obj,'fontSize'));
  }
}

function getStyle(object, styleName) {
  console.log(object.getSelectionStyles())
  return object[styleName];
}
      

canvas{
 border:2px solid #000;
}
      

<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id='c' width=400 height=400></canvas>
      

Run codeHide result




You need to use getStyleAtPosition(position)

to get style at that particular position.

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Fontsize test'))
canvas.on('text:selection:changed', onSelectionChanged);

$("#fontSize").on("input", function() {
  var obj = canvas.getActiveObject();
  if (obj) {
    setStyle(obj, 'fontSize', $("#fontSize").val());
    canvas.renderAll();
  }
});

function setStyle(object, styleName, value) {
  if (object.setSelectionStyles && object.isEditing) {
    var style = {};
    style[styleName] = value;
    object.setSelectionStyles(style);
  } else {
    object[styleName] = value;

  }
}

function onSelectionChanged() {
  var obj = canvas.getActiveObject();
  if (obj.selectionStart > -1) {
     console.log(obj.getSelectionStyles())
    $('#fontSize').val(getStyle(obj, 'fontSize'));
  }
}

function getStyle(object, styleName) {
  return (object.getStyleAtPosition && object.isEditing) ?
    object.getStyleAtPosition(object.selectionStart)[styleName] : object[styleName];
}
      

canvas{
 border:2px solid #000;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>-->
<canvas id='c' width=400 height=120></canvas>
<label for="fontSize" hint="Font size"><a style="font-size:12px;color:#999">T</a><a style="font-size:19px;color:#ccc">T</a></label>
<input type="range" value="" min="1" max="120" step="1" id="fontSize">
      

Run codeHide result


+1


source







All Articles