html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Minimal D3 Drawing App</title>
<style>
body { margin: 0; font-family: sans-serif; }
#controls {
display: flex;
padding: 10px;
background: #f0f0f0;
gap: 10px;
}
#layers {
border-left: 1px solid #ccc;
padding-left: 10px;
min-width: 200px;
}
.layer-item {
cursor: pointer;
margin-bottom: 5px;
}
.selected {
background: #ddf;
}
svg {
border: 1px solid #ccc;
background: #fff;
display: block;
}
input[type=number], input[type=color] {
width: 60px;
}
</style>
</head>
<body>
<div id="controls">
<div>
<button onclick="setMode('rect')">Rectangle</button>
<button onclick="setMode('line')">Line w/ Arrow</button>
<button onclick="setMode('text')">Text</button>
</div>
<div id="layers"></div>
</div>
<svg id="canvas" width="800" height="600"></svg>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
let mode = 'select';
let elements = [];
let selectedElement = null;
const svg = d3.select("#canvas");
const layersDiv = d3.select("#layers");
// Define arrow marker
svg.append("defs").append("marker")
.attr("id", "arrow")
.attr("viewBox", "0 0 10 10")
.attr("refX", 10)
.attr("refY", 5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M 0 0 L 10 5 L 0 10 z")
.attr("fill", "#000");
function setMode(newMode) {
mode = newMode;
console.log("Mode:", mode);
}
function updateLayers() {
layersDiv.selectAll(".layer-item").remove();
elements.forEach((el, i) => {
const div = layersDiv.append("div")
.attr("class", "layer-item" + (selectedElement === el ? " selected" : ""))
.text(el.type + " #" + (i+1))
.on("click", () => {
selectElement(el);
});
});
}
function selectElement(el) {
selectedElement = el;
updateLayers();
renderUI();
}
function renderUI() {
// Remove previous UI
d3.selectAll(".ui-control").remove();
const container = layersDiv.append("div").attr("class", "ui-control");
if (selectedElement && selectedElement.type === 'text') {
container.append("input")
.attr("type", "text")
.property("value", selectedElement.text)
.on("input", function(event) {
selectedElement.text = event.target.value;
updateElement(selectedElement);
});
container.append("br");
container.append("label").text("Font Size:");
container.append("input")
.attr("type", "number")
.property("value", selectedElement.fontSize)
.on("input", function(event) {
selectedElement.fontSize = +event.target.value;
updateElement(selectedElement);
});
}
if (selectedElement && selectedElement.type === 'line') {
container.append("label").text("Stroke Width:");
container.append("input")
.attr("type", "number")
.property("value", selectedElement.strokeWidth)
.on("input", function(event) {
selectedElement.strokeWidth = +event.target.value;
updateElement(selectedElement);
});
}
if (selectedElement && selectedElement.type === 'rect') {
container.append("label").text("Fill Color:");
container.append("input")
.attr("type", "color")
.property("value", selectedElement.fill)
.on("input", function(event) {
selectedElement.fill = event.target.value;
updateElement(selectedElement);
});
}
// Common position controls
if (selectedElement) {
container.append("br");
container.append("label").text("X:");
container.append("input")
.attr("type", "number")
.property("value", selectedElement.x)
.on("input", function(event) {
selectedElement.x = +event.target.value;
updateElement(selectedElement);
});
container.append("label").text("Y:");
container.append("input")
.attr("type", "number")
.property("value", selectedElement.y)
.on("input", function(event) {
selectedElement.y = +event.target.value;
updateElement(selectedElement);
});
}
}
function updateElement(el) {
const sel = d3.select(`[data-id="${el.id}"]`);
if (el.type === 'rect') {
sel.attr("x", el.x)
.attr("y", el.y)
.attr("width", el.width)
.attr("height", el.height)
.attr("fill", el.fill);
} else if (el.type === 'line') {
sel.attr("x1", el.x1)
.attr("y1", el.y1)
.attr("x2", el.x2)
.attr("y2", el.y2)
.attr("stroke-width", el.strokeWidth);
} else if (el.type === 'text') {
sel.attr("x", el.x)
.attr("y", el.y)
.attr("font-size", el.fontSize)
.text(el.text);
}
}
let drawing = false;
let startX, startY;
svg.on("mousedown", function(event) {
const coords = d3.pointer(event);
startX = coords[0];
startY = coords[1];
drawing = true;
});
svg.on("mouseup", function(event) {
if (!drawing) return;
drawing = false;
const coords = d3.pointer(event);
const endX = coords[0];
const endY = coords[1];
if (mode === 'rect') {
const rect = svg.append("rect")
.attr("x", Math.min(startX, endX))
.attr("y", Math.min(startY, endY))
.attr("width", Math.abs(endX - startX))
.attr("height", Math.abs(endY - startY))
.attr("fill", "#99ccff")
.attr("stroke", "#333")
.attr("stroke-width", 1)
.attr("data-id", elements.length);
const el = {
id: elements.length,
type: 'rect',
x: Math.min(startX, endX),
y: Math.min(startY, endY),
width: Math.abs(endX - startX),
height: Math.abs(endY - startY),
fill: "#99ccff"
};
elements.push(el);
updateLayers();
}
if (mode === 'line') {
const line = svg.append("line")
.attr("x1", startX)
.attr("y1", startY)
.attr("x2", endX)
.attr("y2", endY)
.attr("stroke", "#000")
.attr("stroke-width", 2)
.attr("marker-end", "url(#arrow)")
.attr("data-id", elements.length);
const el = {
id: elements.length,
type: 'line',
x1: startX,
y1: startY,
x2: endX,
y2: endY,
strokeWidth: 2
};
elements.push(el);
updateLayers();
}
});
svg.on("dblclick", function(event) {
const coords = d3.pointer(event);
const text = svg.append("text")
.attr("x", coords[0])
.attr("y", coords[1])
.attr("font-size", 16)
.attr("contenteditable", true)
.attr("data-id", elements.length)
.text("New Text")
.on("click", function(event) {
event.stopPropagation();
selectElement(elements.find(e => e.id == this.__data__.id));
});
const el = {
id: elements.length,
type: 'text',
x: coords[0],
y: coords[1],
fontSize: 16,
text: "New Text"
};
elements.push(el);
updateLayers();
selectElement(el);
});
</script>
</body>
</html>