Below is an example of a single‑file HTML “logical circuit editor” where you can add logic‐gates (AND, OR, NOT), move them around on a canvas, and create connections between an output (red circle on the right) and an input (red circle on the left). (This is a basic example meant as a starting point and can be extended with more features such as multiple inputs per gate, logic simulation, zooming, undo/redo, etc.)
To try it out, simply copy the code into a file (for example, circuit.html) and open it in your browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Logical Circuit Editor</title>
<style>
body {
margin: 0;
font-family: sans-serif;
user-select: none;
}
/* Toolbox area on the left */
#toolbox {
width: 150px;
background: #f1f1f1;
height: 100vh;
padding: 10px;
float: left;
box-sizing: border-box;
border-right: 1px solid #ccc;
}
/* The main canvas area where circuits are built */
#canvas {
position: relative;
margin-left: 150px;
height: 100vh;
background: #eee;
overflow: hidden;
}
/* Style for each gate element */
.gate {
position: absolute;
width: 80px;
height: 50px;
background: white;
border: 1px solid #333;
border-radius: 5px;
text-align: center;
cursor: move;
box-shadow: 1px 1px 5px rgba(0,0,0,0.2);
}
/* Connector styles (small circle) */
.connector {
width: 10px;
height: 10px;
background: red;
border-radius: 50%;
position: absolute;
cursor: pointer;
}
/* Input connector positioned on the left */
.input {
left: -5px;
top: 20px;
}
/* Output connector positioned on the right */
.output {
right: -5px;
top: 20px;
}
/* Some basic styling for buttons */
#toolbox button {
width: 100%;
margin-bottom: 8px;
padding: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<div id="toolbox">
<h3>Tools</h3>
<button onclick="addGate('AND')">AND Gate</button>
<button onclick="addGate('OR')">OR Gate</button>
<button onclick="addGate('NOT')">NOT Gate</button>
</div>
<div id="canvas">
<!-- An SVG overlay to draw connection lines -->
<svg id="connections" style="position:absolute; top:0; left:0; width:100%; height:100%; pointer-events:none;"></svg>
</div>
<script>
let gateCounter = 0;
// Stores connection objects: each with endpoints and an SVG line element
const connections = [];
// Stores the current “starting” connector when building a new connection
let currentConnection = null; // Format: { gate, connector }
// Adds a new gate to the canvas at a fixed starting position
function addGate(type) {
const canvas = document.getElementById('canvas');
const gate = document.createElement('div');
gate.classList.add('gate');
gate.setAttribute('data-type', type);
gate.setAttribute('data-id', gateCounter++);
gate.style.left = '50px';
gate.style.top = '50px';
// Display the type in the middle of the gate
gate.innerHTML = '<div style="margin-top:15px;">' + type + '</div>';
// Create input connector (left side)
const input = document.createElement('div');
input.classList.add('connector', 'input');
input.title = "Input";
input.addEventListener('click', function(e) {
e.stopPropagation();
handleInputClick(gate, input);
});
gate.appendChild(input);
// Create output connector (right side)
const output = document.createElement('div');
output.classList.add('connector', 'output');
output.title = "Output";
output.addEventListener('click', function(e) {
e.stopPropagation();
handleOutputClick(gate, output);
});
gate.appendChild(output);
// Enable dragging for the gate
gate.addEventListener('mousedown', function(e){
dragElement(e, gate);
});
canvas.appendChild(gate);
}
// When an output is clicked, store the gate as the start of a new connection
function handleOutputClick(gate, connector) {
// If no connection is in progress, record the starting point.
if (!currentConnection) {
currentConnection = { gate: gate, connector: connector };
}
}
// When an input is clicked and a connection is in progress, connect the two endpoints.
function handleInputClick(gate, connector) {
if (currentConnection) {
// Create a connection from the stored output connector to this input connector.
connectGates(currentConnection.gate, currentConnection.connector, gate, connector);
currentConnection = null;
}
}
// Create a connection (a line) between two gate connectors.
function connectGates(fromGate, fromConnector, toGate, toConnector) {
const svg = document.getElementById('connections');
const line = document.createElementNS("http://www.w3.org/2000/svg", 'line');
line.setAttribute('stroke', 'black');
line.setAttribute('stroke-width', '2');
svg.appendChild(line);
// Store the connection so that if a gate moves, the line can be updated.
const conn = { fromGate, fromConnector, toGate, toConnector, line };
connections.push(conn);
updateConnectionLine(conn);
}
// Compute the connector positions and update the corresponding SVG line attributes.
function updateConnectionLine(conn) {
const canvasRect = document.getElementById('canvas').getBoundingClientRect();
const fromRect = conn.fromConnector.getBoundingClientRect();
const toRect = conn.toConnector.getBoundingClientRect();
const x1 = fromRect.left + fromRect.width/2 - canvasRect.left;
const y1 = fromRect.top + fromRect.height/2 - canvasRect.top;
const x2 = toRect.left + toRect.width/2 - canvasRect.left;
const y2 = toRect.top + toRect.height/2 - canvasRect.top;
conn.line.setAttribute('x1', x1);
conn.line.setAttribute('y1', y1);
conn.line.setAttribute('x2', x2);
conn.line.setAttribute('y2', y2);
}
// Updates all connections (should be called after a gate is moved)
function updateAllConnections() {
connections.forEach(conn => {
updateConnectionLine(conn);
});
}
// Simple drag functionality for gates.
function dragElement(e, element) {
e.preventDefault();
let pos3 = e.clientX;
let pos4 = e.clientY;
function elementDrag(e) {
const pos1 = pos3 - e.clientX;
const pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Update element’s position
element.style.left = (element.offsetLeft - pos1) + "px";
element.style.top = (element.offsetTop - pos2) + "px";
// Update any connections attached to this gate
updateAllConnections();
}
function closeDragElement() {
document.removeEventListener('mousemove', elementDrag);
document.removeEventListener('mouseup', closeDragElement);
}
document.addEventListener('mousemove', elementDrag);
document.addEventListener('mouseup', closeDragElement);
}
</script>
</body>
</html>
How It Works
-
Toolbox & Adding Gates:
On the left you’ll see three buttons. Clicking one (say "AND Gate") calls the JavaScript function addGate(type), which creates a new div with the type label and two small connector divs. The new gate is appended to the canvas.
-
Dragging:
Each gate has an event listener for mousedown. While dragging the gate, its CSS left and top values update, and the code refreshes connection lines (drawn with an overlaid SVG element) so that the connectors remain linked.
-
Connecting Gates:
Clicking on an output connector starts a new connection. Then clicking on an input connector (of another gate) completes the connection. A new SVG <line> is drawn between the two connector centers.
Note: This is a basic demo. For production use or more advanced editing (e.g., multiple inputs/outputs, wiring curves, persistence, simulation, etc.), you would want to expand this code or use specialized libraries.
Happy editing!