会走动的图形html5时钟示例
来源:易贤网 阅读:692 次 日期:2016-06-23 09:28:12
温馨提示:易贤网小编为您整理了“会走动的图形html5时钟示例”,方便广大网友查阅!

使用html5制作时钟

代码如下:

<!doctype html>

<html>

<head>

<title>html5时钟</title>

</head>

<body>

<canvas id = canvas></canvas></p> <p> <script>

var clock = function (canvas, options) {

this.canvas = canvas;

this.ctx = this.canvas.getcontext(2d);

this.options = options;

};</p> <p> clock.prototype = {

constructor: clock,

drawcircle: function () {

var ctx = this.ctx;

ctx.strokestyle = black;

ctx.arc(this.canvas.width / 2, this.canvas.height / 2, 50, 0, 2 * math.pi, false);

ctx.stroke();

},

drawnum: function () {

var ctx = this.ctx;

var angle = math.pi * 2 / 12;

for (var i = 1; i <= 12; i += 1) {

ctx.font = 20px georgia;

ctx.textalign = center;

ctx.textbaseline = 'middle';

ctx.filltext(string(i), this.canvas.width / 2 + math.cos(3 *math.pi / 2 + angle * i) * 40, this.canvas.height / 2 + math.sin(3 * math.pi / 2 + angle * i) * 40);

}

},

drawpointer: function () {

var ctx = this.ctx;

var that = this;

var date, hour, minute, second;

date = new date();

hour = date.gethours();

if (hour > 12) {

hour = hour % 12;

}

minute = date.getminutes();

second = date.getseconds();</p> <p> var b = minute * math.pi / 30;

var c = second * math.pi / 30;

var a = hour * math.pi / 6 + math.pi / 6 * minute / 60;

var minuteangle = math.pi * 2 / 3600;

var secondangle = math.pi * 2 / 60;

var hourangle = math.pi * 2 / 12 / 3600;</p> <p> ctx.beginpath();

ctx.save();

ctx.translate(that.canvas.width / 2, that.canvas.height / 2);

ctx.arc(0, 0, 3, 0, 2 * math.pi, false);

ctx.fill();

ctx.closepath();

ctx.beginpath();

a += hourangle;

ctx.rotate(a);

ctx.fillrect(-2, -22, 4, 30);

ctx.closepath();

ctx.beginpath();

b += minuteangle;

ctx.rotate(b - a);

ctx.fillrect(-1.5, -26, 3, 35);

ctx.closepath();

ctx.beginpath();

c += secondangle;

ctx.rotate(c - b);

ctx.fillrect(-1, -30, 2, 40);

ctx.closepath();

ctx.restore();

},

repaint: function () {

this.drawpointer();

this.drawcircle();

this.drawnum();

},

tik: function () {

var that = this;

var ctx = this.ctx;

this.repaint();

window.timer = setinterval(function () {

ctx.clearrect(0, 0, that.canvas.width, that.canvas.height);

that.repaint();

}, 1000);

}

};</p> <p> var options;

var clock = new clock(document.getelementbyid(canvas), options);

clock.tik();

</script>

</body>

</html>

更多信息请查看网页制作
易贤网手机网站地址:会走动的图形html5时钟示例
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标