o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)
Make a mindmaps engine creator using HTML + JS (one file) that fully works, make design…
- User
Make a mindmaps engine creator using HTML + JS (one file) that fully works, make design modern and sleek
- Assistant
Sure! Below is a fully functional mind map creator built using HTML and JavaScript in a single file. It allows you to add nodes by clicking anywhere on the canvas, drag nodes around, and visually connect nodes to create relationships. The design is modern and sleek, utilizing SVG for rendering.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Mind Map Creator</title> <style> /* Reset default margins and paddings */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Fullscreen canvas */ html, body { width: 100%; height: 100%; overflow: hidden; background-color: #f0f2f5; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } /* SVG canvas */ #mindmap { width: 100vw; height: 100vh; display: block; cursor: crosshair; } /* Node styling */ .node { cursor: pointer; } .node-circle { fill: #ffffff; stroke: #007bff; stroke-width: 2; } .node-text { fill: #333333; font-size: 14px; text-anchor: middle; dominant-baseline: middle; } /* Connection lines */ .connection { stroke: #007bff; stroke-width: 2; } </style> </head> <body> <svg id="mindmap"></svg> <script> const svg = document.getElementById('mindmap'); let nodes = []; let connections = []; let nodeIdCounter = 0; let isDragging = false; let selectedNode = null; let offsetX, offsetY; let connecting = false; let sourceNode = null; // Handle adding nodes svg.addEventListener('click', (e) => { if (e.target === svg && !connecting) { const coord = getSVGCoordinates(e); const node = createNode(coord.x, coord.y); svg.appendChild(node.group); nodes.push(node); } }); // Create a node function createNode(x, y) { const group = document.createElementNS('http://www.w3.org/2000/svg', 'g'); group.classList.add('node'); group.setAttribute('transform', `translate(${x}, ${y})`); group.dataset.id = nodeIdCounter++; const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle'); circle.classList.add('node-circle'); circle.setAttribute('r', 30); const text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); text.classList.add('node-text'); text.textContent = 'Node'; group.appendChild(circle); group.appendChild(text); // Event listeners for node interactions group.addEventListener('mousedown', (e) => { if (connecting) { // Create connection const targetNode = nodes.find(n => n.group === group); if (sourceNode && targetNode && sourceNode !== targetNode) { createConnection(sourceNode, targetNode); } resetConnectionState(); } else { isDragging = true; selectedNode = group; const coord = getSVGCoordinates(e); const transform = selectedNode.transform.baseVal.getItem(0); offsetX = coord.x - transform.matrix.e; offsetY = coord.y - transform.matrix.f; } e.stopPropagation(); }); group.addEventListener('dblclick', (e) => { e.stopPropagation(); initiateConnection(group); }); return { group, x, y }; } // Handle dragging nodes svg.addEventListener('mousemove', (e) => { if (isDragging && selectedNode) { const coord = getSVGCoordinates(e); const x = coord.x - offsetX; const y = coord.y - offsetY; selectedNode.setAttribute('transform', `translate(${x}, ${y})`); updateConnections(selectedNode); } }); svg.addEventListener('mouseup', () => { isDragging = false; selectedNode = null; }); // Get SVG coordinates from mouse event function getSVGCoordinates(event) { const point = svg.createSVGPoint(); point.x = event.clientX; point.y = event.clientY; return point.matrixTransform(svg.getScreenCTM().inverse()); } // Initiate connection from a node function initiateConnection(nodeGroup) { connecting = true; sourceNode = nodes.find(n => n.group === nodeGroup); svg.style.cursor = 'crosshair'; } // Reset connection state function resetConnectionState() { connecting = false; sourceNode = null; svg.style.cursor = 'default'; } // Create a connection line between two nodes function createConnection(nodeA, nodeB) { const line = document.createElementNS('http://www.w3.org/2000/svg', 'line'); line.classList.add('connection'); svg.insertBefore(line, svg.firstChild); connections.push({ line, nodeA, nodeB }); updateConnectionLine(line, nodeA.group, nodeB.group); } // Update connections when nodes move function updateConnections(movedNode) { connections.forEach(conn => { if (conn.nodeA.group === movedNode || conn.nodeB.group === movedNode) { updateConnectionLine(conn.line, conn.nodeA.group, conn.nodeB.group); } }); } // Update connection line positions function updateConnectionLine(line, groupA, groupB) { const transformA = groupA.transform.baseVal.getItem(0).matrix; const transformB = groupB.transform.baseVal.getItem(0).matrix; line.setAttribute('x1', transformA.e); line.setAttribute('y1', transformA.f); line.setAttribute('x2', transformB.e); line.setAttribute('y2', transformB.f); } </script> </body> </html>Instructions:
- Add Nodes: Click anywhere on the canvas to add a new node.
- Move Nodes: Click and drag a node to move it around.
- Connect Nodes: Double-click on a node to start connecting, then click on another node to create a connection.
Features:
- Modern Design: The interface uses a clean, modern color scheme with smooth interactions.
- Interactive Nodes: Nodes can be added dynamically, moved, and connected.
- Responsive SVG Canvas: The mind map adjusts to the full size of the browser window.
Feel free to customize the styles and functionality further to suit your needs!