javascript - How to edit a FabricJS IText and apply new styles (e.g. highlighting, etc...) -


i want edit highlighted characters in text in canvas using fabric.js, change color, font, style etc.

just http://fabricjs.com/test/misc/itext.html

to @user43250937 hey uhm. tried , works! :d thanks. tried underline, bold, italic, have problem changing color of text, tried :

// "cinput" id of color picker.   addhandler('cinput', function(obj) {     var color =  $("#cinput").val();      var iscolor = (getstyle(obj, 'fill') || '').indexof(color) > -1;       setstyle(obj, 'fill', iscolor ? '' : color);  }); 

usually answers without description of tried , didn't work ignored here, i'm answering time because fabricjs library quite complex , documentation bit lacking sometimes...

that page has samples itext object, text can edited in place (the content of basic fabricjs text object can modified outside via javascript).

building static itext object different styles applied it's simple:

html:

<canvas id="canv" width="250" height="300" style="border: 1px solid rgb(204, 204, 204); width: 400px; height: 400px; -webkit-user-select: none;"></canvas> 

javascript:

var canvas=new fabric.canvas('canv');  var itextsample = new fabric.itext('hello\nworld', {   left: 50,   top: 50,   fontfamily: 'helvetica',   fill: '#333',   lineheight: 1.1,   styles: {     0: {       0: { textdecoration: 'underline', fontsize: 80 },       1: { textbackgroundcolor: 'red' }     },     1: {       0: { textbackgroundcolor: 'rgba(0,255,0,0.5)' },       4: { fontsize: 20 }     }   } });  canvas.add(itextsample); 

link jsfiddle

as can see specify style every character of each each line (first hello line, world line).

if want dynamic ability select text , change appearance/style there work do, you'll need to:

  1. add button or clickable element each style (bold,italic,change color, change background,etc...) want apply dynamically;
  2. add click listener each button code changes style of selected text inside itext.

you'll need basic function add handlers reuse every style button:

function addhandler(id, fn, eventname) {   document.getelementbyid(id)[eventname || 'onclick'] = function() {     var el = this;     if (obj = canvas.getactiveobject()) {       fn.call(el, obj);       canvas.renderall();     }   }; } 

and helper functions change styles:

function setstyle(object, stylename, value) {   if (object.setselectionstyles && object.isediting) {     var style = { };     style[stylename] = value;     object.setselectionstyles(style);   }   else {     object[stylename] = value;   } }  function getstyle(object, stylename) {   return (object.getselectionstyles && object.isediting)     ? object.getselectionstyles()[stylename]     : object[stylename]; }   addhandler('underline', function(obj) {   var isunderline = (getstyle(obj, 'textdecoration') || '').indexof('underline') > -1;   setstyle(obj, 'textdecoration', isunderline ? '' : 'underline'); }); 

link working jsfiddle working underline button.

a bit of coding involved can see, it's not complex, full list of available style options can check fabricjs documentation.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -