Fabric.js 中支持的文本更改事件

Text changed event supported in Fabric.js

本文关键字:事件 文本 js 支持 Fabric      更新时间:2023-09-26

我目前正在使用最新版本的Fabric.js即1.4.0。现在可以使用 Itext 对象进行文本编辑。我想在编辑 IText 对象时执行一些东西。到目前为止,我已经找到了modifiedmoving事件,但它们的工作方式与我想要的方式不同。

canvas.on('object:modified', function(e) {
  //do something, maybe count the number of characters
});

它们仅在移动对象时起作用。无论如何,每当Itext的文本发生变化时,我们都可以执行一些东西吗?那是。。。使用 1.4.0 中提供的新功能,即直接文本编辑。谢谢。

对于 IText 更改的文本更改事件,请尝试此操作:

canvas.on('text:changed', function(e) {
    console.log('text:changed', e.target, e);
});

或者对于 IText 对象更改:

object.on('changed', function(e) {
    console.log('changed', object, e);
});

对于任何寻找答案的人,在 Xenyal 的答案中添加更多信息,我发现最新版本的 fabric.js 不支持画布下的text:changed

使用(最新版本)http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js

whatevertext.on("text:changed", function(e) {
    //this works with the latest release build where whatevertext is the name of the object
});

使用(开发版本)https://rawgit.com/kangax/fabric.js/master/dist/fabric.js

canvas.on('text:changed', function(e) {
    //this will only work using the dev build
});