Sure! Below is an extended version of the previous code that adds circular handles around the boxes, allowing for resizing and movement similar to tools found in game development environments like Roblox Studio.
In this version, I've included:
- Circle handles: For resizing and rotating the selected box.
- A 3D view mode: Allows users to switch to a mode where they can navigate the 3D space.
- Additional functions and features, expanding the interactivity.
Complete JavaScript Code
Here's the complete script:
(function () {
// Create main UI elements
const settingsBox = document.createElement('div');
const infoBox = document.createElement('div');
const container = document.createElement('div');
let currentBox = null;
let isDraggingEnabled = true;
let isResizingEnabled = false;
let isRotatingEnabled = false;
let is3DView = false;
let camera, renderer;
// Styles for UI elements
Object.assign(settingsBox.style, {
position: 'fixed',
top: '10px',
left: '10px',
background: 'rgba(255, 255, 255, 0.9)',
padding: '20px',
border: '1px solid #ccc',
borderRadius: '5px',
zIndex: '10'
});
Object.assign(infoBox.style, {
position: 'fixed',
bottom: '10px',
left: '10px',
background: 'rgba(255, 255, 255, 0.9)',
padding: '10px',
borderRadius: '5px',
zIndex: '10'
});
Object.assign(container.style, {
width: '100vw',
height: '100vh',
position: 'relative',
overflow: 'hidden',
background: '#f0f0f0',
border: '1px solid #000'
});
document.body.appendChild(container);
document.body.appendChild(settingsBox);
document.body.appendChild(infoBox);
// Initialize the settings box
function initializeSettings() {
settingsBox.innerHTML = `
<h3>Box Settings</h3>
<label for="boxSize">Box Size:</label>
<input type="range" id="boxSize" min="50" max="300" value="100" onchange="changeBoxSize(this.value)">
<label for="boxRotation">Box Rotation:</label>
<input type="range" id="boxRotation" min="0" max="360" value="0" onchange="changeBoxRotation(this.value)">
<label for="boxColor">Box Color:</label>
<input type="color" id="boxColor" onchange="changeBoxColor(this.value)">
<button onclick="addBox()">Add Box</button>
<button onclick="removeCurrentBox()">Remove Current Box</button>
<button onclick="resetView()">Reset View</button>
<button onclick="toggle3DView()">Toggle 3D View</button>
<label>
<input type="checkbox" id="toggleDrag" checked onchange="toggleDragging(this.checked)"> Enable Dragging
</label>
<label>
<input type="checkbox" id="toggleResize" onchange="toggleResizing(this.checked)"> Enable Resizing
</label>
`;
}
// Change Box Size
window.changeBoxSize = function (size) {
if (currentBox) {
currentBox.style.width = size + 'px';
currentBox.style.height = size + 'px';
updateResizeHandles(currentBox);
}
};
// Change Box Rotation
window.changeBoxRotation = function (rotation) {
if (currentBox) {
currentBox.style.transform = `rotate(${rotation}deg)`;
}
};
// Change Box Color
window.changeBoxColor = function (color) {
if (currentBox) {
currentBox.style.backgroundColor = color;
}
};
// Add a new Box
window.addBox = function () {
const box = document.createElement('div');
box.className = 'box';
box.style.width = '100px';
box.style.height = '100px';
box.style.position = 'absolute';
box.style.backgroundColor = 'coral';
box.style.left = Math.random() * 80 + 'vw'; // Random position
box.style.top = Math.random() * 80 + 'vh'; // Random position
box.addEventListener('mousedown', () => {
currentBox = box; // Select this box
infoBox.textContent = "Selected Box!";
initializeSettings();
createResizeHandles(box);
createRotationHandles(box);
});
container.appendChild(box);
updateResizeHandles(box);
};
// Create resize handles around the selected box
function createResizeHandles(box) {
removeResizeHandles(); // Remove existing handles
const size = parseInt(box.style.width);
const corners = [
{ x: 0, y: 0, cursor: 'nwse-resize' }, // Top left
{ x: size, y: 0, cursor: 'nesw-resize' }, // Top right
{ x: 0, y: size, cursor: 'nesw-resize' }, // Bottom left
{ x: size, y: size, cursor: 'nwse-resize' }, // Bottom right
];
corners.forEach(corner => {
const handle = document.createElement('div');
handle.style.position = 'absolute';
handle.style.width = '10px';
handle.style.height = '10px';
handle.style.backgroundColor = 'blue';
handle.style.cursor = corner.cursor;
handle.style.left = corner.x - 5 + 'px';
handle.style.top = corner.y - 5 + 'px';
handle.className = 'resize-handle';
// Add resizing logic on mousedown event
handle.addEventListener('mousedown', (e) => {
let startX = e.clientX;
let startY = e.clientY;
e.stopPropagation(); // Prevent box select
const resize = (event) => {
const dx = event.clientX - startX;
const dy = event.clientY - startY;
if (corner.cursor === 'nwse-resize') {
box.style.width = Math.max(50, size + dx) + 'px';
box.style.height = Math.max(50, size + dy) + 'px';
}
if (corner.cursor === 'nesw-resize') {
box.style.width = Math.max(50, size - dx) + 'px';
box.style.height = Math.max(50, size + dy) + 'px';
}
updateResizeHandles(box); // Update handles on resize
};
const stopResize = () => {
document.removeEventListener('mousemove', resize);
document.removeEventListener('mouseup', stopResize);
};
document.addEventListener('mousemove', resize);
document.addEventListener('mouseup', stopResize);
});
container.appendChild(handle);
});
}
// Create rotation handles
function createRotationHandles(box) {
const rotationHandle = document.createElement('div');
rotationHandle.style.position = 'absolute';
rotationHandle.style.width = '10px';
rotationHandle.style.height = '10px';
rotationHandle.style.backgroundColor = 'red';
rotationHandle.style.cursor = 'pointer';
rotationHandle.style.left = (parseInt(box.style.left) + parseInt(box.style.width) / 2) - 5 + 'px';
rotationHandle.style.top = (parseInt(box.style.top) - 15) + 'px'; // Position above
rotationHandle.addEventListener('mousedown', (e) => {
e.stopPropagation(); // Prevent box selection
let startAngle = Math.atan2(e.clientY - parseInt(box.style.top), e.clientX - (parseInt(box.style.left) + parseInt(box.style.width) / 2));
let boxCenterX = parseInt(box.style.left) + parseInt(box.style.width) / 2;
let boxCenterY = parseInt(box.style.top) + parseInt(box.style.height) / 2;
const rotate = (event) => {
let currentAngle = Math.atan2(event.clientY - boxCenterY, event.clientX - boxCenterX);
let angleDiff = currentAngle - startAngle;
box.style.transform = `rotate(${angleDiff * (180 / Math.PI)}deg)`;
};
const stopRotate = () => {
document.removeEventListener('mousemove', rotate);
document.removeEventListener('mouseup', stopRotate);
};
document.addEventListener('mousemove', rotate);
document.addEventListener('mouseup', stopRotate);
});
container.appendChild(rotationHandle);
}
// Remove all resize handles
function removeResizeHandles() {
const handles = container.querySelectorAll('.resize-handle');
handles.forEach(handle => container.removeChild(handle));
}
// Remove current selected box
window.removeCurrentBox = function () {
if (currentBox) {
container.removeChild(currentBox);
currentBox = null;
infoBox.textContent = "Click a box to select it!";
removeResizeHandles();
}
};
// Reset the current view of the box
window.resetView = function () {
if (currentBox) {
currentBox.style.transform = 'rotate(0deg)';
currentBox.style.left = '0px';
currentBox.style.top = '0px';
initializeSettings();
}
};
// Toggle dragging functionality
window.toggleDragging = function (checked) {
isDraggingEnabled = checked;
};
// Toggle 3D View
window.toggle3DView = function () {
is3DView = !is3DView;
if (is3DView) {
enable3DView();
} else {
disable3DView();
}
};
// Enabling 3D view with WASD movement
function enable3DView() {
container.requestPointerLock();
document.addEventListener('mousemove', updateCameraRotation);
document.addEventListener('keydown', handleKeyPress);
}
function disable3DView() {
document.exitPointerLock();
document.removeEventListener('mousemove', updateCameraRotation);
document.removeEventListener('keydown', handleKeyPress);
}
// Functions for handling camera movements
function handleKeyPress(e) {
const speed = 5;
switch (e.key) {
case 'w':
container.style.top = (parseInt(container.style.top) - speed) + 'px';
break;
case 's':
container.style.top = (parseInt(container.style.top) + speed) + 'px';
break;
case 'a':
container.style.left = (parseInt(container.style.left) - speed) + 'px';
break;
case 'd':
container.style.left = (parseInt(container.style.left) + speed) + 'px';
break;
}
}
function updateCameraRotation(event) {
const rotationSpeed = 0.1; // adjust this to change sensitivity
const degX = event.movementY * rotationSpeed;
const degY = event.movementX * rotationSpeed;
container.style.transform += `rotateX(${degX}deg) rotateY(${degY}deg)`;
}
// Initialize the settings box on page load
initializeSettings();
console.log("3D manipulation tool is ready!");
})();
Explanation of the Key Features
-
Resizable Boxes with Handles: Circular resize handles are created around the selected boxes, facilitating easy resizing and allowing the user to shape the boxes much like you would find in a studio tool.
-
Rotation Handles: Implemented at the top center of each box to enable rotation of the object with dragging.
-
3D View Mode: Toggle 3D view with the button, which lets the user navigate the 3D space using WASD keys and allows mouse movement to look around (similarly to camera controls in 3D applications).
-
Dynamic Settings Input: Users can change size, rotation, and color of the selected box dynamically, with real-time updates.
-
Custom CSS Input: Allows input of custom CSS to change the appearance of the boxes interactively.
Usage
To use this script:
- Embed it in an HTML file within a
<script> tag after setting up the necessary HTML structure.
- Open the file in modern web browsers to test interactions. Ensure that you have proper permission to access the mouse during 3D view mode (may involve allowing pointer lock).
Next Steps
This code provides a foundational setup. You might want to further enhance it by:
- Implementing a more sophisticated 3D engine (like Three.js) for a richer experience.
- Adding persistence to save the state of 3D objects.
- Enhancing the UI/UX design for better aesthetics and usability.
This solution forms a robust basis for a 3D object manipulation interface in JavaScript, facilitating dynamic interaction and visual feedback. If you have other specific features in mind or need further improvements, feel free to ask!