How to Draw a Text String using JavaScript fillText() Method (original) (raw)

Summary: in this tutorial, you’ll learn how to use the JavaScript fillText() method to draw a text string to a canvas.

Introduction to the JavaScript fillText() method

The fillText() is a method of a 2D drawing context. The fillText() method allows you to draw a text string at a coordinate with the fill derived from the current fillStyle.

The following shows the syntax of the fillText() method:

ctx.fillText(text, y , y [, maxWidth])Code language: CSS (css)

The fillText() accepts the following parameters:

JavaScript fillText() example

Let’s take some examples of using the JavaScript fillText() method.

1) Draw a filled text example

This example draws the words "Hello, Canvas!" on a canvas using the fillText() method.

HTML

`

JavaScript fillText Demo
<canvas id="canvas" height="400" width="500">
</canvas>

<script src="js/app.js"></script>
`Code language: HTML, XML (xml)

JavaScript

The following shows the JavaScript code that draws the text:

const canvas = document.getElementById('canvas'); if (canvas.getContext) { const ctx = canvas.getContext('2d'); ctx.fillStyle = 'green'; ctx.font = '60px san-serif'; ctx.fillText('Hello, Canvas!', 100, 200); }Code language: JavaScript (javascript)

Output:

How it works.

2) Constraint the text size

The following example draws the words 'Hello, Canvas!' with a maximum with of 250px.

HTML

<canvas id="canvas" height="400" width="500"> </canvas> Code language: HTML, XML (xml)

JavaScript

`const canvas = document.getElementById('canvas'); if (canvas.getContext) { const ctx = canvas.getContext('2d'); ctx.fillStyle = 'green';

ctx.font = '60px san-serif';
ctx.fillText('Hello, Canvas!', 100, 200, 250);

}`Code language: JavaScript (javascript)

Output:

Text alignments

To align the text on the canvas, you use the textAlign property:

ctx.textAlign = value;

The alignment is relative to the x of the fillText() method.

The value can be one of the following values:

The default value for the texAlign is start.

The following example demonstrates the various options of the textAlign property:

HTML

`

`Code language: HTML, XML (xml)

JavaScript

`const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const x = canvas.width / 2;

ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke();

ctx.font = '25px san-serif';

ctx.textAlign = 'left'; ctx.fillText('left-aligned', x, 40);

ctx.textAlign = 'center'; ctx.fillText('center-aligned', x, 85);

ctx.textAlign = 'right'; ctx.fillText('right-aligned', x, 130);

// LTR locale canvas.setAttribute('dir', 'ltr'); ctx.textAlign = 'start'; ctx.fillText('start', x, 175); ctx.textAlign = 'end'; ctx.fillText('end', x, 220);

// RTL locale canvas.setAttribute('dir', 'rtl'); ctx.textAlign = 'start'; ctx.fillText('start', x, 265); ctx.textAlign = 'end'; ctx.fillText('end', x, 305);`Code language: JavaScript (javascript)

To change the locale to LTR or RTL, you set the dir attribute value of the canvas to 'ltr' and 'rtl'.

Here is the output:

Text baseline

To specify the text baseline for the drawing text, you use the textBaseline property of the 2D drawing context:

ctx.textBaseline = value;

The value of the textBaseline can be one of the following values:

Text baseline example

The following example illustrates various textBaseline values.

HTML

<canvas id="canvas" width="550" height="500"></canvas> Code language: HTML, XML (xml)

JavaScript

`` const canvas = document.getElementById('canvas');

if (canvas.getContext) { const ctx = canvas.getContext('2d'); const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']; const str = 'The quick brown fox jumps over the lazy dog'; ctx.font = '20px san-serif'; ctx.strokeStyle = 'red';

baselines.forEach((baseline, index) => {
    // set the text baseline
    ctx.textBaseline = baseline;

    const y = 75 + index * 75;
    // draw a line
    ctx.beginPath();
    ctx.moveTo(0, y + 0.5);
    ctx.lineTo(500, y + 0.5);
    ctx.stroke();

    // draw the text
    ctx.fillText(`${str}(${baseline})`, 0, y);
});

} ``Code language: JavaScript (javascript)

Output:

Summary

Was this tutorial helpful ?