canvas时钟效果
作者:59580 发布时间:2024-05-05 09:14:22
标签:canvas,时钟
效果如下:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>canvas时钟</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
</style>
<script>
window.onload = function(){
var WINDOW_WIDTH = document.body.scrollWidth;
var WINDOW_HEIGHT = document.body.scrollHeight-10;
var RADIUS = 7; //球半径
var NUMBER_GAP = 10; //数字之间的间隙
var u = 0.65; //碰撞能量损耗系数
var context; //Canvas绘制上下文
var balls = []; //存储彩色的小球
const colors = ["#33B5E5", "#0099CC", "#AA66CC", "#9933CC", "#99CC00", "#669900", "#FFBB33", "#FF8800", "#FF4444", "#CC0000"]; //彩色小球的颜色
var currentNums = []; //屏幕显示的8个字符
var 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,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1, 1, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 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]
], //7
[
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[0, 1, 1, 1, 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],
[0, 1, 1, 1, 1, 1, 0]
], //8
[
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[0, 1, 1, 1, 0, 1, 1],
[0, 0, 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, 1, 1, 0, 0, 0, 0]
], //9
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 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]
] //:
];
function drawDatetime(cxt) {
var nums = [];
var date = new Date();
var hours = date.getHours();
var num1 = Math.floor(hours / 10);
var num2 = hours % 10;
context.fillStyle = colors[(date.getSeconds()%10)];
var offsetX = WINDOW_WIDTH/4,
offsetY = 30;
nums.push({
num: num1
});
nums.push({
num: num2
});
nums.push({
num: 10
}); //冒号
var minutes = date.getMinutes();
var num1 = Math.floor(minutes / 10);
var num2 = minutes % 10;
nums.push({
num: num1
});
nums.push({
num: num2
});
nums.push({
num: 10
}); //冒号
var seconds = date.getSeconds();
var num1 = Math.floor(seconds / 10);
var num2 = seconds % 10;
nums.push({
num: num1
});
nums.push({
num: num2
});
for(var x = 0; x < nums.length; x++) {
nums[x].offsetX = offsetX;
offsetX = drawSingleNumber(offsetX, offsetY, nums[x].num, cxt);
//两个数字连一块,应该间隔一些距离
if(x < nums.length - 1) {
if((nums[x].num != 10) && (nums[x + 1].num != 10)) {
offsetX += NUMBER_GAP;
}
}
}
//说明这是初始化
if(currentNums.length == 0) {
currentNums = nums;
} else {
//进行比较
for(var index = 0; index < currentNums.length; index++) {
if(currentNums[index].num != nums[index].num) {
//不一样时,添加彩色小球
addBalls(nums[index]);
currentNums[index].num = nums[index].num;
}
}
}
renderBalls(cxt);
updateBalls();
return date;
}
function addBalls(item) {
var num = item.num;
var numMatrix = digit[num];
for(var y = 0; y < numMatrix.length; y++) {
for(var x = 0; x < numMatrix[y].length; x++) {
if(numMatrix[y][x] == 1) {
var ball = {
offsetX: item.offsetX + RADIUS + RADIUS * 2 * x,
offsetY: 30 + RADIUS + RADIUS * 2 * y,
color: colors[Math.floor(Math.random() * colors.length)],
g: 1.5 + Math.random(),
vx: Math.pow(-1, Math.ceil(Math.random() * 10)) * 4 + Math.random(),
vy: -5
}
balls.push(ball);
}
}
}
}
function renderBalls(cxt) {
for(var index = 0; index < balls.length; index++) {
cxt.beginPath();
cxt.fillStyle = balls[index].color;
cxt.arc(balls[index].offsetX, balls[index].offsetY, RADIUS, 0, 2 * Math.PI);
cxt.fill();
}
}
function updateBalls() {
var i = 0;
for(var index = 0; index < balls.length; index++) {
var ball = balls[index];
ball.offsetX += ball.vx;
ball.offsetY += ball.vy;
ball.vy += ball.g;
if(ball.offsetY > (WINDOW_HEIGHT - RADIUS)) {
ball.offsetY = WINDOW_HEIGHT - RADIUS;
ball.vy = -ball.vy * u;
}
if(ball.offsetX > RADIUS && ball.offsetX < (WINDOW_WIDTH - RADIUS)) {
balls[i] = balls[index];
i++;
}
}
//去除出边界的球
for(; i < balls.length; i++) {
balls.pop();
}
}
function drawSingleNumber(offsetX, offsetY, num, cxt) {
var numMatrix = digit[num];
for(var y = 0; y < numMatrix.length; y++) {
for(var x = 0; x < numMatrix[y].length; x++) {
if(numMatrix[y][x] == 1) {
cxt.beginPath();
cxt.arc(offsetX + RADIUS + RADIUS * 2 * x, offsetY + RADIUS + RADIUS * 2 * y, RADIUS, 0, 2 * Math.PI);
cxt.fill();
}
}
}
cxt.beginPath();
offsetX += numMatrix[0].length * RADIUS * 2;
return offsetX;
}
var canvas = document.getElementById("canvas");
canvas.width = WINDOW_WIDTH;
canvas.height = WINDOW_HEIGHT;
context = canvas.getContext("2d");
//记录当前绘制的时刻
var currentDate = new Date();
setInterval(function() {
//清空整个Canvas,重新绘制内容
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
drawDatetime(context);
}, 50)
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
来源:http://www.qdfuns.com/notes/35662/8cbbf29ebe0e38bd893b22d00e873634.html


猜你喜欢
- 下列语句部分是Mssql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECL
- 1、有了已经训练好的模型参数,对这个模型的某些层做了改变,如何利用这些训练好的模型参数继续训练:pretrained_params = to
- file->setting->project->project interperter双击右侧出现的pip,弹出安装包,搜
- 该组件基于技术栈,主要涉及vue-cli生成的webpack项目脚手架,在此脚手架项目基础上完成的,整合了element-ui开源vue的U
- 返回页面的类容,weburl为页面urlFunction GetBytes(weburl) '创建
- 一般写ASP PHP代码的朋友都估计是采用直接操作SQL的吧~ 看以下的代码 <% dim conn,rs&nbs
- 在html里的每一个标签都有其自身的意义,而H标签作为标题标签,它的意义更是至关重要。对于H标签的用法特别是h1的用法一直是个争议的问题,也
- 今天调试某页面样式,发现chrome下出现问题,但是同样基于webkit引擎的safari没有问题,很是郁闷。于是寻找针对google ch
- 主要实现功能1、用户输入用户名,在用户名文件中查找对应的用户,若无对应用户名则打印输入错误2、用户名输入正确后,进行密码匹配。输入密码正确则
- 看lifesinger的《由Kimi找茬想到的》,我想到的:1、 我不同意将“合并付款”定调在“很多卖家都需要”。这个“很多”在卖家里面大概
- Doing INTERSECT and MINUS in MySQL Doing an INTERSECT An INTERSECT is
- Python 内置的四种常用数据结构:列表(list)、元组(tuple)、字典(dict)以及集合(set)。这四种数据结构一但都可用于保
- Springboot连接数据库第一步springboot继承Mybatis及数据库连接依赖(上一篇文章已经记录 ) 第二步reso
- 前言:stored procedure 完成特定功能的SQL语句集,存储在数据库中,经过第一次编译之后再次调用不需要编译(效率较高)1、存储
- 本文主要介绍了pandas导出数据到文件的四种方式,分享给大家,主要也是给自己留个笔记,具体如下:import pandas as pdim
- 这篇文章主要介绍了Pycharm debug调试时带参数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 前言说到二手房信息,不知道你们心里最先跳出来的公司(网站)是什么,反正我心里第一个跳出来的是网站是 58 同城。哎呦,我这暴脾气,想到就赶紧
- python开启debug模式的代码如下所示:import requests session = requests.session()imp
- 本文实例为大家分享了JavaScript自定义日历效果的具体代码,供大家参考,具体内容如下实现思路:获取每个月的第一天是星期几,然后把前面的
- 本文为大家分享了pygame游戏之旅的第5篇,供大家参考,具体内容如下在游戏中添加显示文字:这里自己定义一个crash函数接口:def cr