佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > html5 canvas實(shí)現(xiàn)圓形時(shí)鐘代碼分享

html5 canvas實(shí)現(xiàn)圓形時(shí)鐘代碼分享

熱門標(biāo)簽:百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 美圖手機(jī) 智能手機(jī) 使用U盤裝系統(tǒng) 硅谷的囚徒呼叫中心 網(wǎng)站建設(shè) 阿里云 檢查注冊(cè)表項(xiàng)

復(fù)制代碼
代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML CLOCK</title>
</head>
<body>
<canvas width="500" height="500" id="clock">
你的瀏覽器不支持canvas標(biāo)簽,時(shí)針顯示不出來(lái)哦!
</canvas>

<script type="text/javascript">
//畫(huà)布背景顏色
var clockBackGroundColor = "#ABCDEF";
//時(shí)針顏色
var hourPointColor = "#000";
//時(shí)針粗細(xì)
var hourPointWidth = 7;
//時(shí)針長(zhǎng)度
var hourPointLength = 100;
//分針顏色
var minPointColor = "#000";
//分針粗細(xì)
var minPointWidth = 5;
//分針長(zhǎng)度
var minPointLength = 150;
//秒針顏色
var secPointColor = "#F00";
//秒針粗細(xì)
var secPointWidth = 3;
//秒針長(zhǎng)度
var secPointLength = 170;
//表盤顏色
var clockPanelColor = "#ABCDEF";
//表盤刻度顏色
var scaleColor = "#000";
//表盤大刻度寬度 3 6 9 12
var scaleBigWidth = 9;
//表盤大刻度的長(zhǎng)度
var scaleBigLength = 15;
//表盤小刻度的寬度
var scaleSmallWidth = 5;
//表盤小刻度的長(zhǎng)度
var scaleSmallLength = 10;
//圓心顏色
var centerColor = 'red';


//時(shí)鐘畫(huà)布
var clock = document.getElementById('clock');
clock.style.background = clockBackGroundColor;
//時(shí)針畫(huà)布的作圖環(huán)境(畫(huà)板)
var panel = clock.getContext('2d');


//畫(huà)線
/**
*畫(huà)線段
*
*
*/
function drowLine(p,width,color,startX,startY,endX,endY,ran,cX,cY){
//保存?zhèn)魅氲漠?huà)板,相當(dāng)于每次作畫(huà)新開(kāi)一個(gè)圖層
p.save();
//設(shè)置畫(huà)筆寬度
p.lineWidth = width;
//設(shè)置畫(huà)筆顏色
p.strokeStyle = color;
//新開(kāi)啟作圖路徑,避免和之前畫(huà)板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
p.translate(cX,cY);
//旋轉(zhuǎn)
p.rotate(ran);
//移動(dòng)畫(huà)筆到開(kāi)始位置
p.moveTo(startX,startY);
//移動(dòng)畫(huà)筆到結(jié)束位置
p.lineTo(endX,endY);
//畫(huà)線操作
p.stroke();
//關(guān)閉作圖路徑,避免和之后在畫(huà)板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫(huà)板對(duì)象上覆蓋圖層
p.restore();
}
/**
*畫(huà)水平線
*/
function drowHorizontalLine(p,width,length,color,startX,startY,ran,cX,cY){
drowLine(p,width,color,startX,startY,startX+length,startY,ran,cX,cY);
}
/**
*畫(huà)圈圈
*/
function drowCircle(p,width,color,centreX,centreY,r){
p.save();
//設(shè)置畫(huà)筆寬度
p.lineWidth = width;
//設(shè)置畫(huà)筆顏色
p.strokeStyle = color;
//新開(kāi)啟作圖路徑,避免和之前畫(huà)板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
//畫(huà)圈圈
p.arc(centreX,centreY,r,0,360,false);
//畫(huà)線操作
p.stroke();
//關(guān)閉作圖路徑,避免和之后在畫(huà)板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫(huà)板對(duì)象上覆蓋圖層
p.restore();
}
function drowPoint(p,width,color,centreX,centreY,r){
p.save();
//設(shè)置畫(huà)筆寬度
p.lineWidth = width;
//設(shè)置畫(huà)筆顏色
p.fillStyle = color;
//新開(kāi)啟作圖路徑,避免和之前畫(huà)板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
//畫(huà)圈圈
p.arc(centreX,centreY,r,0,360,false);
//畫(huà)線操作
p.fill();
//關(guān)閉作圖路徑,避免和之后在畫(huà)板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫(huà)板對(duì)象上覆蓋圖層
p.restore();
}
function drowScales(p){
//畫(huà)小刻度
for(var i = 0;i < 60;i++){
drowHorizontalLine(p,scaleSmallWidth,scaleSmallLength,scaleColor,195-scaleSmallLength,0,i*6*Math.PI/180,250,250);
}
//畫(huà)大刻度
for(var i = 0;i < 12;i++){
drowHorizontalLine(p,i%3==0?scaleBigWidth*1.2:scaleBigWidth,i%3==0?scaleBigLength*1.2:scaleBigLength,scaleColor,195-scaleBigLength,0,i*30*Math.PI/180,250,250);
//可以添加數(shù)字刻度
}
}
function drowHourPoint(p,hour){
drowHorizontalLine(p,hourPointWidth,hourPointLength,hourPointColor,-10,0,(hour-3)*30*Math.PI/180,250,250);
}
function drowMinPoint(p,min){
drowHorizontalLine(p,minPointWidth,minPointLength,minPointColor,-15,0,(min-15)*6*Math.PI/180,250,250);
}
function drowSecPoint(p,sec){
drowHorizontalLine(p,secPointWidth,secPointLength,secPointColor,-15,0,(sec-15)*6*Math.PI/180,250,250);
}
function drowClock(){
panel.clearRect(0,0,500,500);

panel.fillText("",10,20);
panel.fillText("<a ,10,40</a>);
var date = new Date();
var sec = date.getSeconds();
var min = date.getMinutes();
var hour = date.getHours() + min/60;
drowCircle(panel,7,'blue',250,250,200);
drowScales(panel);

drowHourPoint(panel,hour);
drowMinPoint(panel,min);
drowSecPoint(panel,sec);

drowPoint(panel,1,centerColor,250,250,7);
//drowHorizontalLine(panel,10,10,'red',-5,0,0,250,250);
}
//drowHorizontalLine(panel,7,30,'#F00',0,0,Math.PI,250,250);
drowClock();
setInterval(drowClock,1000);
function save(){
var image = clock.toDataURL("image/png").replace("image/png", "image/octet-stream");
location.href=image;
}
</script>
</body>
</html>

標(biāo)簽:煙臺(tái) 山南 通遼 懷化 湖北 黃山 賀州 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5 canvas實(shí)現(xiàn)圓形時(shí)鐘代碼分享》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    加查县| 汾阳市| 竹山县| 高尔夫| 兴城市| 上蔡县| 凤翔县| 九龙坡区| 万宁市| 南乐县| 盘山县| 太谷县| 长兴县| 育儿| 勃利县| 伊宁市| 朔州市| 绥宁县| 曲阜市| 镇沅| 勃利县| 嵊州市| 卓资县| 蚌埠市| 崇阳县| 中江县| 吴堡县| 灵台县| 道孚县| 泸溪县| 天全县| 永昌县| 鄂州市| 平果县| 夏津县| 博乐市| 鹤岗市| 宁津县| 广州市| 龙川县| 蓝田县|