Skip to main content
<CodeWithBhurtel/>
HomeLearnProjectsChallengesBlogAboutContactLogin
<CodeWithBhurtel/>

Learn through structured lessons, real projects, and live challenges. Free tutorials for HTML, CSS, JavaScript, and more.

Pages

  • Home
  • Learn
  • Blog
  • Projects
  • Challenges
  • About
  • Contact

Lessons

  • HTML
  • CSS
  • JavaScript

© 2026 CodeWithBhurtel. All rights reserved.

Privacy PolicyTerms & Conditions
Projects

Heart Animation

IntermediateCSS, HTML, JavaScript

The hardest part of this project to understand is the relationship between the time variable, the cosine function and the pulse function.

Heart Animation – project preview
Share this project
Follow me on social media

About this project

This project takes a single mathematical idea and turns it into something you can actually see move on screen. Instead of loading an image of a heart, the code calculates thousands of coordinates using a trigonometric formula and plots them as individual glowing particles. Each particle chases a target point on the heart shape. Because each particle moves at its own speed, they never arrive all at once. That delay is what creates the soft trailing glow that makes the animation feel alive.The beating effect comes from a cosine function that runs in the background. It continuously scales the heart slightly larger and slightly smaller. Your eye reads that rhythmic size change as a heartbeat. The canvas is never fully cleared between frames either. Each new frame paints a slightly transparent black layer over the previous one. Old particle positions fade out slowly instead of disappearing instantly.

That one trick is responsible for the smoke-like trails you see behind every particle.The project is also responsive. It checks the screen size before anything starts and adjusts both the number of particles and the scale of the heart accordingly. Mobile devices get fewer particles to keep performance smooth. Desktop screens get the full density.

What you will learn

You will learn how the HTML5 Canvas API works from scratch. Drawing to a canvas is nothing like manipulating the DOM. There are no elements to select or styles to apply. You simply clear a rectangle and redraw everything from coordinates on every single frame. You will understand how parametric equations work in a visual context. The heart formula takes a single angle value and returns an X and Y coordinate. By running that formula hundreds of times across different angle values you trace out the full shape. That same approach works for circles, spirals and many other curves.You will get a solid understanding of how particle systems behave. Each particle in this project is just a plain JavaScript object with a position, a velocity and a color. The magic is not in any single particle. It comes from hundreds of them moving independently but all targeting the same shape. You will also see how requestAnimationFrame creates smooth animation. It ties your loop to the browser's own rendering cycle so frames stay consistent and the animation never tears or stutters.

Finally you will understand how HSLA color values work. The particles use randomized hue, saturation and lightness values with a low alpha. That low alpha is what makes them look like embers instead of solid dots.

Understand with AI

Get an AI-generated overview or a file-by-file code breakdown. Ask questions about the project below the overview.

AI Overview: Heart Animation

This project is a visually striking web-based animation that renders a "beating" heart made of hundreds of glowing particles. Its main purpose is to demonstrate how mathematical equations can be used to create organic, fluid motion on a digital canvas. The animation is responsive, adjusting its particle density and scale based on whether the user is on a mobile device or a desktop computer.

The project works by using the HTML5 Canvas API to draw and update frames many times per second. Instead of using a static image, the code calculates the coordinates of a heart shape using a parametric mathematical formula. A system of individual particles is then programmed to chase these coordinates, creating a trailing effect that looks like glowing embers or smoke forming a heart.

Key Code Parts

1. The Canvas Setup
The HTML file provides a simple container for the animation using the <canvas> element, which acts as the drawing board for the JavaScript logic.

<canvas id="heart"></canvas>

2. Defining the Heart Shape
The heart isn't an image; it is defined by a mathematical function. This snippet calculates the X and Y coordinates needed to draw a perfect heart curve.

var heartPosition = function (rad) {
    return [Math.pow(Math.sin(rad), 3), -(15 * Math.cos(rad) - 5 * Math.cos(2 * rad) - 2 * Math.cos(3 * rad) - Math.cos(4 * rad))];
};

3. Particle Initialization
The script creates an array of particle objects, each with its own unique speed, color (using HSLA values), and "trace" history to create the glowing tail effect.

e[i] = {
    vx: 0,
    vy: 0,
    speed: rand() + 5,
    q: ~~(rand() * heartPointsCount),
    f: "hsla(0," + ~~(40 * rand() + 100) + "%," + ~~(60 * rand() + 20) + "%,.3)",
    trace: []
};

4. The Beating Pulse
To make the heart look like it is breathing or beating, the pulse function dynamically scales the target coordinates up and down over time.

var pulse = function (kx, ky) {
    for (i = 0; i < pointsOrigin.length; i++) {
        targetPoints[i] = [];
        targetPoints[i][0] = kx * pointsOrigin[i][0] + width / 2;
        targetPoints[i][1] = ky * pointsOrigin[i][1] + height / 2;
    }
};

5. The Animation Loop
The loop function is the engine of the project; it clears the screen slightly (to leave trails), updates the position of every particle, and requests the next frame to keep the motion smooth.

var loop = function () {
    var n = -Math.cos(time);
    pulse((1 + n) * .5, (1 + n) * .5);
    window.requestAnimationFrame(loop, canvas);
};

Ask about this project

Have a question about how this project works? Ask below and get an answer based on the project code.

Code Breakdown

Structured technical breakdown for beginners. Anyone can generate or regenerate; your version is saved in this browser (local storage).

No breakdown yet. Generate one to see a file-by-file explanation. Your version is saved in this browser.

Comments

No comments yet. Sign in to leave a comment.

Sign in to leave a comment.