Drawing Text

Rendering text in regular HTML is easy, rendering text in an HTML5 Canvas is a little more involved but the effects that can be applied to text is greater than that of plain HTML and CSS.  Here we show how to render some basic text using a variety of fonts, colors and font weights.

<body onload="drawCanvas();">
    <div>
        <canvas id="myCanvas" width="500" height="150">
            <p>Your browser doesn't support canvas.</p>
        </canvas>
    </div>
</body>

Firstly we set the font property, this property can contain the font name and size as well as the font weight, here we set it to Arial, 30px in size.  The fillStyle controls how the text is painted onto the canvas, here we set it to a solid Red.

Finally we call the fillText method that does that actual text rendering, this method takes the text string to draw and the x and y position from the top-left of the canvas.

Canvas Text

function drawCanvas() {
    // Get our Canvas element
    var surface = document.getElementById("myCanvas");

    if (surface.getContext) {
        // If Canvas is supported
        var context = surface.getContext('2d');

        // Set the baseline
        context.textBaseline = "top";
        
        // Draw some text in Arial
        context.font = "30px Arial";
        context.fillStyle = "#ff0000";
        context.fillText("30px Arial Text", 10, 10);
    }
}

Gradient Fills

So far we could have displayed the above text using standard HTML and CSS, however when using the Canvas to draw text you have a whole lot more possibilities, one of these is the ability to apply gradients to text:

// Now some in Times New Roman Blue
context.font = "25px Times New Roman";
context.fillStyle = "#0000ff";
context.fillText("25px Times New Roman Text", 10, 40);

// And some in Tahoma Bold
context.font = "bold 15px Tahoma";
context.fontweight
context.fillStyle = "#000000";
context.fillText("15px Tahoma Bold Text", 10, 70);

Canvas Text

Here we create a vertical gradient that flows from dark Red to bright Red.  For more details on using gradients and fill styles please see the Shapes and Fill Styles tutorial.

Measuring Text

A common requirement when dealing with text is to be able to measure how many pixels a block of text occupies.  For this we can use the measureText() method that can be found on the graphics context:

context.font = "30px Arial";

var gradient = context.createLinearGradient(0, 0, 0, 40);
// Add 2 steps to it
gradient.addColorStop(0, "rgb(100,0,0)");
gradient.addColorStop(1, "rgb(255,0,0)");
// Assign our gradient to the fillStyle
context.fillStyle = gradient;
context.fillText("30px Arial Text with Red Gradient", 10, 10);

This method returns a size object which contains the text dimensions.

Post your Comments

 
 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 2,770

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 2,625

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 2,478

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 2,692

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 2,156

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 2,479

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 2,304

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 2,480

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 3,122

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 2,195

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average