Below you’ll find a single-file “drag & drop” logic-circuit editor.
Just copy the whole block into a file called, for example, logic-editor.html and open it in your browser – nothing else to install.
What it can do
• Palette with INPUT, NOT, AND, OR, OUTPUT gates
• Drag gates around the canvas
• Click the OUTPUT stub of one gate, then an INPUT stub of another to draw a wire
• Click an INPUT gate rectangle itself to toggle 0/1 (red ⇄ green)
• Press Simulate to propagate values through the network and light the OUTPUT bubbles
(Implementation uses a very naïve stable-iteration approach, good enough for small acyclic circuits.)
Single-file source
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Logic Circuit Editor – single file demo</title>
<style>
body{font-family:sans-serif;margin:0;padding:0;user-select:none;}
h2{margin:8px 16px;}
#toolbar{padding:8px 16px;background:#f0f0f0;border-bottom:1px solid #ccc;}
#toolbar button{margin-right:4px;margin-bottom:4px;padding:6px 10px;font-weight:600;}
canvas{display:block;width:100%;height:600px;background:#fff;cursor:default}
p{margin:8px 16px 16px;color:#666;font-size:.9em;}
</style>
</head>
<body>
<h2>Logic Circuit Editor (single-file demo)</h2>
<div id="toolbar">
<button data-gate="INPUT">INPUT</button>
<button data-gate="NOT">NOT</button>
<button data-gate="AND">AND</button>
<button data-gate="OR">OR</button>
<button data-gate="OUTPUT">OUTPUT</button>
<button id="simulateBtn" style="background:#4caf50;color:#fff;">Simulate</button>
</div>
<canvas id="canvas" width="1600" height="600"></canvas>
<p>Drag gates. Click an INPUT gate to toggle its value. To connect: click a gate’s output stub
(𝚘 on the right), then an input stub (𝚘 on the left) of another gate.</p>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
/* ------- basic drawing parameters ------- */
const W = 60, H = 40; // gate rectangle size
const R = 5; // connector radius
/* ------- data model ------- */
const gates = []; // {id,type,x,y,inputsCount,inputs[],value}
const wires = []; // {id,fromGate,toGate,toInput}
const uuid = () => Math.random().toString(36).substr(2,9);
/* ------- helpers to compute connector coordinates ------- */
function inPos(gate, i){
return {x: gate.x - 10,
y: gate.y + H * (i+1)/(gate.inputsCount+1)};
}
function outPos(gate){
return {x: gate.x + W + 10,
y: gate.y + H/2};
}
/* ------- add gate ------- */
function addGate(type, x=40, y=40){
const g = {id:uuid(),type,x,y,value:false};
switch(type){
case 'INPUT': g.inputsCount = 0; break;
case 'NOT': g.inputsCount = 1; break;
case 'AND':
case 'OR': g.inputsCount = 2; break;
case 'OUTPUT': g.inputsCount = 1; break;
}
g.inputs = Array(g.inputsCount).fill(null); // wire ids
gates.push(g);
draw();
}
/* ------- draw the whole scene ------- */
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
/* wires first (so they stay under gates) */
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
wires.forEach(w=>{
const g1 = gates.find(g=>g.id===w.fromGate);
const g2 = gates.find(g=>g.id===w.toGate);
if(!g1||!g2) return;
const a = outPos(g1);
const b = inPos(g2, w.toInput);
ctx.beginPath();
ctx.moveTo(a.x,a.y);
ctx.lineTo(b.x,b.y);
ctx.stroke();
});
/* gates & connectors */
gates.forEach(g=>{
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#000';
ctx.lineWidth = 1.5;
ctx.fillRect(g.x,g.y,W,H);
ctx.strokeRect(g.x,g.y,W,H);
/* label */
ctx.fillStyle = '#000';
ctx.font = '12px sans-serif';
ctx.textAlign='center';
ctx.textBaseline='middle';
ctx.fillText(g.type, g.x+W/2, g.y+H/2);
/* input connectors */
for(let i=0;i<g.inputsCount;i++){
const p = inPos(g,i);
ctx.beginPath();
ctx.arc(p.x,p.y,R,0,Math.PI*2);
ctx.fillStyle = 'lightgray';
ctx.fill();
ctx.stroke();
}
/* output connector */
const o = outPos(g);
ctx.beginPath();
ctx.arc(o.x,o.y,R,0,Math.PI*2);
ctx.fillStyle='lightgray';
ctx.fill();
ctx.stroke();
/* value indicator for INPUT/OUTPUT after simulation */
if(g.type==='INPUT' || g.type==='OUTPUT'){
ctx.fillStyle = g.value ? 'green' : 'red';
ctx.beginPath();
ctx.arc(g.x+W-10, g.y+10, 6, 0, Math.PI*2);
ctx.fill();
}
});
}
/* ------- mouse interaction ------- */
let dragGate = null, dx=0, dy=0;
let wiringFrom = null; // gate object we started wiring from (output)
canvas.addEventListener('mousedown', e=>{
const mx=e.offsetX, my=e.offsetY;
/* click inside a gate? */
for(const g of gates){
if(mx>=g.x && mx<=g.x+W && my>=g.y && my<=g.y+H){
if(g.type==='INPUT'){ // toggle value
g.value = !g.value;
draw(); return;
}
dragGate = g; dx = mx-g.x; dy = my-g.y;
return;
}
}
/* click an output stub? -> begin wiring */
for(const g of gates){
const p = outPos(g);
if(dist(mx,my,p.x,p.y)<R+2){
wiringFrom = g;
return;
}
}
/* click on input stub? while wiring -> create wire */
for(const g of gates){
for(let i=0;i<g.inputsCount;i++){
const p = inPos(g,i);
if(dist(mx,my,p.x,p.y)<R+2 && wiringFrom){
const w = {id:uuid(),fromGate:wiringFrom.id,toGate:g.id,toInput:i};
wires.push(w);
g.inputs[i]=w.id;
wiringFrom = null;
draw(); return;
}
}
}
});
canvas.addEventListener('mousemove', e=>{
if(dragGate){
dragGate.x = e.offsetX - dx;
dragGate.y = e.offsetY - dy;
draw();
}
});
canvas.addEventListener('mouseup', ()=>{
dragGate = null;
wiringFrom = null;
});
function dist(x1,y1,x2,y2){ return Math.hypot(x1-x2,y1-y2); }
/* ------- simulation ------- */
document.getElementById('simulateBtn').addEventListener('click', simulate);
function simulate(){
/* reset every non-input gate’s value */
gates.forEach(g=>{ if(g.type!=='INPUT') g.value=false; });
let changed=true;
/* crude 10-iteration settle (works for simple feed-forward graphs) */
for(let step=0; step<10 && changed; step++){
changed=false;
gates.forEach(g=>{
if(g.type==='INPUT') return;
const ins = g.inputs.map(id=>{
const w = wires.find(w=>w.id===id);
const src = w && gates.find(g2=>g2.id===w.fromGate);
return src ? src.value : false;
});
const prev = g.value;
switch(g.type){
case 'NOT': g.value = !ins[0]; break;
case 'AND': g.value = ins[0] && ins[1]; break;
case 'OR': g.value = ins[0] || ins[1]; break;
case 'OUTPUT': g.value = ins[0]; break;
}
if(g.value!==prev) changed=true;
});
}
draw();
}
/* ------- toolbar buttons ------- */
document.querySelectorAll('#toolbar button[data-gate]')
.forEach(btn=>btn.addEventListener('click',
()=>addGate(btn.dataset.gate, 60, 60)));
draw(); // initial blank scene
</script>
</body>
</html>
Have fun experimenting!
Feel free to extend with more gates, persistence (localStorage/JSON export), or fancier routing if you need a fuller-featured editor.