Search code examples
javascriptfabricjs

How to get current cursor position when editing fabric.Textbox


How to get current cursor position on text in fabric.Textbox

for example, the following should return = 6

enter image description here


Solution

  • I sort of figured it out. selectionStart and selectionEnd did the trick:

    var activeObj = canvas.getActiveObject();
    var caretPositionStart = activeObj.selectionStart;
    var caretPositionEnd = activeObj.selectionEnd;
    activeObj.enterEditing();
    activeObj.insertChars("HI", null, caretPositionStart, caretPositionEnd);