2016-02-14 86 views
1
(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中獲取目標元素的屬性值。 這裏我得到了目標元素的類型,但是我想要該圖像元素的屬性值(圖像源)來標識在此畫布中選擇的圖像元素。如何在fabric.js中獲取目標元素的屬性值

回答

0

這裏是解決一個小提琴:https://jsfiddle.net/jimedelstein/yb9yf0vr/

直接讓e.target.getSrc()

您可能需要首先做一個類型檢查(和你已經知道該怎麼做每現有的代碼

。 !

希望這就是你要找的

0

,你可以也試試這個:

//instead of e.target you can get the selected object with findTarget() 
var targetObj= this.findTarget(); 

//now that we have the selected object you can get the src property of the _element property. 


    console.log(targetObj._element.src);//1st way 
    console.log(targetObj._element.currentSrc);//2nd way 
    console.log(targetObj.getSrc());//3rd way 

希望有所幫助,祝你好運。