vasturiano / react-force-graph

React component for 2D, 3D, VR and AR force directed graphs
https://vasturiano.github.io/react-force-graph/example/large-graph/
MIT License
2.17k stars 276 forks source link

Text node alignment based on Nodes position #209

Open chaitali-pathak opened 4 years ago

chaitali-pathak commented 4 years ago

I am not able to align text nodes. I have used const paintRing1 = useCallback((node, ctx, globalScale) => { // add ring just for highlighted nodes // debugger; ctx.beginPath(); ctx.arc(node.x, node.y, NODE_R 1.1, 0, 2 Math.PI, false); ctx.fillStyle = node === hoverNode ? 'black' : 'black'; if ((props.hideLabels) && node.x < 0) { return } const label = ${node.name}; const fontSize = 10 / globalScale; ctx.font = ${fontSize}px MarsCentra-Book; ctx.textBaseline = 'middle'; ctx.textAlign = 'start'; ctx.fillStyle = 'red'; //node.color; ctx.fillText(label, node.x, node.y + 15); // node.opacity = 0.4

}, [hoverNode,highlightLinks]);

this as my NodeCanvasObject and nodeCanvasObjectMode={()=>"after"} What should I do to align my text nodes ? @vasturiano Please help me

vasturiano commented 4 years ago

@chaitali-pathak if you wish to center the text horizontally you can do ctx.textAlign = 'center'. https://www.w3schools.com/tags/canvas_textalign.asp