HTML5 Canvas画数字时钟

先不说废话,没代码算个蛋。

一些地方注释都写得比较清楚,不过这只是部分,因为只有秒针,但是时针,分针的逻辑都是一致的。

代码中有些坐标不知道为什么较不准,看看就好

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAIAAABqltZ3AAAXiElEQVR4nO3dfVATd/7AcWfuZo4bmAvXRMJJBA44S6VVqp6AhYFDWvQEcye2qAgyh9aHqmi9+lC9MT4UW7CHgpOcVJ4L9KQFL1Cw5DA2KTJCJUoGUVMbLEKGbHMJ2UtRAuzvj53b3zYgDXkgfNnP6y/LlK/fpe8um3347hwCANTMcfUEAJgyqBagB6oF6IFqAXqgWoAeqBagB6oF6IFqAXqgWoAeqBagB6oF6IFqAXqgWoAeqBagB6oF6IFqAXqgWoAeqBagB6oF6IFqAXqgWoAeqBagB6oF6IFqAXqgWoAeqNbpBAJBVlbWuXPnzp07d+HChZKSkpKSkrKyMrFYLJVKFQqFWq3W6/Wjo6OunikyoFqnE1jt73//e1FRUUNDw61btzQaDXT8LFCt01lfrYWsrKyioqKrV6/euXMHwzBXb8cMAtU6nc3VWvjoo48aGxs7Ozv/85//uHqbXAyqdTpHVUs5efJkSUmJXC4fGBhw9ca5BlTrdA6vllJVVeXqjXMNqNbpnFftjRs3XL1xrgHVOp2Tks3Kyurv73f1xrkGVOt0Tqq2oqLC1VvmMlCt0zmp2q+++srVW+YyUK3TOSPZU6dO9fb2unrLXAaqdTpnVFteXu7qzXIlqNbpnFGtTCZz9Wa5ElTrdA5P9sSJE48ePXL1ZrkSVOt0Dq+2pKTE1dvkYlCt0zm82uvXr7t6m1wMqnU6h1erVqtdvU0uBtU6nWOTLSoqGhsbc/U2uRhU63SOrba5udnVG+R6UO10GBwc1Ov1Go1GrVY/fPhQoVAoFAq5XC4Wi4uKirKzs62v9ptvvnH11rgeVDsjDA0N9fX1KZXKa9euffLJJ++///6EyV66dGlkZMTVk3U9qHYmevr0qVqtbmlpqa6uPn/+PFWtRCJx9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8clJS0vbt248ePXrs2LF9+/alpaXFx8fzeLzg4ODMzMy6ujpm9kqCan9kaGhIqVRKpdLi4uKcnByBQFBcXFxcXNza2jptB444jsvl8vLy8vz8fIFAIBAI8vPzq6qqpFIpcz5vTQ6qBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBeiBagF6oFqAHqgWoAeqBej5P3YVofXTaGPCAAAAAElFTkSuQmCC" alt="" />类似就是这样,没有经过美化。

 <html>
<head>
<title></title>
</head>
<body>
<canvas id="clock1.0" width="600" height="300" style="border:1px solid"></canvas>
<script type="text/javascript">
var canvas;
var ctx;
var width = 600;
var height = 300;
var c_r = Math.min(width, height) / 3;
var pattern;
var j = new Date().getSeconds();//获取当前时间的秒数
function init() {
canvas = document.getElementById('clock1.0');
ctx = canvas.getContext('2d');//你懂得,必须得写的
ctx.fillStyle = "white";
ctx.fillRect(0, 0, width, height);
waiquan();
neiquan();//其实圆环的产生是黑色的外圈+背景色的内圈
miaoshu();//内圈中写个秒数,去掉后也可以做圆环进度条
}
var image = new Image();
image.onload = function (e) {
pattern = ctx.createPattern(image, 'repeat');
}//加载个图片背景图
function miaoshu() { var miao = new Date().getSeconds();
if (miao < 10) {
miao = "0" + miao;
}//秒数10以下,显示的是单位数,不美观
ctx.font = '80px arial';
image.src = 'redball.png';
ctx.fillStyle = pattern;//这个背景图是用来做数字文字背景,其实完全没必要的
ctx.fillText(miao, width / 2-45, height / 2+30);//图片填充的文字
ctx.strokeText(miao, width / 2 - 45, height / 2 + 30);//描边文字
}
function neiquan() {//内圈就是一个园,背景色的圆
ctx.beginPath();
ctx.moveTo(width / 2, height / 2);
ctx.fillStyle = "white";
ctx.arc(width / 2, height / 2, c_r *0.81, 0, 2 * Math.PI, false);//自己控制内圈半径
ctx.fill();
}
function waiquan() {//外圈就需要考虑阴影面积了
if (j < 60) {
var hudu = 2 * Math.PI / 360 * (j * 6);//采用弧度机制,360度分为60格,每格6度,360度为2PI,所以j最大值为59
ctx.beginPath();
ctx.moveTo(width / 2, height / 2);//这里添加moveTo,其实是定义Fill()时的起始点,这里就是从圆心
//不添加上面这句,那么你画出来的阴影就不是扇形,是。类似切冬瓜。一圈一圈。。
ctx.arc(width / 2, height / 2, c_r, 3 / 2 * Math.PI, 3 / 2 * Math.PI + hudu, false);//这里就注意arc()的第四个参数,是弧度的起始位置,一般以前看到的都是0,这里要从3/2PI开始
//var grd = ctx.createLinearGradient(width / 2, height / 2, c_r/2, 3 / 2 * Math.PI, width / 2, height / 2, c_r, 3 / 2 * Math.PI); //本来想搞高渐变的,目前不成功
//grd.addColorStop(0, "#000000");
//grd.addColorStop(1, "#ffff00");
//ctx.fillStyle = grd;
ctx.fillStyle = "grey";
ctx.fill();
j++
}
else {
j = 1;//这里可扩展到分针,j=1表示秒针重置,这时分针+1
}
} function clean() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, width, height);//以背景色,清除画布
} setInterval(function () {//这里要注意的是清除的步骤,不然效果会很差
//先清空画布,把之前状态的清空,外圈,因为外圈黑色,再画内圈,把外圈中心盖掉,再画时间
clean();
waiquan();
neiquan();
miaoshu(); }, 1000)//1000ms,走的快的钟就不是钟了
window.onload("load", init(), true);//init作为初始加载函数
</script>
</body>
</html>
上一篇:python模块之paramiko


下一篇:011-Python-进程、线程于协程