简体   繁体   中英

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

How to get targeted element's attribute value in fabric.js. Here I get type of target element but I want attribute value(source of image) for that image element for identify which image element selected in this canvas.

Here is a fiddle with the solution: https://jsfiddle.net/jimedelstein/yb9yf0vr/

Simply call e.target.getSrc()

You may want to do a type check first (and you already know how to do that per your existing code.

Hope that is what you were looking for!

you can as well try this :

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

Hope helps, good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM