228

Cardiac Pulse & ECG

Biomedical cardiovascular simulation combining the classic mathematical cardioid heart contraction with a multi-Gaussian P-QRS-T electrocardiogram trace.

Playground
60 FPS Canvas 2D
Click + Drag to interact with field
</>

Full Executable Algorithm Code

// 035 - Cardiac Pulse & ECG (anatomy)
// 1:1 Original algorithm engine source
function createCardiacPulse() {
  const MAX_HISTORY = 300;
  const ecgHistory = new Float32Array(MAX_HISTORY);
  let historyIdx = 0;
  function ecgWaveform(phase) {
    const p = phase % 1;
    const pWave = 0.18 * Math.exp(-Math.pow((p - 0.2) / 0.04, 2));
    const qWave = -0.15 * Math.exp(-Math.pow((p - 0.36) / 0.015, 2));
    const rWave = 1 * Math.exp(-Math.pow((p - 0.4) / 0.02, 2));
    const sWave = -0.3 * Math.exp(-Math.pow((p - 0.44) / 0.018, 2));
    const tWave = 0.35 * Math.exp(-Math.pow((p - 0.65) / 0.07, 2));
    return pWave + qWave + rWave + sWave + tWave;
  }
  return {
    setup() {
      ecgHistory.fill(0);
      historyIdx = 0;
    },
    render(context, timeState, params) {
      const { ctx, width, height } = context;
      const bpm = Number(params.heartRateBPM || 72);
      const freq = bpm / 60;
      const t = timeState.time;
      const beatPhase = t * freq % 1;
      ctx.fillStyle = "#06070a";
      ctx.fillRect(0, 0, width, height);
      const cx = width * 0.5;
      const cy = height * 0.45;
      const heartScale = Math.min(width, height) / 480;
      const ecgVal = ecgWaveform(beatPhase);
      ecgHistory[historyIdx] = ecgVal;
      historyIdx = (historyIdx + 1) % MAX_HISTORY;
      const pulseSize = 1 + ecgVal * 0.22;
      ctx.save();
      ctx.translate(cx, cy);
      ctx.scale(heartScale * pulseSize, heartScale * pulseSize);
      ctx.beginPath();
      const heartSteps = 100;
      for (let i = 0; i <= heartSteps; i++) {
        const phi = i / heartSteps * Math.PI * 2;
        const hx = 16 * Math.pow(Math.sin(phi), 3) * 6;
        const hy = -(13 * Math.cos(phi) - 5 * Math.cos(2 * phi) - 2 * Math.cos(3 * phi) - Math.cos(4 * phi)) * 6;
        if (i === 0) ctx.moveTo(hx, hy);
        else ctx.lineTo(hx, hy);
      }
      ctx.closePath();
      const heartHue = 350;
      ctx.fillStyle = hsla(heartHue, 90, 45, 0.3 + ecgVal * 0.4);
      ctx.fill();
      ctx.strokeStyle = hsla(355, 95, 70, 0.9);
      ctx.lineWidth = 3;
      ctx.stroke();
      for (let side = -1; side <= 1; side += 2) {
        ctx.beginPath();
        ctx.moveTo(0, -90);
        ctx.bezierCurveTo(side * 45, -135, side * 75, -95, side * 50, -45);
        ctx.strokeStyle = hsla(15, 95, 65, 0.85);
        ctx.lineWidth = 4;
        ctx.stroke();
      }
      ctx.restore();
      const ecgY = height * 0.84;
      const traceW = width * 0.88;
      const startX = width * 0.06;
      ctx.beginPath();
      for (let i = 0; i < MAX_HISTORY; i++) {
        const sampleIdx = (historyIdx + i) % MAX_HISTORY;
        const px = startX + i / MAX_HISTORY * traceW;
        const py = ecgY - ecgHistory[sampleIdx] * 48;
        if (i === 0) ctx.moveTo(px, py);
        else ctx.lineTo(px, py);
      }
      ctx.strokeStyle = "#34d399";
      ctx.lineWidth = 2.2;
      ctx.shadowColor = "#34d399";
      ctx.shadowBlur = 12;
      ctx.stroke();
      ctx.shadowBlur = 0;
      ctx.strokeStyle = "rgba(52, 211, 153, 0.15)";
      ctx.lineWidth = 1;
      ctx.beginPath();
      ctx.moveTo(startX, ecgY);
      ctx.lineTo(startX + traceW, ecgY);
      ctx.stroke();
    }
  };
}

// Default parameters from content metadata
const defaultParams = [
  {
    "key": "heartRateBPM",
    "label": "Heart Rate (BPM)",
    "type": "range",
    "min": 40,
    "max": 160,
    "step": 2,
    "defaultValue": 72,
    "description": "Beats per minute cardiac frequency"
  }
];

if (!window.__art_instances) window.__art_instances = {};
if (!window.__art_instances['cardiac-pulse']) {
  const inst = typeof createCardiacPulse === 'function' ? createCardiacPulse() : null;
  if (inst && inst.setup) {
    inst.setup({ ctx, width, height, dpr: 1, aspectRatio: width / height }, defaultParams);
  }
  window.__art_instances['cardiac-pulse'] = inst;
}

const instance = window.__art_instances['cardiac-pulse'];
if (instance && instance.render) {
  instance.render(
    { ctx, width, height, dpr: 1, aspectRatio: width / height },
    { time, deltaTime: dt, frameCount: Math.floor(time * 60), fps: 60 },
    defaultParams
  );
}
Edit in Interactive Playground Zero Dependencies • Standalone Canvas 2D
ƒ

Mathematical Formulation

high
Analytical Equation
VECG(t)=i{P,Q,R,S,T}aiexp((tμi)22σi2),x(t)=16sin3(t),y(t)=(13cost5cos2t2cos3tcos4t)V_{\text{ECG}}(t) = \sum_{i \in \{P, Q, R, S, T\}} a_i \exp\left( -\frac{(t - \mu_i)^2}{2\sigma_i^2} \right), \quad x(t) = 16\sin^3(t), \quad y(t) = -(13\cos t - 5\cos 2t - 2\cos 3t - \cos 4t)
Click to expand
Compact Formula
ecg(t) = P(0.18) + Q(-0.15) + R(1.0) + S(-0.3) + T(0.35), heart_scale = 1 + 0.22*ecg

Mathematical Tags

#heart #cardiac #ecg #anatomy #biology #cardioid #pulse
Author: Math Art Core Target: 60 FPS

Export & Embed: Cardiac Pulse & ECG

4K PNG Snapshot

High-resolution single frame render

WebM Video (5s Loop)

60 FPS browser-captured stream

Standalone JS Script

Complete executable Canvas 2D algorithm

HTML Iframe Embed

<iframe src="https://art.fazleyrabbi.xyz/embed/cardiac-pulse" width="500" height="500" frameborder="0" loading="lazy"></iframe>
ESC
↑↓ Navigate Select
110 Mathematical Artworks
Made by Fazley Rabbi

Support the Project

Keep mathematical creative coding alive & open source

Or via Direct Payoneer ($0 Fee)
Payoneer Customer ID: $0 Fee Direct
24076084

💡 Payoneer app: Go to Pay → Pay to recipient → Enter ID 24076084.

Thank you for supporting generative mathematical animations! ✨