站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        canvas的實例–時鐘動畫

        平時在公司不忙的時候,就喜歡寫一些小效果什么的,一來復(fù)習(xí)復(fù)習(xí),二來可以發(fā)現(xiàn)一些問題。

        今天在群里看別人發(fā)了一手表的圖片,臥槽…妥妥的工作好多年的節(jié)奏,后來想想還是做好自己的事情算了,想那多干啥,就畫了一個手表….

        直接上代碼:

        html

        <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>canvas clock</title><script type="text/javascript" src="percent.js?1.1.10"></script></head><body><canvas id="canvas" width="600" height="600"></canvas></body></html><script type="text/javascript">clock.canvasClock();</script>

        js

        var clock=(function(){function _canvasClock(){var cvs=document.getElementById('canvas');var ctx=cvs.getContext('2d');var PI=Math.PI;var lineWidth=5;                                             //線寬var gradient=ctx.createLinearGradient(10,10,580,580);        //設(shè)置圓的顏色漸變gradient.addColorStop("0","#a251ff");          gradient.addColorStop(1,"#2ec2ff");          ctx.fillStyle = '#ef6670';          ctx.fillRect(0,0,600,600);var drawBig=function(){var time=new Date();var second=time.getSeconds();                            //秒var Minute=time.getMinutes();                            //分var hour=time.getHours();                                //時//hour=hour+Minute/60;hour=hour>12?hour-12:hour;                               //表盤只有12小時                          ctx.clearRect(0,0,600,600);                              //清空給定矩形內(nèi)的指定像素//畫圓            ctx.beginPath();              ctx.lineWidth=lineWidth;              ctx.strokeStyle=gradient;              ctx.arc(300,300,290,0, PI * 2,false);              ctx.stroke();              ctx.closePath();                            ctx.beginPath();              ctx.lineWidth=lineWidth;              ctx.strokeStyle=gradient;              ctx.arc(300,300,10,0, PI * 2,false);              ctx.stroke();              ctx.closePath();            for(var i=0;i<60;i++){                    ctx.save();                         //保存之前畫布狀態(tài)   ctx.lineWidth=4;                   //設(shè)置分針的粗細                 ctx.strokeStyle="#616161";          //設(shè)置分針的顏色                 ctx.translate(300,300);             //畫布圓點設(shè)置        ctx.rotate(i*PI/30);                //角度*Math.PI/180=弧度,設(shè)置旋轉(zhuǎn)角度                   ctx.beginPath();                    //開始一條路徑ctx.moveTo(0,-287);                 //相對畫布圓點路徑的起點ctx.lineTo(0,-283);                 //相對畫布圓點路徑的終點ctx.closePath();                    //結(jié)束一條路徑ctx.stroke();                       //實際地繪制出通過 moveTo()和 lineTo()方法定義的路徑ctx.restore();                      //restore() 方法將繪圖狀態(tài)置為保存值            }            for(var i=0;i<12;i++){                  ctx.save();                  ctx.lineWidth=4;                  ctx.strokeStyle=gradient;                      ctx.translate(300,300);                     ctx.rotate(i*PI/6);                ctx.beginPath();                    ctx.moveTo(0,-287);                  ctx.lineTo(0,-278);                   ctx.closePath();                    ctx.stroke();                    ctx.restore();                }            //時針              ctx.save();                         ctx.lineWidth=3;                                ctx.strokeStyle="#0f0f0f";                              ctx.translate(300,300);              ctx.rotate(hour*PI/6+second*PI/108000);                ctx.beginPath();                ctx.moveTo(0,-238);              ctx.lineTo(0,10);                ctx.closePath();                ctx.stroke();                ctx.restore();                //分針              ctx.save();                ctx.lineWidth=3;                ctx.strokeStyle="#010101";                ctx.translate(300,300);                ctx.rotate(Minute*PI/30+second*PI/1800);                ctx.beginPath();                ctx.moveTo(0,-258);                ctx.lineTo(0,15);                ctx.closePath();                ctx.stroke();              ctx.restore();                              //秒針                          ctx.save();                ctx.strokeStyle="#ff100d";                ctx.lineWidth=3;                ctx.translate(300,300);                             ctx.rotate(second*PI/30);                             ctx.beginPath();              ctx.moveTo(0,-278);                ctx.lineTo(0,20);                ctx.closePath();                ctx.stroke();                              ctx.beginPath();                        //時針分針秒針交點  ctx.arc(0,0,6,0,2*PI,false);              ctx.closePath();                                ctx.fillStyle="#fff";              ctx.fill();                             ctx.stroke();                   ctx.restore();                requestAnimationFrame(drawBig);            //實現(xiàn)動畫修改的接口        };          drawBig();          setInterval(drawBig,1000);                    //每1s重繪一次    };return{          canvasClock:_canvasClock,      }  }())

        本來準備封裝一下的,要下班時來任務(wù)了,飛了….

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 国产精品视频第一区二区三区| 久久成人精品| 久久久久人妻精品一区三寸蜜桃| 国产a精品视频| 亚洲av午夜福利精品一区人妖 | 日韩精品无码中文字幕一区二区| 黑人巨大精品欧美一区二区| 久久国产精品成人免费| 精品国产乱码久久久久久1区2区| 亚洲无线观看国产精品| 人妻少妇看A偷人无码精品| 国产伦精品一区二区三区视频金莲| 亚洲国产精品不卡在线电影| 97久久精品国产精品青草| 久久国产精品无码一区二区三区 | 88久久精品无码一区二区毛片| 国产精品特级毛片一区二区三区| 久热这里只有精品12| 无码人妻精品一区二区| 一区二区三区精品高清视频免费在线播放 | 久久精品中文无码资源站 | 亚洲国产精品人人做人人爽 | 少妇人妻精品一区二区三区| 伊人久久精品影院| 亚洲欧美国产∧v精品综合网| 毛片a精品**国产| 久久精品国产精品亚洲人人| 国产精品亚洲αv天堂无码| 国产A∨国片精品一区二区| 99久久免费国产精品| 99精品国产一区二区| 国产成人精品a视频一区| 国产精品v欧美精品v日本精| 人精品影院| 精品无码一级毛片免费视频观看 | 日本精品一区二区三区在线视频一 | 国产香蕉国产精品偷在线观看| 国産精品久久久久久久| 久久精品视频91| 亚洲精品一级无码鲁丝片| 中国精品18videosex性中国|