I recentemente ho creato uno strumento di mind mapping usando fabric.js e ho riscontrato lo stesso problema.
Per ottenere ciò che hai descritto (cambiando il testo su e dopo la creazione di elementi testuali nell'area di disegno), ho usato jquery per rilevare l'evento keydown. Supponendo di aver selezionato l'elemento testuale desiderato nell'area di tessuto, il seguente frammento cambierà il testo.
$(document).keydown(function(e){
var keyPressed = String.fromCharCode(e.which);
var text = canvas.getActiveObject();
if (text)
{
var newText = '';
var stillTyping = true;
if (e.which == 27) //esc
{
if (!text.originalText) return; //if there is no original text, there is nothing to undo
newText = text.originalText;
stillTyping = false;
}
//if the user wants to make a correction
else
{
//Store the original text before beginning to type
if (!text.originalText)
{
text.originalText = text.text;
}
//if the user wants to remove all text, or the element entirely
if (e.which == 46) //delete
{
activeObject.element.remove(true);
return;
}
else if (e.which == 16) { //shift
newText = text.text;
}
else if (e.which == 8) //backspace
{
e.preventDefault();
newText = text.text.substr(0, text.text.length - 1);
}
else if (e.which == 13) //enter
{
//canvas clear selection
canvas.discardActiveObject();
canvas.renderAll();
canvasBeforeSelectionCleared({ memo: { target: text} });
newText = text.text;
stillTyping = false;
}
//if the user is typing alphanumeric characters
else if (
(e.which > 64 && e.which < 91) || //A-Z
(e.which > 47 && e.which < 58) || //0-9
(e.which == 32) || //Space
(keyPressed.match(/[!&()"'?-]/)) //Accepted special characters
)
{
if (text.text == text.originalText) text.text = '';
if (keyPressed.match(/[A-Z]/) && !e.shiftKey)
keyPressed = keyPressed.toLowerCase();
newText = text.text + keyPressed;
}
}
text.set({ text: newText }); //Change the text
canvas.renderAll(); //Update the canvas
if (!stillTyping)
{
this.text.originalText = null;
}
}
});
Utilizzando questa tecnica, posso selezionare un elemento di testo nella tela tessuto, e iniziare a digitare il testo viene sostituito. Puoi cambiarlo in modo che non cancelli il testo ogni volta che selezioni l'elemento.
Ci sono alcuni compromessi con questo metodo. Ad esempio, non è possibile selezionare il testo come se fosse in un normale elemento di testo di input HTML e non vi è alcun cursore lampeggiante, pertanto il cursore "virtuale" si trova sempre alla fine del testo.
Se si voleva davvero si potrebbe disegnare un cursore lampeggiante alla fine del testo.
cosa intendi per campo di testo interattivo? – hjpotter92
@TheJumpingFrog: Significa aggiungere testo alla tela e modificarne il contenuto in seguito. – Marcel
Esattamente quello che Marcel ha detto – Kappei