SVG Icon Morphing Animation
Preview
Edit
Download
Run
Download Options
HTML File
Download as .html file
CSS File
Download as .css file
JavaScript File
Download as .js file
Combined HTML
All code in one HTML file
Project ZIP
All files in a ZIP archive
Cancel
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SVG Icon Morph Animation</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="icon-container" id="iconController"> <svg class="play active" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <path d="M8 5v14l11-7z"/> </svg> <svg class="pause" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/> </svg> <svg class="stop" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <path d="M6 6h12v12H6z"/> </svg> </div> <script src="script.js"></script> </body> </html>
CSS
body { display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f2f5; font-family: 'Arial', sans-serif; } .icon-container { width: 120px; height: 120px; position: relative; cursor: pointer; } svg { width: 100%; height: 100%; position: absolute; top: 0; left: 0; } path { transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1); transform-origin: center; } .play path { fill: #4CAF50; } .pause path { fill: #FFC107; } .stop path { fill: #F44336; } /* Initial states */ .pause, .stop { opacity: 0; transform: scale(0.8); } /* Active states */ .active { opacity: 1; transform: scale(1); z-index: 10; } /* Animation for the next state */ .next { opacity: 0; transform: scale(1.2); z-index: 5; }
JavaScript
const container = document.getElementById('iconController'); const icons = { play: document.querySelector('.play'), pause: document.querySelector('.pause'), stop: document.querySelector('.stop') }; let currentState = 'play'; container.addEventListener('click', () => { // Determine next state let nextState; if (currentState === 'play') nextState = 'pause'; else if (currentState === 'pause') nextState = 'stop'; else nextState = 'play'; // Set up animation classes icons[currentState].classList.remove('active'); icons[currentState].classList.add('next'); icons[nextState].classList.remove('next'); icons[nextState].classList.add('active'); // Reset the previous icon after animation completes setTimeout(() => { icons[currentState].classList.remove('next'); }, 600); currentState = nextState; });
Output Preview
Desktop
Mobile
Fullscreen