• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > HTML5实现时钟效果

HTML5实现时钟效果

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含PHP,PHP100,PHP教程,PHP论坛,EclipsePHP,视频教程等相关知识,匿名希望在学习及工作中可以帮助到您
[导读] 代码如下复制代码 以下是完整代码,保存到html文件可以查看效果。<!DOCTYPE html><html><head><meta http-equiv="Content-type" content="text html; charset=utf-8" ><title>HTML5时钟

代码如下 复制代码

以下是完整代码,保存到html文件可以查看效果。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>HTML5时钟-柯乐义</title>
</head>
<body>
<p><a href="http://www.keleyi.com" target="_blank">柯乐义</a> <a href="http://www.keleyi.com/a/bjac/4f6d3873d0571805.htm" target="_blank">原文</a>
<h1>HTML5时钟</h1>
<canvas id="canvas" width="200" height="200" style="border:1px solid #000;">柯乐义提示您,请使用支持HTML5的浏览器,例如Chrome,IE9,IE10,Firefox等。</canvas>
</p>
<script type="text/javascript" language="javascript" charset="utf-8">
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
if (ctx) {
var timerId;
var frameRate = 60;
function canvObject() {
this.x = 0;
this.y = 0;
this.rotation = 0;
this.borderWidth = 2;
this.borderColor = '#000000';
this.fill = false;
this.fillColor = '#ff0000';
this.update = function () {
if (!this.ctx) throw new Error('柯乐义提示:您没指定ctx对象。');
var ctx = this.ctx
ctx.save();
ctx.lineWidth = this.borderWidth;
ctx.strokeStyle = this.borderColor;
ctx.fillStyle = this.fillColor;
ctx.translate(this.x, this.y);
if (this.rotation) ctx.rotate(this.rotation * Math.PI / 180);
if (this.draw) this.draw(ctx);
if (this.fill) ctx.fill();
ctx.stroke();
ctx.restore();
}
};
function Line() { };
Line.prototype = new canvObject();
Line.prototype.fill = false;
Line.prototype.start = [0, 0];
Line.prototype.end = [5, 5];
Line.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.moveTo.apply(ctx, this.start);
ctx.lineTo.apply(ctx, this.end);
ctx.closePath();
};

function Circle() { };
Circle.prototype = new canvObject();
Circle.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, 2 * Math.PI, true);
ctx.closePath();
};

var circle = new Circle();
circle.ctx = ctx;
circle.x = 100;
circle.y = 100;
circle.radius = 90;
circle.fill = true;
circle.borderWidth = 6;
circle.fillColor = '#ffffff';

var hour = new Line();
hour.ctx = ctx;
hour.x = 100;
hour.y = 100;
hour.borderColor = "#000000";
hour.borderWidth = 10;
hour.rotation = 0;
hour.start = [0, 20];
hour.end = [0, -50];

var minute = new Line();
minute.ctx = ctx;
minute.x = 100;
minute.y = 100;
minute.borderColor = "#333333";
minute.borderWidth = 7;
minute.rotation = 0;
minute.start = [0, 20];
minute.end = [0, -70];

var seconds = new Line();
seconds.ctx = ctx;
seconds.x = 100;
seconds.y = 100;
seconds.borderColor = "#ff0000";
seconds.borderWidth = 4;
seconds.rotation = 0;
seconds.start = [0, 20];
seconds.end = [0, -80];

var center = new Circle();
center.ctx = ctx;
center.x = 100;
center.y = 100;
center.radius = 5;
center.fill = true;
center.borderColor = 'orange';

for (var i = 0, ls = [], cache; i < 12; i++) {
cache = ls[i] = new Line();
cache.ctx = ctx;
cache.x = 100;
cache.y = 100;
cache.borderColor = "orange";
cache.borderWidth = 2;
cache.rotation = i * 30;
cache.start = [0, -70];
cache.end = [0, -80];
}

timerId = setInterval(function () {
// 清除画布
ctx.clearRect(0, 0, 200, 200);
// 填充背景色
ctx.fillStyle = 'orange';
ctx.fillRect(0, 0, 200, 200);
// 表盘
circle.update();
// 刻度
for (var i = 0; cache = ls[i++]; ) cache.update();
// 时针
hour.rotation = (new Date()).getHours() * 30;
hour.update();
// 分针
minute.rotation = (new Date()).getMinutes() * 6;
minute.update();
// 秒针
seconds.rotation = (new Date()).getSeconds() * 6;
seconds.update();
// 中心圆
center.update();
}, (1000 / frameRate) | 0);
} else {
alert('柯乐义提示:您的浏览器不支持HTML5无法预览.');
}
</script>
</body>
</html>

以上就是 HTML5实现时钟效果的详细内容,更多请关注微课江湖其它相关文章!

您可能想查找下面的文章:

  • 编写html5时调试发现脚本php等网页js、css等失效
  • HTML5中文件上传的代码
  • 使用PHP和HTML5 FormData实现无刷新文件上传
  • 快速掌握前端开发技巧
  • html5+JavaScript进行邮箱地址验证
  • linux下HTML5用户及用户组管理命令详解
  • jquery 遍历parent()方法
  • html5视频与声频详解
  • html5+php如何实现文件拖动上传功能
  • html5入门之设计原理解析

相关文章

  • 2018-12-03如何解决微信通过H5页面直接打开本地app
  • 2018-12-03html5实现文字轮滚的示例代码
  • 2018-12-03HTML5 canvas画布详解(四)
  • 2017-08-06html5使用canvas画一条线
  • 2017-08-06让ie浏览器成为支持html5的浏览器的解决方法(使用html5shiv)
  • 2017-08-06HTML5中使用postMessage实现两个网页间传递数据
  • 2018-12-03解析HTML5中的新功能本地存储localStorage_html5教程技巧
  • 2017-08-06HTML5中新标签和常用标签详解
  • 2018-12-03Html5简单实现涂鸦板的示例代码
  • 2018-12-03有关优化策略的文章推荐10篇

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • HTML5 微格式和相关的属性名称
    • 为什么说「一入前端深似海」,入行需谨慎?
    • phonegap使用方法介绍(七)播放音频的实现方法
    • 页面重绘及回流的优化方法
    • 关于HTML5语义标签的实践(blog页面)
    • bookblock:可帮助你生成翻页效果的jQuery插件
    • html5 canvas-1.canvas介绍(hello canvas)
    • 举例详解HTML5中使用JSON格式提交表单_html5教程技巧
    • HTML最新标准HTML5总结(必看)
    • 熟练JavaScript的步骤应该是?

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有