六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

用canvas做出時鐘完成步驟

[摘要]對于H5來說,canvas是比較有特色的一個地方了,我們可以用canvas畫各種各樣的圖形,做一些動態(tài)小游戲都可以。今天就來教大家用canvas做一個小時鐘。那么首先在這個頁面里面我使用了兩個canvas,一個用來繪制靜態(tài)的時鐘表盤和刻度,另一個用來繪制時鐘的三個指針,然后用定位讓他們重合到一起。...
對于H5來說,canvas是比較有特色的一個地方了,我們可以用canvas畫各種各樣的圖形,做一些動態(tài)小游戲都可以。今天就來教大家用canvas做一個小時鐘。

那么首先在這個頁面里面我使用了兩個canvas,一個用來繪制靜態(tài)的時鐘表盤和刻度,另一個用來繪制時鐘的三個指針,然后用定位讓他們重合到一起。然后這里沒什么好說的,下面附上代碼。

JavaScript Code復制內容到剪貼板

<canvas id="plate">   
        畫表盤   
</canvas>   
<canvas id="needles">   
        畫時針   
var plate=document.getElementById('plate');   
var needles=document.getElementById('needles');   
needles.setAttribute('style','position:absolute;top:8px;left:8px;');  //這里因為chrome里面,body的magin值為8px,所以我這里就沒設為0了。   
var cntP=plate.getContext('2d');   
var cntH=needles.getContext('2d');   
plate.width=800;   
plate.height=500;   
needles.width=800;   
needles.height=500;

到了這里準備工作就做完了,下面就準備繪制時鐘了。我先定義了一個繪制時鐘表盤的構造函數(shù)

JavaScript Code復制內容到剪貼板

function drawclock(cnt,radius,platelen,linewidth,numLen,NUMLEN){   
            this.cnt=cnt;   
            this.radius=radius;   
            this.platelen=platelen;   
            this.linewidth=linewidth;   
            this.numLen=numLen;   
            this.NUMLEN=NUMLEN;   
            this.getCalibCoor=function(i){     
                //獲得表盤刻度兩端的坐標   
                var X=200+this.radius*Math.sin(6*i*Math.PI/180);   
                var Y=200-this.radius*Math.cos(6*i*Math.PI/180);   
                var x=200+(this.radius-this.platelen)*Math.sin(6*i*Math.PI/180);   
                var y=200-(this.radius-this.platelen)*Math.cos(6*i*Math.PI/180);   
  
                // 獲得分鐘數(shù)字的坐標   
                var numx=200+(this.radius-this.platelen-this.numLen)*Math.sin(6*i*Math.PI/180);   
                var numy=200-(this.radius-this.platelen-this.numLen)*Math.cos(6*i*Math.PI/180);   
                //獲得小時數(shù)字的坐標   
                var numX=200+(this.radius-this.platelen-this.NUMLEN)*Math.sin(6*i*Math.PI/180);     
                var numY=200-(this.radius-this.platelen-this.NUMLEN)*Math.cos(6*i*Math.PI/180);   
                return {X:X,Y:Y,x:x,y:y,numx:numx,numy:numy,numX:numX,numY:numY};   
            };   
            this.drawCalibration=function(){ //畫刻度   
                for(var i=0,coorObj;i<60;i++){   
                    coorObj=this.getCalibCoor(i);   
                    this.cnt.beginPath();   
                    this.cnt.moveTo(coorObj.X,coorObj.Y);   
                    this.cnt.lineTo(coorObj.x,coorObj.y);   
                    this.cnt.closePath();   
  
                    this.cnt.lineWidth=this.linewidth;   
                    this.cnt.strokeStyle='#ddd';   
                    i%5==0&&(this.cnt.strokeStyle='#aaa')   
                    &&(this.cnt.lineWidth=this.linewidth*2);   
                    i%15==0&&(this.cnt.strokeStyle='#999')   
                    &&(this.cnt.lineWidth=this.linewidth*3);   
                    this.cnt.stroke();   
  
                    this.cnt.font='10px Arial';   
                    this.cnt.fillStyle='rgba(0,0,0,.2)';   
                    this.cnt.fillText(i,coorObj.numx-7,coorObj.numy+3);   
                    i%5==0&&(this.cnt.fillStyle='rgba(0,0,0,.5)')   
                        &&(this.cnt.font='18px Arial')   
                        &&(this.cnt.fillText(i/5,coorObj.numX-5,coorObj.numY+5));   
                }   
            };   
        }   
      var clock=new drawclock(cntP,200,5,1,10,25); //實例化一個表盤對象   
      clock.drawCalibration();

這里最重要的部分就應該是獲得刻度和數(shù)字繪制的坐標了。我把繪制刻度的起始點放在了表盤的邊緣上,然后從表盤的半徑上減去刻度的長度,就可以得到刻度終點的位置,然后利用角度和三角函數(shù)得到兩個點的坐標。最后就可以繪制出表盤的刻度了。下面繪制出表盤上的數(shù)字也是一樣的方法。我這里吧表盤的中心放在了(200,200)這里位置。到了這里我們就已經(jīng)繪制好了一個靜態(tài)的時鐘表盤。

下面我又定義了一個繪制時鐘指針的構造函數(shù)。

JavaScript Code復制內容到剪貼板

function clockNeedle(cnt,R,lineWidth,strokeStyle,lineCap,obj){   
            this.R=R;   
            this.cnt=cnt;   
            this.lineWidth=lineWidth;   
            this.strokeStyle=strokeStyle;   
            this.lineCap=lineCap;   
            this.obj=obj;   
            this.getNeedleCoor=function(i){   
                var X=200+this.R*0.8*Math.sin(i); //起點的坐標   
                var Y=200-this.R*0.8*Math.cos(i);   
  
                var x=200-20*Math.sin(i); //終點的坐標   
                var y=200+20*Math.cos(i);   
                return {X:X,Y:Y,x:x,y:y};   
            };   
            this.drawNeedle=function(){   
                var d=new Date().getTime();   
                var angle;   
                switch(this.obj){   
                    case 0:   
                    angle=(d/3600000%24+8)/12*360*Math.PI/180;   
                    break;   
                    case 1:   
                    angle=d/60000%60/60*360*Math.PI/180;   
                    break;   
                    case 2:   
                    angle=d/1000%60/60*360*Math.PI/180;   
                    break;   
                }   
                var coorobj=this.getNeedleCoor(angle);   
                this.cnt.beginPath();   
                this.cnt.moveTo(coorobj.x,coorobj.y);   
                this.cnt.lineTo(coorobj.X,coorobj.Y);   
                // this.cnt.closePath();   
  
                this.cnt.lineWidth=this.lineWidth;   
                this.cnt.strokeStyle=this.strokeStyle;   
                this.cnt.lineCap=this.lineCap;   
                this.cnt.stroke();   
            }   
        }

這里有兩個地方需要說一下:1、在我們獲得當前時間的的毫秒數(shù),然后轉換為小時的時候,對24取模計算出當天的小時數(shù)的時候,這里需要加上8。2、如果想要使用lineCap這個屬性嗎,那么上面在設置路徑的時候,不要用closePath()。

到了這里我們還需要一個來繪制指針的方法,并且讓指針看起來能夠轉動:

JavaScript Code復制內容到剪貼板

function draw(){   
            cntH.clearRect(0,0,needles.width,needles.height);   
            var mzneedle=new clockNeedle(cntH,200,1,'rgba(0,0,0,.5)','round',2);   
            //最后一個參數(shù)0代表畫時針,1畫分針,2畫秒針   
            var fzneedle=new clockNeedle(cntH,80,3,'rgba(0,0,0,.4)','round',0);   
            var szneedle=new clockNeedle(cntH,140,2,'rgba(0,0,0,.3)','round',1);   
            mzneedle.drawNeedle();   
            fzneedle.drawNeedle();   
            szneedle.drawNeedle();   
            cntH.arc(200,200,5,0,2*Math.PI);   
            cntH.fillStyle='rgba(0,0,0,.5)';   
            cntH.fill();   
 }   
 setInterval(draw,1);


相信看了這些案例你已經(jīng)掌握了方法,更多精彩請關注php中文網(wǎng)其它相關文章!

相關閱讀:

用Js操作HTTP的Cookie的實現(xiàn)步驟

Js操作BOM對象模型的詳細介紹

關于本地的Web-storage存儲的詳細介紹

以上就是用canvas做出時鐘實現(xiàn)步驟的詳細內容,更多請關注php中文網(wǎng)其它相關文章!


網(wǎng)站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產(chǎn)和維護的網(wǎng)站。




主站蜘蛛池模板: 四虎精品成人免费影视 | 香蕉中文在线 | 欧美在线高清视频 | 天天射天天舔 | 日韩欧美亚州 | 亚洲欧洲一区二区三区在线观看 | 香蕉成人福利片视频在线下载 | 色橹橹欧美在线观看高清视频 | 欧洲毛片| 在线视频欧美亚洲 | 桃花综合久久久久久久久久网 | 青青在线视频免费观看 | 在线天堂中文新版www | 色猫咪av在线网址 | 日韩中文字幕精品免费一区 | 日本最新免费二区三区 | 亚洲精品综合在线 | 四虎影视在永久在线观看 2019 | 综合网色| 一级女性全黄久久生活片免费 | 欧美自拍视频在线 | 一二三四免费观看在线影视大全 | 一区二区三区在线播放视频 | 日韩一级一欧美一级国产 | 天天做夜夜做久久做狠狠 | 亚洲国产精品久久久天堂麻豆 | 亚洲自偷精品视频自拍 | 日韩一区二区精品久久高清 | 欧美一区二区三区精品影视 | 日日夜夜中文字幕 | 欧美午夜视频在线观看 | 中文成人在线视频 | 色六月婷婷 | 四虎精品影院永久在线播放 | 日韩欧美中国a v | 中文字幕字幕乱码熟 | 青草导航 | 欧美一区视频 | 性荡视频在线播放视频 | 性国产精品| 午夜h|