CKTIS / JavaScript
Canvas 2D: stroke, fill, font, and text baseline
Set stroke width and paint, fill paint, the font, and the vertical text anchor on a Canvas 2D rendering context.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const ctx = canvas.getContext("2d");
// After checking that ctx is not null:
ctx.lineWidth = 4;
ctx.strokeStyle = "#1d4ed8";
ctx.fillStyle = "#dbeafe";
ctx.font = "20px sans-serif";
ctx.textBaseline = "top";
// Read any of these properties with ctx.propertyName.CanvasRenderingContext2D.lineWidth/strokeStyle/fillStyle/font/textBaseline names five properties on the context returned by canvas.getContext("2d"), not on the canvas element itself. They are Web APIs, not ECMAScript features: availability depends on the runtime, and getContext() can return null. Assignments change state for later drawing, not pixels already drawn. save() stores the state and restore() brings it back without undoing the drawing or changes to the current path.
lineWidth sets stroke thickness in canvas coordinate units; a scaling transform can change the visible thickness. The stroke is centered on the path. The initial width is 1. Zero, negative numbers, Infinity, -Infinity, and NaN are ignored.
strokeStyle sets the paint for outlines, such as those drawn by strokeRect() and strokeText(). fillStyle sets the paint for filled areas, such as those drawn by fillRect() and fillText(). Both accept a CSS color string, a CanvasGradient, or a CanvasPattern, and both start out black. An invalid color string leaves the previous value in place.
font takes a string in CSS font shorthand syntax, such as "bold 20px sans-serif". It starts at "10px sans-serif". Invalid strings are ignored, and a line-height setting has no effect here. If a font is unavailable or has not loaded, the browser falls back to another font; glyph appearance can differ across environments.
textBaseline chooses the vertical reference that fillText() and strokeText() place at y. "top", "middle", and "bottom" refer to the top, middle, and bottom of the font's em box, not the edges of the visible glyphs. The other options are "hanging" (hanging baseline), "alphabetic" (alphabetic baseline, the default), and "ideographic" (ideographic under-baseline). Other strings are ignored. Horizontal alignment is controlled separately by textAlign.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Canvas 2D drawing styles</title>
</head>
<body>
<canvas id="demo" width="240" height="80"></canvas>
<script>
const canvas = document.getElementById("demo");
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Canvas 2D is unavailable");
const readStyles = () => [
ctx.lineWidth, ctx.strokeStyle, ctx.fillStyle, ctx.font, ctx.textBaseline
].join(" | ");
ctx.save();
ctx.lineWidth = 4;
ctx.lineWidth = 0;
ctx.strokeStyle = "#1d4ed8";
ctx.fillStyle = "#dbeafe";
ctx.fillRect(10, 10, 220, 60);
ctx.strokeRect(10, 10, 220, 60);
ctx.fillStyle = "#172554";
ctx.font = "20px sans-serif";
ctx.textBaseline = "top";
ctx.fillText("Canvas 2D", 22, 20);
console.log(readStyles());
ctx.restore();
console.log(readStyles());
</script>
</body>
</html>Result
4 | #1d4ed8 | #172554 | 20px sans-serif | top 1 | #000000 | #000000 | 10px sans-serif | alphabetic
Save the example as an HTML file, open it in a browser, and check the developer console. The first line shows the values of five context properties before `restore()`. The width stays at `4` despite the assignment of `0`. Of the five logged properties, `strokeRect()` uses `lineWidth` and `strokeStyle` for the rectangle outline, while `fillText()` uses `fillStyle`, `font`, and `textBaseline`. The second line shows the restored initial values. The light-blue rectangle with its blue outline and dark text remains visible.
Related lessons
- JavaScript · Lesson 26 · Intermediate · Version: 3