這個例子是html5 Canvas模擬網頁loading加載實例。效果圖如下:
該實例代碼:(預覽地址:http://html5demos.com/canvas)
- <!DOCTYPEhtml>
- <htmllang=enxmlns=http://www.w3.org/1999/xhtml>
- <head>
- <metahttp-equiv=Content-Typecontent=text/html;charset=utf-8/>
- <title>HTML5Demo:canvas</title>
- <style>
- body{
- font:normal16px/20pxHelvetica,sans-serif;
- background:rgb(237,237,236);
- margin:0;
- margin-top:40px;
- padding:0;
- }
- article,section,header,footer{
- display:block;
- }
- #wrapper{
- width:600px;
- margin:0auto;
- background:#fffurl(images/shade.jpg)repeat-xcenterbottom;
- -moz-border-radius:10px;
- -webkit-border-radius:10px;
- border-top:1pxsolid#fff;
- padding-bottom:76px;
- }
- h1{
- padding-top:10px;
- }
- h2{
- font-size:100%;
- font-style:italic;
- }
- header,
- article>*,
- footera,
- footerp{
- margin:20px;
- }
- footer>*{
- margin:20px;
- color:#999;
- }
- article{
- position:relative;
- }
- </style>
- <script>
- //Fordiscussionandcomments,see:http://remysharp.com/2009/01/07/html5-enabling-script/
- (function(){if(!/*@cc_on!@*/0)return;vare=abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,mark,menu,meter,nav,output,progress,section,time,video.split(','),i=e.length;while(i--){document.createElement(e[i])}})()
- </script>
- </head>
- <body>
- <sectionid=wrapper>
- <header>
- <h1>Canvas</h1>
- </header>
- <article></article>
- <footer><ahref=/>HTML5demo</a></footer>
- </section>
- <scriptsrc=h5utils.js></script>
- <script>
- buildSpinner({x:50,y:50,size:20,degrees:30});
- functionbuildSpinner(data){
- varcanvas=document.createElement('canvas');
- canvas.height=100;
- canvas.width=300;
- document.getElementsByTagName('article')[0].appendChild(canvas);
- varctx=canvas.getContext(2d),
- i=0,degrees=data.degrees,loops=0,degreesList=[];
- for(i=0;i<degrees;i++){
- degreesList.push(i);
- }
- //reset
- i=0;
- //soIcankillitlater
- window.canvasTimer=setInterval(draw,1000/degrees);
- functionreset(){
- ctx.clearRect(0,0,100,100);//clearcanvas
- varleft=degreesList.slice(0,1);
- varright=degreesList.slice(1,degreesList.length);
- degreesList=right.concat(left);
- }
- functiondraw(){
- varc,s,e;
- vard=0;
- if(i==0){
- reset();
- }
- ctx.save();
- d=degreesList[i];
- c=Math.floor(255/degrees*i);
- ctx.strokeStyle='rgb('+c+','+c+','+c+')';
- ctx.lineWidth=data.size;
- ctx.beginPath();
- s=Math.floor(360/degrees*(d));
- e=Math.floor(360/degrees*(d+1))-1;
- ctx.arc(data.x,data.y,data.size,(Math.PI/180)*s,(Math.PI/180)*e,false);
- ctx.stroke();
- ctx.restore();
- i++;
- if(i>=degrees){
- i=0;
- }
- }
- }
- </script>
- <script>
- vargaJsHost=((https:==document.location.protocol)?https://ssl.:http://www.);
- document.write(unescape(%3Cscriptsrc='+gaJsHost+google-analytics.com/ga.js'type='text/javascript'%3E%3C/script%3E));
- </script>
- <script>
- try{
- varpageTracker=_gat._getTracker(UA-1656750-18);
- pageTracker._trackPageview();
- }catch(err){}</script>
- </body>
- </html>