javascript canvas實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘例子
本文實(shí)例為大家分享了javascript canvas實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>鐘表</title>
<script type="text/javascript">
window.onload=function(){
//1.獲取畫(huà)布
var canvas=document.getElementById("canvas");
//2.獲取上下文對(duì)象(獲取畫(huà)筆)
var cx=canvas.getContext("2d");
//3.設(shè)置畫(huà)筆樣式
function clock(){
cx.fillStyle="tomato";
//4.繪制圖形
//繪制表盤(pán)
cx.beginPath(); //繪制圓形
cx.arc(300,300,200,0,Math.PI*2);//設(shè)置圓 arc(x,y,r,begin,end,c);x,y:圓的圓的圓心坐標(biāo) r:圓的半徑 begin,end:開(kāi)始角度和結(jié)束角度;Math.PI=180 Math.PI/180=1度 c:是否按照逆時(shí)針進(jìn)行繪制true:按照逆時(shí) false:順時(shí)針
cx.closePath();//關(guān)閉路徑
cx.fill();//
//繪制時(shí)刻度
cx.lineWidth=2;
cx.strokeStyle="black";
for(var i=0;i<12;i++){
cx.save();
cx.translate(300,300);//形移
cx.rotate(i*(Math.PI/6));
cx.beginPath();
cx.moveTo(0,-180);
cx.lineTo(0,-200);
cx.stroke();
cx.closePath();
cx.fillStyle="black";//繪制數(shù)字
cx.font="16px blod";
cx.rotate(Math.PI/6);
cx.fillText(i+1,-6,-220);//文字
cx.restore();
}
//繪制分刻度
for(var i=0;i<60;i++){
cx.save();
cx.translate(300,300);
cx.rotate(i*(Math.PI/30));
cx.beginPath();
cx.moveTo(0,-190);
cx.lineTo(0,-200);
cx.stroke();
cx.closePath();
cx.restore();
}
//獲取當(dāng)前時(shí)間
var today=new Date();
var hour=today.getHours();
var min=today.getMinutes();
var sec=today.getSeconds();
hour=hour+min/60;
//繪制時(shí)針
cx.lineWidth=4;
cx.save();
cx.translate(300,300);
cx.beginPath();
cx.rotate(hour*(Math.PI/6));//旋轉(zhuǎn)
cx.moveTo(0,10);
cx.lineTo(0,-130);
cx.closePath();
cx.stroke();
cx.restore(); //回滾
//繪制分針
cx.lineWidth=2;
cx.save();
cx.translate(300,300);
cx.beginPath();
cx.rotate(min*(Math.PI/30));
cx.moveTo(0,10);
cx.lineTo(0,-160);
cx.closePath();
cx.stroke();
cx.restore(); //回滾
//繪制秒針
cx.lineWidth=1;
cx.strokeStyle="black";
cx.save();
cx.translate(300,300);
cx.beginPath();
cx.rotate(sec*(Math.PI/30));
cx.moveTo(0,10);
cx.lineTo(0,-160);
cx.closePath();
cx.stroke();
cx.restore();
//繪制交叉處
cx.fillStyle='#ccc';
cx.strokeStyle="red";
cx.save();
cx.translate(300,300);
cx.beginPath();
cx.beginPath();
cx.arc(0,0,4,0,Math.PI*2);
cx.closePath();
cx.fill();
cx.closePath();
cx.stroke();
cx.restore();
setTimeout(clock,1000);
}
// setInterval(clock,1000);
clock();
}
</script>
</head>
<body>
<canvas id="canvas" width="1300px" height="600px" style="background-color: #ccc;"></canvas>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js canvas實(shí)現(xiàn)寫(xiě)字動(dòng)畫(huà)效果
- JS+canvas繪制的動(dòng)態(tài)機(jī)械表動(dòng)畫(huà)效果
- JS基于HTML5的canvas標(biāo)簽實(shí)現(xiàn)炫目的色相球動(dòng)畫(huà)效果實(shí)例
- javascript+HTML5的Canvas實(shí)現(xiàn)Lab單車(chē)動(dòng)畫(huà)效果
- js+canvas實(shí)現(xiàn)畫(huà)板功能
- js+canvas實(shí)現(xiàn)刮刮獎(jiǎng)功能
- js實(shí)現(xiàn)貪吃蛇游戲 canvas繪制地圖
- 使用js和canvas實(shí)現(xiàn)時(shí)鐘效果
- javascript使用canvas實(shí)現(xiàn)餅狀圖效果
- JS Canvas接口和動(dòng)畫(huà)效果大全
相關(guān)文章
JS簡(jiǎn)單生成兩個(gè)數(shù)字之間隨機(jī)數(shù)的方法
這篇文章主要介紹了JS簡(jiǎn)單生成兩個(gè)數(shù)字之間隨機(jī)數(shù)的方法,涉及javascript數(shù)值運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-08-08
window.open以post方式將內(nèi)容提交到新窗口
最近在做web項(xiàng)目,碰到需要跨頁(yè)面?zhèn)鬟f參數(shù)的功能,就是那種需要把當(dāng)前頁(yè)面的內(nèi)容帶到新開(kāi)的子窗體中,以前的做法是傳一個(gè)id過(guò)去,然后在新窗口中去讀數(shù)據(jù)庫(kù)的內(nèi)容;比較有意思的是直接通過(guò)調(diào)用form的submit方法不能觸發(fā)onsubmit事件,查看了幫助文檔,必須手動(dòng)的觸發(fā),否則只能看到頁(yè)面刷新而沒(méi)有打開(kāi)新窗口2012-12-12
在微信小程序中獲取用戶(hù)位置的詳細(xì)過(guò)程
小程序需要用戶(hù)授權(quán)獲取用戶(hù)位置,獲取到經(jīng)緯度請(qǐng)求數(shù)據(jù)列表并解析為城市地址,用戶(hù)選擇城市時(shí)把城市解析為經(jīng)緯度,下面這篇文章主要給大家介紹了關(guān)于如何在微信小程序中獲取用戶(hù)位置的相關(guān)資料,需要的朋友可以參考下2022-08-08
點(diǎn)擊頁(yè)面任何位置隱藏div的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇點(diǎn)擊頁(yè)面任何位置隱藏div的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
純?JS?實(shí)現(xiàn)的輕量化圖片編輯器實(shí)例詳解
這篇文章主要為大家介紹了純JS實(shí)現(xiàn)的輕量化圖片編輯器實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
JavaScript前端實(shí)現(xiàn)壓縮圖片功能
這篇文章主要介紹了JavaScript前端實(shí)現(xiàn)壓縮圖片功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

