Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to create Airpods Animation using HTML CSS and JS

    15 July 2025

    How to create Heart Animation using HTML CSS and JS

    11 July 2025

    How to create Tab Bar Navigation using HTML CSS and JS

    9 July 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to create Heart Animation using HTML CSS and JS
    JavaScript

    How to create Heart Animation using HTML CSS and JS

    Coding StellaBy Coding Stella11 July 2025Updated:11 July 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create a Heart Animation using HTML, CSS, and JavaScript! ❤️ This project is perfect for adding a cute and interactive touch to your website, whether for Valentine’s Day or just for fun.

    We’ll use:

    • HTML to structure the heart shape.
    • CSS to style the heart and add smooth pulsing or floating animations.
    • JavaScript (optional) to add extra effects like clicking to create more hearts or triggering animations on events.

    Whether you’re a beginner or want to practice adding interactive elements, this project is a simple and creative way to learn.

    HTML :

    This HTML file sets up a full-screen heart animation using Three.js and GSAP: it includes a hidden SVG heart path that defines particle positions, links the required CSS and JavaScript files, loads Three.js, OrbitControls, and GSAP libraries, and initializes everything through the linked script.js file.

    <!DOCTYPE html>
    <html lang="en" >
    <head>
      <meta charset="UTF-8">
      <title>Heart Animation | @coding.stella</title>
      <meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="./style.css">
    </head>
    <body>
    
    <svg viewBox="0 0 600 552">
      <path d="M300,107.77C284.68,55.67,239.76,0,162.31,0,64.83,0,0,82.08,0,171.71c0,.48,0,.95,0,1.43-.52,19.5,0,217.94,299.87,379.69v0l0,0,.05,0,0,0,0,0v0C600,391.08,600.48,192.64,600,173.14c0-.48,0-.95,0-1.43C600,82.08,535.17,0,437.69,0,360.24,0,315.32,55.67,300,107.77" fill="#ee5282"/>
    </svg>
    
    <script src='https://cdn.jsdelivr.net/npm/three@0.136.0/build/three.min.js'></script>
    <script src='https://cdn.jsdelivr.net/npm/three@0.136.0/examples/js/controls/OrbitControls.js'></script>
    <script src='https://unpkg.co/gsap@3/dist/gsap.min.js'></script><script  src="./script.js"></script>
    
    </body>
    </html>

    CSS :

    This CSS removes body margins and scrollbars, positions controls and canvas elements absolutely for full-screen layout, hides the SVG used for particle paths, and ensures everything layers correctly using z-index and positioning.

    body {
      margin: 0;
      overflow: hidden;
    }
    
    .controls {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
    }
    
    canvas {
      position: absolute;
    }
    
    svg {
      position: absolute;
      display: none;
    }

    JavaScript:

    This code creates an animated 3D particle heart using Three.js and GSAP; it places particles along an SVG path, randomly offsets them in 3D space, then animates them moving in and out from the center while the whole scene slowly rotates, and resizes dynamically with the browser window.

    console.clear();
    
    /* SETUP */
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      5000
    );
    camera.position.z = 500;
    
    const renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio(window.devicePixelRatio > 1 ? 2 : 1);
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    
    /* CONTROLS */
    const controlsWebGL = new THREE.OrbitControls(camera, renderer.domElement);
    
    /* PARTICLES */
    // Create a global gsap timeline that contains all tweens
    const tl = gsap.timeline({
      repeat: -1,
      yoyo: true
    });
    
    const path = document.querySelector("path");
    const length = path.getTotalLength();
    const vertices = [];
    for (let i = 0; i < length; i += 0.1) {
      const point = path.getPointAtLength(i);
      const vector = new THREE.Vector3(point.x, -point.y, 0);
      vector.x += (Math.random() - 0.5) * 30;
      vector.y += (Math.random() - 0.5) * 30;
      vector.z += (Math.random() - 0.5) * 70;
      vertices.push(vector);
      // Create a tween for that vector
      tl.from(vector, {
          x: 600 / 2, // Center X of the heart
          y: -552 / 2, // Center Y of the heart
          z: 0, // Center of the scene
          ease: "power2.inOut",
          duration: "random(2, 5)" // Random duration
        },
        i * 0.002 // Delay calculated from the distance along the path
      );
    }
    const geometry = new THREE.BufferGeometry().setFromPoints(vertices);
    const material = new THREE.PointsMaterial( { color: 0xee5282, blending: THREE.AdditiveBlending, size: 3 } );
    const particles = new THREE.Points(geometry, material);
    // Offset the particles in the scene based on the viewbox values
    particles.position.x -= 600 / 2;
    particles.position.y += 552 / 2;
    scene.add(particles);
    
    gsap.fromTo(scene.rotation, {
      y: -0.2
    }, {
      y: 0.2,
      repeat: -1,
      yoyo: true,
      ease: 'power2.inOut',
      duration: 3
    });
    
    /* RENDERING */
    function render() {
      requestAnimationFrame(render);
      // Update the geometry from the animated vertices
      geometry.setFromPoints(vertices);
      renderer.render(scene, camera);
    }
    
    /* EVENTS */
    function onWindowResize() {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
    }
    window.addEventListener("resize", onWindowResize, false);
    
    requestAnimationFrame(render);

    In short, creating a Heart Animation with HTML, CSS, and JavaScript is an easy and fun way to bring websites to life with a sweet visual effect. It’s great for learning basic animations and adding interactive touches to your designs. Let’s keep exploring and making web pages more lively and engaging! ❤️

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    Animation heart JavaScript valentine
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to create Tab Bar Navigation using HTML CSS and JS
    Next Article How to create Airpods Animation using HTML CSS and JS
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to create Airpods Animation using HTML CSS and JS

    15 July 2025
    JavaScript

    How to create Tab Bar Navigation using HTML CSS and JS

    9 July 2025
    JavaScript

    How to create Newton’s Sun and Moon Loading Animation using HTML CSS and JS

    5 July 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202420K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202419K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202417K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202413K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    Frontend vs Backend : The Face and Brains of the Internet

    17 January 2024

    10 HTML Tips and Tricks Every Developer Should Know

    27 January 2024

    15 Free Login & Registration Form Projects in HTML CSS & JS | Frontend Project

    25 January 2024

    How to make Valentine’s Day Card using HTML & CSS

    13 February 2024
    Latest Post

    How to create Airpods Animation using HTML CSS and JS

    15 July 2025

    How to create Heart Animation using HTML CSS and JS

    11 July 2025

    How to create Tab Bar Navigation using HTML CSS and JS

    9 July 2025

    How to create Newton’s Sun and Moon Loading Animation using HTML CSS and JS

    5 July 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood