0Pricing
JavaScript Academy · Lesson

Working with Images and Text

Draw images and styled text on canvas.

Working with Images and Text is a free JavaScript 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 JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loading an Image

Canvas draws from an HTMLImageElement. Create an Image, set its src, and wait for onload before drawing — the pixels are not available until then.

const img = new Image();
img.onload = () => ctx.drawImage(img, 0, 0);
img.src = "/logo.png";

drawImage Basics

drawImage(img, dx, dy) paints the image at the given position at its natural size.

img.onload = () => {
  ctx.drawImage(img, 20, 20);
};

Scaling Images

The 5-argument form drawImage(img, dx, dy, dWidth, dHeight) scales the image into a target box.

ctx.drawImage(img, 0, 0, 160, 90); // fit into 160x90

Cropping with Source Rect

The 9-argument form lets you crop: drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight). The first four describe the source slice.

ctx.drawImage(img, 0, 0, 50, 50, 10, 10, 100, 100); // crop 50x50 → scale to 100x100

Drawing Filled Text

fillText(text, x, y) paints text using fillStyle. The y is the text baseline by default.

ctx.fillStyle = "#2c3e50";
ctx.font = "24px sans-serif";
ctx.fillText("Hello Canvas", 20, 50);

Outlined Text

strokeText(text, x, y) draws only the outline using strokeStyle and lineWidth.

ctx.font = "40px bold serif";
ctx.lineWidth = 2;
ctx.strokeStyle = "navy";
ctx.strokeText("Outline", 20, 80);

The font Property

font follows CSS shorthand: [style] [weight] size family. Size and family are required.

ctx.font = "italic bold 28px Georgia, serif";

Text Alignment

textAlign (start/end/left/right/center) sets horizontal anchoring; textBaseline (top/middle/alphabetic/bottom) sets vertical anchoring around the given point.

ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Centered", canvas.width / 2, canvas.height / 2);

Measuring Text

measureText(text) returns a TextMetrics object. Its width tells you how wide the text will render — essential for layout and wrapping.

const m = ctx.measureText("How wide?");
console.log(m.width); // pixels

Simple Word Wrapping

Combine measureText with a loop to wrap long strings across lines.

function wrap(ctx, text, x, y, maxW, lh) {
  let line = "";
  for (const word of text.split(" ")) {
    const test = line + word + " ";
    if (ctx.measureText(test).width > maxW && line) {
      ctx.fillText(line, x, y);
      line = word + " ";
      y += lh;
    } else {
      line = test;
    }
  }
  ctx.fillText(line, x, y);
}

Shadows for Depth

Shadow properties apply to shapes, images, and text: shadowColor, shadowBlur, shadowOffsetX/Y.

ctx.shadowColor = "rgba(0,0,0,0.4)";
ctx.shadowBlur = 8;
ctx.shadowOffsetX = 3;
ctx.shadowOffsetY = 3;
ctx.fillText("With shadow", 20, 60);

Quick Check

Test image and text drawing.

Recap: Images and Text

You learned to load images and draw them with scaling and cropping via drawImage, render text with fillText/strokeText, set font, align text, measure widths with measureText, and add shadows. Next: animation.

Frequently asked questions

Is the “Working with Images and Text” lesson free?

Yes — the full text of “Working with Images and Text” is free to read here on the web, and the JavaScript 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 JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Working with Images and Text”?

Draw images and styled text on canvas. You practise JavaScript 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 JavaScript Academy?

No prior experience is required. JavaScript 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 “Working with Images and Text” 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 JavaScript Academy lesson?

Yes. Every JavaScript 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

  1. Setting Up the Canvas Context
  2. Drawing Shapes and Paths
  3. Working with Images and Text
  4. Animation with requestAnimationFrame
← Back to JavaScript Academy