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

How to get targeted element's attribute value in fabric.js. 如何在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/ 这是解决方案的小提琴: https : //jsfiddle.net/jimedelstein/yb9yf0vr/

Simply call e.target.getSrc() 只需调用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. 希望有帮助,祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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