The Technical Architecture Behind Meme Generators

Published: (March 24, 2026 at 10:04 PM EDT)
3 min read
Source: Dev.to

Source: Dev.to

The canvas rendering pipeline

At its core, a meme generator is a two‑layer compositing system: a background image and text overlays. The HTML5 Canvas API provides everything needed.

async function generateMeme(imageUrl, topText, bottomText) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');

  const img = new Image();
  img.crossOrigin = 'anonymous';

  return new Promise((resolve) => {
    img.onload = () => {
      canvas.width = img.width;
      canvas.height = img.height;

      // Draw background image
      ctx.drawImage(img, 0, 0);

      // Configure text style
      ctx.fillStyle = 'white';
      ctx.strokeStyle = 'black';
      ctx.lineWidth = img.width / 200;
      ctx.textAlign = 'center';
      ctx.font = `bold ${img.width / 12}px Impact`;

      // Top text
      ctx.strokeText(topText, canvas.width / 2, img.width / 8);
      ctx.fillText(topText, canvas.width / 2, img.width / 8);

      // Bottom text
      ctx.strokeText(bottomText, canvas.width / 2, img.height - img.width / 20);
      ctx.fillText(bottomText, canvas.width / 2, img.height - img.width / 20);

      resolve(canvas.toDataURL('image/png'));
    };
    img.src = imageUrl;
  });
}

Why Impact font is the standard

The Impact font became the meme standard not through design choice but through availability. It ships with every Windows installation since Windows 98, making it the most universally available heavy condensed sans‑serif. Its thick strokes and tight spacing make it readable at small sizes when overlaid on busy images.

The white text with black outline technique ensures readability on any background. The outline is drawn first with strokeText, then the fill is drawn on top with fillText. This two‑pass approach produces the characteristic meme text look.

Text wrapping on canvas

Canvas fillText does not wrap text automatically. If the text is wider than the image, it overflows. You need manual word wrapping:

function wrapText(ctx, text, maxWidth) {
  const words = text.split(' ');
  const lines = [];
  let currentLine = words[0];

  for (let i = 1; i < words.length; i++) {
    const testLine = currentLine + ' ' + words[i];
    if (ctx.measureText(testLine).width > maxWidth) {
      lines.push(currentLine);
      currentLine = words[i];
    } else {
      currentLine = testLine;
    }
  }
  lines.push(currentLine);
  return lines;
}

The measureText method returns the pixel width of a string in the current font context. This is the only reliable way to determine line breaks because character widths vary by font.

Dynamic font sizing

For a polished meme generator, the font size should adapt to the text length. Short text gets a large font; long text gets a smaller font to fit within the image.

function fitFontSize(ctx, text, maxWidth, maxSize, minSize) {
  for (let size = maxSize; size >= minSize; size -= 2) {
    ctx.font = `bold ${size}px Impact`;
    if (ctx.measureText(text).width <= maxWidth) {
      return size;
    }
  }
  return minSize;
}

It handles text wrapping, dynamic sizing, and exports clean PNGs using the canvas pipeline described above.

0 views
Back to Blog

Related posts

Read more »

NetNostalgia

Overview I’ve always been fascinated by how fast the internet evolved. From messy, colorful websites in the 90s to the clean, minimal design we have today — it...