在這之前
你需要了解一下方法的使用:
beginPath()
closePath()
moveTo()
lineTo()
fill()
stroke()
fillRect()
clearRect()
這些我在前面的文章介紹過毡泻,可以看:
畫個圓
arc()
方法
arc(x, y, radius, startAngle, endAngle, anticlockwise)
= > 畫一個以(x,y)為圓心的以radius為半徑的圓挥悼獭(圓)蓝牲,從startAngle開始到endAngle結(jié)束粱栖,按照anticlockwise給定的方向(默認(rèn)為順時針)來生成。
該方法有五個參數(shù):x,y為繪制圓弧所在圓上的圓心坐標(biāo)太颤。radius為半徑谨垃。startAngle以及endAngle參數(shù)用弧度定義了開始以及結(jié)束的弧度幌甘。這些都是以x軸為基準(zhǔn)。參數(shù)anticlockwise 為一個布爾值蚁飒。為true時动壤,是逆時針方向,否則順時針方向淮逻。
注意:arc()函數(shù)中的角度單位是弧度琼懊,不是度數(shù)。角度與弧度的js表達(dá)式:radians=(Math.PI/180)*degrees爬早。
//畫一個帶邊框的實(shí)心圓
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 600;
canvas.height = 600;
ctx.beginPath();
var x = 200, // x 坐標(biāo)值
y = 200, // y 坐標(biāo)值
radius = 50, //半徑
startAngle = 0 ; //開始點(diǎn)
endAngle = Math.PI * 2; //結(jié)束點(diǎn)
anticlockwise = true; //逆時針
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
ctx.lineWidth = 10;
ctx.fillStyle = "#1208ff";
ctx.strokeStyle = "#333";
ctx.stroke();
ctx.fill();
實(shí)現(xiàn)的效果圖如下:
準(zhǔn)備工作
會畫個圓之后呢哼丈,就要開始繪制我們的電子時鐘。開始之前筛严,我們需要理清思路醉旦。首先,我們要創(chuàng)建個二維數(shù)組放置我們從0-9的點(diǎn)陣圖形,當(dāng)元素的值為1的時候车胡,就要將其繪制出來檬输。以下是二維數(shù)組的片段:
[
[0,0,1,1,1,0,0],
[0,1,1,0,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,0,1,1,0],
[0,0,1,1,1,0,0]
]//0
我們要做的就是將 0 - 9 個數(shù)字用二維數(shù)組表示出來。
繪制電子時鐘的數(shù)字
首先匈棘,我們要遍歷我們的二維數(shù)組丧慈,如果元素的值為 1 ,則我們就將他繪制成圓形主卫,那如何確定每個元素的圓心呢逃默,看下面這張圖:
接著我們寫這樣一個繪制數(shù)字的函數(shù):
function drawNum(x, y, num, ctx){
for (var i = 0; i < digit[num].length; i++){
for(var j = 0; j < digit[num][i].length; j++){
if( digit[num][i][j] == 1){
ctx.beginPath();
ctx.fillStyle = "rgb(0, 102, 153)";
ctx.arc(x+(RADIUS+1)*2*j+(RADIUS+1), y+(RADIUS+1)*2*i+(RADIUS+1), RADIUS, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
}
}
}
然后,調(diào)用該函數(shù)來繪制我們的數(shù)字:
var RADIUS = 4; // 圓的半徑
drawNum(0, 0, 1, ctx);
繪制的效果如下:
)
繪制簡單的電子時鐘
我們需要定義一個函數(shù)draw()
來繪制我們的電子時鐘簇搅。
function draw(ctx){
var curDate = new Date();
var hour = curDate.getHours();
var minute = curDate.getMinutes();
var seconds = curDate.getSeconds();
drawNum(Margin_X, Margin_Y, parseInt(hour/10), ctx);
drawNum(Margin_X + (RADIUS+1)*15, Margin_Y, parseInt(hour%10), ctx);
drawNum(Margin_X + (RADIUS+1)*30, Margin_Y, 10, ctx);
drawNum(Margin_X + (RADIUS+1)*45, Margin_Y, parseInt(minute/10), ctx);
drawNum(Margin_X + (RADIUS+1)*60, Margin_Y, parseInt(minute%10), ctx);
drawNum(Margin_X + (RADIUS+1)*75, Margin_Y, 10, ctx);
drawNum(Margin_X + (RADIUS+1)*90, Margin_Y, parseInt(seconds/10), ctx);
drawNum(Margin_X + (RADIUS+1)*105, Margin_Y, parseInt(seconds%10), ctx);
}
為了讓每個數(shù)字之間有些間隔笑旺,不重疊在一起,定義了兩個變量 Margin_X
和Margin_Y
來控制它距畫布左邊和頂部的距離馍资。初始值都是30筒主;
var Margin_X = 30; // 離 canvas 原點(diǎn)的坐標(biāo)值 x
var Margin_Y = 30; // 離 canvas 原點(diǎn)的坐標(biāo)值 y
讓電子時鐘動起來
setInterval()
方法
setInterval(function,time)
= > 該方法會循環(huán)執(zhí)行一個函數(shù),時間間隔為 time(ms)
我們利用 setInterval
方法讓我們的電子時鐘動起來鸟蟹。
setInterval(function(){
draw(ctx);
}, 500);
相應(yīng)的乌妙,我們需要在重復(fù)繪制前,清楚我們的畫布建钥,不然會導(dǎo)致數(shù)字重疊在一起藤韵。這里用到了clearRect()
清楚我們的畫布。
ctx.clearRect(0,0,1024,786);
附上完整的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>canvas</title>
</head>
<body>
<canvas id="canvas" width = "1024" height= "786" style="display: block; margin: 50px auto;" >
當(dāng)前瀏覽器不支持canvas
</canvas>
<script src="digit.js"></script>
<script type="text/javascript">
//電子時鐘
var RADIUS = 4; // 圓的半徑
var Margin_X = 30; // 離 canvas 原點(diǎn)的坐標(biāo)值 x
var Margin_Y = 30; // 離 canvas 原點(diǎn)的坐標(biāo)值 y
window.onload = function(){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
setInterval(function(){
draw(ctx);
}, 500);
}
function draw(ctx){
ctx.clearRect(0,0,1024,786);
var curDate = new Date();
var hour = curDate.getHours();
var minute = curDate.getMinutes();
var seconds = curDate.getSeconds();
drawNum(Margin_X, Margin_Y, parseInt(hour/10), ctx);
drawNum(Margin_X + (RADIUS+1)*15, Margin_Y, parseInt(hour%10), ctx);
drawNum(Margin_X + (RADIUS+1)*30, Margin_Y, 10, ctx);
drawNum(Margin_X + (RADIUS+1)*45, Margin_Y, parseInt(minute/10), ctx);
drawNum(Margin_X + (RADIUS+1)*60, Margin_Y, parseInt(minute%10), ctx);
drawNum(Margin_X + (RADIUS+1)*75, Margin_Y, 10, ctx);
drawNum(Margin_X + (RADIUS+1)*90, Margin_Y, parseInt(seconds/10), ctx);
drawNum(Margin_X + (RADIUS+1)*105, Margin_Y, parseInt(seconds%10), ctx);
}
function drawNum(x, y, num, ctx){
for (var i = 0; i < digit[num].length; i++){
for(var j = 0; j < digit[num][i].length; j++){
if( digit[num][i][j] == 1){
ctx.beginPath();
ctx.fillStyle = "rgb(0, 102, 153)";
ctx.arc(x+(RADIUS+1)*2*j+(RADIUS+1), y+(RADIUS+1)*2*i+(RADIUS+1), RADIUS, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
}
}
}
</script>
</body>
</html>
digit.js
digit =
[
[
[0,0,1,1,1,0,0],
[0,1,1,0,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,0,1,1,0],
[0,0,1,1,1,0,0]
],//0
[
[0,0,0,1,1,0,0],
[0,1,1,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[1,1,1,1,1,1,1]
],//1
[
[0,1,1,1,1,1,0],
[1,1,0,0,0,1,1],
[0,0,0,0,0,1,1],
[0,0,0,0,1,1,0],
[0,0,0,1,1,0,0],
[0,0,1,1,0,0,0],
[0,1,1,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,1,1],
[1,1,1,1,1,1,1]
],//2
[
[1,1,1,1,1,1,1],
[0,0,0,0,0,1,1],
[0,0,0,0,1,1,0],
[0,0,0,1,1,0,0],
[0,0,1,1,1,0,0],
[0,0,0,0,1,1,0],
[0,0,0,0,0,1,1],
[0,0,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0]
],//3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1,0],
[0,1,1,0],
[0,0,0,0],
[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0],
[0,0,0,0]
]//:
];
實(shí)現(xiàn)的效果圖如下:
甚至我們還可以這樣子: