Fabric.js支持文本更改事件

chr*_*o25 2 javascript fabricjs web

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

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

它们仅在移动对象时起作用.无论如何我们可以在Itext的文本发生变化时执行某些操作吗?那就是......使用1.4.0中提供的新功能,即直接文本编辑.非常感谢你.

Xen*_*yal 5

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

canvas.on('text:changed', function(e) {
    console.log('text:changed', e.target, e);
});
Run Code Online (Sandbox Code Playgroud)

或者这对IText对象的更改:

object.on('changed', function(e) {
    console.log('changed', object, e);
});
Run Code Online (Sandbox Code Playgroud)


chr*_*o25 5

对于任何寻找答案的人,在 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
});
Run Code Online (Sandbox Code Playgroud)

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

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