在过去的几周里,我一直在大量使用Fabric.js,但关于文本字段,我只发现可以在创建时设置文本。
是否有可能创建一个交互式文字字段,或者我必须找到解决方法来实现这一点?(通过交互式文本字段,我指的是单击画布上的某个区域并直接在其中编写的区域。)
在过去的几周里,我一直在大量使用Fabric.js,但关于文本字段,我只发现可以在创建时设置文本。
是否有可能创建一个交互式文字字段,或者我必须找到解决方法来实现这一点?(通过交互式文本字段,我指的是单击画布上的某个区域并直接在其中编写的区域。)
fabric.js 的最新版本包含一个 IText 类,它将文本的编辑和选择交互动态地结合在一起。请使用最新版本的 fabric.js 尝试以下代码:
canvas.add(new fabric.IText('Tap and Type', {
fontFamily: 'arial black',
left: 100,
top: 100 ,
}));
http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js
而不是我的本地副本,这解决了我的问题。然而,我仍然不确定为什么自定义构建没有包含IText。 - teewuane$(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;
}
}
});
使用这种技术,我可以在fabric画布中选择文本元素,开始输入,然后该文本将被替换。您可以更改它以使其不会每次选择元素时都清除文本。但这种方法存在一些妥协。例如,您不能像正常的HTML输入文本元素一样选择文本,也没有闪烁的光标,因此“虚拟”光标始终位于文本末尾。如果您真的想要,您可以在文本末尾绘制一个闪烁的光标。 text.set({ text: newText }); //Change the text
canvas.renderAll(); //Update the canvas
这正是我一直在寻找的 :) 非常感谢!
// write text
context.fillText("text1",0,0);
// refresh canvas and write new text
context.clearRect(0,0,canvas.width,canvas.height);
context.fillText("text2",0,0);
试试这个(这是来自我的应用程序):
Text Color: <input id="text-color" type="text" value = "#FF0000" name="textColor" />
textColor.onchange = function() {
canvas.getActiveObject().setColor(this.value);
canvas.renderAll();
};
function updateControls() {
textControl.value = canvas.getActiveObject().getText();
}
canvas.on({
'object:selected': updateControls,
});
布料具有交互式文本对象。
const sample = "lorem ipsum";
const itext = fabric.IText(sample, {left: 0, top: 0});
要编辑文本,请双击文本字段进入编辑模式