This commit is contained in:
Your Name
2025-10-31 09:23:48 -04:00
commit 635228783a

221
hamburger.html Normal file
View File

@@ -0,0 +1,221 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hamburger Menu Morphing Demo</title>
<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#divSvgOut {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
margin-bottom: 20px;
}
button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Hamburger Morphing</h1>
<div id="divSvgOut"></div>
<div>
<button onclick="animateTo('burger')">Hamburger</button>
<button onclick="animateTo('x')">X</button>
<button onclick="animateTo('plus')">Plus</button>
<button onclick="animateTo('minus')">Minus</button>
<button onclick="animateTo('arrow_left')">Arrow Left</button>
<button onclick="animateTo('arrow_down')">Arrow Down</button>
<button onclick="animateTo('arrow_up')">Arrow Up</button>
<button onclick="animateTo('arrow_right')">Arrow Right</button>
</div>
<div style="margin-top: 20px; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9; width: 400px; height: 200px; overflow: auto;">
<textarea id="svgCode" style="font-size: 12px; width: 100%; height: 150px; margin: 0; font-family: monospace;"></textarea>
</div>
<button onclick="renderSvg()" style="margin-top: 10px; padding: 10px 20px; font-size: 16px; cursor: pointer;">Render SVG</button>
<script>
let draw;
let line1;
let line2;
let line3;
let stroke = {
color: "#000",
width: 38,
linecap: "round",
linejoin: "round",
};
const svgDrawBurger = () => {
draw = SVG().addTo("#divSvgOut").size("100px", "100px").viewbox(0, 0, 120, 120);
line1 = draw.line(20, 20, 100, 19).fill("#fff").stroke(stroke);
line2 = draw.line(20, 60, 100, 60).fill("#fff").stroke(stroke);
line3 = draw.line(20, 100, 100, 101).fill("#fff").stroke(stroke);
};
const animateTo = (svgType) => {
if (svgType == "x") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 20, y2: 100 });
line2.animate(500, 0, "now").attr({ x1: 100, x2: 20, y1: 100, y2: 20 });
line3.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 100, y2: 20 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "plus") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 60, y2: 60 });
line2.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 20, y2: 100 });
line3.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 60, y2: 60 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "minus") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 60, y2: 60 });
line2.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 60, y2: 60 });
line3.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 60, y2: 60 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "burger") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 20, y2: 20 });
line2.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 60, y2: 60 });
line3.animate(500, 0, "now").attr({ x1: 20, x2: 100, y1: 100, y2: 100 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "arrow_left") {
try {
line1.animate(500, 0, "now").attr({ x1: 60, x2: 20, y1: 20, y2: 60 });
line2.animate(500, 0, "now").attr({ x1: 80, x2: 100, y1: 60, y2: 60 });
line3.animate(500, 0, "now").attr({ x1: 60, x2: 20, y1: 100, y2: 60 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "arrow_down") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 60, y1: 60, y2: 100 });
line2.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 20, y2: 40 });
line3.animate(500, 0, "now").attr({ x1: 100, x2: 60, y1: 60, y2: 100 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "arrow_up") {
try {
line1.animate(500, 0, "now").attr({ x1: 20, x2: 60, y1: 60, y2: 20 });
line2.animate(500, 0, "now").attr({ x1: 60, x2: 60, y1: 100, y2: 80 });
line3.animate(500, 0, "now").attr({ x1: 100, x2: 60, y1: 60, y2: 20 });
} catch (error) {}
updateSvgCode();
return;
}
if (svgType == "arrow_right") {
try {
line1.animate(500, 0, "now").attr({ x1: 60, x2: 100, y1: 20, y2: 60 });
line2.animate(500, 0, "now").attr({ x1: 20, x2: 40, y1: 60, y2: 60 });
line3.animate(500, 0, "now").attr({ x1: 60, x2: 100, y1: 100, y2: 60 });
} catch (error) {}
updateSvgCode();
return;
}
};
const updateSvgCode = () => {
setTimeout(() => {
const svgElement = document.querySelector('#divSvgOut svg');
if (svgElement) {
// Get the current attributes of the lines
const lines = svgElement.querySelectorAll('line');
let svgCode = `<svg viewBox="0 0 120 120" width="100px" height="100px">\n`;
lines.forEach(line => {
const x1 = Math.round(line.getAttribute('x1'));
const y1 = Math.round(line.getAttribute('y1'));
const x2 = Math.round(line.getAttribute('x2'));
const y2 = Math.round(line.getAttribute('y2'));
const stroke = line.getAttribute('stroke') || '#000';
const strokeWidth = line.getAttribute('stroke-width') || '38';
const strokeLinecap = line.getAttribute('stroke-linecap') || 'round';
const strokeLinejoin = line.getAttribute('stroke-linejoin') || 'round';
const fill = line.getAttribute('fill') || '#fff';
svgCode += ` <line x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}" stroke="${stroke}" stroke-width="${strokeWidth}" stroke-linecap="${strokeLinecap}" stroke-linejoin="${strokeLinejoin}" fill="${fill}"></line>\n`;
});
svgCode += `</svg>`;
document.getElementById('svgCode').value = svgCode;
}
}, 510); // Wait for animation to complete
};
const renderSvg = () => {
const svgCode = document.getElementById('svgCode').value;
document.getElementById('divSvgOut').innerHTML = svgCode;
};
const formatSvg = (svgString) => {
// Round decimal values to integers
svgString = svgString.replace(/(\d+\.\d+)/g, (match) => Math.round(parseFloat(match)));
// Simple XML formatter
let formatted = '';
let indent = 0;
const indentStr = ' ';
// Split by tags
const parts = svgString.split(/(<[^>]*>)/);
for (let part of parts) {
if (part.trim() === '') continue;
if (part.startsWith('</')) {
indent--;
formatted += indentStr.repeat(indent) + part + '\n';
} else if (part.startsWith('<') && !part.endsWith('/>')) {
formatted += indentStr.repeat(indent) + part + '\n';
if (!part.includes('</')) indent++;
} else if (part.startsWith('<') && part.endsWith('/>')) {
formatted += indentStr.repeat(indent) + part + '\n';
} else {
formatted += indentStr.repeat(indent) + part.trim() + '\n';
}
}
return formatted.trim();
};
// Initialize the SVG
svgDrawBurger();
</script>
</body>
</html>