2014-10-26 83 views
0

以下是顯示問題的JSFiddle。 在我的應用程序中,將svg對象添加到畫布後,每次點擊它都會改變它的比例。 請注意,每點擊一次,您可以點擊對象的區域向左上移,直到無法點擊它爲止。 重現此問題:只需單擊對象幾次即可。最多9次點擊後,您將無法再點擊它。使用FabricJS縮放svg對象時,不可能選擇它

這裏是我的代碼:

Fcanvas = new fabric.Canvas('c'); 
var svgStr = '<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="16.386984" height="13.381006" id="svg2985" style="overflow:visible"> <metadata  id="metadata2993"> <rdf:RDF>  <cc:Work   rdf:about="">  <dc:format>image/svg+xml</dc:format>  <dc:type   rdf:resource="http://purl.org/dc/dcmitype/StillImage" />  <dc:title></dc:title>  </cc:Work> </rdf:RDF> </metadata> <defs  id="defs2991" /> <path  d="m 9.0444374,10.298006 c 0,0 2.2872386,3.083 0,3.083 -2.2892466,0 0,-3.083 0,-3.083 z M 5.0224919,9.9230051 c 0,0 2.1476202,2.8959999 0,2.8959999 -2.1476125,0 0,-2.8959999 0,-2.8959999 z m 7.8539491,-9e-7 c 0,0 1.91599,2.5829998 0,2.5829998 -1.917995,0 0,-2.5829998 0,-2.5829998 z M 9.8278236,1.1490701e-5 C 9.9712086,-3.2632367e-4 10.119073,0.00677886 10.271509,0.02205994 13.52348,0.3470585 13.653477,2.2780447 13.328483,3.5790343 c 3.414971,-0.4879956 4.876958,4.796968 -0.487996,4.796968 l -6.9919448,0 c -5.60194948,0 -6.6599391,-1.2459898 -5.3359528,-3.8959732 1.3279914,-2.6549802 5.3359528,-1.3889904 5.3359528,-1.3889904 0,0 1.0637803,-3.08415723 3.9792814,-3.091027209299 z"  id="path2987"  style="fill:#00adee" /></svg>' 

fabric.loadSVGFromString(svgStr, function (objects, options) { 
    var obj = fabric.util.groupSVGElements(objects, options); 
    obj.setFill('red'); 
    obj.scaleX=3; 
    obj.scaleY=3; 
    obj.left = 200 
    obj.top = 50 
    obj.selectable = false; 
    obj.perPixelTargetFind = true 
    Fcanvas.add(obj).renderAll();     
}); 

Fcanvas.on('mouse:up', function (e) { 
    //alert('fabric mouse click '+e.target); 
    console.log('fabric mouse click '+e.target); 
    if (e.target) { 
     if (e.target.opacity==.5) { 
      e.target.opacity = 1 
      e.target.scaleX = e.target.scaleX+1 
      e.target.scaleY = e.target.scaleY+1 
      console.log('set opacity to 1'); 
     }else { 
      e.target.opacity = .5 
      e.target.scaleX = e.target.scaleX+1 
      e.target.scaleY = e.target.scaleY+1 
      console.log('set opacity to .5'); 
     } 
     Fcanvas.renderAll(); 
    } 
}); 
Fcanvas.on('mouse:over', function (e) { 
    alert(e.target); 
     e.target.opacity = .5; 
     //e.target.setFill('black'); 
     Fcanvas.renderAll(); 
}); 
Fcanvas.on('mouse:out', function (e) { 
     e.target.opacity = 1; 
     //e.target.setFill('black'); 
     Fcanvas.renderAll(); 
}); 

這裏是的jsfiddle: http://jsfiddle.net/orihadar/rtL0m87h/2/

另一個問題是,鼠標:在&的功能不起作用。

有人可以添加到JSFiddle FabricJS(1.412)的最新版本?

回答

1

看看這個小提琴:http://jsfiddle.net/rtL0m87h/3/

您還沒有設定目標,這就是爲什麼它經過一番點擊不工作的COORDS。現在它在我的小提琴裏工作得很完美。您必須通過執行來設置對象的Coords:e.target.setCoords()

確保在以編程方式修改任何對象時,修改對象完成後需要設置對象。

+0

如果對您有幫助,請選出答案 – Innodel 2014-10-29 04:09:44