Download - All IT eBooks
Transcript
10
1. Introduction to HTML5
context.rotate(Math.PI/4);
I am now rotating the canvas 45 degrees; rotate() uses radians to measure
angles. Math.PI is the JavaScript constant for PI.
context.fillText("Hello World!", canvas.width/4, 0);
I am now writing the text to x and y coordinates. I use width of the
canvas divided by 4 to make it more in the middle of the canvas block.
context.restore();
I am now restoring the canvas from its rotation. This leaves the “Hello
World!” at an angle. Note that to the viewer, it appears I operated solely
on the text. What I actually did was rotate the entire canvas, write the
text normally, and then restore the orientation of the canvas. Rotatewrite-restore is a very common technique in graphics programming.
<body onload="onload()">
This tells the browser to execute the custom onload function after the
page has finished loading. This is important because I want the canvas
available before I begin using it.
<canvas id="canvas" width="320" height="480">Sorry!</canvas>
This sets up my HTML5 canvas block. A browser that didn’t support
canvas would display the inner text or possibly leave it blank.
Like many examples of “Hello World!”, we could take a different path. If all
wanted was rotated text, we could turn the canvas into
<div id="mytext">Hello World!</div> and just use the CSS3 transform property.
No JavaScript needed. See Listing 1.3 for the example code and Figure 1.3 for
the output.
we
<! doctype html >
< html lang = " en " >
< head >
< meta charset = " UTF -8 " >
< title > HTML5 Hello World CSS Angled </ title >
</ head >
< style >
# mytext
{
font - weight : bold ;
font - size :30 px ;
font - family : " Times " , Georgia , Serif ;
transform : rotate (45 deg ); /* Non - prefixed future versions */
-ms - transform : rotate (45 deg ); /* IE */
-moz - transform : rotate (45 deg ); /* Firefox */
- webkit - transform : rotate (45 deg ); /* Safari and Chrome */
-o - transform : rotate (45 deg ); /* Opera */
position : absolute ;
top : 100 px ;