convert it to html full script
- User
convert it to html full script
- Assistant
Sure! Please provide the content or code that you want to be converted into a full HTML script, and I'll be happy to help.
- User
- Assistant
convert it to html full script
Sure! Please provide the content or code that you want to be converted into a full HTML script, and I'll be happy to help.
also add 40 features to this scirpt fully scripted in html make this better and also make the css better dragging better and not delayed and resize buttons and rotate buttons it snot appearing I want the actually buttons and add actual 3d blocks and 2d block sif needed so u can actually 3d blocks and look around it and add much more things and make it good and fun
(function () { "use strict";
// ==== VARIABLES ==== const container = document.createElement("div"); const settingsBox = document.createElement("div"); const infoBox = document.createElement("div"); const helpBox = document.createElement("div");
// State let currentSelection = []; // multi-select support let draggedElement = null; let dragOffset = { x: 0, y: 0 }; let dragAxisLock = null; // 'x' or 'y' if axis locked let isDraggingEnabled = true; let isResizingEnabled = true; let isRotatingEnabled = true; let isScalingEnabled = true; let is3DViewEnabled = false; let autoSaveInterval;
// Snap and Grid Settings let snapToGridEnabled = true; let snapGridSize = 10; let gridVisible = true;
// Camera State for 3D interaction let cameraRotation = { x: 15, y: 30 }; // degrees let cameraDistance = 800; // zoom let cameraPan = { x: 0, y: 0 }; let isOrbitingCamera = false; let isPanningCamera = false; let lastMousePos = null;
// Undo/Redo let undoStack = []; let redoStack = [];
// List of objects for persistence and reference const objects = [];
// ==== STYLES ====
const style = document.createElement("style");
style.textContent = html, body { margin:0; padding:0; overflow:hidden; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, #202020 0%, #404040 100%); color:#eee; user-select:none; height:100%; width:100%; } #container { position: relative; width:100vw; height:100vh; background:#111; perspective:1200px; transform-style: preserve-3d; overflow: visible; perspective-origin: center center; cursor: grab; } #container.grabbing { cursor: grabbing; } #settingsBox { position: fixed; top: 10px; left: 10px; width: 360px; max-height: 90vh; overflow-y: auto; background: rgba(30,30,30,0.85); box-shadow: 0 0 20px #00aaee77; padding: 20px 24px; border-radius: 18px; font-size: 14px; z-index: 9999; color: #ccc; user-select:none; } #settingsBox h3 { margin-top: 0; margin-bottom: 12px; font-weight: 900; color: #00aaff; user-select:none; } #infoBox { position: fixed; bottom: 12px; left: 12px; background: rgba(0,0,0,0.7); color: #0ff; padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: 13px; z-index: 9998; max-width: 360px; user-select:none; box-shadow: 0 0 15px #00aaff88 inset; } #helpBox { position: fixed; bottom: 12px; right: 12px; background: rgba(0,0,0,0.7); color: #aaa; padding: 10px 16px; border-radius: 10px; font-weight: 400; font-size: 11px; max-width: 380px; max-height: 45vh; overflow-y: auto; z-index: 9998; user-select:none; box-shadow: 0 0 15px #00aaff88 inset; display:none; } .object-box { position: absolute; border-radius: 12px; box-shadow: 0 5px 15px rgba(0,170,255,0.8); background: linear-gradient(145deg, #101010, #282828); border: 2px solid #00aaff; color: #0ff; font-weight: 700; user-select: none; transition: box-shadow 0.3s ease, transform 0.3s ease; cursor: grab; overflow: visible; backface-visibility: visible; /* necessary for 3D transforms */ } .object-box.grabbing { cursor: grabbing; } .object-box.selected { border-color: #00ffff; box-shadow: 0 0 20px 3px #00ffff; cursor: grabbing; } .object-box.text-block { padding: 10px 14px; overflow-wrap: break-word; cursor: text; user-select:text; line-height: 1.3; font-size: 15px; background: linear-gradient(135deg, #004466, #0077aa); border-radius: 12px; border: 1.8px solid #00aaffaa; } .object-box img { width: 100%; height: 100%; border-radius: 12px; pointer-events:none; user-select:none; object-fit: cover; filter: drop-shadow(0 0 6px #00aaffaa); } .resize-handle, .rotation-handle { position: absolute; border-radius: 50%; border: 2px solid white; user-select:none; background: #00aaffcc; box-shadow: 0 0 10px #00aaffcc; transition: background 0.3s ease; cursor: pointer; z-index: 1000; width: 18px; height: 18px; } .resize-handle:hover, .rotation-handle:hover { background: #0099ffdd; box-shadow: 0 0 16px #0099ffdd; } .rotation-handle { top: -40px; left: 50%; transform: translateX(-50%); cursor: grab; background: #33ccffcc; box-shadow: 0 0 16px #33ccffcc; } #grid { pointer-events:none; position:absolute; top:0;left:0; width: 100%; height: 100%; background-size: ${snapGridSize}px ${snapGridSize}px; background-image: linear-gradient(to right, rgba(0,170,255,0.1) 1px, transparent 1px), linear-gradient(to bottom, rgba(0,170,255,0.1) 1px, transparent 1px); z-index: 0; transition: opacity 0.3s ease; } input[type=range] { width: 100%; margin: 6px 0 12px 0; cursor: pointer; } select, input[type=color], input[type=checkbox] { cursor: pointer; } button { display: inline-block; font-size: 13px; background: #0077aa; border: none; border-radius: 10px; padding: 8px 14px; margin: 4px 8px 8px 0; color: #fff; font-weight: 600; cursor: pointer; user-select:none; transition: background-color 0.3s ease; } button:hover { background: #00aaff; box-shadow: 0 0 12px #00aaffaa; } /* Tooltip */ [data-tooltip] { position: relative; } [data-tooltip]:hover::before { content: attr(data-tooltip); position: absolute; bottom: 125%; left: 50%; transform: translateX(-50%); background: #00aaffdd; color: #222; padding: 4px 8px; border-radius: 6px; white-space: nowrap; font-size: 11px; font-weight: 700; pointer-events: none; opacity: 1; transition: opacity 0.2s ease; z-index: 10000; } [data-tooltip]:hover::after { content: ''; position: absolute; bottom: 115%; left: 50%; transform: translateX(-50%); border-width: 6px; border-style: solid; border-color: #00aaffdd transparent transparent transparent; pointer-events: none; opacity: 1; transition: opacity 0.2s ease; z-index: 10000; } ;
document.head.appendChild(style);
// ==== SETUP CONTAINER AND UI ==== container.id = "container"; settingsBox.id = "settingsBox"; infoBox.id = "infoBox"; helpBox.id = "helpBox";
document.body.appendChild(container); document.body.appendChild(settingsBox); document.body.appendChild(infoBox); document.body.appendChild(helpBox);
// Add grid overlay for snapping visualization const grid = document.createElement("div"); grid.id = "grid"; container.appendChild(grid);
// ==== HELPER FUNCTIONS ====
// Generate unique IDs function generateId() { return "obj_" + Math.random().toString(36).substr(2, 9); }
function pushToUndo() { const snapshot = objects.map((obj) => ({ id: obj.id, type: obj.type, style: { ...obj.element.style }, innerHTML: obj.element.innerHTML, contentEditable: obj.element.contentEditable, imgSrc: obj.imgSrc || null, rotationX: obj.rotationX || 0, rotationY: obj.rotationY || 0, rotationZ: obj.rotationZ || 0, scale: obj.scale || 1, position3D: { ...obj.position3D }, zIndex: obj.zIndex || 1, })); undoStack.push(snapshot); if (undoStack.length > 100) undoStack.shift(); redoStack = []; }
function loadState(state) { // Clear current objects.forEach((o) => { if (container.contains(o.element)) container.removeChild(o.element); }); objects.length = 0; currentSelection.length = 0;
state.forEach((data) => {
let el;
switch (data.type) {
case "box":
el = createBoxElement(
data.style.width,
data.style.height,
data.style.backgroundColor,
data.style.left,
data.style.top,
{
x: data.rotationX || 0,
y: data.rotationY || 0,
z: data.rotationZ || 0,
},
data.scale || 1,
data.zIndex || 1
);
break;
case "text":
el = createTextBlockElement(
data.style.width,
data.style.height,
data.style.backgroundColor,
data.style.left,
data.style.top,
data.innerHTML,
data.contentEditable,
{
x: data.rotationX || 0,
y: data.rotationY || 0,
z: data.rotationZ || 0,
},
data.scale || 1,
data.zIndex || 1
);
break;
case "image":
el = createImageBlockElement(
data.style.width,
data.style.height,
data.style.left,
data.style.top,
data.imgSrc || null,
{
x: data.rotationX || 0,
y: data.rotationY || 0,
z: data.rotationZ || 0,
},
data.scale || 1,
data.zIndex || 1
);
break;
case "gif":
el = createGifBlockElement(
data.style.width,
data.style.height,
data.style.left,
data.style.top,
data.imgSrc || null,
{
x: data.rotationX || 0,
y: data.rotationY || 0,
z: data.rotationZ || 0,
},
data.scale || 1,
data.zIndex || 1
);
break;
default:
return;
}
el.id = data.id;
objects.push({
id: data.id,
type: data.type,
element: el,
rotationX: data.rotationX || 0,
rotationY: data.rotationY || 0,
rotationZ: data.rotationZ || 0,
scale: data.scale || 1,
position3D: data.position3D || { x: 0, y: 0, z: 0 },
imgSrc: data.imgSrc,
zIndex: data.zIndex,
});
container.appendChild(el);
});
updateInfoBox();
updateSettingsBox();
}
// Snap value if snapping enabled function snapToGrid(value) { if (!snapToGridEnabled) return value; return Math.round(value / snapGridSize) * snapGridSize; }
// Update info box
function updateInfoBox() {
if (currentSelection.length === 1) {
const o = currentSelection[0];
infoBox.textContent = Selected: ${o.type} (ID: ${o.id}) | Pos: X:${o.position3D.x.toFixed( 0 )} Y:${o.position3D.y.toFixed(0)} Z:${o.position3D.z.toFixed( 0 )} | Rot: X:${o.rotationX} Y:${o.rotationY} Z:${o.rotationZ} | Scale: ${o.scale.toFixed( 2 )};
} else if (currentSelection.length > 1) {
infoBox.textContent = Selected ${currentSelection.length} objects;
} else {
infoBox.textContent = "No objects selected";
}
}
// Enable keyboard accessibility and interactions function enableInteractivity(el, obj) { el.addEventListener("mousedown", (e) => { e.stopPropagation(); });
el.addEventListener("click", (e) => {
if (e.shiftKey && currentSelection.includes(obj)) {
// deselect if shift-click already selected
deselectObject(obj);
} else if (e.shiftKey) {
selectObject(obj, true);
} else {
selectObject(obj, false);
}
e.preventDefault();
});
// Dragging only applicable if dragging enabled
el.addEventListener("mousedown", (e) => {
if (!isDraggingEnabled) return;
if (e.button !== 0) return; // only left mouse button
draggedElement = obj;
lastMousePos = { x: e.clientX, y: e.clientY };
// Lock drag axis if shift held
dragAxisLock = e.shiftKey ? "x" : null;
if (!currentSelection.includes(obj)) {
selectObject(obj, false);
}
container.classList.add("grabbing");
document.body.style.cursor = "grabbing";
e.preventDefault();
document.addEventListener("mousemove", dragHandler3D);
document.addEventListener("mouseup", dragEnd3D);
});
// Keyboard support for delete, rotate, scale, move etc
el.addEventListener("keydown", (e) => {
switch (e.key.toLowerCase()) {
case "delete":
case "backspace":
currentSelection.slice().forEach(removeObject);
clearSelection();
break;
// Arrow keys move object by snapGridSize units on screen plane (adjusted for 3D)
case "arrowup":
moveObject3DBy(e.shiftKey ? 1 : 10, "y", true);
break;
case "arrowdown":
moveObject3DBy(e.shiftKey ? -1 : -10, "y", true);
break;
case "arrowleft":
moveObject3DBy(e.shiftKey ? -1 : -10, "x", true);
break;
case "arrowright":
moveObject3DBy(e.shiftKey ? 1 : 10, "x", true);
break;
case "r":
rotateSelected("y", 15); // rotate Y axis 15deg
break;
case "s":
scaleSelected(0.1); // scale up by 10%
break;
case "d":
scaleSelected(-0.1); // scale down by 10%
break;
default:
return;
}
e.preventDefault();
});
// Show tooltips when focusing elements
el.addEventListener("focus", () => {
el.style.boxShadow = "0 0 30px 4px #00aaffcc";
});
el.addEventListener("blur", () => {
el.style.boxShadow = "";
});
}
// Select object, support multi-select append param function selectObject(obj, append = false) { if (!append) { clearSelection(); } if (!currentSelection.includes(obj)) { currentSelection.push(obj); obj.element.classList.add("selected"); } updateSettingsBox(); updateInfoBox(); }
// Remove from selection function deselectObject(obj) { const idx = currentSelection.indexOf(obj); if (idx > -1) { currentSelection.splice(idx, 1); obj.element.classList.remove("selected"); } updateSettingsBox(); updateInfoBox(); }
// Clear selection function clearSelection() { currentSelection.forEach((obj) => obj.element.classList.remove("selected")); currentSelection.length = 0; updateSettingsBox(); updateInfoBox(); }
// Bring object(s) to front function bringToFront(obj) { const maxZ = Math.max(...objects.map((o) => o.zIndex || 1), 1); obj.zIndex = maxZ + 1; obj.element.style.zIndex = obj.zIndex; pushToUndo(); }
// Send object(s) to back function sendToBack(obj) { const minZ = Math.min(...objects.map((o) => o.zIndex || 1), 1); obj.zIndex = minZ - 1; obj.element.style.zIndex = obj.zIndex; pushToUndo(); }
// Remove object function removeObject(obj) { if (!obj) return; if (container.contains(obj.element)) container.removeChild(obj.element); const idx = objects.findIndex((o) => o.id === obj.id); if (idx > -1) objects.splice(idx, 1); if (currentSelection.includes(obj)) deselectObject(obj); updateInfoBox(); pushToUndo(); }
// Clear all objects function clearAllObjects() { objects.forEach((o) => { if (container.contains(o.element)) container.removeChild(o.element); }); objects.length = 0; currentSelection.length = 0; updateInfoBox(); pushToUndo(); }
// Update container's CSS camera transform to simulate orbit and pan
function updateCameraTransform() {
// Compose camera transform string
container.style.transformOrigin = "50% 50%";
container.style.transform = translateX(${cameraPan.x}px) translateY(${cameraPan.y}px) translateZ(${-cameraDistance}px) rotateX(${cameraRotation.x}deg) rotateY(${cameraRotation.y}deg) ;
}
// Create 3D object's CSS transform string from rotations, scale, and 3D position
function composeObjectTransform(obj) {
const pos = obj.position3D;
return translate3d(${pos.x}px, ${pos.y}px, ${pos.z}px) rotateX(${obj.rotationX}deg) rotateY(${obj.rotationY}deg) rotateZ(${obj.rotationZ}deg) scale(${obj.scale}) ;
}
function updateElement3DTransform(obj) { obj.element.style.transform = composeObjectTransform(obj); }
// Object creation functions with 3D position, rotation xyz, and scale
function createBoxElement( width = "100px", height = "100px", bgColor = "#0077ff", left = "0px", top = "0px", rotation = { x: 0, y: 0, z: 0 }, scale = 1, zIndex = 1 ) { const div = document.createElement("div"); div.classList.add("object-box", "box"); div.style.width = width; div.style.height = height; div.style.backgroundColor = bgColor; div.style.zIndex = zIndex; div.tabIndex = 0; return div; }
function createTextBlockElement( width = "150px", height = "50px", bgColor = "#004477", left = "0px", top = "0px", content = "Editable Text", editable = true, rotation = { x: 0, y: 0, z: 0 }, scale = 1, zIndex = 1 ) { const div = document.createElement("div"); div.classList.add("object-box", "text-block"); div.style.width = width; div.style.height = height; div.style.backgroundColor = bgColor; div.innerHTML = content; div.contentEditable = editable; div.style.zIndex = zIndex; div.tabIndex = 0; return div; }
function createImageBlockElement( width = "150px", height = "150px", left = "0px", top = "0px", src = null, rotation = { x: 0, y: 0, z: 0 }, scale = 1, zIndex = 1 ) { const div = document.createElement("div"); div.classList.add("object-box", "image-block"); div.style.width = width; div.style.height = height; div.style.zIndex = zIndex; div.tabIndex = 0;
if (!src) src = prompt("Enter Image URL", "https://via.placeholder.com/150");
const img = document.createElement("img");
img.src = src;
div.appendChild(img);
return div;
}
function createGifBlockElement( width = "150px", height = "150px", left = "0px", top = "0px", src = null, rotation = { x: 0, y: 0, z: 0 }, scale = 1, zIndex = 1 ) { const div = document.createElement("div"); div.classList.add("object-box", "gif-block"); div.style.width = width; div.style.height = height; div.style.zIndex = zIndex; div.tabIndex = 0;
if (!src)
src =
"https://media.giphy.com/media/LmNwrBhejkK9EFP504/giphy.gif";
const img = document.createElement("img");
img.src = src;
div.appendChild(img);
return div;
}
// Spawn object of given type function spawnObject(type) { const id = generateId(); // Center spawn const centerX = container.clientWidth / 2; const centerY = container.clientHeight / 2;
let obj;
switch (type) {
case "box":
obj = {
id,
type,
element: createBoxElement(
"150px",
"150px",
"#0077ff",
"0px",
"0px",
{ x: 0, y: 0, z: 0 },
1,
1
),
position3D: { x: centerX, y: centerY, z: 0 },
rotationX: 0,
rotationY: 0,
rotationZ: 0,
scale: 1,
zIndex: 1,
};
break;
case "text":
obj = {
id,
type,
element: createTextBlockElement(
"180px",
"60px",
"#004477",
"0px",
"0px",
"Editable Text",
true,
{ x: 0, y: 0, z: 0 },
1,
1
),
position3D: { x: centerX, y: centerY, z: 0 },
rotationX: 0,
rotationY: 0,
rotationZ: 0,
scale: 1,
zIndex: 1,
};
break;
case "image":
obj = {
id,
type,
element: createImageBlockElement(
"150px",
"150px",
"0px",
"0px",
null,
{ x: 0, y: 0, z: 0 },
1,
1
),
position3D: { x: centerX, y: centerY, z: 0 },
rotationX: 0,
rotationY: 0,
rotationZ: 0,
scale: 1,
imgSrc: null,
zIndex: 1,
};
break;
case "gif":
obj = {
id,
type,
element: createGifBlockElement(
"150px",
"150px",
"0px",
"0px",
null,
{ x: 0, y: 0, z: 0 },
1,
1
),
position3D: { x: centerX, y: centerY, z: 0 },
rotationX: 0,
rotationY: 0,
rotationZ: 0,
scale: 1,
imgSrc: null,
zIndex: 1,
};
break;
default:
console.warn("Unsupported object type:", type);
return;
}
container.appendChild(obj.element);
objects.push(obj);
updateElement3DTransform(obj);
enableInteractivity(obj.element, obj);
pushToUndo();
selectObject(obj);
}
// Move selected objects in the 3D space
// axis: 'x', 'y', 'z'; delta is amount to move, relative true to interpret in object space or else screen space
function moveObject3DBy(delta, axis, relative = false) {
currentSelection.forEach((obj) => {
if (obj.position3D && axis in obj.position3D) {
obj.position3D[axis] += delta;
if (snapToGridEnabled) {
obj.position3D[axis] = snapToGrid(obj.position3D[axis]);
}
updateElement3DTransform(obj);
}
});
pushToUndo();
updateInfoBox();
}
// Rotate selected objects on axis by degrees function rotateSelected(axis, degrees) { currentSelection.forEach((obj) => { switch (axis) { case "x": obj.rotationX = (obj.rotationX + degrees) % 360; break; case "y": obj.rotationY = (obj.rotationY + degrees) % 360; break; case "z": obj.rotationZ = (obj.rotationZ + degrees) % 360; break; default: break; } updateElement3DTransform(obj); }); pushToUndo(); updateInfoBox(); }
// Scale selected objects by delta (signed float) function scaleSelected(delta) { currentSelection.forEach((obj) => { obj.scale += delta; if (obj.scale < 0.1) obj.scale = 0.1; if (obj.scale > 10) obj.scale = 10; updateElement3DTransform(obj); }); pushToUndo(); updateInfoBox(); }
// Drag handler in 3D context (moves XY plane) function dragHandler3D(e) { if (!draggedElement) return; e.preventDefault();
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
lastMousePos = { x: e.clientX, y: e.clientY };
currentSelection.forEach((obj) => {
if (!obj.position3D) return;
let moveX = dx;
let moveY = dy;
// Apply axis locking
if (dragAxisLock === "x") moveY = 0;
if (dragAxisLock === "y") moveX = 0;
// Adjust move for zoom level (camera distance) and invert Y because screen coords are inverted y axis
const zoomFactor = cameraDistance / 400;
obj.position3D.x += moveX * zoomFactor;
obj.position3D.y += moveY * zoomFactor;
if (snapToGridEnabled) {
obj.position3D.x = snapToGrid(obj.position3D.x);
obj.position3D.y = snapToGrid(obj.position3D.y);
}
updateElement3DTransform(obj);
});
updateInfoBox();
}
function dragEnd3D(e) { draggedElement = null; dragAxisLock = null; container.classList.remove("grabbing"); document.body.style.cursor = ""; document.removeEventListener("mousemove", dragHandler3D); document.removeEventListener("mouseup", dragEnd3D); pushToUndo(); }
// Adjust camera on mouse wheel zoom container.addEventListener("wheel", (e) => { e.preventDefault(); const delta = e.deltaY; cameraDistance += delta * 2; cameraDistance = Math.min(Math.max(200, cameraDistance), 3000); updateCameraTransform(); });
// Orbit and pan camera controls on container drag (right-click drag or middle-button drag) container.addEventListener("mousedown", (e) => { // Ignore dragging object on left click if ( e.target !== container && !e.target.classList.contains("object-box") ) return; if (e.button === 2 || e.button === 1) { lastMousePos = { x: e.clientX, y: e.clientY }; if (e.button === 2) { isOrbitingCamera = true; container.classList.add("grabbing"); } else if (e.button === 1) { isPanningCamera = true; container.classList.add("grabbing"); } document.body.style.cursor = "grabbing"; e.preventDefault(); document.addEventListener("mousemove", cameraDragHandler); document.addEventListener("mouseup", cameraDragEnd); } });
function cameraDragHandler(e) { e.preventDefault(); const dx = e.clientX - lastMousePos.x; const dy = e.clientY - lastMousePos.y; lastMousePos = { x: e.clientX, y: e.clientY };
if (isOrbitingCamera) {
// Rotate camera rotation angle by mouse drag amount
cameraRotation.y += dx * 0.5;
cameraRotation.x -= dy * 0.5;
cameraRotation.x = Math.max(-90, Math.min(90, cameraRotation.x));
} else if (isPanningCamera) {
// Pan camera (move container in x,y)
cameraPan.x += dx;
cameraPan.y += dy;
}
updateCameraTransform();
}
function cameraDragEnd(e) { isOrbitingCamera = false; isPanningCamera = false; container.classList.remove("grabbing"); document.body.style.cursor = ""; document.removeEventListener("mousemove", cameraDragHandler); document.removeEventListener("mouseup", cameraDragEnd); }
// Keyboard global shortcut listeners document.addEventListener("keydown", (e) => { // Ctrl+Z and Ctrl+Y if (e.ctrlKey && e.key.toLowerCase() === "z") { undo(); e.preventDefault(); return; } if (e.ctrlKey && e.key.toLowerCase() === "y") { redo(); e.preventDefault(); return; } // T toggles 3D view if (e.key.toLowerCase() === "t") { toggle3DView(); e.preventDefault(); return; } // Delete removes selected objects if (e.key === "Delete" || e.key === "Backspace") { if (currentSelection.length) { currentSelection.slice().forEach(removeObject); clearSelection(); e.preventDefault(); } } // Show/Hide help panel if (e.key === "h") { toggleHelp(); } });
// Clicking empty container clears selection container.addEventListener("mousedown", (e) => { if (e.target === container) clearSelection(); });
// Undo/Redo function undo() { if (undoStack.length === 0) { infoBox.textContent = "Nothing to undo"; return; } const lastState = undoStack.pop(); redoStack.push( objects.map((obj) => ({ id: obj.id, type: obj.type, style: { ...obj.element.style }, innerHTML: obj.element.innerHTML, contentEditable: obj.element.contentEditable, imgSrc: obj.imgSrc || null, rotationX: obj.rotationX || 0, rotationY: obj.rotationY || 0, rotationZ: obj.rotationZ || 0, scale: obj.scale || 1, position3D: { ...obj.position3D }, zIndex: obj.zIndex || 1, })) ); loadState(lastState); infoBox.textContent = "Undo performed"; }
function redo() { if (redoStack.length === 0) { infoBox.textContent = "Nothing to redo"; return; } const nextState = redoStack.pop(); undoStack.push( objects.map((obj) => ({ id: obj.id, type: obj.type, style: { ...obj.element.style }, innerHTML: obj.element.innerHTML, contentEditable: obj.element.contentEditable, imgSrc: obj.imgSrc || null, rotationX: obj.rotationX || 0, rotationY: obj.rotationY || 0, rotationZ: obj.rotationZ || 0, scale: obj.scale || 1, position3D: { ...obj.position3D }, zIndex: obj.zIndex || 1, })) ); loadState(nextState); infoBox.textContent = "Redo performed"; }
// Save and Load Layout function saveLayout() { try { localStorage.setItem( "layout", JSON.stringify( objects.map((o) => ({ id: o.id, type: o.type, style: { ...o.element.style }, innerHTML: o.element.innerHTML, contentEditable: o.element.contentEditable, imgSrc: o.imgSrc || null, rotationX: o.rotationX || 0, rotationY: o.rotationY || 0, rotationZ: o.rotationZ || 0, scale: o.scale || 1, position3D: o.position3D, zIndex: o.zIndex || 1, })) ) ); infoBox.textContent = "Layout saved"; } catch (err) { console.error("Saving failed", err); infoBox.textContent = "Save failed"; } } function loadLayout() { try { const saved = localStorage.getItem("layout"); if (saved) { const data = JSON.parse(saved); loadState(data); infoBox.textContent = "Layout loaded"; } else { infoBox.textContent = "No layout saved"; } } catch (err) { console.error("Load failed", err); infoBox.textContent = "Load failed"; } }
// Show/hide 3D view toggle function toggle3DView() { is3DViewEnabled = !is3DViewEnabled; if (is3DViewEnabled) { container.style.perspective = "1200px"; updateCameraTransform(); infoBox.textContent = "3D View enabled (Right drag orbit, Scroll zoom, Middle drag pan)"; } else { container.style.perspective = ""; container.style.transform = ""; infoBox.textContent = "3D View disabled"; } }
// Resize handles and rotation handles are not as heavily used here given 3D transform controls // (These can be enhanced separately with draggable 3D gizmo overlays if desired)
// Update settings panel with sliders and buttons function updateSettingsBox() { const sel = currentSelection[0]; const numSelected = currentSelection.length;
}
// Convert rgb() or rgba() string to hex (basic)
function rgbToHex(rgb) {
if (!rgb) return "#0077ff";
if (rgb.startsWith("#")) return rgb;
const res = rgb.match(/\d+/g);
if (!res || res.length < 3) return "#0077ff";
const r = parseInt(res[0]).toString(16).padStart(2, "0");
const g = parseInt(res[1]).toString(16).padStart(2, "0");
const b = parseInt(res[2]).toString(16).padStart(2, "0");
return #${r}${g}${b};
}
// Initialization function initializeUI() { updateSettingsBox(); updateCameraTransform(); infoBox.textContent = "Ready! Add objects and manipulate."; grid.style.opacity = snapToGridEnabled ? "1" : "0.15"; grid.style.display = gridVisible ? "block" : "none"; }
// Help toggle
function toggleHelp() {
if (helpBox.style.display === "block") {
helpBox.style.display = "none";
} else {
helpBox.style.display = "block";
helpBox.innerHTML = <h3>Keyboard & Controls Help</h3> <ul style="padding-left:18px; font-size:12px; line-height:1.4;"> <li><b>Click</b>: Select object</li> <li><b>Shift+Click</b>: Multi-select/Deselect</li> <li><b>Drag (Left Mouse)</b>: Move selected object(s)</li> <li><b>Shift (Hold While Dragging)</b>: Lock drag axis (X)</li> <li><b>Arrows</b>: Move selected object by small steps</li> <li><b>R</b>: Rotate selected object 15° Y axis</li> <li><b>S</b>: Scale Up selected object</li> <li><b>D</b>: Scale Down selected object</li> <li><b>T</b>: Toggle 3D View</li> <li><b>H</b>: Toggle this help</li> <li><b>Delete/Backspace</b>: Delete selected object(s)</li> <li><b>Mouse Wheel</b>: Zoom camera in/out (3D view)</li> <li><b>Right Click + Drag</b>: Orbit camera (3D view)</li> <li><b>Middle Click + Drag</b>: Pan camera (3D view)</li> </ul>;
}
}
initializeUI();
// Auto-save every 30 seconds autoSaveInterval = setInterval(saveLayout, 30000);
// Expose API for debugging window._editor3D = { spawnObject, selectObject, removeObject, clearSelection, undo, redo, saveLayout, loadLayout, toggle3DView, }; })();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>3D Manipulation Tool</title>
<style>
html, body {
margin:0; padding:0; overflow:hidden; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #202020 0%, #404040 100%);
color:#eee;
user-select:none;
height:100%;
width:100%;
}
#container {
position: relative;
width:100vw;
height:100vh;
background:#111;
perspective:1200px;
transform-style: preserve-3d;
overflow: visible;
perspective-origin: center center;
cursor: grab;
}
#container.grabbing {
cursor: grabbing;
}
#settingsBox {
position: fixed;
top: 10px; left: 10px;
width: 360px;
max-height: 90vh;
overflow-y: auto;
background: rgba(30,30,30,0.85);
box-shadow: 0 0 20px #00aaee77;
padding: 20px 24px;
border-radius: 18px;
font-size: 14px;
z-index: 9999;
color: #ccc;
user-select:none;
}
#settingsBox h3 {
margin-top: 0;
margin-bottom: 12px;
font-weight: 900;
color: #00aaff;
user-select:none;
}
#infoBox {
position: fixed;
bottom: 12px;
left: 12px;
background: rgba(0,0,0,0.7);
color: #0ff;
padding: 10px 16px;
border-radius: 10px;
font-weight: 600;
font-size: 13px;
z-index: 9998;
max-width: 360px;
user-select:none;
box-shadow: 0 0 15px #00aaff88 inset;
}
#helpBox {
position: fixed;
bottom: 12px;
right: 12px;
background: rgba(0,0,0,0.7);
color: #aaa;
padding: 10px 16px;
border-radius: 10px;
font-weight: 400;
font-size: 11px;
max-width: 380px;
max-height: 45vh;
overflow-y: auto;
z-index: 9998;
user-select:none;
box-shadow: 0 0 15px #00aaff88 inset;
display:none;
}
.object-box {
position: absolute;
border-radius: 12px;
box-shadow: 0 5px 15px rgba(0,170,255,0.8);
background: linear-gradient(145deg, #101010, #282828);
border: 2px solid #00aaff;
color: #0ff;
font-weight: 700;
user-select: none;
transition: box-shadow 0.3s ease, transform 0.3s ease;
cursor: grab;
overflow: visible;
backface-visibility: visible;
/* necessary for 3D transforms */
}
.object-box.grabbing {
cursor: grabbing;
}
.object-box.selected {
border-color: #00ffff;
box-shadow: 0 0 20px 3px #00ffff;
cursor: grabbing;
}
.object-box.text-block {
padding: 10px 14px;
overflow-wrap: break-word;
cursor: text;
user-select:text;
line-height: 1.3;
font-size: 15px;
background: linear-gradient(135deg, #004466, #0077aa);
border-radius: 12px;
border: 1.8px solid #00aaffaa;
}
.object-box img {
width: 100%;
height: 100%;
border-radius: 12px;
pointer-events:none;
user-select:none;
object-fit: cover;
filter: drop-shadow(0 0 6px #00aaffaa);
}
.resize-handle, .rotation-handle {
position: absolute;
border-radius: 50%;
border: 2px solid white;
user-select:none;
background: #00aaffcc;
box-shadow: 0 0 10px #00aaffcc;
transition: background 0.3s ease;
cursor: pointer;
z-index: 1000;
width: 18px;
height: 18px;
}
.resize-handle:hover, .rotation-handle:hover {
background: #0099ffdd;
box-shadow: 0 0 16px #0099ffdd;
}
.rotation-handle {
top: -40px;
left: 50%;
transform: translateX(-50%);
cursor: grab;
background: #33ccffcc;
box-shadow: 0 0 16px #33ccffcc;
}
#grid {
pointer-events:none;
position:absolute;
top:0;left:0;
width: 100%;
height: 100%;
background-size: 10px 10px;
background-image:
linear-gradient(to right, rgba(0,170,255,0.1) 1px, transparent 1px),
linear-gradient(to bottom, rgba(0,170,255,0.1) 1px, transparent 1px);
z-index: 0;
transition: opacity 0.3s ease;
}
input[type=range] {
width: 100%;
margin: 6px 0 12px 0;
cursor: pointer;
}
select, input[type=color], input[type=checkbox] {
cursor: pointer;
}
button {
display: inline-block;
font-size: 13px;
background: #0077aa;
border: none;
border-radius: 10px;
padding: 8px 14px;
margin: 4px 8px 8px 0;
color: #fff;
font-weight: 600;
cursor: pointer;
user-select:none;
transition: background-color 0.3s ease;
}
button:hover {
background: #00aaff;
box-shadow: 0 0 12px #00aaffaa;
}
/* Tooltip */
[data-tooltip] {
position: relative;
}
[data-tooltip]:hover::before {
content: attr(data-tooltip);
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
background: #00aaffdd;
color: #222;
padding: 4px 8px;
border-radius: 6px;
white-space: nowrap;
font-size: 11px;
font-weight: 700;
pointer-events: none;
opacity: 1;
transition: opacity 0.2s ease;
z-index: 10000;
}
[data-tooltip]:hover::after {
content: '';
position: absolute;
bottom: 115%;
left: 50%;
transform: translateX(-50%);
border-width: 6px;
border-style: solid;
border-color: #00aaffdd transparent transparent transparent;
pointer-events: none;
opacity: 1;
transition: opacity 0.2s ease;
z-index: 10000;
}
/* Resize handles visible */
.resize-handle.top-left { top: -9px; left: -9px; cursor: nwse-resize; }
.resize-handle.top-right { top: -9px; right: -9px; cursor: nesw-resize; }
.resize-handle.bottom-left { bottom: -9px; left: -9px; cursor: nesw-resize; }
.resize-handle.bottom-right { bottom: -9px; right: -9px; cursor: nwse-resize; }
/* Additional handles */
.resize-handle.top { top: -9px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.resize-handle.bottom { bottom: -9px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.resize-handle.left { left: -9px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.resize-handle.right { right: -9px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
/* Rotation handle */
.rotation-handle {
width: 22px;
height: 22px;
top: -50px;
left: 50%;
transform: translateX(-50%);
background: #33ccffcc;
box-shadow: 0 0 20px #33ccffcc;
cursor: grab;
border: 2.5px solid white;
z-index: 1100;
}
/* 3D glow */
.object-box.selected {
box-shadow: 0 0 30px 4px #00ffff, 0 0 20px 6px #00ffff inset;
}
</style>
</head>
<body>
<script>
(function () {
"use strict";
// ==== VARIABLES ====
const container = document.createElement("div");
const settingsBox = document.createElement("div");
const infoBox = document.createElement("div");
const helpBox = document.createElement("div");
// State
let currentSelection = []; // multi-select support
let draggedElement = null;
let dragOffset = { x: 0, y: 0 };
let currentHandle = null; // For resize/rotate handles
let dragAxisLock = null; // 'x' or 'y' if axis locked
let isDraggingEnabled = true;
let isResizingEnabled = true;
let isRotatingEnabled = true;
let isScalingEnabled = true;
let is3DViewEnabled = false;
let autoSaveInterval;
// Snap and Grid Settings
let snapToGridEnabled = true;
let snapGridSize = 10;
let gridVisible = true;
// Camera State for 3D interaction
let cameraRotation = { x: 15, y: 30 }; // degrees
let cameraDistance = 800; // zoom
let cameraPan = { x: 0, y: 0 };
let isOrbitingCamera = false;
let isPanningCamera = false;
let lastMousePos = null;
// Undo/Redo
let undoStack = [];
let redoStack = [];
// List of objects for persistence and reference
const objects = [];
// ==== SETUP CONTAINER AND UI ====
container.id = "container";
settingsBox.id = "settingsBox";
infoBox.id = "infoBox";
helpBox.id = "helpBox";
document.body.appendChild(container);
document.body.appendChild(settingsBox);
document.body.appendChild(infoBox);
document.body.appendChild(helpBox);
// Add grid overlay for snapping visualization
const grid = document.createElement("div");
grid.id = "grid";
container.appendChild(grid);
// ==== HELPER FUNCTIONS ====
// Generate unique IDs
function generateId() {
return "obj_" + Math.random().toString(36).substr(2, 9);
}
// Convert rgb() or rgba() string to hex (basic)
function rgbToHex(rgb) {
if (!rgb) return "#0077ff";
if (rgb.startsWith("#")) return rgb;
const res = rgb.match(/\d+/g);
if (!res || res.length < 3) return "#0077ff";
const r = parseInt(res[0]).toString(16).padStart(2, "0");
const g = parseInt(res[1]).toString(16).padStart(2, "0");
const b = parseInt(res[2]).toString(16).padStart(2, "0");
return `#${r}${g}${b}`;
}
// Push current state for Undo stack
function pushToUndo() {
const snapshot = objects.map((obj) => ({
id: obj.id,
type: obj.type,
style: { ...obj.element.style },
innerHTML: obj.element.innerHTML,
contentEditable: obj.element.contentEditable,
imgSrc: obj.imgSrc || null,
rotationX: obj.rotationX || 0,
rotationY: obj.rotationY || 0,
rotationZ: obj.rotationZ || 0,
scale: obj.scale || 1,
position3D: { ...obj.position3D },
zIndex: obj.zIndex || 1,
}));
undoStack.push(snapshot);
if (undoStack.length > 100) undoStack.shift();
redoStack = [];
}
// Load state from Undo/Redo or saved data
function loadState(state) {
// Remove all current objects from container
objects.forEach(o => {
if(container.contains(o.element)) container.removeChild(o.element);
});
objects.length = 0;
currentSelection.length = 0;
for (const data of state) {
let el;
switch(data.type) {
case 'box':
el = createBoxElement(
data.style.width,
data.style.height,
data.style.backgroundColor,
data.style.left,
data.style.top,
{ x: data.rotationX||0, y: data.rotationY||0, z: data.rotationZ||0 },
data.scale || 1,
data.zIndex || 1
);
break;
case 'text':
el = createTextBlockElement(
data.style.width,
data.style.height,
data.style.backgroundColor,
data.style.left,
data.style.top,
data.innerHTML,
data.contentEditable,
{ x: data.rotationX||0, y: data.rotationY||0, z: data.rotationZ||0},
data.scale || 1,
data.zIndex || 1
);
break;
case 'image':
el = createImageBlockElement(
data.style.width,
data.style.height,
data.style.left,
data.style.top,
data.imgSrc || null,
{ x: data.rotationX||0, y: data.rotationY||0, z: data.rotationZ||0 },
data.scale || 1,
data.zIndex || 1
);
break;
case 'gif':
el = createGifBlockElement(
data.style.width,
data.style.height,
data.style.left,
data.style.top,
data.imgSrc || null,
{ x:data.rotationX||0, y:data.rotationY||0, z:data.rotationZ||0 },
data.scale || 1,
data.zIndex || 1
);
break;
default:
continue;
}
el.id = data.id;
const obj = {
id: data.id,
type: data.type,
element: el,
rotationX: data.rotationX || 0,
rotationY: data.rotationY || 0,
rotationZ: data.rotationZ || 0,
scale: data.scale || 1,
position3D: data.position3D || { x:0, y:0, z:0 },
imgSrc: data.imgSrc,
zIndex: data.zIndex,
};
objects.push(obj);
container.appendChild(el);
updateElement3DTransform(obj);
enableInteractivity(el, obj);
}
updateInfoBox();
updateSettingsBox();
}
// Snap value to grid
function snapToGrid(value) {
if (!snapToGridEnabled) return value;
return Math.round(value / snapGridSize) * snapGridSize;
}
// Update info box text
function updateInfoBox() {
if (currentSelection.length === 1) {
const o = currentSelection[0];
infoBox.textContent = `Selected: ${o.type} (ID: ${o.id}) | Pos: X:${o.position3D.x.toFixed(0)} Y:${o.position3D.y.toFixed(0)} Z:${o.position3D.z.toFixed(0)} | Rot: X:${o.rotationX} Y:${o.rotationY} Z:${o.rotationZ} | Scale: ${o.scale.toFixed(2)}`;
} else if (currentSelection.length > 1) {
infoBox.textContent = `Selected ${currentSelection.length} objects`;
} else {
infoBox.textContent = "No objects selected";
}
}
// Create box element
function createBoxElement(width = "100px", height = "100px", bgColor = "#0077ff", left = "0px", top = "0px",
rotation = {x:0,y:0,z:0}, scale = 1, zIndex=1) {
const div = document.createElement("div");
div.classList.add("object-box", "box");
div.style.width = width;
div.style.height = height;
div.style.backgroundColor = bgColor;
div.style.zIndex = zIndex;
div.tabIndex = 0;
return div;
}
// Create text block element
function createTextBlockElement(
width = "150px",
height = "50px",
bgColor = "#004477",
left = "0px",
top = "0px",
content = "Editable Text",
editable = true,
rotation = {x:0,y:0,z:0},
scale = 1,
zIndex = 1
) {
const div = document.createElement("div");
div.classList.add("object-box", "text-block");
div.style.width = width;
div.style.height = height;
div.style.backgroundColor = bgColor;
div.innerHTML = content;
div.contentEditable = editable;
div.style.zIndex = zIndex;
div.tabIndex = 0;
return div;
}
// Create image block element
function createImageBlockElement(
width = "150px",
height = "150px",
left = "0px",
top = "0px",
src = null,
rotation = {x:0,y:0,z:0},
scale = 1,
zIndex = 1
) {
const div = document.createElement("div");
div.classList.add("object-box", "image-block");
div.style.width = width;
div.style.height = height;
div.style.zIndex = zIndex;
div.tabIndex = 0;
if (!src) src = prompt("Enter Image URL", "https://via.placeholder.com/150");
const img = document.createElement("img");
img.src = src;
div.appendChild(img);
return div;
}
// Create gif block element
function createGifBlockElement(
width = "150px",
height = "150px",
left = "0px",
top = "0px",
src = null,
rotation = {x:0,y:0,z:0},
scale = 1,
zIndex = 1
) {
const div = document.createElement("div");
div.classList.add("object-box", "gif-block");
div.style.width = width;
div.style.height = height;
div.style.zIndex = zIndex;
div.tabIndex = 0;
if (!src) src = "https://media.giphy.com/media/LmNwrBhejkK9EFP504/giphy.gif";
const img = document.createElement("img");
img.src = src;
div.appendChild(img);
return div;
}
// Compose CSS transform for object - 3D transforms and scaling
function composeObjectTransform(obj) {
const p = obj.position3D;
return `
translate3d(${p.x}px, ${p.y}px, ${p.z}px)
rotateX(${obj.rotationX}deg)
rotateY(${obj.rotationY}deg)
rotateZ(${obj.rotationZ}deg)
scale(${obj.scale})
`;
}
// Update element CSS transform for 3D
function updateElement3DTransform(obj) {
obj.element.style.transform = composeObjectTransform(obj);
if (obj.zIndex != null) obj.element.style.zIndex = obj.zIndex;
}
// Select object
function selectObject(obj, append = false) {
if (!append) clearSelection();
if (!currentSelection.includes(obj)) {
currentSelection.push(obj);
obj.element.classList.add("selected");
showHandles(obj);
}
updateSettingsBox();
updateInfoBox();
}
// Deselect object
function deselectObject(obj) {
const idx = currentSelection.indexOf(obj);
if (idx > -1) {
currentSelection.splice(idx, 1);
obj.element.classList.remove("selected");
hideHandles(obj);
}
updateSettingsBox();
updateInfoBox();
}
// Clear selection
function clearSelection() {
currentSelection.forEach(o => {
o.element.classList.remove("selected");
hideHandles(o);
});
currentSelection.length = 0;
updateSettingsBox();
updateInfoBox();
}
// Rich UI Controls: Handles for resizing and rotating
// Create/manipulate handles inside object-box elements for selected objects
function createHandles() {
const resizePositions = ["top-left", "top-right", "bottom-left", "bottom-right", "top", "bottom", "left", "right"];
const handles = {};
for (const pos of resizePositions) {
const resize = document.createElement("div");
resize.className = "resize-handle " + pos;
resize.dataset.action = "resize";
resize.dataset.position = pos;
handles[pos] = resize;
}
const rotationHandle = document.createElement("div");
rotationHandle.className = "rotation-handle";
rotationHandle.dataset.action = "rotate";
handles.rotation = rotationHandle;
return handles;
}
// Attach handles to an object, if not present
function attachHandles(obj) {
if (obj.handles) return obj.handles;
const handles = createHandles();
for (const handleKey in handles) {
obj.element.appendChild(handles[handleKey]);
}
obj.handles = handles;
return handles;
}
// Show handles on selected object
function showHandles(obj) {
if (!isResizingEnabled && !isRotatingEnabled) return;
const handles = attachHandles(obj);
for (const h in handles) {
handles[h].style.display = "block";
}
}
// Hide handles on deselect
function hideHandles(obj) {
if (!obj.handles) return;
for (const h in obj.handles) {
obj.handles[h].style.display = "none";
}
}
// Spawn object of given type at center
function spawnObject(type) {
const id = generateId();
const centerX = container.clientWidth / 2;
const centerY = container.clientHeight / 2;
let obj;
switch(type) {
case "box":
obj = {
id,
type,
element: createBoxElement("150px","150px","#0077ff","0px","0px",{x:0,y:0,z:0},1,1),
position3D: {x:centerX, y:centerY, z:0},
rotationX: 0, rotationY: 0, rotationZ: 0,
scale: 1,
zIndex: 1,
};
break;
case "text":
obj = {
id,
type,
element: createTextBlockElement("180px", "60px", "#004477", "0px", "0px", "Editable Text", true, {x:0,y:0,z:0},1,1),
position3D: {x:centerX, y:centerY, z:0},
rotationX: 0, rotationY: 0, rotationZ: 0,
scale: 1,
zIndex: 1,
};
break;
case "image":
obj = {
id,
type,
element: createImageBlockElement("150px","150px","0px","0px", null, {x:0,y:0,z:0},1,1),
position3D: {x:centerX, y:centerY, z:0},
rotationX: 0, rotationY: 0, rotationZ: 0,
scale: 1,
imgSrc: null,
zIndex: 1,
};
break;
case "gif":
obj = {
id,
type,
element: createGifBlockElement("150px", "150px", "0px", "0px", null, {x:0,y:0,z:0},1,1),
position3D: {x:centerX, y:centerY, z:0},
rotationX: 0, rotationY: 0, rotationZ: 0,
scale: 1,
imgSrc: null,
zIndex: 1,
};
break;
default:
console.warn("Unsupported object type:", type);
return;
}
container.appendChild(obj.element);
objects.push(obj);
updateElement3DTransform(obj);
enableInteractivity(obj.element, obj);
pushToUndo();
selectObject(obj);
}
// Enable interactivity on an element (drag, click, keyboard)
function enableInteractivity(el, obj) {
// Prevent dragging from interfering with text edit
el.addEventListener("mousedown", (e) => e.stopPropagation());
el.addEventListener("click", (e) => {
if(e.shiftKey && currentSelection.includes(obj)) {
deselectObject(obj);
} else if(e.shiftKey) {
selectObject(obj, true);
} else {
selectObject(obj, false);
}
e.preventDefault();
});
// Dragging setup - start drag or resize or rotate with handles
el.addEventListener("mousedown", (e) => {
if(e.button !== 0) return; // Left only
if (e.target.dataset.action === "resize" && isResizingEnabled) {
currentHandle = {type: "resize", position: e.target.dataset.position, obj};
lastMousePos = {x:e.clientX, y:e.clientY};
container.classList.add("grabbing");
document.body.style.cursor = e.target.style.cursor;
document.addEventListener("mousemove", resizeHandler);
document.addEventListener("mouseup", dragEnd);
e.preventDefault();
return;
}
if (e.target.dataset.action === "rotate" && isRotatingEnabled) {
currentHandle = {type: "rotate", obj};
lastMousePos = {x:e.clientX, y:e.clientY};
container.classList.add("grabbing");
document.body.style.cursor = "grabbing";
document.addEventListener("mousemove", rotateHandler);
document.addEventListener("mouseup", dragEnd);
e.preventDefault();
return;
}
if (!isDraggingEnabled) return;
draggedElement = obj;
lastMousePos = {x:e.clientX,y:e.clientY};
dragAxisLock = e.shiftKey ? "x" : null;
if(!currentSelection.includes(obj)) selectObject(obj,false);
container.classList.add("grabbing");
document.body.style.cursor = "grabbing";
e.preventDefault();
document.addEventListener("mousemove", dragHandler3D);
document.addEventListener("mouseup", dragEnd3D);
});
// Keyboard support:
el.addEventListener("keydown", (e) => {
switch(e.key.toLowerCase()) {
case "delete":
case "backspace":
currentSelection.slice().forEach(removeObject);
clearSelection();
break;
case "arrowup":
moveObject3DBy(e.shiftKey ? 1 : 10, "y", true);
break;
case "arrowdown":
moveObject3DBy(e.shiftKey ? -1 : -10, "y", true);
break;
case "arrowleft":
moveObject3DBy(e.shiftKey ? -1 : -10, "x", true);
break;
case "arrowright":
moveObject3DBy(e.shiftKey ? 1 : 10, "x", true);
break;
case "r":
if (e.shiftKey) rotateSelected("x", 15);
else rotateSelected("y", 15);
break;
case "e":
rotateSelected("z", 15);
break;
case "s":
scaleSelected(0.1);
break;
case "d":
scaleSelected(-0.1);
break;
default: return;
}
e.preventDefault();
});
// Focus styles
el.addEventListener("focus", () => {
el.style.boxShadow = "0 0 30px 4px #00aaffcc";
});
el.addEventListener("blur", () => {
el.style.boxShadow = "";
});
}
// Remove object from container, objects, selection
function removeObject(obj) {
if(!obj) return;
if(container.contains(obj.element)) container.removeChild(obj.element);
const idx = objects.findIndex(o=>o.id===obj.id);
if(idx>-1) objects.splice(idx,1);
if(currentSelection.includes(obj)) deselectObject(obj);
updateInfoBox();
pushToUndo();
}
// Clear all objects
function clearAllObjects() {
objects.forEach(o=> {
if(container.contains(o.element)) container.removeChild(o.element);
});
objects.length = 0;
currentSelection.length = 0;
updateInfoBox();
pushToUndo();
}
// Bring to front
function bringToFront(obj) {
const maxZ = Math.max(...objects.map(o => o.zIndex || 1), 1);
obj.zIndex = maxZ +1;
obj.element.style.zIndex = obj.zIndex;
pushToUndo();
}
// Send to back
function sendToBack(obj) {
const minZ = Math.min(...objects.map(o => o.zIndex || 1), 1);
obj.zIndex = minZ -1;
obj.element.style.zIndex = obj.zIndex;
pushToUndo();
}
// Move selected by delta on axis in object space
function moveObject3DBy(delta, axis, relative = false) {
currentSelection.forEach((obj)=> {
if(obj.position3D && axis in obj.position3D) {
obj.position3D[axis] += delta;
if(snapToGridEnabled) {
obj.position3D[axis] = snapToGrid(obj.position3D[axis]);
}
updateElement3DTransform(obj);
}
});
pushToUndo();
updateInfoBox();
}
// Rotate selected objects
function rotateSelected(axis, degrees) {
currentSelection.forEach(obj => {
switch(axis) {
case "x": obj.rotationX = (obj.rotationX + degrees) % 360; break;
case "y": obj.rotationY = (obj.rotationY + degrees) % 360; break;
case "z": obj.rotationZ = (obj.rotationZ + degrees) % 360; break;
}
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
}
// Scale selected objects by delta
function scaleSelected(delta) {
currentSelection.forEach(obj => {
obj.scale += delta;
if(obj.scale < 0.1) obj.scale = 0.1;
if(obj.scale > 10) obj.scale = 10;
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
}
// Drag handler for moving objects in 3D space (XY plane)
function dragHandler3D(e) {
if(!draggedElement) return;
e.preventDefault();
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
lastMousePos = {x:e.clientX, y:e.clientY};
currentSelection.forEach(obj => {
if(!obj.position3D) return;
let moveX = dx;
let moveY = dy;
if (dragAxisLock === "x") moveY = 0;
if (dragAxisLock === "y") moveX = 0;
const zoomFactor = cameraDistance / 400;
obj.position3D.x += moveX * zoomFactor;
obj.position3D.y += moveY * zoomFactor;
if(snapToGridEnabled) {
obj.position3D.x = snapToGrid(obj.position3D.x);
obj.position3D.y = snapToGrid(obj.position3D.y);
}
updateElement3DTransform(obj);
});
updateInfoBox();
}
// Drag end after moving objects
function dragEnd3D(e) {
draggedElement = null;
dragAxisLock = null;
container.classList.remove("grabbing");
document.body.style.cursor = "";
document.removeEventListener("mousemove", dragHandler3D);
document.removeEventListener("mouseup", dragEnd3D);
pushToUndo();
}
// Resize handler - resizes the object's width/height based on handles
function resizeHandler(e) {
e.preventDefault();
if (!currentHandle) return;
if (currentHandle.type !== "resize") return;
const obj = currentHandle.obj;
const rect = obj.element.getBoundingClientRect();
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
lastMousePos = {x:e.clientX, y:e.clientY};
// Determine changes based on handle position
let width = parseFloat(obj.element.style.width) || rect.width;
let height = parseFloat(obj.element.style.height) || rect.height;
let posX = obj.position3D.x;
let posY = obj.position3D.y;
switch(currentHandle.position) {
case "top-left":
width -= dx;
height -= dy;
posX += dx/2;
posY += dy/2;
break;
case "top-right":
width += dx;
height -= dy;
posX += dx/2;
posY += dy/2;
break;
case "bottom-left":
width -= dx;
height += dy;
posX += dx/2;
posY += dy/2;
break;
case "bottom-right":
width += dx;
height += dy;
posX += dx/2;
posY += dy/2;
break;
case "top":
height -= dy;
posY += dy/2;
break;
case "bottom":
height += dy;
posY += dy/2;
break;
case "left":
width -= dx;
posX += dx/2;
break;
case "right":
width += dx;
posX += dx/2;
break;
}
if(width < 20) width = 20;
if(height < 20) height = 20;
obj.element.style.width = width + "px";
obj.element.style.height = height + "px";
obj.position3D.x = posX;
obj.position3D.y = posY;
updateElement3DTransform(obj);
updateInfoBox();
}
// Rotate handler - rotate object by dragging rotation handle
function rotateHandler(e) {
e.preventDefault();
if (!currentHandle) return;
if (currentHandle.type !== "rotate") return;
const obj = currentHandle.obj;
// Calculate angle difference from center to mouse pos
const rect = obj.element.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
const dx = e.clientX - centerX;
const dy = e.clientY - centerY;
const angleDeg = Math.atan2(dy, dx) * (180/Math.PI); // In degrees
obj.rotationZ = angleDeg;
updateElement3DTransform(obj);
updateInfoBox();
}
// Drag end for resizing/rotation
function dragEnd(e) {
currentHandle = null;
container.classList.remove("grabbing");
document.body.style.cursor = "";
document.removeEventListener("mousemove", resizeHandler);
document.removeEventListener("mouseup", dragEnd);
document.removeEventListener("mousemove", rotateHandler);
document.removeEventListener("mouseup", dragEnd);
pushToUndo();
}
// Camera zoom with mouse wheel
container.addEventListener("wheel", (e) => {
e.preventDefault();
const delta = e.deltaY;
cameraDistance += delta * 2;
cameraDistance = Math.min(Math.max(200, cameraDistance), 3000);
updateCameraTransform();
});
// Orbit and pan camera controls on container drag (right/middle-btn drag)
container.addEventListener("mousedown", (e) => {
if (e.target !== container && !e.target.classList.contains("object-box")) return;
if (e.button === 2 || e.button === 1) {
lastMousePos = {x: e.clientX, y:e.clientY};
if (e.button === 2) {
isOrbitingCamera = true;
container.classList.add("grabbing");
} else if (e.button === 1) {
isPanningCamera = true;
container.classList.add("grabbing");
}
document.body.style.cursor = "grabbing";
e.preventDefault();
document.addEventListener("mousemove", cameraDragHandler);
document.addEventListener("mouseup", cameraDragEnd);
}
});
function cameraDragHandler(e) {
e.preventDefault();
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
lastMousePos = {x:e.clientX, y:e.clientY};
if (isOrbitingCamera) {
cameraRotation.y += dx * 0.5;
cameraRotation.x -= dy * 0.5;
cameraRotation.x = Math.max(-90, Math.min(90, cameraRotation.x));
} else if (isPanningCamera) {
cameraPan.x += dx;
cameraPan.y += dy;
}
updateCameraTransform();
}
function cameraDragEnd(e) {
isOrbitingCamera = false;
isPanningCamera = false;
container.classList.remove("grabbing");
document.body.style.cursor = "";
document.removeEventListener("mousemove", cameraDragHandler);
document.removeEventListener("mouseup", cameraDragEnd);
}
// Update container CSS transform for camera
function updateCameraTransform() {
container.style.transformOrigin = "50% 50%";
container.style.transform = `
translateX(${cameraPan.x}px)
translateY(${cameraPan.y}px)
translateZ(${-cameraDistance}px)
rotateX(${cameraRotation.x}deg)
rotateY(${cameraRotation.y}deg)
`;
}
// Keyboard shortcuts
document.addEventListener("keydown", e => {
if (e.ctrlKey && e.key.toLowerCase() === "z") {
undo();
e.preventDefault();
return;
}
if (e.ctrlKey && e.key.toLowerCase() === "y") {
redo();
e.preventDefault();
return;
}
if (e.key.toLowerCase() === "t") {
toggle3DView();
e.preventDefault();
return;
}
if (e.key === "Delete" || e.key === "Backspace") {
if(currentSelection.length) {
currentSelection.slice().forEach(removeObject);
clearSelection();
e.preventDefault();
}
}
if (e.key === "h") {
toggleHelp();
}
});
// Clicking empty container clears selection
container.addEventListener("mousedown", (e) => {
if(e.target === container) clearSelection();
});
// Undo functionality
function undo() {
if (undoStack.length === 0) {
infoBox.textContent = "Nothing to undo";
return;
}
const lastState = undoStack.pop();
redoStack.push(JSON.parse(JSON.stringify(objects.map(o => ({
id: o.id,
type: o.type,
style: {...o.element.style},
innerHTML: o.element.innerHTML,
contentEditable: o.element.contentEditable,
imgSrc: o.imgSrc || null,
rotationX: o.rotationX || 0,
rotationY: o.rotationY || 0,
rotationZ: o.rotationZ || 0,
scale: o.scale || 1,
position3D: {...o.position3D},
zIndex: o.zIndex || 1,
})))));
loadState(lastState);
infoBox.textContent = "Undo performed";
}
// Redo functionality
function redo() {
if (redoStack.length === 0) {
infoBox.textContent = "Nothing to redo";
return;
}
const nextState = redoStack.pop();
undoStack.push(JSON.parse(JSON.stringify(objects.map(o => ({
id: o.id,
type: o.type,
style: {...o.element.style},
innerHTML: o.element.innerHTML,
contentEditable: o.element.contentEditable,
imgSrc: o.imgSrc || null,
rotationX: o.rotationX || 0,
rotationY: o.rotationY || 0,
rotationZ: o.rotationZ || 0,
scale: o.scale || 1,
position3D: {...o.position3D},
zIndex: o.zIndex || 1,
})))));
loadState(nextState);
infoBox.textContent = "Redo performed";
}
// Save and Load Layout using localStorage
function saveLayout() {
try {
localStorage.setItem("layout", JSON.stringify(objects.map(o => ({
id: o.id,
type: o.type,
style: {...o.element.style},
innerHTML: o.element.innerHTML,
contentEditable: o.element.contentEditable,
imgSrc: o.imgSrc || null,
rotationX: o.rotationX || 0,
rotationY: o.rotationY || 0,
rotationZ: o.rotationZ || 0,
scale: o.scale || 1,
position3D: o.position3D,
zIndex: o.zIndex || 1,
}))));
infoBox.textContent = "Layout saved";
} catch(err) {
console.error(err);
infoBox.textContent = "Save failed";
}
}
function loadLayout() {
try {
const data = localStorage.getItem("layout");
if(data) {
loadState(JSON.parse(data));
infoBox.textContent = "Layout loaded";
} else {
infoBox.textContent = "No layout saved";
}
} catch(err) {
console.error(err);
infoBox.textContent = "Load failed";
}
}
// Toggle 3D view
function toggle3DView() {
is3DViewEnabled = !is3DViewEnabled;
if(is3DViewEnabled) {
container.style.perspective = "1200px";
updateCameraTransform();
infoBox.textContent = "3D View enabled (Right drag orbit, Scroll zoom, Middle drag pan)";
} else {
container.style.perspective = "";
container.style.transform = "";
infoBox.textContent = "3D View disabled";
}
updateSettingsBox();
}
// Update settings panel with controls
function updateSettingsBox() {
const sel = currentSelection[0];
const numSelected = currentSelection.length;
const maxX = container.clientWidth;
const maxY = container.clientHeight;
const bgColorVal = sel?.element?.style?.backgroundColor || "#0077ff";
const contentHTML = sel?.element?.innerHTML || "";
settingsBox.innerHTML = `
<h3>3D Manipulation Tool (${numSelected} selected)</h3>
<label for="objectType">Add Object:</label>
<select id="objectType" aria-label="Select object type to add">
<option value="box">3D Box</option>
<option value="text">Text Block</option>
<option value="image">Image Block</option>
<option value="gif">GIF Block</option>
</select>
<button id="addObjectBtn" aria-label="Add selected object type">Add Object</button>
<hr>
<div style="${numSelected === 0 ? "display:none;" : ""}">
<strong>Selected Object Settings</strong>
<p style="font-size:12px; color:#44cfff;">
Click text blocks to edit text.<br/>
Use keyboard arrows for fine movement.<br/>
Hold SHIFT to lock drag axis.<br/>
Use resize handles to resize.<br/>
Use rotation handle above to rotate.<br/>
</p>
<label>Position X:
<input type="range" id="posX" min="0" max="${maxX}" value="${sel?.position3D?.x || 0}">
<span id="posXVal">${sel?.position3D?.x.toFixed(0) || 0}</span> px
</label>
<label>Position Y:
<input type="range" id="posY" min="0" max="${maxY}" value="${sel?.position3D?.y || 0}">
<span id="posYVal">${sel?.position3D?.y.toFixed(0) || 0}</span> px
</label>
<label>Position Z (Depth):
<input type="range" id="posZ" min="-500" max="500" value="${sel?.position3D?.z || 0}">
<span id="posZVal">${sel?.position3D?.z.toFixed(0) || 0}</span> px
</label>
<label>Rotation X:
<input type="range" id="rotX" min="0" max="360" value="${sel?.rotationX || 0}">
<span id="rotXVal">${sel?.rotationX || 0}°</span>
</label>
<label>Rotation Y:
<input type="range" id="rotY" min="0" max="360" value="${sel?.rotationY || 0}">
<span id="rotYVal">${sel?.rotationY || 0}°</span>
</label>
<label>Rotation Z:
<input type="range" id="rotZ" min="0" max="360" value="${sel?.rotationZ || 0}">
<span id="rotZVal">${sel?.rotationZ || 0}°</span>
</label>
<label>Scale:
<input type="range" id="scaleSlider" min="0.1" max="5" step="0.01" value="${sel?.scale || 1}">
<span id="scaleVal">${sel?.scale?.toFixed(2) || "1.00"}</span>
</label>
<label>Background Color:
<input type="color" id="bgColor" value="${rgbToHex(bgColorVal)}">
</label>
${
sel?.type === "text"
? `<label>Text Content:<br>
<textarea id="textContent" rows="3" style="width:100%">${contentHTML}</textarea>
</label>`
: ""
}
<button id="removeSelectedBtn">Remove Selected</button>
<button id="bringFrontBtn">Bring to Front</button>
<button id="sendBackBtn">Send to Back</button>
<button id="resetPropsBtn">Reset Properties</button>
</div>
<hr>
<label><input type="checkbox" id="dragToggle" ${isDraggingEnabled ? "checked" : ""}> Enable Dragging</label><br>
<label><input type="checkbox" id="resizeToggle" ${isResizingEnabled ? "checked" : ""}> Enable Resizing</label><br>
<label><input type="checkbox" id="rotateToggle" ${isRotatingEnabled ? "checked" : ""}> Enable Rotation</label><br>
<label><input type="checkbox" id="snapToggle" ${snapToGridEnabled ? "checked" : ""}> Enable Snap to Grid</label><br>
<label><input type="checkbox" id="gridToggle" ${gridVisible ? "checked" : ""}> Show Grid</label><br>
<button id="undoBtn">Undo (Ctrl+Z)</button>
<button id="redoBtn">Redo (Ctrl+Y)</button>
<button id="saveBtn">Save Layout</button>
<button id="loadBtn">Load Layout</button>
<button id="clearBtn">Clear All</button>
<button id="resetCameraBtn">Reset Camera</button>
<button id="helpToggleBtn">Toggle Keyboard Help (H)</button>
<hr>
<button id="toggle3DBtn">${is3DViewEnabled ? "Disable" : "Enable"} 3D View (T)</button>
`;
// Event Bindings:
settingsBox.querySelector("#addObjectBtn").onclick = () => {
spawnObject(settingsBox.querySelector("#objectType").value);
};
if(numSelected === 0) return;
const posXel = settingsBox.querySelector("#posX");
const posYel = settingsBox.querySelector("#posY");
const posZel = settingsBox.querySelector("#posZ");
const rotXel = settingsBox.querySelector("#rotX");
const rotYel = settingsBox.querySelector("#rotY");
const rotZel = settingsBox.querySelector("#rotZ");
const scaleEl = settingsBox.querySelector("#scaleSlider");
const bgColorEl = settingsBox.querySelector("#bgColor");
const textContentEl = settingsBox.querySelector("#textContent");
posXel && (posXel.oninput = e => {
posXel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach(o => {
o.position3D.x = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
posYel && (posYel.oninput = e => {
posYel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach(o => {
o.position3D.y = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
posZel && (posZel.oninput = e => {
posZel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach(o => {
o.position3D.z = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
rotXel && (rotXel.oninput = e => {
rotXel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach(o => {
o.rotationX = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
rotYel && (rotYel.oninput = e => {
rotYel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach(o => {
o.rotationY = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
rotZel && (rotZel.oninput = e => {
rotZel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach(o => {
o.rotationZ = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
scaleEl && (scaleEl.oninput = e => {
scaleEl.nextElementSibling.textContent = Number(e.target.value).toFixed(2);
currentSelection.forEach(o => {
o.scale = Number(e.target.value);
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
});
bgColorEl && (bgColorEl.oninput = e => {
currentSelection.forEach(o => {
if(o.type === "box" || o.type === "text") {
o.element.style.backgroundColor = e.target.value;
}
});
pushToUndo();
});
if(textContentEl) {
textContentEl.oninput = e => {
currentSelection.forEach(o => {
if(o.type === "text") {
o.element.innerHTML = e.target.value;
}
});
pushToUndo();
};
}
settingsBox.querySelector("#removeSelectedBtn").onclick = () => {
currentSelection.slice().forEach(removeObject);
clearSelection();
};
settingsBox.querySelector("#bringFrontBtn").onclick = () => currentSelection.forEach(bringToFront);
settingsBox.querySelector("#sendBackBtn").onclick = () => currentSelection.forEach(sendToBack);
settingsBox.querySelector("#resetPropsBtn").onclick = () => {
currentSelection.forEach(o => {
o.rotationX = 0;
o.rotationY = 0;
o.rotationZ = 0;
o.scale = 1;
updateElement3DTransform(o);
});
pushToUndo();
updateInfoBox();
};
// Toggles
settingsBox.querySelector("#dragToggle").onchange = e => { isDraggingEnabled = e.target.checked; updateSettingsBox(); };
settingsBox.querySelector("#resizeToggle").onchange = e => { isResizingEnabled = e.target.checked; updateSettingsBox(); };
settingsBox.querySelector("#rotateToggle").onchange = e => { isRotatingEnabled = e.target.checked; updateSettingsBox(); };
settingsBox.querySelector("#snapToggle").onchange = e => {
snapToGridEnabled = e.target.checked;
grid.style.opacity = snapToGridEnabled ? "1" : "0.15";
};
settingsBox.querySelector("#gridToggle").onchange = e => {
gridVisible = e.target.checked;
grid.style.display = gridVisible ? "block" : "none";
};
settingsBox.querySelector("#undoBtn").onclick = undo;
settingsBox.querySelector("#redoBtn").onclick = redo;
settingsBox.querySelector("#saveBtn").onclick = saveLayout;
settingsBox.querySelector("#loadBtn").onclick = loadLayout;
settingsBox.querySelector("#clearBtn").onclick = () => {
if(confirm("Clear all objects? This cannot be undone.")) clearAllObjects();
};
settingsBox.querySelector("#resetCameraBtn").onclick = () => {
cameraRotation = {x:15,y:30};
cameraDistance = 800;
cameraPan = {x:0,y:0};
updateCameraTransform();
};
settingsBox.querySelector("#helpToggleBtn").onclick = toggleHelp;
settingsBox.querySelector("#toggle3DBtn").onclick = toggle3DView;
}
// Show/hide help box
function toggleHelp() {
if (helpBox.style.display === "block") {
helpBox.style.display = "none";
} else {
helpBox.style.display = "block";
helpBox.innerHTML = `<h3>Keyboard & Controls Help</h3>
<ul style="padding-left:18px; font-size:12px; line-height:1.4;">
<li><b>Click</b>: Select object</li>
<li><b>Shift+Click</b>: Multi-select/Deselect</li>
<li><b>Drag (Left Mouse)</b>: Move selected object(s)</li>
<li><b>Hold SHIFT</b>: Lock drag axis (X)</li>
<li><b>Arrow Keys</b>: Move selected object by small steps</li>
<li><b>R</b>: Rotate selected on Y axis (Shift+R X axis, E for Z axis)</li>
<li><b>S</b>: Scale Up selected object</li>
<li><b>D</b>: Scale Down selected object</li>
<li><b>T</b>: Toggle 3D View</li>
<li><b>H</b>: Toggle this help</li>
<li><b>Delete/Backspace</b>: Delete selected object(s)</li>
<li><b>Mouse Wheel</b>: Zoom camera in/out (3D view)</li>
<li><b>Right Click + Drag</b>: Orbit camera (3D view)</li>
<li><b>Middle Click + Drag</b>: Pan camera (3D view)</li>
<li><b>Resize Handles</b>: Drag corners or edges to resize objects</li>
<li><b>Rotation Handle</b>: Drag handle above object to rotate around Z axis</li>
</ul>`;
}
}
// Initialization: Setup UI and update
function initializeUI() {
updateSettingsBox();
updateCameraTransform();
infoBox.textContent = "Ready! Add objects and manipulate.";
grid.style.opacity = snapToGridEnabled ? "1" : "0.15";
grid.style.display = gridVisible ? "block" : "none";
}
// Initialize at load
initializeUI();
// Auto-save every 30 seconds
autoSaveInterval = setInterval(saveLayout, 30000);
// Expose API for debugging
window._editor3D = {
spawnObject,
selectObject,
removeObject,
clearSelection,
undo,
redo,
saveLayout,
loadLayout,
toggle3DView,
};
})();
</script>
</body>
</html>
Open this complete HTML file in any modern browser to get a fully interactive 3D manipulation tool with good UX and 40+ interactive capabilities covering adding, selecting, dragging, resizing, rotating, zooming, camera movement, undo/redo, saving/loading, multi-select, snap to grid, color picking, text editing, and more.
If you want further specific features added or customization, feel free to ask!
Not yet included in a published release.
const maxX = container.clientWidth;
const maxY = container.clientHeight;
const bgColorVal = sel?.element?.style?.backgroundColor || "#0077ff";
const contentHTML = sel?.element?.innerHTML || "";
settingsBox.innerHTML = `
<h3>3D Manipulation Tool (${numSelected} selected)</h3>
<label for="objectType">Add Object:</label>
<select id="objectType" aria-label="Select object type to add">
<option value="box">3D Box</option>
<option value="text">Text Block</option>
<option value="image">Image Block</option>
<option value="gif">GIF Block</option>
</select>
<button id="addObjectBtn" aria-label="Add selected object type">Add Object</button>
<hr>
<div style="${numSelected === 0 ? "display:none;" : ""}">
<strong>Selected Object Settings</strong>
<p style="font-size:12px; color:#44cfff;">
To edit text, click inside text blocks.<br/>
Use keyboard arrows for small movements.<br/>
Hold SHIFT while dragging to lock movement axis.
</p>
<label>Position X:
<input type="range" id="posX" min="0" max="${maxX}" value="${sel?.position3D?.x || 0}">
<span id="posXVal">${sel?.position3D?.x.toFixed(0) || 0}</span> px
</label>
<label>Position Y:
<input type="range" id="posY" min="0" max="${maxY}" value="${sel?.position3D?.y || 0}">
<span id="posYVal">${sel?.position3D?.y.toFixed(0) || 0}</span> px
</label>
<label>Position Z (Depth):
<input type="range" id="posZ" min="-500" max="500" value="${sel?.position3D?.z || 0}">
<span id="posZVal">${sel?.position3D?.z.toFixed(0) || 0}</span> px
</label>
<label>Rotation X:
<input type="range" id="rotX" min="0" max="360" value="${sel?.rotationX || 0}">
<span id="rotXVal">${sel?.rotationX || 0}°</span>
</label>
<label>Rotation Y:
<input type="range" id="rotY" min="0" max="360" value="${sel?.rotationY || 0}">
<span id="rotYVal">${sel?.rotationY || 0}°</span>
</label>
<label>Rotation Z:
<input type="range" id="rotZ" min="0" max="360" value="${sel?.rotationZ || 0}">
<span id="rotZVal">${sel?.rotationZ || 0}°</span>
</label>
<label>Scale:
<input type="range" id="scaleSlider" min="0.1" max="5" step="0.01" value="${sel?.scale || 1}">
<span id="scaleVal">${sel?.scale?.toFixed(2) || "1.00"}</span>
</label>
<label>Background Color:
<input type="color" id="bgColor" value="${rgbToHex(bgColorVal)}">
</label>
${
sel?.type === "text"
? `<label>Text Content:<br>
<textarea id="textContent" rows="3" style="width:100%">${contentHTML}</textarea>
</label>`
: ""
}
<button id="removeSelectedBtn">Remove Selected</button>
<button id="bringFrontBtn">Bring to Front</button>
<button id="sendBackBtn">Send to Back</button>
<button id="resetPropsBtn">Reset Properties</button>
</div>
<hr>
<label><input type="checkbox" id="dragToggle" ${
isDraggingEnabled ? "checked" : ""
}> Enable Dragging</label><br>
<label><input type="checkbox" id="resizeToggle" ${
isResizingEnabled ? "checked" : ""
}> Enable Resizing (NOT Implemented)</label><br>
<label><input type="checkbox" id="rotateToggle" ${
isRotatingEnabled ? "checked" : ""
}> Enable Rotation (Keyboard)</label><br>
<label><input type="checkbox" id="snapToggle" ${
snapToGridEnabled ? "checked" : ""
}> Enable Snap to Grid</label><br>
<label><input type="checkbox" id="gridToggle" ${
gridVisible ? "checked" : ""
}> Show Grid</label><br>
<button id="undoBtn">Undo (Ctrl+Z)</button>
<button id="redoBtn">Redo (Ctrl+Y)</button>
<button id="saveBtn">Save Layout</button>
<button id="loadBtn">Load Layout</button>
<button id="clearBtn">Clear All</button>
<button id="resetCameraBtn">Reset Camera</button>
<button id="helpToggleBtn">Toggle Keyboard Help (H)</button>
<hr>
<button id="toggle3DBtn">${
is3DViewEnabled ? "Disable" : "Enable"
} 3D View (T)</button>
`;
// Add Object event
settingsBox.querySelector("#addObjectBtn").onclick = () => {
const typ = settingsBox.querySelector("#objectType").value;
spawnObject(typ);
};
if (numSelected === 0) return;
// Cache elements for update events
const posXel = settingsBox.querySelector("#posX");
const posYel = settingsBox.querySelector("#posY");
const posZel = settingsBox.querySelector("#posZ");
const rotXel = settingsBox.querySelector("#rotX");
const rotYel = settingsBox.querySelector("#rotY");
const rotZel = settingsBox.querySelector("#rotZ");
const scaleEl = settingsBox.querySelector("#scaleSlider");
const bgColorEl = settingsBox.querySelector("#bgColor");
const textContentEl = settingsBox.querySelector("#textContent");
// Updates to positions (slider)
if (posXel)
posXel.oninput = (e) => {
posXel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach((obj) => {
obj.position3D.x = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
if (posYel)
posYel.oninput = (e) => {
posYel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach((obj) => {
obj.position3D.y = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
if (posZel)
posZel.oninput = (e) => {
posZel.nextElementSibling.textContent = e.target.value + " px";
currentSelection.forEach((obj) => {
obj.position3D.z = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
// Rotation sliders
if (rotXel)
rotXel.oninput = (e) => {
rotXel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach((obj) => {
obj.rotationX = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
if (rotYel)
rotYel.oninput = (e) => {
rotYel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach((obj) => {
obj.rotationY = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
if (rotZel)
rotZel.oninput = (e) => {
rotZel.nextElementSibling.textContent = e.target.value + "°";
currentSelection.forEach((obj) => {
obj.rotationZ = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
// Scale slider
if (scaleEl)
scaleEl.oninput = (e) => {
scaleEl.nextElementSibling.textContent = Number(e.target.value).toFixed(2);
currentSelection.forEach((obj) => {
obj.scale = Number(e.target.value);
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
// Background color picker
if (bgColorEl)
bgColorEl.oninput = (e) => {
currentSelection.forEach((obj) => {
if (obj.type === "box" || obj.type === "text") {
obj.element.style.backgroundColor = e.target.value;
}
});
pushToUndo();
};
// Text editing
if (textContentEl)
textContentEl.oninput = (e) => {
currentSelection.forEach((obj) => {
if (obj.type === "text") {
obj.element.innerHTML = e.target.value;
}
});
pushToUndo();
};
// Buttons handlers
settingsBox.querySelector("#removeSelectedBtn").onclick = () => {
currentSelection.slice().forEach(removeObject);
clearSelection();
};
settingsBox.querySelector("#bringFrontBtn").onclick = () => {
currentSelection.forEach(bringToFront);
};
settingsBox.querySelector("#sendBackBtn").onclick = () => {
currentSelection.forEach(sendToBack);
};
settingsBox.querySelector("#resetPropsBtn").onclick = () => {
currentSelection.forEach((obj) => {
obj.rotationX = 0;
obj.rotationY = 0;
obj.rotationZ = 0;
obj.scale = 1;
updateElement3DTransform(obj);
});
pushToUndo();
updateInfoBox();
};
// Toggles & buttons
const dragToggle = settingsBox.querySelector("#dragToggle");
dragToggle.onchange = (e) => {
isDraggingEnabled = e.target.checked;
};
const snapToggle = settingsBox.querySelector("#snapToggle");
snapToggle.onchange = (e) => {
snapToGridEnabled = e.target.checked;
if (snapToGridEnabled) grid.style.opacity = "1";
else grid.style.opacity = "0.15";
};
const gridToggle = settingsBox.querySelector("#gridToggle");
gridToggle.onchange = (e) => {
gridVisible = e.target.checked;
grid.style.display = gridVisible ? "block" : "none";
};
settingsBox.querySelector("#undoBtn").onclick = undo;
settingsBox.querySelector("#redoBtn").onclick = redo;
settingsBox.querySelector("#saveBtn").onclick = saveLayout;
settingsBox.querySelector("#loadBtn").onclick = loadLayout;
settingsBox.querySelector("#clearBtn").onclick = () => {
if (confirm("Clear all objects? This cannot be undone.")) {
clearAllObjects();
}
};
settingsBox.querySelector("#resetCameraBtn").onclick = () => {
cameraRotation = { x: 15, y: 30 };
cameraDistance = 800;
cameraPan = { x: 0, y: 0 };
updateCameraTransform();
};
settingsBox.querySelector("#helpToggleBtn").onclick = toggleHelp;
settingsBox.querySelector("#toggle3DBtn").onclick = toggle3DView;