I'm using sigmajs in pure js can I give you some code and you convert it to pure…
- User
I'm using sigmajs in pure js can I give you some code and you convert it to pure javascipt?
- Assistant
Of course! I'll do my best to help you convert your code to pure JavaScript. Please go ahead and provide me with the code you'd like assistance with.
- User
- Assistant
Here's the converted code in pure JavaScript:
const container = document.getElementById("sigma-container"); const graph = new graphology.Graph(); graph.addNode("n1", { x: 0, y: 0, size: 10, color: chroma.random().hex() }); graph.addNode("n2", { x: -5, y: 5, size: 10, color: chroma.random().hex() }); graph.addNode("n3", { x: 5, y: 5, size: 10, color: chroma.random().hex() }); graph.addNode("n4", { x: 0, y: 10, size: 10, color: chroma.random().hex() }); graph.addEdge("n1", "n2"); graph.addEdge("n2", "n4"); graph.addEdge("n4", "n3"); graph.addEdge("n3", "n1"); const layout = new graphology.layouts.ForceAtlas2Layout(graph); layout.start(); const renderer = new sigma.Renderer(graph, container); let draggedNode = null; let isDragging = false; renderer.bind("downNode", function (e) { isDragging = true; draggedNode = e.node; graph.setNodeAttribute(draggedNode, "highlighted", true); }); renderer.mouseCaptor.bind("mousemovebody", function (e) { if (!isDragging || !draggedNode) return; const pos = renderer.viewportToGraph(e); graph.setNodeAttribute(draggedNode, "x", pos.x); graph.setNodeAttribute(draggedNode, "y", pos.y); e.preventSigmaDefault(); e.original.preventDefault(); e.original.stopPropagation(); }); renderer.mouseCaptor.bind("mouseup", function () { if (draggedNode) { graph.removeNodeAttribute(draggedNode, "highlighted"); } isDragging = false; draggedNode = null; }); renderer.mouseCaptor.bind("mousedown", function () { if (!renderer.customBBox) renderer.customBBox = renderer.getBBox(); }); renderer.bind("clickStage", function (event) { const coordForGraph = renderer.viewportToGraph({ x: event.x, y: event.y }); const node = { ...coordForGraph, size: 10, color: chroma.random().hex(), }; const closestNodes = graph .nodes() .sort(function (a, b) { const attrsA = graph.getNodeAttributes(a); const attrsB = graph.getNodeAttributes(b); const distanceA = Math.pow(node.x - attrsA.x, 2) + Math.pow(node.y - attrsA.y, 2); const distanceB = Math.pow(node.x - attrsB.x, 2) + Math.pow(node.y - attrsB.y, 2); return distanceA - distanceB; }) .slice(0, 2); const id = uuid(); graph.addNode(id, node); closestNodes.forEach(function (e) { graph.addEdge(id, e); }); }); - User
are you sure it is a sigma code?
- Assistant
I apologize for the confusion. The provided code does not seem to be using Sigma library. It appears to be using the Graphology library to create and manipulate a graph object and perform layout calculations using a force-directed layout algorithm. However, there are no Sigma-specific functionalities in the code.
If you specifically need assistance with Sigma-related code, please provide the Sigma specific code you would like to convert to pure JavaScript, and I'll be happy to help you with it.