Write HTML + JS (one file) website that allow user create mindmaps
- User
Write HTML + JS (one file) website that allow user create mindmaps
- Assistant
Write HTML + JS (one file) website that allow user create mindmaps
Certainly! Below is a simple HTML file with embedded JavaScript that allows a user to create mind maps. This one-page application lets you add nodes, drag them around, edit their text, and draw connections between them.
Copy and paste the following code into a file with a .html extension (e.g., mindmap.html), and open it in your web browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Mind Map Creator</title>
<style>
body {
margin: 0;
overflow: hidden;
user-select: none;
font-family: Arial, sans-serif;
}
#canvas {
width: 100vw;
height: 100vh;
display: block;
}
.node {
position: absolute;
min-width: 100px;
min-height: 40px;
padding: 10px;
background-color: #fffdd0;
border: 1px solid #ccc;
border-radius: 5px;
cursor: move;
text-align: center;
box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
}
.node:focus {
outline: 2px solid #4A90E2;
}
#addNodeBtn {
position: absolute;
top: 10px;
left: 10px;
z-index: 1000;
padding: 10px 20px;
font-size: 16px;
}
#instructions {
position: absolute;
top: 10px;
right: 10px;
z-index: 1000;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
}
#instructions h2 {
margin-top: 0;
}
#instructions p {
margin: 5px 0;
}
</style>
</head>
<body>
<button id="addNodeBtn">Add Node</button>
<div id="instructions">
<h2>How to Use:</h2>
<p><strong>Add Nodes:</strong> Click "Add Node" button.</p>
<p><strong>Move Nodes:</strong> Click and drag nodes.</p>
<p><strong>Edit Text:</strong> Double-click a node.</p>
<p><strong>Connect Nodes:</strong> Click a node, then Shift+Click another node.</p>
</div>
<canvas id="canvas"></canvas>
<script>
(function() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const nodes = [];
const lines = [];
let isDragging = false;
let dragNode = null;
let offsetX = 0;
let offsetY = 0;
let selectedNode = null;
function initCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
initCanvas();
window.addEventListener('resize', () => {
initCanvas();
drawLines();
});
function addNode(x = window.innerWidth / 2, y = window.innerHeight / 2, text = 'Node') {
const node = document.createElement('div');
node.className = 'node';
node.contentEditable = false;
node.innerText = text;
node.style.left = x + 'px';
node.style.top = y + 'px';
document.body.appendChild(node);
node.addEventListener('mousedown', (e) => {
if (!node.isContentEditable) {
isDragging = true;
dragNode = node;
offsetX = e.clientX - node.offsetLeft;
offsetY = e.clientY - node.offsetTop;
}
});
node.addEventListener('click', (e) => {
if (e.shiftKey && selectedNode && selectedNode !== node) {
lines.push({ from: selectedNode, to: node });
drawLines();
selectedNode = null;
} else {
selectedNode = node;
}
e.stopPropagation();
});
node.addEventListener('dblclick', (e) => {
node.contentEditable = true;
node.focus();
e.stopPropagation();
});
node.addEventListener('blur', () => {
node.contentEditable = false;
});
nodes.push(node);
}
document.getElementById('addNodeBtn').addEventListener('click', () => {
addNode();
});
document.addEventListener('mousemove', (e) => {
if (isDragging && dragNode) {
dragNode.style.left = (e.clientX - offsetX) + 'px';
dragNode.style.top = (e.clientY - offsetY) + 'px';
drawLines();
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
dragNode = null;
});
function drawLines() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
lines.forEach(line => {
const fromRect = line.from.getBoundingClientRect();
const toRect = line.to.getBoundingClientRect();
const fromX = fromRect.left + fromRect.width / 2;
const fromY = fromRect.top + fromRect.height / 2;
const toX = toRect.left + toRect.width / 2;
const toY = toRect.top + toRect.height / 2;
ctx.beginPath();
ctx.moveTo(fromX, fromY);
ctx.lineTo(toX, toY);
ctx.strokeStyle = '#888';
ctx.lineWidth = 2;
ctx.stroke();
});
}
document.addEventListener('click', () => {
selectedNode = null;
});
// Initialize with a central node
addNode(window.innerWidth / 2 - 50, window.innerHeight / 2 - 25, 'Central Idea');
})();
</script>
</body>
</html>
Instructions:
Add Nodes: Click the "Add Node" button at the top-left corner to create new nodes.
Move Nodes: Click and drag nodes to reposition them anywhere on the screen.
Edit Node Text: Double-click on a node to make it editable. Type your text, and click outside the node to save.
Connect Nodes:
Features:
Dynamic Canvas: The canvas resizes automatically when the window size changes.
Intuitive Interface: Basic controls ensure anyone can start mapping ideas quickly.
Simple Styling: Nodes have a clean look and can be customized further via CSS.
Notes:
No Deletion Functionality: This basic version doesn't support deleting nodes or connections.
No Save Feature: The mind map isn't saved when you close the browser. To add this, you would need to implement a saving mechanism (e.g., local storage or exporting to a file).
Customization: Feel free to modify the code to add more features like colors, different node shapes, or export options.
Screenshot:
[image: Mind Map Screenshot] open
Enjoy creating your mind maps!