[英]fabric.js assign event listener to an object for multiple events
I am asking this mainly out of curiosity as it is possible to replicate code but being deep in rails at the minute, repetition makes me feel dirty and I can't find it in the docs. 我主要是出于好奇而问这个问题,因为有可能复制代码但是在一分钟内深陷铁轨,重复让我觉得很脏,我在文档中找不到它。
Suppose I have a rect = fabric.Rect
and I wanted to add a listener for say, moving and modified that do the same thing: 假设我有一个rect = fabric.Rect
,我想添加一个监听器来说,移动和修改它们做同样的事情:
rect.on('moving', function() {
console.log('moving or modified');
});
rect.on('modified', function() {
console.log('moving or modified');
});
is it possible to combine these in some way? 是否有可能以某种方式结合这些?
I'm very new to JS and so this could be a simple thing in JS that I've not come across yet but again, it's not mentioned in Fabric docs that I've seen. 我是JS的新手,所以这在JS中可能是一个简单的事情,我还没有遇到过,但是在我看过的Fabric文档中没有提到它。
I'm a little late, but just in case anyone else has this problem - you just need to move your common code into a function, and then pass key/value pairs to the on() function: 我有点迟了,但万一其他人有这个问题 - 你只需要将你的公共代码移动到一个函数中,然后将键/值对传递给on()函数:
function doLog() {
console.log('moving or modified');
}
rect.on({
'moving': doLog,
'modified': doLog
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.