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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
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 160x90Cropping 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 100x100Drawing 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); // pixelsSimple 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.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Working with Images and Text” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Working with Images and Text”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Setting Up the Canvas Context
- Drawing Shapes and Paths
- Working with Images and Text
- Animation with requestAnimationFrame