Back to Components
Canvas Heart Particle Animation – Pulsing Love Effect
Component

Canvas Heart Particle Animation – Pulsing Love Effect

CodewithLord
February 8, 2026

A beautiful heart-shaped particle animation rendered on HTML Canvas using pure JavaScript. The heart pulses smoothly and adapts to screen size, creating a romantic and mesmerizing visual effect.



❤️ Description


This project showcases a pulsing heart animation built using HTML Canvas and vanilla JavaScript. Hundreds of tiny particles dynamically move to form a heart shape, creating a smooth breathing / heartbeat effect.

The animation is fully responsive, performance-optimized, and works beautifully across desktop and mobile devices.


Why This Animation Feels Magical


  • Heart shape generated mathematically (no images)
  • Smooth pulse animation synced with sine waves
  • Particle trails create a dreamy effect
  • Fully responsive canvas
  • Mobile-friendly performance tuning
  • Perfect for Valentine projects, love pages, and creative demos

Core Concept


  • A heart curve equation generates target points
  • Particles are attracted toward these points
  • Pulse effect scales the heart in real time
  • Motion trails are drawn using alpha blending
  • requestAnimationFrame ensures smooth rendering



💻 Step 1: HTML Structure


Complete HTML Code


1<!DOCTYPE html> 2<html lang="en"> 3 4<head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>Heart Animation | CodewithLord </title> 8 <link rel="stylesheet" href="style.css"> 9</head> 10 11<body> 12 <canvas id="heart"></canvas> 13 <script src="script.js"></script> 14</body> 15</html>

HTML Breakdown


  • Single <canvas> fills the entire viewport
  • No DOM elements required for animation
  • JavaScript controls all rendering logic


🎨 Step 2: CSS – Canvas Styling


CSS Code


1canvas { 2 position: absolute; 3 left: 0; 4 top: 0; 5 width: 100%; 6 height: 100%; 7 background-color: #00000033; 8}

CSS Breakdown


  • Canvas covers full screen
  • Semi-transparent background enables trail effect
  • Absolute positioning prevents layout shifts


⚙️ Step 3: JavaScript – Heart & Particle Logic


JavaScript Code


1window.requestAnimationFrame = 2 window.__requestAnimationFrame || 3 window.requestAnimationFrame || 4 window.webkitRequestAnimationFrame || 5 window.mozRequestAnimationFrame || 6 window.oRequestAnimationFrame || 7 window.msRequestAnimationFrame || 8 (function () { 9 return function (callback, element) { 10 var lastTime = element.__lastTime; 11 if (lastTime === undefined) { 12 lastTime = 0; 13 } 14 var currTime = Date.now(); 15 var timeToCall = Math.max(1, 33 - (currTime - lastTime)); 16 window.setTimeout(callback, timeToCall); 17 element.__lastTime = currTime + timeToCall; 18 }; 19 })(); 20window.isDevice = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(((navigator.userAgent || navigator.vendor || window.opera)).toLowerCase())); 21var loaded = false; 22var init = function () { 23 if (loaded) return; 24 loaded = true; 25 var mobile = window.isDevice; 26 var koef = mobile ? 0.5 : 1; 27 var canvas = document.getElementById('heart'); 28 var ctx = canvas.getContext('2d'); 29 var width = canvas.width = koef * innerWidth; 30 var height = canvas.height = koef * innerHeight; 31 var rand = Math.random; 32 ctx.fillStyle = "rgba(0,0,0,1)"; 33 ctx.fillRect(0, 0, width, height); 34 35 var heartPosition = function (rad) { 36 //return [Math.sin(rad), Math.cos(rad)]; 37 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))]; 38 }; 39 var scaleAndTranslate = function (pos, sx, sy, dx, dy) { 40 return [dx + pos[0] * sx, dy + pos[1] * sy]; 41 }; 42 43 window.addEventListener('resize', function () { 44 width = canvas.width = koef * innerWidth; 45 height = canvas.height = koef * innerHeight; 46 ctx.fillStyle = "rgba(0,0,0,1)"; 47 ctx.fillRect(0, 0, width, height); 48 }); 49 50 var traceCount = mobile ? 20 : 50; 51 var pointsOrigin = []; 52 var i; 53 var dr = mobile ? 0.3 : 0.1; 54 for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 210, 13, 0, 0)); 55 for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 150, 9, 0, 0)); 56 for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 90, 5, 0, 0)); 57 var heartPointsCount = pointsOrigin.length; 58 59 var targetPoints = []; 60 var pulse = function (kx, ky) { 61 for (i = 0; i < pointsOrigin.length; i++) { 62 targetPoints[i] = []; 63 targetPoints[i][0] = kx * pointsOrigin[i][0] + width / 2; 64 targetPoints[i][1] = ky * pointsOrigin[i][1] + height / 2; 65 } 66 }; 67 68 var e = []; 69 for (i = 0; i < heartPointsCount; i++) { 70 var x = rand() * width; 71 var y = rand() * height; 72 e[i] = { 73 vx: 0, 74 vy: 0, 75 R: 2, 76 speed: rand() + 5, 77 q: ~~(rand() * heartPointsCount), 78 D: 2 * (i % 2) - 1, 79 force: 0.2 * rand() + 0.7, 80 f: "hsla(0," + ~~(40 * rand() + 100) + "%," + ~~(60 * rand() + 20) + "%,.3)", 81 trace: [] 82 }; 83 for (var k = 0; k < traceCount; k++) e[i].trace[k] = { x: x, y: y }; 84 } 85 86 var config = { 87 traceK: 0.4, 88 timeDelta: 0.01 89 }; 90 91 var time = 0; 92 var loop = function () { 93 var n = -Math.cos(time); 94 pulse((1 + n) * .5, (1 + n) * .5); 95 time += ((Math.sin(time)) < 0 ? 9 : (n > 0.8) ? .2 : 1) * config.timeDelta; 96 ctx.fillStyle = "rgba(0,0,0,.1)"; 97 ctx.fillRect(0, 0, width, height); 98 for (i = e.length; i--;) { 99 var u = e[i]; 100 var q = targetPoints[u.q]; 101 var dx = u.trace[0].x - q[0]; 102 var dy = u.trace[0].y - q[1]; 103 var length = Math.sqrt(dx * dx + dy * dy); 104 if (10 > length) { 105 if (0.95 < rand()) { 106 u.q = ~~(rand() * heartPointsCount); 107 } 108 else { 109 if (0.99 < rand()) { 110 u.D *= -1; 111 } 112 u.q += u.D; 113 u.q %= heartPointsCount; 114 if (0 > u.q) { 115 u.q += heartPointsCount; 116 } 117 } 118 } 119 u.vx += -dx / length * u.speed; 120 u.vy += -dy / length * u.speed; 121 u.trace[0].x += u.vx; 122 u.trace[0].y += u.vy; 123 u.vx *= u.force; 124 u.vy *= u.force; 125 for (k = 0; k < u.trace.length - 1;) { 126 var T = u.trace[k]; 127 var N = u.trace[++k]; 128 N.x -= config.traceK * (N.x - T.x); 129 N.y -= config.traceK * (N.y - T.y); 130 } 131 ctx.fillStyle = u.f; 132 for (k = 0; k < u.trace.length; k++) { 133 ctx.fillRect(u.trace[k].x, u.trace[k].y, 1, 1); 134 } 135 } 136 //ctx.fillStyle = "rgba(255,255,255,1)"; 137 //for (i = u.trace.length; i--;) ctx.fillRect(targetPoints[i][0], targetPoints[i][1], 2, 2); 138 139 window.requestAnimationFrame(loop, canvas); 140 }; 141 loop(); 142}; 143 144var s = document.readyState; 145if (s === 'complete' || s === 'loaded' || s === 'interactive') init(); 146else document.addEventListener('DOMContentLoaded', init, false);

<br>

## JavaScript Breakdown

<br>

### Key Concepts Used

<br>

**Heart Equation**
→ Mathematical formula generates heart shape

**Particle System**
→ Each particle follows a target heart point

**Pulse Animation**
→ Heart scales using sine & cosine waves

**Trails Effect**
→ Semi-transparent redraw creates motion blur

**requestAnimationFrame**
→ Smooth 60fps animation loop

<br>

## ❤️ Animation Flow

<br>

- Calculate heart curve points
- Assign particles random starting positions
- Pull particles toward heart shape
- Apply pulsing scale transformation
- Render trails frame-by-frame

<br>

## 📱 Responsive Behavior

<br>

- Canvas resizes on window resize
- Reduced particle density on mobile devices
- Performance-friendly calculations

<br>

## 🚀 Use Cases

<br>

- Valentine landing pages ❤️
- Love proposals & surprises
- Creative coding portfolios
- YouTube Shorts / Instagram Reels visuals
- Canvas animation tutorials

<br>

## 💡 Enhancement Ideas

<br>

- Add mouse interaction
- Sync pulse with music
- Change heart color dynamically
- Export as background animation

<br>

---

✨ **A perfect blend of math, art, and emotion — powered by JavaScript Canvas.**

Love this component?

Explore more components and build amazing UIs.

View All Components