javascript - Calculating line coordinates in Fabric.js -
i developing self graphing program draws binary tree using fabric.js. far have added draw node functions , working on drawing links between nodes, coordinates isn't getting calculated properly. can me point out error is? function(node1,node2,angle) draws node2 @ 200pixels away node1 @ angle passed. found start points of line following same method founded circle center adding circle's radius 20 in case instead of 200 pixels. end points calculated subtracting 20 200 , hence using value 180. code wrote:
code:
function addnode(node1,node2,angle) { var intialx=parseint(node1.get('left')); var initialy=parseint(node1.get('top')); if(angle<180) { var pointx =math.abs(math.cos(angle*math.pi/180)*200+intialx); var pointy=math.abs(math.sin(angle*math.pi/180)*200-initialy); var initiallinex=math.abs(math.cos(angle*math.pi/180)*20+intialx); var initialliney=math.abs(math.sin(angle*math.pi/180)*20-initialy); var finallinex=math.abs(math.cos(angle*math.pi/180)*180+intialx); var finalliney=math.abs(math.sin(angle*math.pi/180)*180-initialy); } else { var pointx =math.abs(math.cos(angle*math.pi/180)*200+intialx); var pointy=math.abs(math.sin(angle*math.pi/180)*200+initialy); var initiallinex=math.abs(math.cos(angle*math.pi/180)*20+intialx); var initialliney=math.abs(math.sin(angle*math.pi/180)*20+initialy); var finallinex=math.abs(math.cos(angle*math.pi/180)*180+intialx); var finalliney=math.abs(math.sin(angle*math.pi/180)*180+initialy); } var x=new fabric.circle({ radius:20,originx: 'center', originy: 'center',fill:'red'}); var value1=string(node2); var text= new fabric.text(value1, {fontsize: 10, originx: 'center', originy: 'center'}); var group = new fabric.group([ x, text ], {left:pointx, top: pointy, angle:0}); canvas.add(group); canvas.add(new fabric.line([initiallinex, initialliney, finallinex, finalliney], { stroke: 'red' })); return group; }
output: enter image description here
Comments
Post a Comment