<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG Test Site</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
<style>
/* ... (unchanged styles) ... */
</style>
</head>
<body>
<!-- Header -->
<header>
<h1>SVG Test Site</h1>
</header>
<!-- Canvases Wrapper -->
<div class="canvases-wrapper" id="canvases"></div>
<!-- Add Canvas Button -->
<button id="add-canvas-btn" title="Add New Canvas" aria-label="Add New Canvas">+</button>
<!-- Toast Notification -->
<div class="toast" id="toast">Action Successful</div>
<script>
(function() {
const canvasesContainer = document.getElementById('canvases');
const addCanvasBtn = document.getElementById('add-canvas-btn');
const toast = document.getElementById('toast');
const MAX_CANVASES = 3;
let canvasCount = 0;
// Default SVG Code
const defaultSVG = `<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="80" fill="#4CAF50" stroke="#333" stroke-width="4"/>
<text x="100" y="115" font-size="30" text-anchor="middle" fill="#fff">SVG</text>
</svg>`;
// Initialize with one canvas
addCanvas();
// Add Canvas Event
addCanvasBtn.addEventListener('click', addCanvas);
function addCanvas() {
if (canvasCount >= MAX_CANVASES) return;
canvasCount++;
const canvasId = `canvas-${canvasCount}`;
const canvasDiv = document.createElement('div');
canvasDiv.classList.add('canvas-container');
canvasDiv.setAttribute('id', canvasId);
// Remove Button (not for first canvas)
if (canvasCount > 1) {
const removeBtn = document.createElement('button');
removeBtn.classList.add('remove-canvas-btn');
removeBtn.innerHTML = '×';
removeBtn.title = 'Remove Canvas';
removeBtn.setAttribute('aria-label', 'Remove Canvas');
removeBtn.addEventListener('click', () => {
canvasesContainer.removeChild(canvasDiv);
canvasCount--;
if (canvasCount < MAX_CANVASES) {
addCanvasBtn.disabled = false;
}
showToast('Canvas Removed');
});
canvasDiv.appendChild(removeBtn);
}
// Editor and Preview Layout
const editorPreviewDiv = document.createElement('div');
editorPreviewDiv.classList.add('editor-preview');
// Editor Container
const editorContainer = document.createElement('div');
editorContainer.classList.add('editor-container');
const textarea = document.createElement('textarea');
textarea.placeholder = 'Enter your SVG code here...';
textarea.value = defaultSVG;
editorContainer.appendChild(textarea);
// Action Buttons
const actionButtons = document.createElement('div');
actionButtons.classList.add('action-buttons');
// Download Button
const downloadBtn = document.createElement('button');
downloadBtn.title = 'Download SVG';
downloadBtn.setAttribute('aria-label', 'Download SVG');
downloadBtn.setAttribute('data-tooltip', 'Download SVG');
downloadBtn.innerHTML = '⬇️';
actionButtons.appendChild(downloadBtn);
// Share Button
const shareBtn = document.createElement('button');
shareBtn.title = 'Share Link';
shareBtn.setAttribute('aria-label', 'Share Link');
shareBtn.setAttribute('data-tooltip', 'Share Link');
shareBtn.innerHTML = '🔗';
actionButtons.appendChild(shareBtn);
// Fullscreen Button
const fullscreenBtn = document.createElement('button');
fullscreenBtn.title = 'Full Screen';
fullscreenBtn.setAttribute('aria-label', 'Full Screen');
fullscreenBtn.setAttribute('data-tooltip', 'Full Screen');
fullscreenBtn.innerHTML = '⛶';
actionButtons.appendChild(fullscreenBtn);
// Upload Button
const uploadBtn = document.createElement('button');
uploadBtn.title = 'Upload SVG';
uploadBtn.setAttribute('aria-label', 'Upload SVG');
uploadBtn.setAttribute('data-tooltip', 'Upload SVG');
uploadBtn.innerHTML = '📤';
actionButtons.appendChild(uploadBtn);
// Hidden File Input for Upload
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/svg+xml';
fileInput.style.display = 'none';
editorContainer.appendChild(fileInput);
editorContainer.appendChild(actionButtons);
// Preview Container
const previewContainer = document.createElement('div');
previewContainer.classList.add('preview-container');
const previewDiv = document.createElement('div');
previewDiv.setAttribute('id', `svg-preview-${canvasCount}`);
previewDiv.innerHTML = defaultSVG;
previewContainer.appendChild(previewDiv);
editorPreviewDiv.appendChild(editorContainer);
editorPreviewDiv.appendChild(previewContainer);
canvasDiv.appendChild(editorPreviewDiv);
canvasesContainer.appendChild(canvasDiv);
// Event Listeners
textarea.addEventListener('input', () => {
updatePreview(textarea.value, previewDiv);
});
downloadBtn.addEventListener('click', () => {
downloadSVG(textarea.value);
});
shareBtn.addEventListener('click', () => {
shareLink(textarea.value);
});
fullscreenBtn.addEventListener('click', () => {
toggleFullscreen(previewDiv.innerHTML);
});
uploadBtn.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
if (file.type !== 'image/svg+xml') {
showToast('Invalid file type. Please upload an SVG file.', true);
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
textarea.value = content;
updatePreview(content, previewDiv);
showToast('SVG Uploaded Successfully');
};
reader.onerror = function() {
showToast('Error reading the SVG file.', true);
};
reader.readAsText(file);
}
// Reset the file input value to allow uploading the same file again if needed
event.target.value = '';
});
// Load SVG from URL hash only for the first canvas
if (canvasCount === 1) {
loadSVGFromHash(textarea, previewDiv);
}
// Disable Add Canvas button if [Redacted Person] reached
if (canvasCount >= MAX_CANVASES) {
addCanvasBtn.disabled = true;
}
showToast('Canvas Added');
}
function updatePreview(svgContent, previewDiv) {
if (svgContent.trim()) {
try {
const parser = new DOMParser();
const doc = parser.parseFromString(svgContent, "image/svg+xml");
const svg = doc.querySelector('svg');
if (!svg) throw new Error('No SVG element found');
// Ensure the SVG has a viewBox
if (!svg.getAttribute('viewBox')) {
const width = svg.getAttribute('width') || '100%';
const height = svg.getAttribute('height') || '100%';
const widthNum = parseFloat(width) || 100;
const heightNum = parseFloat(height) || 100;
svg.setAttribute('viewBox', `0 0 ${widthNum} ${heightNum}`);
svg.removeAttribute('width');
svg.removeAttribute('height');
}
const serializer = new XMLSerializer();
const updatedSVG = serializer.serializeToString(svg);
previewDiv.innerHTML = updatedSVG;
} catch (e) {
console.error('Invalid SVG:', e);
previewDiv.innerHTML = '<p style="color: red;">Invalid SVG Code</p>';
}
} else {
previewDiv.innerHTML = '<p style="color: red;">Invalid SVG Code</p>';
}
}
function toggleFullscreen(svgContent) {
const fsOverlay = document.createElement('div');
fsOverlay.classList.add('fullscreen-overlay');
const fsPreview = document.createElement('div');
fsPreview.setAttribute('id', 'fs-preview');
fsPreview.innerHTML = svgContent;
const closeBtn = document.createElement('button');
closeBtn.classList.add('close-btn');
closeBtn.innerHTML = '✖️';
closeBtn.title = 'Close Full Screen';
closeBtn.setAttribute('aria-label', 'Close Full Screen');
closeBtn.addEventListener('click', () => {
fsOverlay.classList.add('fadeOut');
setTimeout(() => {
document.body.removeChild(fsOverlay);
}, 300);
});
fsOverlay.appendChild(fsPreview);
fsOverlay.appendChild(closeBtn);
document.body.appendChild(fsOverlay);
}
function downloadSVG(svgContent) {
if (!svgContent.trim()) {
showToast('No SVG code to download', true);
return;
}
const blob = new Blob([svgContent], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement