[英]How to trigger fabric.js “object:modified” event programmatically?
I want to fire the 'object:modified' event programmatically.我想以编程方式触发 'object:modified' 事件。 I already tried with "fire" and "trigger" methods.我已经尝试过“火”和“触发”方法。
var canvas = new fabric.Canvas('c');
canvas.on("object:modified", function (e) {
alert("object modified");
});
var text = new fabric.Text('Text', {
fontFamily: 'Hoefler Text',
left: 10,
top: 10
});
canvas.add(text);
$('.fillText').click(function(){
text.setFill($(this).data('color'));
canvas.renderAll();
text.trigger('modified');
});
$('#moveText').click(function(){
text.setLeft(50);
text.setTop(50);
text.setCoords();
canvas.renderAll();
text.trigger('modified');
});
https://jsfiddle.net/gb4u85q4/ https://jsfiddle.net/gb4u85q4/
You can trigger events using canvas.trigger('<eventname>', options);
您可以使用canvas.trigger('<eventname>', options);
触发事件canvas.trigger('<eventname>', options);
. . fire
is deprecated, so you should probably avoid using that. fire
已被弃用,因此您可能应该避免使用它。
Since you wanted to trigger object:modified
, you can do that in the following way, while passing which object was modified:由于您想触发object:modified
,您可以通过以下方式执行此操作,同时传递已修改的对象:
canvas.trigger('object:modified', {target: text});
I updated your JSFiddle with the solution added to it.我使用添加到其中的解决方案更新了您的JSFiddle 。 :) (note that I changed the alert
to an console.log
because I find alerts annoying, you can view the output of console.log
in the developer tools, which can be opened in for example Google Chrome by pressing F12) :)(注意我把alert
改成了console.log
因为我觉得alerts很烦人,你可以在开发者工具中查看console.log
的输出,比如谷歌浏览器可以按F12打开)
Meanwhile you should use fire
.同时你应该使用fire
。
canvas.fire('object:modified');
In the observabile mixin
observe
,stopObserving
,trigger
are removed.在 observabile mixinobserve
,stopObserving
,trigger
被删除。 Keep usingon
,off
,fire
.继续使用on
、off
、fire
。 Those are shorter and also all our documentation refer to this kind of names.那些更短,而且我们所有的文档都引用了这种名称。
For more details see Version 4 breaking changes .有关更多详细信息,请参阅第 4 版重大更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.