簡體   English   中英

如何在fabric.js中獲取目標元素的屬性值

[英]How to get targeted element's attribute value in fabric.js

(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()

您可能需要先進行類型檢查(並且您已經知道如何按照現有代碼進行檢查。)

希望那是您想要的!

您也可以嘗試以下方法:

 //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

希望有幫助,祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM