HTML5 Drawing flag with canvas

Source: Internet
Author: User

At the HTML5 list, we mention that HTML 5 is crowned with a lot of hats, the highest of which is the controversial "flash killer". It critics like to use the word, the killer everywhere. Whether it's a killer or not, some of the new features introduced in HTML 5 are really exciting. One of the most anticipated is the canvas element.

As part of the HTML5 standard, the canvas element allows the script to render bitmap images dynamically. This HTML element is designed for client-side vector graphics. It does not act on its own, but presents a drawing API to client JavaScript so that the script can draw everything it wants to draw onto a canvas. The HTML 5 canvas provides a way to draw graphics through JavaScript, which is simple but powerful. Each canvas element has a "context" (pictured as a page on the drawing board) in which any graphic can be drawn. Xingcheng Fayon Gift

Most browsers support the 2D canvas context-including Opera, Firefox, Konqueror and Safari. And some versions of Opera also support 3D canvas, Firefox can also be used in the form of plug-in support 3D canvas. We can even use the <canvas> tag in IE and build a compatible canvas with open source JavaScript code (initiated by Google) on the basis of the VML support of IE. See also: http://excanvas.sourceforge.net/.

Effect Demo

<ptml> <pead> <title>canvas tutorial</title> <script type= "Text/javascript" > Fun Ction Draw () {var ctx = document.getElementById (' canvas '). GetContext (' 2d '); Draw the red canvas Ctx.fillstyle = ' #f00 '; Ctx.fillrect (0,0,450,300); Ctx.translate (75,75); Draw large pentagram ctx.save (); Ctx.rotate (MATH.PI*2/7); Drawstar (ctx,40); Ctx.restore (); Draw four small pentagram ctx.save (); Ctx.translate (80,-50); Drawstar (ctx,10); Ctx.restore (); Ctx.save (); Ctx.translate (110,-10); Drawstar (ctx,10); Ctx.restore (); Ctx.save (); Ctx.translate (110,30); Drawstar (ctx,10); Ctx.restore (); Ctx.save (); Ctx.translate (80,70); Drawstar (ctx,10); Ctx.restore (); }//plotting the Pentagram function Drawstar (ctx,r) {ctx.save (); Ctx.beginpath () Ctx.moveto (r,0); for (Var i=0;i<9;i++) {ctx.rotate (MATH.PI/5); if (i%2 = = 0) {Ctx.lineto ((r/0.525731) *0.200811,0); } else {Ctx.lineto (r,0); }} ctx.closepath (); ctx.fillstyle= ' #FFFF00 '; Ctx.fill (); Ctx.restore ();} </script> </pead> <body onload= "Draw ();" > <canvas id= "Canvas" width= "height=" ></canvas> </body></ptml>

The HTML5 code is as follows:

View Source print?
01 <html>
02 <head>
03 <title>Canvas tutorial</title>
04 <scripttype="text/javascript">
05       function draw() {
06           var ctx = document.getElementById(‘canvas‘).getContext(‘2d‘);
07           //绘制红色的画布
08           ctx.fillStyle = ‘#f00‘;
09           ctx.fillRect(0,0,450,300);
10           ctx.translate(75,75);
11           //绘制大五角星
12           ctx.save();
13           ctx.rotate(Math.PI*2/7);
14           drawStar(ctx,40);  
15           ctx.restore();
16           //绘制四个小五角星
17           ctx.save();
18           ctx.translate(80,-50);
19           drawStar(ctx,10);  
20           ctx.restore();
21           ctx.save();
22           ctx.translate(110,-10);
23           drawStar(ctx,10);   
24           ctx.restore();
25           ctx.save();
26           ctx.translate(110,30);
27           drawStar(ctx,10);   
28           ctx.restore();
29           ctx.save();
30           ctx.translate(80,70);
31           drawStar(ctx,10);   
32           ctx.restore();
33         }
34         //绘制五角星函数
35     function drawStar(ctx,r){
36           ctx.save();
37           ctx.beginPath()
38           ctx.moveTo(r,0);
39           for (var i=0;i<9;i++){
40             ctx.rotate(Math.PI/5);
41             if(i%2 == 0) {
42               ctx.lineTo((r/0.525731)*0.200811,0);
43             } else {
44               ctx.lineTo(r,0);
45             }
46           }
47           ctx.closePath();
48           ctx.fillStyle=‘#FFFF00‘;
49           ctx.fill();
50           ctx.restore();
51     }
52     </script>
53 </head>
54 <bodyonload="draw();">
55 <canvasid="canvas" width="800"height="300"></canvas>
56 </body>
57 </html>

HTML5 Drawing flag with canvas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.