<! DOCTYPEhtml> <Htmlxmlns = "http://www.w3.org/1999/xhtml"> <Head> <Metahttp-equiv = "Content-type" content = "text/html; charset = UTF-8"> <Title> HTML5 </title> <Scripttype = "text/javascript" charset = "UTF-8"> // This function will be called after the page is fully loaded Function pageLoaded (){ // Obtain the reference of the canvas object. Note that the tCanvas name must be the same as the id in the following body. Var canvas = document. getElementById ('tcanvas '); // Obtain the 2D drawing environment of the canvas Var context = canvas. getContext ('2d '); // The draw code will appear here // Draw complex pig // Fill triangle Context. beginPath (); Context. moveTo (10,120); // starts from (10, 20) Context. lineTo (10,180); // indicates that the image starts from (10,120) and ends with (10,180 ). Context. lineTo (110,150); // indicates that the image starts from (10,180) and ends with (110,150 ). Context. fill (); // close the shape and draw it in filling mode // The outer border of the triangle Context. beginPath (); Context. moveTo (140,160); // starts from the vertex (140,160) Context. lineTo (140,220 ); Context. lineTo (40,190 ); Context. closePath (); // close the path Context. stroke (); // fill in with hollow // A complex Polygon Context. beginPath (); Context. moveTo (160,160); // starts from the vertex (160,160) Context. lineTo (170,220 ); Context. lineTo (240,210 ); Context. lineTo (260,170 ); Context. lineTo (190,140 ); Context. closePath (); Context. stroke (); // Draw an arc // Draw a semi-Arc Context. beginPath (); // Draw an arc with a radius of 40 and an angle from 0 to 100,300 degrees counterclockwise at (180 ). Context. arc (100,300, 40, 0 * Math. PI, 1 * Math. PI, true); // The radian of PI is 180 °. Context. stroke (); // Draw a solid circle Context. beginPath (); // Draw an arc with a radius of 30 and an angle of 0 to 100,300 degrees counterclockwise at (360 ). Context. arc (100,300, 30, 0 * Math. PI, 2 * Math. PI, true); // 2 * Math. PI is 360 ° Context. fill (); // Draw a 3/4 arc Context. beginPath (); // Draw an arc with a radius of 25 clockwise and an angle of 0 to 200,300 ° at (270 ). Context. arc (200,300, 25, 0 * Math. PI, 3/2 * Math. PI, false ); Context. stroke (); } </Script> </Head> <Bodyonload = "pageLoaded ();"> <Canvaswidth = "400" height = "400" id = "tCanvas" style = "border: black1pxsolid;"> <! -- The following font is displayed if the browser does not support it --> Tip: your browser does not support Tag </Canvas> </Body> </Html> |