right now with our code the svg its not turning gradient only solid coolor fix it so it…
- User
right now with our code the svg its not turning gradient only solid coolor fix it so it works with gradients with our gradient tool, and make it live upadte also with the gradient applying to the selection, also make it so the select is indlvidual not all right now make it only one selector is show with the 4 corner etc, Write the full complete code dont skip anything, write the full code with the new edits you did just write the full complete code , fix it and figure out a way to fix this issue, just fix this problem just fix the logic but keep the css/design the same:
<script> let currentShape = null; let copiedElement = null; let codeArea = document.getElementById('codeArea'); let codeElement = document.getElementById('code'); let gradientTool = document.getElementById('gradientTool'); let strokeTool = document.getElementById('strokeTool'); // Renamed for clarity let undoStack = []; let redoStack = []; let gradientColors = ['#ff0000', '#0000ff']; let isGradientToolActive = false; let isStrokeToolActive = false; function toggleGrid() { const grid = document.getElementById('grid'); grid.style.display = grid.style.display === 'none' ? 'block' : 'none'; saveState(); } function changeCanvasColor(color) { const canvas = document.getElementById('canvas'); canvas.style.backgroundColor = color; updateCode(); // Update code when background color changes saveState(); } 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'; textContent.setAttribute('tabindex', '0'); // Make it focusable text.appendChild(textContent); text.style.left = '50px'; text.style.top = '50px'; text.style.width = '200px'; text.style.height = '50px'; textContent.style.fontSize = '16px'; textContent.style.color = '#000000'; text.style.opacity = 1; canvas.appendChild(text); makeDraggable(text); makeSelectable(text); addTextControls(text); text.addEventListener('dblclick', function() { textContent.focus(); document.execCommand('selectAll', false, null); }); textContent.addEventListener('input', function() { updateLayerPanel(); updateCode(); saveState(); }); updateLayerPanel(); updateCode(); saveState(); } 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); img.onload = function() { let scale = 1; if (img.naturalWidth > 400 || img.naturalHeight > 300) { // Scale down if image is too big scale = Math.min(400 / img.naturalWidth, 300 / img.naturalHeight); } media.style.width = img.naturalWidth * scale + 'px'; media.style.height = img.naturalHeight * scale + 'px'; media.style.left = '50px'; media.style.top = '50px'; media.style.opacity = 1; img.style.width = '100%'; img.style.height = '100%'; }; canvas.appendChild(media); makeDraggable(media); makeSelectable(media); addMediaControls(media); updateLayerPanel(); updateCode(); saveState(); }; 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(); saveState(); } else { clearSelection(); } }); function addShape(shapeType, x, y) { const canvas = document.getElementById('canvas'); const shapeWrapper = document.createElement('div'); shapeWrapper.className = 'canvas-shape'; shapeWrapper.id = `${shapeType}-${Date.now()}`; shapeWrapper.style.left = `${x}px`; shapeWrapper.style.top = `${y}px`; shapeWrapper.style.opacity = 1; shapeWrapper.dataset.shapeType = shapeType; const svgNS = "http://www.w3.org/2000/svg"; const svg = document.createElementNS(svgNS, 'svg'); svg.setAttribute('width', '100'); svg.setAttribute('height', '100'); svg.setAttribute('viewBox', '0 0 100 100'); svg.style.overflow = 'visible'; let shape; if (shapeType === 'rectangle') { shape = document.createElementNS(svgNS, 'rect'); shape.setAttribute('x', 0); shape.setAttribute('y', 0); shape.setAttribute('width', 100); shape.setAttribute('height', 100); shape.style.fill = '#3498db'; } else if (shapeType === 'ellipse') { shape = document.createElementNS(svgNS, 'ellipse'); shape.setAttribute('cx', 50); shape.setAttribute('cy', 50); shape.setAttribute('rx', 50); shape.setAttribute('ry', 50); shape.style.fill = '#e74c3c'; } else if (shapeType === 'triangle') { shape = document.createElementNS(svgNS, 'polygon'); shape.setAttribute('points', '50,0 0,100 100,100'); shape.style.fill = '#2ecc71'; } else if (shapeType === 'star') { shape = document.createElementNS(svgNS, 'polygon'); shape.setAttribute('points', '50,0 61,35 98,35 68,57 79,91 50,70 21,91 32,57 2,35 39,35'); shape.style.fill = '#f1c40f'; } else if (shapeType === 'pentagon') { shape = document.createElementNS(svgNS, 'polygon'); shape.setAttribute('points', '50,0 95,35 75,90 25,90 5,35'); shape.style.fill = '#8e44ad'; } else if (shapeType === 'hexagon') { shape = document.createElementNS(svgNS, 'polygon'); shape.setAttribute('points', '25,0 75,0 100,50 75,100 25,100 0,50'); shape.style.fill = '#e67e22'; } else if (shapeType === 'octagon') { shape = document.createElementNS(svgNS, 'polygon'); shape.setAttribute('points', '30,0 70,0 100,30 100,70 70,100 30,100 0,70 0,30'); shape.style.fill = '#2c3e50'; } svg.appendChild(shape); shapeWrapper.appendChild(svg); // Set default stroke properties shape.setAttribute('stroke', '#000000'); shape.setAttribute('stroke-width', '2'); shape.setAttribute('stroke-dasharray', ''); // For solid stroke canvas.appendChild(shapeWrapper); makeDraggable(shapeWrapper); makeSelectable(shapeWrapper); addShapeControls(shapeWrapper, shapeType); updateLayerPanel(); updateCode(); saveState(); } function makeDraggable(element) { let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; const canvas = document.getElementById('canvas'); let isResizing = false; let currentHandle = null; if (element.classList.contains('canvas-text') || element.classList.contains('canvas-media') || element.classList.contains('canvas-shape')) { addResizeHandles(element); } element.onmousedown = dragMouseDown; function dragMouseDown(e) { if (element.dataset.locked === 'true') { return; } if (e.target.classList.contains('resize-handle')) { isResizing = true; currentHandle = e.target; e.stopPropagation(); document.onmouseup = closeDragElement; document.onmousemove = elementResize; } else { 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 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 if (element.classList.contains('selected')) { showElementControls(element); } } function elementResize(e) { e.preventDefault(); const rect = element.getBoundingClientRect(); let width, height; if (currentHandle.classList.contains('bottom-right')) { width = e.clientX - rect.left; height = e.clientY - rect.top; } else if (currentHandle.classList.contains('bottom-left')) { width = rect.right - e.clientX; height = e.clientY - rect.top; element.style.left = element.offsetLeft + (e.clientX - rect.left) + 'px'; } else if (currentHandle.classList.contains('top-right')) { width = e.clientX - rect.left; height = rect.bottom - e.clientY; element.style.top = element.offsetTop + (e.clientY - rect.top) + 'px'; } else if (currentHandle.classList.contains('top-left')) { width = rect.right - e.clientX; height = rect.bottom - e.clientY; element.style.left = element.offsetLeft + (e.clientX - rect.left) + 'px'; element.style.top = element.offsetTop + (e.clientY - rect.top) + 'px'; } if (width > 20 && height > 20) { element.style.width = width + 'px'; element.style.height = height + 'px'; // For images, adjust the img size if (element.classList.contains('canvas-media')) { const img = element.querySelector('img'); img.style.width = '100%'; img.style.height = '100%'; } // For shapes, adjust SVG size if (element.classList.contains('canvas-shape')) { const svg = element.querySelector('svg'); svg.setAttribute('width', width); svg.setAttribute('height', height); } } // Update controls position if visible if (element.classList.contains('selected')) { showElementControls(element); } updateCode(); saveState(); } function closeDragElement() { document.onmouseup = null; document.onmousemove = null; isResizing = false; currentHandle = null; updateCode(); // Update code after dragging completes saveState(); } } function addResizeHandles(element) { const handles = ['top-left', 'top-right', 'bottom-left', 'bottom-right']; handles.forEach(handle => { const div = document.createElement('div'); div.className = 'resize-handle ' + handle; element.appendChild(div); }); } function makeSelectable(element) { element.addEventListener('click', function(e) { if (element.dataset.locked === 'true') { return; } e.stopPropagation(); // prevent event bubbling clearSelection(); // Deselect others element.classList.add('selected'); // apply selected class showElementControls(element); // Update the global opacity slider const globalOpacity = document.getElementById('globalOpacity'); globalOpacity.value = element.style.opacity * 100; }); } 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}"]`); if (controls) { controls.style.display = 'flex'; const canvas = document.getElementById('canvas'); const canvasRect = canvas.getBoundingClientRect(); controls.style.top = `${element.offsetTop - controls.offsetHeight - 10}px`; controls.style.left = `${element.offsetLeft + (element.offsetWidth / 2) - (controls.offsetWidth / 2)}px`; } } else if (element.classList.contains('canvas-media')) { const controls = document.querySelector(`.media-controls[data-media-id="${element.id}"]`); if (controls) { controls.style.display = 'flex'; const canvas = document.getElementById('canvas'); const canvasRect = canvas.getBoundingClientRect(); controls.style.top = `${element.offsetTop - controls.offsetHeight - 10}px`; controls.style.left = `${element.offsetLeft + (element.offsetWidth / 2) - (controls.offsetWidth / 2)}px`; } } else if (element.classList.contains('canvas-shape')) { const controls = document.querySelector(`.shape-controls[data-shape-id="${element.id}"]`); if (controls) { controls.style.display = 'flex'; const canvas = document.getElementById('canvas'); const canvasRect = canvas.getBoundingClientRect(); controls.style.top = `${element.offsetTop - controls.offsetHeight - 10}px`; controls.style.left = `${element.offsetLeft + (element.offsetWidth / 2) - (controls.offsetWidth / 2)}px`; } } // Show canvas outline const canvasOutline = document.getElementById('canvasOutline'); canvasOutline.style.display = 'block'; canvasOutline.style.top = `${element.offsetTop - 10}px`; canvasOutline.style.left = `${element.offsetLeft - 10}px`; canvasOutline.style.width = `${element.offsetWidth + 20}px`; canvasOutline.style.height = `${element.offsetHeight + 20}px`; } function hideAllControls() { document.querySelectorAll('.text-controls, .media-controls, .shape-controls').forEach(control => { control.style.display = 'none'; }); gradientTool.style.display = 'none'; isGradientToolActive = false; strokeTool.style.display = 'none'; isStrokeToolActive = false; // Hide canvas outline const canvasOutline = document.getElementById('canvasOutline'); canvasOutline.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.innerHTML = ` <button onclick="toggleBold(this, '${textElement.id}')" title="Bold"><i class="material-icons">format_bold</i></button> <button onclick="toggleItalic(this, '${textElement.id}')" title="Italic"><i class="material-icons">format_italic</i></button> <button onclick="toggleUnderline(this, '${textElement.id}')" title="Underline"><i class="material-icons">format_underline</i></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="Roboto">Roboto</option> <option value="'Source Sans Pro'">Source Sans Pro</option> <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.innerHTML = ` <button onclick="removeMedia('${mediaElement.id}')" title="Remove"><i class="material-icons">delete</i></button> <button onclick="resetZoom('${mediaElement.id}')" title="Reset Zoom"><i class="material-icons">zoom_out_map</i></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.innerHTML = ` <input type="color" oninput="changeShapeColor(this.value, '${shapeElement.id}')", value="${getComputedStyle(shapeElement).backgroundColor}" title="Fill Color"> <button onclick="removeShape('${shapeElement.id}')" title="Remove"><i class="material-icons">delete</i></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(); saveState(); } function changeFontSize(size, textId) { const textElement = document.getElementById(textId); const textContent = textElement.querySelector('div'); textContent.style.fontSize = size + 'px'; updateCode(); saveState(); } function changeFontFamily(family, textId) { const textElement = document.getElementById(textId); const textContent = textElement.querySelector('div'); textContent.style.fontFamily = family; updateCode(); saveState(); } 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.classList.toggle('active', !isBold); updateCode(); saveState(); } 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.classList.toggle('active', !isItalic); updateCode(); saveState(); } 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.classList.toggle('active', !isUnderline); updateCode(); saveState(); } // 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'; } updateLayerPanel(); updateCode(); saveState(); } function resetZoom(mediaId) { const mediaElement = document.getElementById(mediaId); const img = mediaElement.querySelector('img'); img.style.transform = 'scale(1)'; img.dataset.scale = 1; // Adjust the container size mediaElement.style.width = img.naturalWidth + 'px'; mediaElement.style.height = img.naturalHeight + 'px'; updateCode(); saveState(); } 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; // Apply scale transform img.style.transform = `scale(${scale})`; // Adjust the container size based on the scaled image size const mediaElement = img.parentElement; mediaElement.style.width = img.naturalWidth * scale + 'px'; mediaElement.style.height = img.naturalHeight * scale + 'px'; updateCode(); saveState(); } // Shape Control Functions function changeShapeColor(color, shapeId) { const shapeElement = document.getElementById(shapeId); const svg = shapeElement.querySelector('svg'); const shape = svg.firstChild; shape.style.fill = color; updateCode(); saveState(); } 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'; } updateLayerPanel(); updateCode(); saveState(); } // Gradient Fill Tool Functions function toggleGradientTool() { if (gradientTool.style.display === 'none') { gradientTool.style.display = 'flex'; updateGradientColors(); updateGradientPreview(); isGradientToolActive = true; updateSelectedShapeGradient(); } else { gradientTool.style.display = 'none'; isGradientToolActive = false; } } function addGradientColor() { gradientColors.push('#ffffff'); updateGradientColors(); updateGradientPreview(); updateSelectedShapeGradient(); } function updateGradientColors() { const gradientColorsDiv = document.getElementById('gradientColors'); gradientColorsDiv.innerHTML = ''; gradientColors.forEach((color, index) => { const colorInput = document.createElement('input'); colorInput.type = 'color'; colorInput.value = color; colorInput.oninput = function() { gradientColors[index] = colorInput.value; updateGradientPreview(); updateSelectedShapeGradient(); }; gradientColorsDiv.appendChild(colorInput); }); } function updateGradientPreview() { const gradientType = document.getElementById('gradientType').value; const angle = document.getElementById('gradientAngle').value; const gradientPreview = document.getElementById('gradientPreview'); let gradientString = ''; if (gradientType === 'linear') { gradientString = `linear-gradient(${angle}deg,${gradientColors.join(', ')})`; } else if (gradientType === 'radial') { gradientString = `radial-gradient(${gradientColors.join(', ')})`; } else if (gradientType === 'conic') { gradientString = `conic-gradient(${gradientColors.join(', ')})`; } gradientPreview.style.background = gradientString; } function updateSelectedShapeGradient() { const selectedElement = document.querySelector('.canvas-shape.selected, .canvas-text.selected'); if (selectedElement && isGradientToolActive) { const gradientType = document.getElementById('gradientType').value; const angle = document.getElementById('gradientAngle').value; let gradientString = ''; if (gradientType === 'linear') { gradientString = `linear-gradient(${angle}deg,${gradientColors.join(', ')})`; } else if (gradientType === 'radial') { gradientString = `radial-gradient(${gradientColors.join(', ')})`; } else if (gradientType === 'conic') { gradientString = `conic-gradient(${gradientColors.join(', ')})`; } // Apply gradient if (selectedElement.classList.contains('canvas-shape')) { const svg = selectedElement.querySelector('svg'); const shape = svg.firstChild; shape.style.fill = gradientString; } else if (selectedElement.classList.contains('canvas-text')) { const textContent = selectedElement.querySelector('div'); textContent.style.background = gradientString; textContent.style.webkitBackgroundClip = 'text'; textContent.style.webkitTextFillColor = 'transparent'; } updateCode(); saveState(); } } // Stroke Tool Functions function toggleStrokeTool() { if (strokeTool.style.display === 'none') { strokeTool.style.display = 'flex'; updateStrokePreview(); isStrokeToolActive = true; updateSelectedStroke(); } else { strokeTool.style.display = 'none'; isStrokeToolActive = false; } } function updateStrokePreview() { const strokeColor = document.getElementById('strokeColor').value; const strokePreview = document.getElementById('strokePreview'); strokePreview.style.background = strokeColor; } function updateSelectedStroke() { const selectedElement = document.querySelector('.canvas-shape.selected, .canvas-text.selected'); if (selectedElement && isStrokeToolActive) { const strokeColor = document.getElementById('strokeColor').value; const strokeWidth = document.getElementById('strokeWidth').value; const isContinuous = document.getElementById('continuousStroke').checked; // Apply stroke if (selectedElement.classList.contains('canvas-shape')) { const svg = selectedElement.querySelector('svg'); const shape = svg.firstChild; shape.setAttribute('stroke', strokeColor); shape.setAttribute('stroke-width', strokeWidth); if (!isContinuous) { // For dashed stroke shape.setAttribute('stroke-dasharray', '5,5'); } else { shape.setAttribute('stroke-dasharray', ''); } } else if (selectedElement.classList.contains('canvas-text')) { selectedElement.style.border = `${strokeWidth}px solid${strokeColor}`; if (!isContinuous) { // For dashed stroke selectedElement.style.borderStyle = 'dashed'; } else { selectedElement.style.borderStyle = 'solid'; } } updateStrokePreview(); updateCode(); saveState(); } } // Global Opacity Control function changeGlobalOpacity(value) { const selectedElement = document.querySelector('.canvas-text.selected, .canvas-media.selected, .canvas-shape.selected'); if (selectedElement) { selectedElement.style.opacity = value / 100; updateCode(); saveState(); } } // Monitor selected element changes to update gradient tools document.addEventListener('click', function(e) { if (isGradientToolActive) { const selectedElement = document.querySelector('.canvas-shape.selected, .canvas-text.selected'); if (selectedElement) { updateSelectedShapeGradient(); } } if (isStrokeToolActive) { const selectedElement = document.querySelector('.canvas-shape.selected, .canvas-text.selected'); if (selectedElement) { updateSelectedStroke(); } } }); // Initial save state saveState(); // Handle click outside to clear selection document.getElementById('canvas').addEventListener('click', function(e) { if (e.target === this) { clearSelection(); } }); // Code Editor Toggles and Updates function toggleCode() { updateCode(); codeArea.style.display = codeArea.style.display === 'block' ? 'none' : 'block'; saveState(); } 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; updateCode(); saveState(); } } makeElementDraggable(codeArea); makeElementDraggable(document.getElementById('layerPanel')); makeElementDraggable(document.getElementById('layerPanelHeader')); 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 for media elements 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 = ''; // Remove resize handles if (clone.classList.contains('canvas-text') || clone.classList.contains('canvas-media') || clone.classList.contains('canvas-shape')) { const handles = clone.querySelectorAll('.resize-handle'); handles.forEach(handle => handle.remove()); } // 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> - Assistant