国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

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

2020-03-24 18:51:01
字體:
供稿:網(wǎ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)簽,時針顯示不出來哦!
/canvas

script type="text/javascript"
//畫布背景顏色
var clockBackGroundColor = "#ABCDEF";
//時針顏色
var hourPointColor = "#000";
//時針粗細(xì)
var hourPointWidth = 7;
//時針長度
var hourPointLength = 100;
//分針顏色
var minPointColor = "#000";
//分針粗細(xì)
var minPointWidth = 5;
//分針長度
var minPointLength = 150;
//秒針顏色
var secPointColor = "#F00";
//秒針粗細(xì)
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)魅氲漠嫲澹喈?dāng)于每次作畫新開一個圖層
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 i++){
drowHorizontalLine(p,scaleSmallWidth,scaleSmallLength,scaleColor,195-scaleSmallLength,0,i*6*Math.PI/180,250,250);
}
//畫大刻度
for(var i = 0;i 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 http://www.phpstudy.net",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
html教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 棋牌| 仪征市| 伊宁市| 乐昌市| 逊克县| 水城县| 彰化市| 望城县| 花莲市| 皮山县| 大庆市| 常熟市| 儋州市| 龙江县| 竹北市| 修文县| 平罗县| 台北市| 寿宁县| 伊通| 永丰县| 桑日县| 和顺县| 商水县| 阿坝县| 宜君县| 堆龙德庆县| 舞阳县| 潮州市| 天长市| 新巴尔虎左旗| 庆云县| 大石桥市| 西乌珠穆沁旗| 邯郸县| 德江县| 格尔木市| 福州市| 榆林市| 涪陵区| 百色市|