繁体   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