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> < ...
随机推荐
- go 的匿名函数和闭包
匿名函数 匿名函数是指不需要定义函数名的一种函数实现方式. 在Go语言中,函数可以像普通变量一样被传递或使用,这与C语言的回调函数比较类似.不同的是,Go语言支持随时在代码里定义匿名函数. 匿名函数由 ...
- 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件
如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDEA在提交项目到本地仓库的时候,会把.idea文件 ...
- [再寄小读者之数学篇](2014-04-01 from 2103471050@qq.com 曲线积分)
求 $\int_\vGa y^2\rd s$, 其中 $\vGa$ 由 $\dps{\sedd{\ba{rl} x^2+y^2+z^2&=a^2\\ x+z&=a \ea}}$ 决定. ...
- 快速安装Java环境
1.部署jdk8 jdk包地址:https://pan.baidu.com/s/1QNGpapGuex00F6HQ5pynHgtar -xzf jdk-8u60-linux-x64.tar.gz #安 ...
- 激活函数——tanh函数(理解)
0 - 定义 $tanh$是双曲函数中的一个,$tanh()$为双曲正切.在数学中,双曲正切“$tanh$”是由基本双曲函数双曲正弦和双曲余弦推导而来. $$tanhx=\frac{sinhx}{co ...
- windows下nginx的简单使用
在网上是可以查得到关于nginx的一些使用,每次要用的时候都会去查一下别人的用法.既然自己用到的频率会比较高,那何不自己做个小小的笔记呢. 我使用的是windows版nginx-1.14.0,版本对命 ...
- SQL server 存储过程中 列传行
select @exchange=exchange,@coupons_type=coupons_type - FLOOR(exchange))) from points_exchange_svc wh ...
- 「luogu2486」[SDOI2011] 染色
https://www.luogu.org/problemnew/show/P2486 轻重链剖分后,问题转化为一个链上的问题: 线段树维护区间内的颜色段数量,左端点.右端点的颜色: 线段树注意事项 ...
- Spring Cloud Context模块
SpringCloud这个框架本身是建立在SpringBoot基础之上的,所以使用SpringCloud的方式与SpringBoot相仿.也是通过类似如下代码进行启动. SpringApplicati ...
- Lua“控制”C
[前言] Lua语言本身是一个功能非常有限,而比较单调的语言,而且标准库也非常的平庸,它的NB之处就在于,它能和C.C++等高级语言完美“私通”.我们可以使用C.C++语言去给Lua写一个完美的库,让 ...