We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

The CanvasRenderingContext2D method fillText(), part of the Canvas 2D API, draws a text string at the specified coordinates, filling the string's characters with the current foreground color. An optional parameter allows specifying a maximum width for the rendered text, which the user agent will achieve by condensing the text or by using a lower font size.

The text is rendered using the font and text layout configuration as defined by the font, textAlign, textBaseline, and direction properties.

To draw the outlines of the characters in a string, call the context's strokeText() method.


CanvasRenderingContext2D.fillText(text, x, y [, maxWidth]);


A DOMString specifying the text string to render into the context. The text is rendered using the settings specified by font, textAlign, textBaseline, and direction.
The x -coordinate of the point at which to begin drawing the text, in pixels.
The y-coordinate of the point at which to begin drawing the text, in pixels.
maxWidth Optional
The maximum number of pixels wide the string may be once rendered. If not specified, there is no limit to the width of the string. However, if this value is provided, the user agent will adjust the kerning, select a more horizontally condensed font (if one is available or can be generated without loss of quality), or scale down to a smaller font size in order to fit the text in the specified width.

Return value



This is just a simple code snippet which uses the fillText() method.


First, we need a canvas to draw into. This code creates a context 600 pixels wide and 250 pixels across.

<canvas id="canvas" width="600" height="250"></canvas>


The JavaScript code for this example follows.

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.font = '48px serif';
ctx.fillText('Hello world', 50, 100);

This code obtains a reference to the <canvas>, then gets a reference to its 2D graphics context.

With that in hand, we set the font to 48-pixel-tall "serif" (the user's default serif font), then call fillText() to draw the text "Hello world", starting at the coordinates (50, 100).


Edit the code below and see your changes update live in the canvas:

Playable code
<canvas id="canvas" width=600 height=250 class="playable-canvas"></canvas>
<div class="playable-buttons">
  <input id="edit" type="button" value="Edit" />
  <input id="reset" type="button" value="Reset" />
<textarea id="code" class="playable-code">
ctx.font = "48px serif";
ctx.fillText("Hello world", 50, 100);</textarea>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

reset.addEventListener("click", function() {
  textarea.value = code;

edit.addEventListener("click", function() {

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.fillText' in that specification.
Living Standard  

Browser compatibility


FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes123.59 Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes Yes


See also

Document Tags and Contributors

Contributors to this page: JamesCoyle, fscholz, Sheppy, thatmichael85, nmve, erikadoyle
Last updated by: JamesCoyle,