濮阳杆衣贸易有限公司

主頁 > 知識庫 > html5 canvas實現(xiàn)圓形時鐘代碼分享

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

熱門標簽:小朱地圖標注 地圖標注柱狀圖 四川移動電銷外呼客戶管理系統(tǒng) 怎么做百度地圖標注 智能芯電話機器人 400開頭的電話好申請不 臨海地圖標注app 咸陽穩(wěn)定外呼系統(tǒng)軟件 百度地圖標注為什么總是封號

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

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

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


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


//畫線
/**
*畫線段
*
*
*/
function drowLine(p,width,color,startX,startY,endX,endY,ran,cX,cY){
//保存?zhèn)魅氲漠嫲?,相當于每次作畫新開一個圖層
p.save();
//設(shè)置畫筆寬度
p.lineWidth = width;
//設(shè)置畫筆顏色
p.strokeStyle = color;
//新開啟作圖路徑,避免和之前畫板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
p.translate(cX,cY);
//旋轉(zhuǎn)
p.rotate(ran);
//移動畫筆到開始位置
p.moveTo(startX,startY);
//移動畫筆到結(jié)束位置
p.lineTo(endX,endY);
//畫線操作
p.stroke();
//關(guān)閉作圖路徑,避免和之后在畫板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫板對象上覆蓋圖層
p.restore();
}
/**
*畫水平線
*/
function drowHorizontalLine(p,width,length,color,startX,startY,ran,cX,cY){
drowLine(p,width,color,startX,startY,startX+length,startY,ran,cX,cY);
}
/**
*畫圈圈
*/
function drowCircle(p,width,color,centreX,centreY,r){
p.save();
//設(shè)置畫筆寬度
p.lineWidth = width;
//設(shè)置畫筆顏色
p.strokeStyle = color;
//新開啟作圖路徑,避免和之前畫板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
//畫圈圈
p.arc(centreX,centreY,r,0,360,false);
//畫線操作
p.stroke();
//關(guān)閉作圖路徑,避免和之后在畫板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫板對象上覆蓋圖層
p.restore();
}
function drowPoint(p,width,color,centreX,centreY,r){
p.save();
//設(shè)置畫筆寬度
p.lineWidth = width;
//設(shè)置畫筆顏色
p.fillStyle = color;
//新開啟作圖路徑,避免和之前畫板上的內(nèi)容產(chǎn)生干擾
p.beginPath();
//畫圈圈
p.arc(centreX,centreY,r,0,360,false);
//畫線操作
p.fill();
//關(guān)閉作圖路徑,避免和之后在畫板上繪制的內(nèi)容產(chǎn)生干擾
p.closePath();
//在傳入的畫板對象上覆蓋圖層
p.restore();
}
function drowScales(p){
//畫小刻度
for(var i = 0;i < 60;i++){
drowHorizontalLine(p,scaleSmallWidth,scaleSmallLength,scaleColor,195-scaleSmallLength,0,i*6*Math.PI/180,250,250);
}
//畫大刻度
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>

標簽:陜西 平頂山 南平 山南 黃石 公主嶺 黃石

巨人網(wǎng)絡(luò)通訊聲明:本文標題《html5 canvas實現(xiàn)圓形時鐘代碼分享》,本文關(guān)鍵詞  html5,canvas,實現(xiàn),圓形,時鐘,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《html5 canvas實現(xiàn)圓形時鐘代碼分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于html5 canvas實現(xiàn)圓形時鐘代碼分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    兰溪市| 宣武区| 乌鲁木齐县| 楚雄市| 正镶白旗| 明溪县| 新昌县| 凉城县| 资中县| 汝州市| 盐城市| 新晃| 新野县| 辽源市| 正定县| 罗田县| 朝阳市| 龙里县| 崇明县| 新田县| 八宿县| 张家口市| 土默特右旗| 彭山县| 贵南县| 右玉县| 庄河市| 泗洪县| 平顶山市| 布拖县| 巫溪县| 日土县| 武隆县| 青州市| 阿拉尔市| 丹巴县| 龙井市| 新沂市| 贵州省| 上高县| 峨边|