10.07.2015 Views

Building the User Interface by Using HTML5: Text, Graphics ... - server

Building the User Interface by Using HTML5: Text, Graphics ... - server

Building the User Interface by Using HTML5: Text, Graphics ... - server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

40 | Lesson 2X REFYou’ll learn how to useJavaScript in Lessons 8,9, and 10.Figure 2-14The Web page with a canvasshapeThe context.fillStyle method fills <strong>the</strong> rectangle with a blue color using <strong>the</strong> RGBvalues 0, 0, 255. The context.fillRect method creates a 200-pixel wide x 100-pixeltall rectangle, positioned 10 pixels down and 20 pixels over from <strong>the</strong> upper-left cornerof <strong>the</strong> canvas and fills it using <strong>the</strong> color specified <strong>by</strong> fillStyle.2. Save <strong>the</strong> file as L2-canvas.html and view it in a browser. The shape should appear asshown in Figure 2-14.LICENSED PRODUCT NOT FOR RESALE3. If a blue rectangle doesn’t appear, go to <strong>the</strong> W3C Markup Validation Service Webpage at http://validator.w3.org. Upload L2-canvas.html and click Check to have<strong>the</strong> service check it. Fix any errors reported <strong>by</strong> <strong>the</strong> checker. Save <strong>the</strong> file again andview it in a browser.4. Leave <strong>the</strong> file, editing tool, and Web browser open if you’re continuing immediatelyto <strong>the</strong> next exercise.CREATING AN OUTLINE OF A SHAPETo create an outline of a rectangle without a fill color, use <strong>the</strong> context.strokeRectmethod. It uses <strong>the</strong> same values as context.fillRect. To modify <strong>the</strong> color of <strong>the</strong> outline(<strong>the</strong> stroke color), use context.strokeStyle. For example, to create a 200 x 100 pixelrectangular outline in red, use <strong>the</strong>se methods in your JavaScript:context.strokeStyle = "red";context.strokeRect(10,20,200,100);USE CANVAS TO CREATE THE OUTLINE OF A SHAPEGET READY. To use <strong>the</strong> canvas element to create <strong>the</strong> outline of a shape, perform <strong>the</strong>following steps:1. In your editing tool, save L2-canvas.html as L2-canvas-stroke.html.2. Replace <strong>the</strong> fillStyle and fillRect code lines with <strong>the</strong> following:context.strokeStyle = "red";context.strokeRect(10,20,200,100);3. Delete <strong>the</strong> width and height attributes from <strong>the</strong> canvas element in <strong>the</strong> body(after id="smlRectangle").4. Save <strong>the</strong> file and view it in a Web browser. The shape should appear as shown inFigure 2-15.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!