HTML canvas font Property

Introduction

Write a 30px high text on the canvas, using the font "Arial":

View in separate window

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="180" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
</script>/*from www .j  a v  a  2 s.c  om*/

</body>
</html>

The font property sets or gets the current font properties for text content on the canvas.

The font property uses the same syntax as the CSS font property.

Default value: 10px sans-serif

context.font = "italic small-caps bold 12px arial";

Property Values

Values
Description
font-style




font style.
Possible values:
normal
italic
oblique
font-variant



font variant.
Possible values:
normal
small-caps
font-weight














font weight.
Possible values:
normal
bold
bolder
lighter
100
200
300
400
500
600
700
800
900
font-size/line-height
font size and the line-height, in pixels
font-family
font family
caption
Use the font captioned controls (like buttons, drop-downs, etc.)
icon
Use the font used to label icons
menu
Use the font used in menus (drop-down menus and menu lists)
message-box
Use the font used in dialog boxes
small-caption
Use the font used for labeling small controls
status-bar
Use the fonts used in window status bar



PreviousNext

Related