CKTIS / JavaScript
Canvas 2D: fill style, stroke style, and line width
`fillStyle` controls what fills a shape, `strokeStyle` controls what paints its outline, and `lineWidth` sets the outline's thickness. Each is a separate Canvas 2D drawing-state property.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
ctx.fillStyle = paint;
ctx.strokeStyle = paint;
ctx.lineWidth = width;
// paint: CSS color string, CanvasGradient, or CanvasPattern
// width: finite number greater than 0, in canvas coordinate-space unitsCanvasRenderingContext2D.fillStyle/strokeStyle/lineWidth groups three instance properties on a Canvas 2D context, such as ctx from canvas.getContext("2d"). They belong to a web platform API, not ECMAScript, so not every JavaScript runtime provides them. Assigning a property does not draw anything.
fillStyle supplies the paint for fill(), fillRect(), and fillText(). strokeStyle supplies the paint for stroke(), strokeRect(), and strokeText(). Both start out black and accept a CSS color string, a CanvasGradient, or a CanvasPattern. An invalid color string leaves the previous value unchanged. Reading the property returns a serialized color (not necessarily the original string) or the assigned object. Changes to that object affect subsequent drawing.
lineWidth is the full thickness of the stroke in coordinate-space units, not necessarily screen pixels. The stroke is centered on the path, and scaling the context can change its visible thickness. The initial value is 1. Set it to a finite number greater than zero: 0, negative numbers, Infinity, -Infinity, and NaN are ignored.
These properties apply to subsequent drawing until changed or until the state is restored or reset. save() and restore() save and restore drawing state, not the current path or pixels. Assigning a pattern from a cross-origin image without CORS permission can block pixel readback: getImageData() then throws SecurityError.
Examples
<!doctype html>
<meta charset="utf-8">
<title>Canvas 2D fill and stroke</title>
<script>
(() => {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 32;
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Canvas 2D is unavailable");
ctx.fillStyle = "#ff0000";
ctx.strokeStyle = "#0000ff";
ctx.lineWidth = 6;
ctx.lineWidth = 0;
ctx.fillRect(8, 8, 16, 16);
ctx.strokeRect(8, 8, 16, 16);
const rgba = (x, y) => ctx.getImageData(x, y, 1, 1).data.join(", ");
console.log(`width: ${ctx.lineWidth}`);
console.log(`fill: ${rgba(16, 16)}`);
console.log(`stroke: ${rgba(6, 16)}`);
console.log(`outside: ${rgba(2, 16)}`);
})();
</script>Result
width: 6 fill: 255, 0, 0, 255 stroke: 0, 0, 255, 255 outside: 0, 0, 0, 0
Save the example as an HTML file and open it in a browser. It does not attach the canvas to the page: it logs the width and the RGBA values inside the fill, on the stroke, and outside the drawing to the browser console. Assigning `0` leaves the width at `6`.
Related lessons
- JavaScript · Lesson 12 · Advanced · Version: 4