html5 Canvas绘制时钟以及绘制运动的圆
1、绘制时钟
<!-- js代码 -->
<script type="text/javascript">
window.onload=function(){
var oC1=document.getElementById('c1')
var oGC=oC1.getContext('2d'); function toDraw(){
var x=200;
var y=200;
var r=150; oGC.clearRect(0,0,oC1.width,oC1.height); var iDate=new Date();
var iHou=iDate.getHours();
var iMin=iDate.getMinutes();
var iSce=iDate.getSeconds(); var oHoursValue = (-90 + iHou*30 + iMin/2) * Math.PI/180;
var oMinValue = (-90 + iMin*6) * Math.PI/180;
var oSenValue = (-90 + iSce*6) * Math.PI/180; oGC.beginPath(); for(var i=0;i<60;i++){
oGC.moveTo(x,y);
oGC.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false);
} oGC.closePath(); oGC.stroke(); oGC.fillStyle='#fff'; oGC.beginPath(); oGC.moveTo(x,y); oGC.arc(x,y,r*19/20,0,360*(i+1)*Math.PI/180,false); oGC.closePath(); oGC.fill(); oGC.lineWidth=3; oGC.beginPath(); for(var i=0;i<12;i++){ oGC.moveTo(x,y); oGC.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI/180,false);
} oGC.closePath(); oGC.stroke(); oGC.fillStyle = '#fff'; oGC.beginPath(); oGC.moveTo(x,y); oGC.arc(x,y,r*18/20,0,360*(i+1)*Math.PI/180,false); oGC.closePath(); oGC.fill(); oGC.lineWidth = 5;
oGC.beginPath();
oGC.moveTo(x,y);
oGC.arc(x,y,r*9/20,oHoursValue,oHoursValue,false); oGC.closePath();
oGC.stroke(); oGC.lineWidth = 3;
oGC.beginPath();
oGC.moveTo(x,y);
oGC.arc(x,y,r*13/20,oMinValue,oMinValue,false); oGC.closePath();
oGC.stroke(); oGC.lineWidth = 1;
oGC.beginPath();
oGC.moveTo(x,y);
oGC.arc(x,y,r*18/20,oSenValue,oSenValue,false); oGC.closePath();
oGC.stroke();
}
setInterval(toDraw,1000);
toDraw();
}
</script>
<canvas id="c1" width="400" height="400"></canvas>
2、绘制运动的圆
<!-- js代码 -->
<script>
window.onload=function(){
var oC=document.getElementById('c1');
var oGC=oC.getContext('2d');
var setArr=[]; setInterval(function(){//进行运动操作 oGC.clearRect(0,0,oC.width,oC.height); for(var i=0;i<setArr.length;i++){
setArr[i].r+=1;//半径每次增大1
setArr[i].c4-=0.01; if(setArr[i].c4<=0){//删除背景为透明的,避免元素过大
setArr.splice(i,1);
}
}
for(var i=0;i<setArr.length;i++){
oGC.beginPath();
oGC.fillStyle='rgba('+setArr[i].c1+','+setArr[i].c2+','+setArr[i].c3+','+setArr[i].c4+')';
oGC.moveTo(setArr[i].x,setArr[i].y);
oGC.arc(setArr[i].x,setArr[i].y,setArr[i].r,0,360*Math.PI/180,false);
oGC.closePath();
oGC.fill();
} },1000/60); setInterval(function(){//添加数据 var x=Math.floor(Math.random()*oC.width);//随机产生圆的x值
var y=Math.floor(Math.random()*oC.height);//随机产生圆的y值
var r=4;//圆的半径
//随机产生圆的颜色rgb
var c1=Math.floor(Math.random()*255);
var c2=Math.floor(Math.random()*255);
var c3=Math.floor(Math.random()*255);
var c4=1;//设置透明度 setArr.push({
x : x,
y : y,
r : r,
c1 : c1,
c2 : c2,
c3 : c3,
c4 : c4
}); },500);//往数组中放元素
}
</script>
<canvas id="c1" width="400" height="400"></canvas>
html5 Canvas绘制时钟以及绘制运动的圆的更多相关文章
- HTML5 Canvas(实战:绘制饼图2 Tooltip)
继上一篇HTML5 Canvas(实战:绘制饼图)之后,笔者研究了一下如何给饼图加鼠标停留时显示的提示框. Plot对象 在开始Coding之前,笔者能够想到的最easy的方式,就是给饼图的每一个区域 ...
- [ZZ+CH] Html5 canvas+js 时钟
总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...
- HTML5 Canvas爱心时钟代码
这是一款数字时钟动画,数字又多个小爱心组成,又何问起整理,随着时间推进,每一秒钟新数字替换旧数字,旧数字离去使用天女散花动画,花是五颜六色的. 查看效果:http://hovertree.com/te ...
- [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)
绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解. arc:画 ...
- html5 canvas绘画时钟
本示例使用HTML5 canvas,模拟显示了一个时钟, 请使用支持HTML5的浏览器预览效果: HTML部分: <!DOCTYPE html> <html lang="e ...
- html5 canvas 笔记三(绘制文本和图片)
绘制文本 fillText(text, x, y [, maxWidth]) 在指定的(x,y)位置填充指定的文本,绘制的最大宽度是可选的. strokeText(text, x, y [, ma ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HTML5 canvas 指针时钟
<!doctype html> <html> <head></head> <body> <canvas id="> 您 ...
- html5 canvas js(时钟)
<!doctype html> <html> <head> <title>canvas</title> </head> < ...
随机推荐
- Entity Framework入门教程(13)---EF中的高并发
EF中的高并发 这里只介绍EF6中database-first开发方案的高并发解决方案,code-first开发方案中的高并发会在以后的EF CodeFirst系列中介绍. EF默认支持乐观并发:我们 ...
- [物理学与PDEs]第4章习题2 反应力学方程组形式的化约 - 能量守恒方程
试证明: 利用连续性方程及动量方程, 能量守恒方程 (2. 15) 可化为 (2. 21) 的形式. 证明: 注意到 $$\beex \bea &\quad\cfrac{\p}{\p t}\s ...
- ntp 服务:Server dropped: Strata too high
1.通过ntpdate -d 服务端IP,显示Server dropped: Strata too high vi /etc/ntp.conf 在ntpd服务端的配置中添加 server 127.12 ...
- 关于JS中的常用表单验证+正则表达式
一.非空验证 trim:去空格(去掉前后的空格),任何字符串都可以用这个方法.写法为:if(v.trim().length==0),表示如果去掉空格后的字符串的长度为0. <body> & ...
- 基于STM32F1的时钟芯片DS1302驱动
目录 DS1302.h源代码 DS1302.c源代码 DS1302.h源代码 /** ********************************************************* ...
- ERROR [main] master.HMasterCommandLine Master exiting
2018-05-18 07:07:26,257 INFO [main-SendThread(localhost:2181)] zookeeper.ClientCnxn: Opening socket ...
- 嵌套调用less函数时参数值的变化及提取部分-遁地龙卷风
在a.less中导入base.lessa.less中的代码 .animate-ripple-ink{ .animation(ripple .5s linear;{ 100%{ opacity:; .t ...
- 第三章Android移植平台工具介绍
第三章Android移植平台工具介绍 进行 Android 移植的学习并不一定需要一款 Android 手机,但必须要有一款主流的开发板,开发板是用来进行嵌入式系统开发的电路板,包括中央处理器.存储器 ...
- POJ 2031 Building a Space Station (计算几何+最小生成树)
题目: Description You are a member of the space station engineering team, and are assigned a task in t ...
- Jmeter学习笔记03-元件作用域及执行顺序
Jmeter可以通过鼠标拖拽来随意改变元件之间的顺序以及元件的层级关系,随着元件所在域的不同,在执行时候,会有不同效果 层级关系和元件类型决定了在测试计划中各元件的执行顺序. 1)元件的作用域: jm ...