用canvas模仿iPhone时钟

[color=darkred][/color]自学前段开发也有一个月了,最近在学html5中的新标签“canvas”,发现canvas不仅仅是一个标签,更是一个能够制作动画效果的画布,于是模仿iPhone中时钟的效果,自己做了一个网页版的时钟,还只是一个雏形,以后还会增加一些自己的创意“抛弃圆形表盘”,“数字用图片或其它形状呈现”,“时针、分钟、秒针也用其它形状代替”,以我现在的基础也许还没有能力做出来,不过我会努力的。

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>炫丽的倒计时效果</title>
</head>
<body>
<canvas id="canvas" style="display: block; margin:50px auto;">
您的浏览器不支持canvas绘图,请更换浏览器。
</canvas>

<script type="text/javascript">
var WINDOW_WIDTH=400;
var WINDOW_HEIGHT=400;

window.οnlοad=function(){

var canvas=document.getElementById("canvas");
if(canvas.getContext("2d")){

var context=canvas.getContext("2d");

}
canvas.width=WINDOW_WIDTH;
canvas.height=WINDOW_HEIGHT;


render(context);
};

function numberToDegree(num){

return ((num*6-90)*2*Math.PI/360);
}

function computePositionByLenghtAndDegree(len,deg){


return {
x:len*Math.cos(deg),
y:len*Math.sin(deg)
}

}

function render(cxt){
var myDate=new Date();
var hour=myDate.getHours()%12;
var minute=myDate.getMinutes();
var second=myDate.getSeconds();

//cxt.clearRect(0,0,cxt.canvas.width,cxt.canvas.height);


//绘制表盘

cxt.fillStyle="#000000";
cxt.beginPath();
cxt.translate(200,200);
cxt.arc(0,0,199,0,2*Math.PI,true);
cxt.closePath();

cxt.fill();

//绘制时钟上面的1~12数字。
cxt.fillStyle = '#ffffff';
cxt.font="60px 'Bodoni MT'";
cxt.textAlign="center";
cxt.textBaseline="middle";

cxt.fillText("12",0,-150);
cxt.fillText("1",150*Math.cos(Math.PI/3),-150*Math.sin(Math.PI/3));
cxt.fillText("2",150*Math.cos(Math.PI/6),-150*Math.sin(Math.PI/6));
cxt.fillText("3",150,0);
cxt.fillText("4",150*Math.cos(Math.PI/6),150*Math.sin(Math.PI/6));
cxt.fillText("5",150*Math.cos(Math.PI/3),150*Math.sin(Math.PI/3));
cxt.fillText("6",0,150);
cxt.fillText("7",-150*Math.cos(Math.PI/3),150*Math.sin(Math.PI/3));
cxt.fillText("8",-150*Math.cos(Math.PI/6),150*Math.sin(Math.PI/6));
cxt.fillText("9",-150,0);
cxt.fillText("10",-150*Math.cos(Math.PI/6),-150*Math.sin(Math.PI/6));
cxt.fillText("11",-150*Math.cos(Math.PI/3),-150*Math.sin(Math.PI/3));

//绘制时针,分针,秒针。
var hourDegree=numberToDegree(hour*5+Math.floor(minute/12));
var minuteDegree=numberToDegree(minute);
var secondDegree=numberToDegree(second);

var hourBeginPot=computePositionByLenghtAndDegree(8,(hourDegree+Math.PI));
var hourEndPot=computePositionByLenghtAndDegree(80,hourDegree);
var minuteBeginPot=computePositionByLenghtAndDegree(10,minuteDegree+Math.PI);
var minuteEndPot=computePositionByLenghtAndDegree(100,minuteDegree);
var secondBeginPot=computePositionByLenghtAndDegree(15,secondDegree+Math.PI);
var secondEndPot=computePositionByLenghtAndDegree(120,secondDegree);

cxt.strokeStyle="#fff";
cxt.beginPath();
cxt.moveTo(hourBeginPot.x,hourBeginPot.y);
cxt.lineTo(hourEndPot.x,hourEndPot.y);
cxt.lineWidth=6;
cxt.closePath();
cxt.stroke();

cxt.strokeStyle="#fff";
cxt.beginPath();
cxt.moveTo(minuteBeginPot.x,minuteBeginPot.y);
cxt.lineTo(minuteEndPot.x,minuteEndPot.y);
cxt.lineWidth=3;
cxt.closePath();
cxt.stroke();
//绘制秒针
cxt.strokeStyle="#ff0000";
cxt.beginPath();
cxt.moveTo(secondBeginPot.x,secondBeginPot.y);
cxt.lineTo(secondEndPot.x,secondEndPot.y);
cxt.lineWidth=2;
cxt.closePath();
cxt.stroke();

cxt.translate(-200,-200);

setTimeout(
function(){render(cxt)}
,50);

}


</script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值