(function() {
var canvas = this.__canvas = new fabric.Canvas('c');
fabric.Object.prototype.transparentCorners = false;
canvas.on({
'object:selected': function(e) {
e.target.opacity = 0.5;
//find which object is selected
console.log(e.target);
console.log(e.target.get('type'));
//How to get targeted element attribute value
//??????????????
},
'object:modified': function(e) {
e.target.opacity = 1;
}
});
fabric.Image.fromURL('img/shoes-hills.png', function(oImg1) {
// scale image down, and flip it, before adding it onto canvas
oImg1.perPixelTargetFind = true;
canvas.add(oImg1);
});
})();
如何在fabric.js中获取目标元素的属性值。
在这里,我得到目标元素的类型,但是我想要该图像元素的属性值(图像源),以标识在此画布中选择的图像元素。
最佳答案
这是解决方法的小提琴:https://jsfiddle.net/jimedelstein/yb9yf0vr/
只需调用e.target.getSrc()
您可能要先进行类型检查(并且您已经知道如何按照现有代码进行检查。)
希望那是您想要的!