Set the text alignment

Description

Set the text alignment

Example


<!DOCTYPE HTML>
<html>
    <head>
        <script>
            window.onload = function(){<!--from w  w w  .j a  v  a2s.c om-->
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                
                context.font = "40pt Calibri";
                context.fillStyle = "black";
                // align text horizontally center
                context.textAlign = "center";
                // align text vertically center
                context.textBaseline = "middle";
                context.fillText("Hello World!", canvas.width / 2, 120);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window