Write the full complete code dont skip anything, write the full code with the new edits you did just write the full complete code, right now the image when I move it is laggy, also I want the box to shrink also when the image shrink/or enlarge when the image goes big, also I want the color picker to hover exactly where the selector right now its off the side but the same postion as right now as the height away just want it to be more x as right now its far:
code
Assistant
<script>
let currentShape = null;
let copiedElement = null;
let codeArea = document.getElementById('codeArea');
let codeElement = document.getElementById('code');
function toggleGrid() {
const grid = document.getElementById('grid');
grid.style.display = grid.style.display === 'none' ? 'block' : 'none';
}
function changeCanvasColor(color) {
const canvas = document.getElementById('canvas');
canvas.style.backgroundColor = color;
updateCode(); // Update code when background color changes
}
function addText() {
const canvas = document.getElementById('canvas');
const text = document.createElement('div');
text.className = 'canvas-text';
text.id = 'text-' + Date.now();
const textContent = document.createElement('div');
textContent.contentEditable = true;
textContent.textContent = 'Double click to edit';
text.appendChild(textContent);
text.style.left = '50px';
text.style.top = '50px';
text.style.width = '200px';
text.style.height = '50px';
text.style.fontSize = '16px';
text.style.color = '#000000';
canvas.appendChild(text);
makeDraggable(text);
makeSelectable(text);
addTextControls(text);
text.addEventListener('dblclick', function() {
textContent.focus();
document.execCommand('selectAll', false, null);
});
updateCode();
}
function addMedia() {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const canvas = document.getElementById('canvas');
const media = document.createElement('div');
media.className = 'canvas-media';
media.id = 'media-' + Date.now();
const img = document.createElement('img');
img.src = e.target.result;
img.dataset.scale = 1;
img.style.transformOrigin = 'center center';
img.onwheel = (e) => {
e.preventDefault();
zoomMedia(img, e);
};
img.ondragstart = () => false;
media.appendChild(img);
media.style.left = '50px';
media.style.top = '50px';
canvas.appendChild(media);
makeDraggable(media);
makeSelectable(media);
addMediaControls(media);
updateCode();
};
reader.readAsDataURL(file);
}
};
input.click();
}
function toggleShapeMenu(event) {
event.stopPropagation(); // Prevent event from bubbling up
const menu = document.getElementById('shapeMenu');
menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
}
document.addEventListener('click', function(e) {
const menu = document.getElementById('shapeMenu');
const button = document.querySelector('.tool-dropdown .tool-button');
if (menu.style.display === 'block' && !menu.contains(e.target) && !button.contains(e.target)) {
menu.style.display = 'none';
}
});
function selectShape(shapeType) {
currentShape = shapeType;
const menu = document.getElementById('shapeMenu');
menu.style.display = 'none';
}
document.getElementById('canvas').addEventListener('click', function(event) {
if (currentShape) {
const canvas = document.getElementById('canvas');
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
addShape(currentShape, x, y);
currentShape = null;
updateCode();
} else {
clearSelection();
}
});
function addShape(shapeType, x, y) {
const canvas = document.getElementById('canvas');
const shape = document.createElement('div');
shape.className = `canvas-shape shape-${shapeType}`;
shape.id = `${shapeType}-${Date.now()}`;
shape.style.left = `${x}px`;
shape.style.top = `${y}px`;
shape.dataset.scale = 1;
if (shapeType === 'triangle') {
shape.style.width = '0';
shape.style.height = '0';
} else if (shapeType === 'star') {
shape.style.width = '100px';
shape.style.height = '100px';
} else {
shape.style.width = '100px';
shape.style.height = '100px';
}
canvas.appendChild(shape);
makeDraggable(shape);
makeSelectable(shape);
addShapeControls(shape, shapeType);
shape.onwheel = (e) => {
e.preventDefault();
zoomShape(shape, e);
};
updateCode();
}
function makeDraggable(element) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
const canvas = document.getElementById('canvas');
element.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
e.stopPropagation();
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
clearSelection();
element.classList.add('selected');
showElementControls(element);
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
let newTop = element.offsetTop - pos2;
let newLeft = element.offsetLeft - pos1;
// Constraints so that the element does not leave the canvas
const canvasRect = canvas.getBoundingClientRect();
const elementRect = element.getBoundingClientRect();
let maxTop = canvas.offsetHeight - element.offsetHeight;
let maxLeft = canvas.offsetWidth - element.offsetWidth;
if (newTop < 0) newTop = 0;
if (newLeft < 0) newLeft = 0;
if (newTop > maxTop) newTop = maxTop;
if (newLeft > maxLeft) newLeft = maxLeft;
element.style.top = newTop + "px";
element.style.left = newLeft + "px";
// Update controls position if visible
const controls = document.querySelector(`.shape-controls[data-shape-id="${element.id}"]`) ||
document.querySelector(`.text-controls[data-text-id="${element.id}"]`) ||
document.querySelector(`.media-controls[data-media-id="${element.id}"]`);
if (controls && controls.style.display === 'flex') {
controls.style.top = `${element.offsetTop - 60}px`;
controls.style.left = `${element.offsetLeft}px`;
}
updateCode();
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
function makeSelectable(element) {
element.addEventListener('click', function(e) {
e.stopPropagation(); // prevent event bubbling
clearSelection(); // Deselect others
element.classList.add('selected'); // apply selected class
showElementControls(element);
});
}
function clearSelection() {
document.querySelectorAll('.canvas-text.selected, .canvas-media.selected, .canvas-shape.selected').forEach(el => {
el.classList.remove('selected');
});
hideAllControls();
}
function showElementControls(element) {
hideAllControls();
if (element.classList.contains('canvas-text')) {
const controls = document.querySelector(`.text-controls[data-text-id="${element.id}"]`);
controls.style.display = 'flex';
controls.style.top = `${element.offsetTop - 50}px`;
controls.style.left = `${element.offsetLeft}px`;
} else if (element.classList.contains('canvas-media')) {
const controls = document.querySelector(`.media-controls[data-media-id="${element.id}"]`);
controls.style.display = 'flex';
controls.style.top = `${element.offsetTop - 50}px`;
controls.style.left = `${element.offsetLeft}px`;
} else if (element.classList.contains('canvas-shape')) {
const controls = document.querySelector(`.shape-controls[data-shape-id="${element.id}"]`);
controls.style.display = 'flex';
controls.style.top = `${element.offsetTop - 60}px`;
controls.style.left = `${element.offsetLeft}px`;
}
}
function hideAllControls() {
document.querySelectorAll('.text-controls, .media-controls, .shape-controls').forEach(control => {
control.style.display = 'none';
});
}
function addTextControls(textElement) {
const wrapper = document.getElementById('text-controls-wrapper');
const controls = document.createElement('div');
controls.className = 'text-controls';
controls.setAttribute('data-text-id', textElement.id);
controls.style.display = 'none';
controls.style.top = `${textElement.offsetTop - 50}px`;
controls.style.left = `${textElement.offsetLeft}px`;
controls.innerHTML = `
<button onclick="toggleBold(this, '${textElement.id}')">B</button>
<button onclick="toggleItalic(this, '${textElement.id}')">I</button>
<button onclick="toggleUnderline(this, '${textElement.id}')">U</button>
<input type="color" onchange="changeTextColor(this.value, '${textElement.id}')" title="Text Color">
<input type="number" min="1" max="100" value="16" onchange="changeFontSize(this.value, '${textElement.id}')" title="Font Size">
<select onchange="changeFontFamily(this.value, '${textElement.id}')" title="Font Family">
<option value="Arial">Arial</option>
<option value="Helvetica">Helvetica</option>
<option value="Times New Roman">Times New Roman</option>
<option value="Courier New">Courier New</option>
<option value="Verdana">Verdana</option>
<option value="Georgia">Georgia</option>
<option value="Palatino">Palatino</option>
<option value="Garamond">Garamond</option>
<option value="Bookman">Bookman</option>
<option value="Comic Sans MS">Comic Sans MS</option>
<option value="Trebuchet MS">Trebuchet MS</option>
<option value="Arial Black">Arial Black</option>
</select>
`;
wrapper.appendChild(controls);
}
function addMediaControls(mediaElement) {
const wrapper = document.getElementById('media-controls-wrapper');
const controls = document.createElement('div');
controls.className = 'media-controls';
controls.setAttribute('data-media-id', mediaElement.id);
controls.style.display = 'none';
controls.style.top = `${mediaElement.offsetTop - 50}px`;
controls.style.left = `${mediaElement.offsetLeft}px`;
controls.innerHTML = `
<button onclick="removeMedia('${mediaElement.id}')">Remove</button>
<button onclick="resetZoom('${mediaElement.id}')">Reset Zoom</button>
`;
wrapper.appendChild(controls);
}
function addShapeControls(shapeElement, shapeType) {
const wrapper = document.getElementById('shape-controls-wrapper');
const controls = document.createElement('div');
controls.className = 'shape-controls';
controls.setAttribute('data-shape-id', shapeElement.id);
controls.style.display = 'none';
controls.style.top = `${shapeElement.offsetTop - 60}px`;
controls.style.left = `${shapeElement.offsetLeft}px`;
controls.innerHTML = `
<input type="color" oninput="changeShapeColor(this.value, '${shapeElement.id}', this)" title="Fill Color">
<button onclick="removeShape('${shapeElement.id}')">Remove</button>
`;
wrapper.appendChild(controls);
}
// Text Control Functions
function changeTextColor(color, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
textContent.style.color = color;
updateCode();
}
function changeFontSize(size, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
textContent.style.fontSize = size + 'px';
updateCode();
}
function changeFontFamily(family, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
textContent.style.fontFamily = family;
updateCode();
}
function toggleBold(button, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
const isBold = textContent.style.fontWeight === 'bold';
textContent.style.fontWeight = isBold ? 'normal' : 'bold';
button.style.backgroundColor = isBold ? '' : '#e0e0e0';
updateCode();
}
function toggleItalic(button, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
const isItalic = textContent.style.fontStyle === 'italic';
textContent.style.fontStyle = isItalic ? 'normal' : 'italic';
button.style.backgroundColor = isItalic ? '' : '#e0e0e0';
updateCode();
}
function toggleUnderline(button, textId) {
const textElement = document.getElementById(textId);
const textContent = textElement.querySelector('div');
const isUnderline = textContent.style.textDecoration === 'underline';
textContent.style.textDecoration = isUnderline ? 'none' : 'underline';
button.style.backgroundColor = isUnderline ? '' : '#e0e0e0';
updateCode();
}
// Media Control Functions
function removeMedia(mediaId) {
const mediaElement = document.getElementById(mediaId);
mediaElement.remove();
const mediaControls = document.querySelector(`.media-controls[data-media-id="${mediaId}"]`);
if (mediaControls) {
mediaControls.style.display = 'none';
}
updateCode();
}
function resetZoom(mediaId) {
const mediaElement = document.getElementById(mediaId);
const img = mediaElement.querySelector('img');
img.style.transform = 'scale(1)';
img.dataset.scale = 1;
updateCode();
}
function zoomMedia(img, e) {
e.preventDefault();
const delta = e.deltaY;
const zoomFactor = 0.05; // Smaller zoom factor for smoother zooming
// Get current scale
let scale = img.dataset.scale ? parseFloat(img.dataset.scale) : 1;
// Update scale
scale *= delta > 0 ? (1 - zoomFactor) : (1 + zoomFactor);
// Limit scale
scale = Math.min(Math.max(0.1, scale), 10);
img.dataset.scale = scale;
// Get bounding rectangle
const rect = img.getBoundingClientRect();
// Get mouse position relative to image
const offsetX = e.clientX - rect.left;
const offsetY = e.clientY - rect.top;
// Calculate origin
const originX = (offsetX / img.offsetWidth) * 100;
const originY = (offsetY / img.offsetHeight) * 100;
// Set transform origin
img.style.transformOrigin = `${originX}% ${originY}%`;
// Apply scale transform
img.style.transform = `scale(${scale})`;
updateCode();
}
// Shape Control Functions
function changeShapeColor(color, shapeId, inputElement) {
const shapeElement = document.getElementById(shapeId);
if (shapeElement.classList.contains('shape-triangle')) {
shapeElement.style.borderBottomColor = color;
} else if (shapeElement.classList.contains('shape-star')) {
shapeElement.style.backgroundColor = color;
} else {
shapeElement.style.backgroundColor = color;
}
// Update the color picker swatch color
inputElement.value = color;
updateCode();
}
function removeShape(shapeId) {
const shapeElement = document.getElementById(shapeId);
shapeElement.remove();
const shapeControls = document.querySelector(`.shape-controls[data-shape-id="${shapeId}"]`);
if (shapeControls) {
shapeControls.style.display = 'none';
}
updateCode();
}
function zoomShape(shape, e) {
e.preventDefault();
const delta = e.deltaY;
const zoomFactor = 0.05; // Adjust the zoom factor as desired
// Get current size
let scale = shape.dataset.scale ? parseFloat(shape.dataset.scale) : 1;
// Update scale
scale *= delta > 0 ? (1 - zoomFactor) : (1 + zoomFactor);
// Limit scale
scale = Math.min(Math.max(0.1, scale), 10);
shape.dataset.scale = scale;
// Apply scale transform
shape.style.transform = `scale(${scale})`;
updateCode();
}
function copyElements() {
const selectedElement = document.querySelector('.canvas-text.selected, .canvas-media.selected, .canvas-shape.selected');
if (selectedElement) {
copiedElement = selectedElement.outerHTML;
}
}
function pasteElements() {
const canvas = document.getElementById('canvas');
if (copiedElement) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = copiedElement;
const element = tempDiv.firstChild;
// Adjust element ID to make it unique
element.id = element.id + '-' + Date.now();
// Keep the element at the same position
element.style.top = element.style.top;
element.style.left = element.style.left;
canvas.appendChild(element);
makeDraggable(element);
makeSelectable(element);
if (element.classList.contains('canvas-text')) {
addTextControls(element);
} else if (element.classList.contains('canvas-media')) {
addMediaControls(element);
} else if (element.classList.contains('canvas-shape')) {
let shapeType;
if (element.classList.contains('shape-rectangle')) {
shapeType = 'rectangle';
} else if (element.classList.contains('shape-ellipse')) {
shapeType = 'ellipse';
} else if (element.classList.contains('shape-star')) {
shapeType = 'star';
} else if (element.classList.contains('shape-triangle')) {
shapeType = 'triangle';
}
addShapeControls(element, shapeType);
element.onwheel = (e) => {
e.preventDefault();
zoomShape(element, e);
};
}
updateCode();
}
}
function toggleCode() {
updateCode();
codeArea.style.display = codeArea.style.display === 'block' ? 'none' : 'block';
}
function makeElementDraggable(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
makeElementDraggable(codeArea);
function updateCode() {
const canvas = document.getElementById('canvas');
const canvasStyle = getComputedStyle(canvas);
const backgroundColor = canvasStyle.backgroundColor;
const elements = canvas.querySelectorAll('.canvas-text, .canvas-media, .canvas-shape');
const elementsArray = Array.prototype.map.call(elements, function(element) {
// Clone the element to avoid modifying the original
let clone = element.cloneNode(true);
// Adjust image src attribute
if (clone.classList.contains('canvas-media')) {
let img = clone.querySelector('img');
if (img) {
let src = img.src;
let fileExtension = '';
if (src.startsWith('data:image/')) {
let match = src.match(/^data:image\/(png|jpeg|jpg);/);
let ext = match ? match[1] : 'png';
if (ext === 'jpeg') ext = 'jpg'; // use jpg instead of jpeg
// Generate 10-digit random number
let randomNumber = Math.floor(1000000000 + Math.random() * 9000000000);
let fileName = randomNumber + '.' + ext;
img.src = fileName;
}
}
}
// Remove selected class and inline styles related to selection
clone.classList.remove('selected');
clone.style.outline = '';
// Include transform styles
const computedStyle = window.getComputedStyle(element);
if (computedStyle.transform !== 'none') {
clone.style.transform = computedStyle.transform;
}
// Return the outerHTML of the cloned element
return clone.outerHTML;
});
// Include styles in the code
const styles = document.querySelector('style').outerHTML;
// Build the complete HTML code
let html = `<!DOCTYPE html>
code
codeElement.textContent = html;
}
// Keyboard shortcuts for copy (Ctrl+C) and paste (Ctrl+V)
document.addEventListener('keydown', function(e) {
const activeElement = document.activeElement;
if (activeElement && (
activeElement.tagName === 'INPUT' ||
activeElement.tagName === 'TEXTAREA' ||
activeElement.isContentEditable
)) {
// Don't interfere with typing in input fields or editable divs
return;
}
if (e.ctrlKey && (e.key === 'c' || e.key === 'C')) {
e.preventDefault();
copyElements();
} else if (e.ctrlKey && (e.key === 'v' || e.key === 'V')) {
e.preventDefault();
pasteElements();
}
});
</script>
Here's the full, updated code with the edits to address your concerns:
Fixed the laggy movement of images: Moved the updateCode(); call from the dragging function to after the drag completes to prevent lag during movement.
Adjusted the bounding box to shrink/enlarge with the image: Updated the zoomMedia function to resize the container div (canvas-media) when the image is zoomed.
Aligned the color picker to hover exactly where the selector is: Modified the positioning of the shape controls to align the color picker over the shape.
Removed the updateCode(); call from the elementDrag function inside makeDraggable.
Added updateCode(); to the closeDragElement function to update the code only after dragging completes.
Bounding Box Resizes with Image:
Updated the zoomMedia function to adjust the container div's (canvas-media) width and height based on the image's natural dimensions and current scale.
Also adjusted the container size in the resetZoom function.
Color Picker Position Adjusted:
Modified the positioning logic in addShapeControls and showElementControls functions to align the shape controls over the shape, ensuring the color picker hovers exactly where the selector is.
Used controls.offsetHeight and controls.offsetWidth to dynamically position the controls based on their dimensions.
These changes should address the laggy movement, ensure the bounding box resizes with the image, and adjust the color picker position as per your requirements.