Memes are the internet's dominant communication medium. A meme generator is technically a canvas compositing engine with text overlay capabilities. Building one teaches you more about image processing than most tutorials.
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];
const metrics = ctx.measureText(testLine);
if (metrics.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;
}
Image export formats
Memes are typically shared as PNG or JPEG. PNG preserves text sharpness but produces larger files. JPEG compresses better but introduces artifacts around text edges.
For meme generation, PNG is the better default because the sharp text-on-image boundary creates visible JPEG artifacts that make the meme look low quality.
For creating memes quickly without installing software, I built a generator at zovo.one/free-tools/meme-generator. It handles text wrapping, dynamic sizing, and exports clean PNGs. The implementation uses exactly the canvas pipeline described above.
I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.
Top comments (0)