Non-overlapping Draggable + Resizable Items (RBush + JSON Config)
by Rajesh Danabal
HTML
<!-- RBush for fast rectangle collision queries -->
<script src="https://unpkg.com/[email protected]/rbush.min.js"></script>
<div class="wrap">
<header>
<button class="btn" id="addItem">+ Add item</button>
<button class="btn" id="save">Save JSON</button>
<button class="btn" id="load">Load JSON</button>
<span style="font-size:12px;color:var(--muted)">Tip: drag by header · Resize from any edge/corner · Snap grid 20px · No overlap</span>
</header>
<div class="main">
<div class="board" id="board" aria-label="Layout board"></div>
<div class="panel">
<h3>Live JSON Config</h3>
<pre id="jsonOut"></pre>
</div>
</div>
</div>
<div class="hint">Use <kbd>Shift</kbd> while nudging with arrow keys to move faster</div>
CSS
:root {
--bg: #0b0f19;
--board: #0f172a;
--card: #111827;
--line: rgba(255,255,255,.08);
--text: #e5e7eb;
--muted: #94a3b8;
--accent: #60a5fa;
--ring: rgba(96,165,250,.5);
}
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial; }
.wrap { height: 100%; display: grid; grid-template-rows: auto 1fr; }
header {
display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line);
background: linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,0));
}
header .btn {
border: 1px solid var(--line); background: #0f172a; padding: 6px 10px; border-radius: 10px; color: var(--text);
cursor: pointer; font-size: 13px;
}
header .btn:hover { outline: 2px solid var(--ring); }
.main {
display: grid; grid-template-columns: 1fr 360px; gap: 12px; height: calc(100vh - 52px);
padding: 12px;
}
.board {
position: relative; background: radial-gradient(1200px 800px at 20% 10%, #0b1020, #091022), var(--board);
border: 1px solid var(--line); border-radius: 16px; overflow: hidden;
/* subtle grid background every 20px */
background-image:
linear-gradient(transparent 19px, rgba(255,255,255,.03) 20px),
linear-gradient(90deg, transparent 19px, rgba(255,255,255,.03) 20px),
radial-gradient(1200px 800px at 20% 10%, #0b1020, #091022);
background-size: 20px 20px, 20px 20px, auto;
background-blend-mode: normal, normal, screen;
}
.panel {
border: 1px solid var(--line); border-radius: 16px; padding: 10px; background: #0b1220; overflow: auto;
}
.panel h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
pre { margin: 0; font-size: 12px; line-height: 1.35; white-space: pre-wrap; word-break: break-word; }
/* Item card */
.item {
position: absolute;...
JavaScript
/* ==========================
CONFIG & STATE
========================== */
const board = document.getElementById('board');
const out = document.getElementById('jsonOut');
const grid = 20;
const minW = grid * 5;
const minH = grid * 4;
let nextId = 4; // after initial 3
// Live JSON config (single source of truth)
let layout = [
{ id: "item1", x: 20, y: 20, w: 200, h: 140, title: "Item 1", content: "Notes about item one." },
{ id: "item2", x: 260, y: 40, w: 220, h: 120, title: "Item 2", content: "Another quick note." },
{ id: "item3", x: 520, y: 120, w: 200, h: 160, title: "Item 3", content: "Drag or resize me." }
];
// RBush for fast collision checks (stores rectangles)
const tree = new RBush();
// index items as {minX, minY, maxX, maxY, id}
function toRB(item) {
return { minX: item.x, minY: item.y, maxX: item.x + item.w, maxY: item.y + item.h, id: item.id };
}
function insertRB(item) { tree.insert(toRB(item)); }
function removeRB(item) { tree.remove(toRB(item), (a, b) => a.id === b.id); }
function updateRB(item, prev) { if (prev) removeRB(prev); insertRB(item); }
function snap(v) { return Math.round(v / grid) * grid; }
function clamp(v, min, max) { return Math.min(Math.max(v, min), max); }
function getBoardRect() { return board.getBoundingClientRect(); }
// Find collisions with a candidate rect (excluding self id)
function collides(candidate, excludeId) {
const box = { minX: candidate.x, minY: candidate.y, maxX: candidate.x + candidate.w, maxY: candidate.y + candidate.h };
const hits = tree.search(box);
return hits.some(h => h.id !== excludeId);
}
// Update JSON output
function renderJSON() {
out.textContent = JSON.stringify(layout, null, 2);
}
// Update node in JSON config
function updateNodeConfig(id, x, y, w, h) {
const node = layout.find(n => n.id === id);
if (!node) return;
const prev = { ...node };
node.x = x; node.y = y;
if (w !== undefined) node.w = w;
if (h !==...