Text and Images on Canvas
Render text strings and draw images onto the canvas.
Text and Images on Canvas is a free HTML Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Drawing Text
Set font: ctx.font = "bold 24px sans-serif". Draw: ctx.fillText("Hello", x, y) or ctx.strokeText("Hello", x, y). The y coordinate is the text baseline — add ascender height above it to position text correctly within a bounding box.
Text Alignment
ctx.textAlign controls horizontal alignment: "left" (default), "center", "right". ctx.textBaseline controls vertical alignment relative to y: "top", "middle", "alphabetic" (default), "bottom". For centered text: set both to "center" and "middle".
measureText for Layout
ctx.measureText("text") returns a TextMetrics object with width and font metrics. Use it to calculate text positions dynamically — centering text in a box, wrapping text at a max width, or right-aligning labels next to data points.
Text Wrapping
Canvas has no built-in text wrapping. Implement it: measure word widths, accumulate until exceeding maxWidth, then break to a new line at lineHeight intervals. This is a common utility function in canvas rendering libraries.
Drawing Images
ctx.drawImage(image, dx, dy) draws an HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement at (dx, dy). Load the image first and draw in its onload handler to ensure pixels are available before drawing.
const img = new Image();
img.onload = () => ctx.drawImage(img, 0, 0);
img.src = "photo.jpg";Scaling Images
ctx.drawImage(image, dx, dy, dw, dh) scales the image to fit dw x dh. No aspect ratio preservation is automatic — calculate dimensions manually. To preserve aspect ratio: dw = img.width * scale; dh = img.height * scale.
Cropping Images
Nine-argument drawImage: ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh). The source rectangle (sx, sy, sw, sh) crops from the image; the destination rectangle (dx, dy, dw, dh) places and scales the crop on canvas.
Image Smoothing
ctx.imageSmoothingEnabled = false disables bilinear interpolation when scaling images. Use for pixel art where crisp, unsmoothed scaling is desired. Leave enabled (default) for photographic images where smooth scaling looks better.
Canvas as Image Source
Pass a canvas as the source to drawImage — rendering one canvas onto another. This enables off-screen rendering: draw complex composites on a hidden canvas, then draw that canvas onto the visible one in a single operation each frame.
globalAlpha and Compositing
ctx.globalAlpha (0 to 1) applies uniform transparency to all subsequent drawing. ctx.globalCompositeOperation controls how new drawings blend with existing pixels: "source-over" (default), "multiply", "screen", "destination-out" (erase), and more.
toDataURL for Export
canvas.toDataURL("image/png") exports the canvas as a base64 PNG data URL. Use it for user-initiated download: create an anchor, set href to the data URL and download attribute to a filename, then programmatically click it.
Knowledge Check
What does ctx.textBaseline = "middle" affect when drawing text?
Summary
Canvas text rendering uses ctx.font, fillText/strokeText, textAlign and textBaseline for positioning, and measureText for layout calculations. Image drawing via drawImage supports scaling and cropping; globalAlpha and globalCompositeOperation control blending. toDataURL exports the canvas for download or further use.
Frequently asked questions
Is the “Text and Images on Canvas” lesson free?
Yes — the full text of “Text and Images on Canvas” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Text and Images on Canvas”?
Render text strings and draw images onto the canvas. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Text and Images on Canvas” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up the Canvas and 2D Context
- Drawing Rectangles and Paths
- Text and Images on Canvas
- Animation with requestAnimationFrame