将事件绑定到 fabricJS 中的对象

Bind events to object in fabricJS

使用最新的 Fabric.js,我有代码:

var imageAdded = new Image();
imageAdded.onload = function (img) {
    var imgAdded = new fabric.Image(imageAdded, {
        clipName: picID,
        clipTo: function (ctx) {
            return _.bind(clipByName, imgAdded)(ctx)
        }
    });
    canvas.add(imgAdded);

    imgAdded.on("object:selected", function (e) { // It doesn't pass this function
        alert(e.target.clipName + " is selected");
        e.target.clipTo = null;
        canvas.renderAll();
    });
};

我感谢每一个建议。谢谢!

如果您想要针对特定​​对象的事件,请使用:

imgAdded.on("selected", function(){alert(this.clipName);});

如果想要 canvas 和所有对象的事件:

canvas.on("object:selected", function(e){alert(e.target.clipName);});