strokeText(text, x, y[, maxWidth]) draws the outline of text using the current strokeStyle, lineWidth, font, alignment, and baseline settings.
Like fillText(), it draws directly to the Canvas and does not add glyphs to the current path.
const canvas = document.getElementById('stroke_text_canvas');
const ctx = canvas.getContext('2d');
ctx.font = '46px serif';
ctx.strokeStyle = '#c2185b';
ctx.lineWidth = 2;
ctx.strokeText('plus2net.com', 30, 85);
ctx.font = 'bold 48px sans-serif';
ctx.fillStyle = '#fff4c2';
ctx.strokeStyle = '#6a4b00';
ctx.lineWidth = 3;
ctx.fillText('Canvas', 30, 80);
ctx.strokeText('Canvas', 30, 80);
An optional fourth argument limits the requested rendered width. The browser may adjust glyph spacing or choose a more condensed/scaled rendering to fit.
ctx.strokeText('A long heading', 20, 70, 220);
strokeStyle controls the outline paint and lineWidth controls its thickness. Very large line widths can make smaller text difficult to read.
Outlined Canvas text remains bitmap content. Keep meaningful text available as semantic HTML and use Canvas text mainly when raster drawing is actually required.
It uses the current strokeStyle.
Yes. lineWidth controls the thickness of the glyph outlines.
Yes. strokeStyle can be a CanvasGradient or CanvasPattern as well as a CSS color.
No. It draws directly without adding the text outlines to the current path.
Yes. Draw the same text at the same coordinates to create filled text with an outline.
fillText() | strokeStyle | lineWidth | font | textBaseline | Canvas tutorial
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.