简体   繁体   中英

manipulate svg file within a fabric.js canvas

I am trying to manipulat an svg file with fabric.js. I am loading the svg string fabric.loadSVGFromString into the canvas correctly. My question is how can I manipulate some parts of the svg.

For example, I imported an electronic breadbord with lot of holes as shown in the pic below 电子板 .

How could I detect if a hole has been clicked? How could I get the clicked part?

Thank you

I did a very short demo of what you could do. The point is create a svg that can be imported as single shapes, lock the shapes on the canvas, then handle click events.

 var canvas = new fabric.Canvas("bb"); svg = '<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" x="0in" y="0in" width="6.50331in" height="2.1in" viewBox="0 0 468.238 151.2" ><g partID="57380"><g id="breadboardbreadboard"> <g id="background"> <rect fill="#D9D9D9" height="151.199" width="468.238" y="0"/> </g> <g id="stripes"><rect fill="#B3B0B0" height="0.4" width="468.238" y="20.932"/> <rect fill="#B3B0B0" height="0.4" width="468.238" y="129.475"/> </g> <g > <rect fill="#FF0000" height="0.4" width="468.238" y="19.2"/> <rect fill="#FF0000" height="0.4" width="468.238" y="148.799"/> <rect fill="#0000FF" height="0.4" width="468.238" y="2.4"/> <rect fill="#0000FF" height="0.4" width="468.238" y="132"/> </g> <g > <rect fill="#CCC9C9" height="7.2" width="468.238" y="71.2"/> </g> <g id="sockets"> <g id="pin1A"> <path fill="#BFBFBF" d="M8.526,115.2c0,-1.322,1.072,-2.394,2.394,-2.394s2.394,1.071,2.394,2.394"/> <path fill="#E6E6E6" d="M13.313,115.2c0,1.322,-1.072,2.394,-2.394,2.394s-2.394,-1.071,-2.394,-2.394"/> <circle r="1.197" fill="#383838" cx="10.92" cy="115.2"/> </g> <g id="pin1B"> <path fill="#BFBFBF" d="M8.526,108c0,-1.322,1.072,-2.395,2.394,-2.395s2.394,1.072,2.394,2.395"/> <path fill="#E6E6E6" d="M13.313,108c0,1.321,-1.072,2.393,-2.394,2.393S8.526,109.322,8.526,108"/> <circle r="1.197" fill="#383838" cx="10.92" cy="108"/> </g> <g id="pin1C"> <path fill="#BFBFBF" d="M8.526,100.799c0,-1.321,1.072,-2.393,2.394,-2.393s2.394,1.071,2.394,2.393"/> <path fill="#E6E6E6" d="M13.313,100.799c0,1.322,-1.072,2.395,-2.394,2.395s-2.394,-1.072,-2.394,-2.395l0,0"/> <circle r="1.197" fill="#383838" cx="10.92" cy="100.8"/> </g></g></g></svg>'; fabric.loadSVGFromString(svg, function(objs) { var obj; for(var i = 0; i < objs.length; i++) { obj = objs[i]; obj.lockMovementX = true; obj.lockMovementY = true; obj.hasControls= false; canvas.add(obj); }}); canvas.on("object:selected", function(evt) { var id = evt.target.id; if (id.slice(0,3) == 'pin') { alert(id.slice(3,5)); } }); 
 <script src="http://www.deltalink.it/andreab/fabric/fabric.js"></script> <canvas width="600" height="400" id="bb" ></canvas> 

One approach is this:

  • first you have to store an array of coords of each hole on your breadboard image
  • after you load your background image loop over each entry in that array and create a shape (say, fabric.Circle ) over the corresponding hole
  • right after a shape is created attach a mousedown event handler to this shape
  • do your click processing in that event handler

Here are a couple of references that can help you with that:

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