热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

jsCanvas实现的日历时钟案例分享

本文主要分享了js实现的日历时钟案例,具有一定的参考价值,下面跟着小编一起来看下吧

Html:













js:

;var calendarWithTime = function(){
 v = navigator.userAgent.toLowerCase().indexOf("android") != -1 || navigator.userAgent.toLowerCase().indexOf("iphone") != -1 || navigator.userAgent.toLowerCase().indexOf("ipad") != -1;
 // 浏览器可见区域
 appWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
 appHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - 3; // chrome下,高度一样是,会出现下拉滚动条
 // 中心点
 centerPoint = {'x':appWidth*0.5,'y':appHeight*0.5};
 // 动画用
 lastFpsUpdateTime = (+new Date);
 // canvas对象
 caObj = null;
 // canvas context对象
 ctxtObj = null;
 // 现在时间
 timeNow = "";
 // 开始年份
 startY = 1988;
 init = function(){
  window.Onload=function(){this.initCanvas();}
 }();
 getDomId = function(id){return document.getElementById(id);}
 initCanvas = function(id){
  this.caObj = this.getDomId("calendarWithTime");
  this.ctxtObj = this.caObj.getContext("2d");
  // 全屏canvas
  this.caObj.style.width = (this.appWidth+'px');
  this.caObj.style.height = (this.appHeight+'px');
  this.caObj.width = this.appWidth;
  this.caObj.height = this.appHeight;
  if (v) {
   caObj.style.border = "none";
  }
  // 开始年份
  startY = Math.floor((new Date()).getFullYear() / 8) * 8;
  // test 
  // startY = Math.floor(2010 / 8) * 8;
  this.lastFpsUpdateTime = (+new Date);
  this.animate();
 }
 doDraw = function(){
  this.ctxtObj.clearRect(0, 0, this.caObj.width, this.caObj.height);
  var date = new Date();
  // test
  /*date.setDate(29);
  date.setMonth(3);
  date.setFullYear(2010);*/
  var afterGap = 8 - (date.getFullYear() - startY);
  var allYears = date.getFullYear()-this.startY+afterGap;
  var allDays = this.getCountDays(date.getFullYear(),date.getMonth());
  this.doDrawDayPanel(31,allDays);
  this.doDrawMonthPanel();
  this.doDrawYearPanel(this.startY,date.getFullYear(),afterGap);
  // 画时间针
  this.doDrawTPanel();
  this.drawYMDHMS(0,0.35,0,0.1,date.getSeconds(),0,30,'s','');
  this.drawYMDHMS(0,0.3,0,0.05,date.getMinutes(),date.getSeconds()/60,30,'m','');
  this.drawYMDHMS(0,0.25,0,0.03,date.getHours() % 12,date.getMinutes()/60,6,'h','');
  this.drawYMDHMS(0.4,0.7,0.4,0.66,date.getDate(),date.getHours()/24,Math.ceil(31*0.5),'d',date.getDate());
  this.drawYMDHMS(0.4,0.6,0.4,0.568,(date.getMonth()),date.getDate()/(allDays+1),6,'M',date.getMonth()+1);
  this.drawYMDHMS(0.4,0.55,0.4,0.52,(date.getFullYear() - this.startY),(date.getMonth()+1)/13,Math.ceil(allYears*0.5),'y',date.getFullYear());
  // 显示时间
  this.getTimeNow();
  this.ctxtObj.save();
  this.ctxtObj.beginPath();
  this.ctxtObj.fillStyle = "#369";
  this.ctxtObj.strokeStyle = "#369";
  this.ctxtObj.fOnt= "30px bold 微软雅黑";
  this.ctxtObj.textAlign="start";
  this.ctxtObj.textBaseline="top";
  this.ctxtObj.fillText(this.timeNow,0,0);
  this.ctxtObj.strokeText(this.timeNow,0,0);
  this.ctxtObj.restore();
  /*
  fillText(String text,float x,float y,[float maxwidth]):填充字符串
  strokeText(String text,float x,float y,[float maxwidth]):绘制边框
  fOnt="bold 45px 宋体"
  textAlign:设置绘制字符串的水平对齐方式,start|end|right|center
  textBaseline:垂直对齐方式:top|hanging|middle|alphabetic|bottom
  */
 }
 doChangeToFrOnt= function(i,x){
  // 转换为画面值
  return (i +Math.ceil(x/4)) % 60;
 }
 doChangeToEnd = function(i,x){
  // 转换为后台值
  return (i +Math.ceil(x/4*3)) % 60;
 }
 doDrawTPanel = function(){
  // 画时钟面板
  var minsLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.3;
  var mineLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.32;
  var maxsLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.28;
  var maxeLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.34;
  var gap = Math.PI/30;
  futOnum= 5;
  this.ctxtObj.save();
   this.ctxtObj.fillStyle = "#369";
  this.ctxtObj.strokeStyle = "#369";
  for(var i =0;i<=59;i++){
   if(i % futOnum==0){
    sLen = maxsLen;
    eLen = maxeLen;
   }else{
    sLen = minsLen;
    eLen = mineLen;
   }
   this.ctxtObj.beginPath();
   this.ctxtObj.moveTo(Math.cos(i*gap)*sLen + this.centerPoint.x ,Math.sin(i*gap)*sLen + this.centerPoint.y);
   this.ctxtObj.lineTo(Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
   this.ctxtObj.stroke();
   this.ctxtObj.closePath();
   /*iDiff = this.doChangeToFront(i); // i => iDiff
   //iDiff2 = this.doChangeToEnd(iDiff,60); // iDiff => i
   this.ctxtObj.fOnt= "2px bold 微软雅黑";
   this.ctxtObj.textAlign="center"
   this.ctxtObj.textBaseline="middle"
   this.ctxtObj.fillText(iDiff,Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
   */

  }
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,Math.min( this.caObj.width, this.caObj.height)*0.5*0.01,0,360,false);
  this.ctxtObj.fill;
  this.ctxtObj.fill();
  this.ctxtObj.closePath();
  this.ctxtObj.restore();
 }
 doDrawYearPanel = function(startYear,nowYear,afterGap){
  // 画年份面板
  var sLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.53;
  var eLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.55;
  var labelLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.60;
  var allYears = nowYear-startYear+afterGap;
  var gap = Math.PI/Math.ceil(allYears*0.5);
  this.ctxtObj.save();
   this.ctxtObj.fillStyle = "#b4ffff";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,eLen+2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
   this.ctxtObj.fillStyle = "white";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,sLen-2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
  this.ctxtObj.restore();
   this.ctxtObj.fillStyle = "#369";
  this.ctxtObj.strokeStyle = "#369";
   for(var i =-2;i<=allYears-3;i++){
   this.ctxtObj.save();
   this.ctxtObj.beginPath();
   this.ctxtObj.moveTo(Math.cos(i*gap)*sLen + this.centerPoint.x ,Math.sin(i*gap)*sLen + this.centerPoint.y);
   this.ctxtObj.lineTo(Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
   this.ctxtObj.closePath();
   this.ctxtObj.stroke();
   iDiff = this.doChangeToFront(i,allYears) + startYear;
   this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
    this.ctxtObj.rotate(i*gap);
   this.ctxtObj.fOnt= "10px bold 微软雅黑";
   this.ctxtObj.textAlign="start";
   this.ctxtObj.textBaseline="bottom";
   this.ctxtObj.fillText(iDiff,sLen,0);
   this.ctxtObj.restore();
  }
 }
 doDrawMOnthPanel= function(){
  // 画年份面板
  var sLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.58;
  var eLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.6;
  var labelLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.70;
  var gap = Math.PI/6;
  this.ctxtObj.save();
   this.ctxtObj.fillStyle = "#fde08c";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,eLen+2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
   this.ctxtObj.fillStyle = "white";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,sLen-2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
  this.ctxtObj.restore();
   this.ctxtObj.fillStyle = "#369";
  this.ctxtObj.strokeStyle = "#369";
   for(var i =-2;i<=9;i++){
   this.ctxtObj.save();
   this.ctxtObj.beginPath();
   this.ctxtObj.moveTo(Math.cos(i*gap)*sLen + this.centerPoint.x ,Math.sin(i*gap)*sLen + this.centerPoint.y);
   this.ctxtObj.lineTo(Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
   this.ctxtObj.closePath();
   this.ctxtObj.stroke();
   iDiff = (this.doChangeToFront(i,12)) % 12+1;
   this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
    this.ctxtObj.rotate(i*gap);
   this.ctxtObj.fOnt= "20px bold 微软雅黑";
   this.ctxtObj.textAlign="start";
   this.ctxtObj.textBaseline="middle";
   this.ctxtObj.fillText((iDiff+'').PadLeft(2,0),eLen,0);
   this.ctxtObj.restore();
  }
 }
 doDrawDayPanel = function(dayCount,realAllDay){
  // 画年份面板
  var sLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.68;
  var eLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.7;
  var labelLen = Math.min( this.caObj.width, this.caObj.height)*0.5*0.80;
  var gap = Math.PI/Math.ceil(dayCount*0.5);
  this.ctxtObj.save();
  this.ctxtObj.fillStyle = "#e587e5";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,eLen+2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
  this.ctxtObj.fillStyle = "white";
  this.ctxtObj.beginPath();
  this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,sLen-2,0,360,false);
  this.ctxtObj.closePath();
  this.ctxtObj.fill();
  this.ctxtObj.restore();
  this.ctxtObj.fillStyle = "#369";
  this.ctxtObj.strokeStyle = "#369";
  for(var i =-2;i<=dayCount-2;i++){
   this.ctxtObj.save();
   this.ctxtObj.beginPath();
   this.ctxtObj.moveTo(Math.cos(i*gap)*sLen + this.centerPoint.x ,Math.sin(i*gap)*sLen + this.centerPoint.y);
   this.ctxtObj.lineTo(Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
   this.ctxtObj.closePath();
   this.ctxtObj.stroke();
   iDiff = (this.doChangeToFront(i,dayCount)) % (dayCount+1);
   if(iDiff<=realAllDay && iDiff!=0){
    this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
     this.ctxtObj.rotate(i*gap);
    this.ctxtObj.fOnt= "20px bold 微软雅黑";
    this.ctxtObj.textAlign="start";
    this.ctxtObj.textBaseline="middle";
    this.ctxtObj.fillText((iDiff+'').PadLeft(2,0),eLen,0);
   }
   this.ctxtObj.restore();
  }
 }
 drawYMDHMS = function(slen,elen,cslen,celen,main,sub,gapM,type,value){
  // 画日期时间针
  var date = new Date();
  var siM = main;
  var siS = sub;
  var gap = Math.PI/gapM;
  var sLen = Math.min( this.caObj.width, this.caObj.height)*0.5*slen;
  var eLen = Math.min( this.caObj.width, this.caObj.height)*0.5*elen;
  var csLen = Math.min( this.caObj.width, this.caObj.height)*0.5*cslen;
  var ceLen = Math.min( this.caObj.width, this.caObj.height)*0.5*celen;
  i = this.doChangeToEnd(siM+siS,gapM*2);
  ci = (i+gapM) % (gapM*2);
  this.ctxtObj.save();
  this.ctxtObj.beginPath();
  if(type=='y'){
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 6;
  }else if(type=='M'){
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 5;
  }else if(type=='d'){
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 4;
  }else if(type=='h'){
   this.ctxtObj.lineWidth = 3;
  }else if(type=='m'){
   this.ctxtObj.lineWidth = 2;
  }else if(type=='s'){
   this.ctxtObj.lineWidth = 1;
  }
  this.ctxtObj.moveTo(Math.cos(i*gap)*sLen + this.centerPoint.x ,Math.sin(i*gap)*sLen + this.centerPoint.y);
  this.ctxtObj.lineTo(Math.cos(i*gap)*eLen + this.centerPoint.x,Math.sin(i*gap)*eLen + this.centerPoint.y);
  this.ctxtObj.moveTo(Math.cos(ci*gap)*csLen + this.centerPoint.x ,Math.sin(ci*gap)*csLen + this.centerPoint.y);
  this.ctxtObj.lineTo(Math.cos(ci*gap)*ceLen + this.centerPoint.x,Math.sin(ci*gap)*ceLen + this.centerPoint.y);
  this.ctxtObj.stroke();
  this.ctxtObj.closePath();
  this.ctxtObj.restore();
  var cpi = ci*gap*360/Math.PI;
  if(type=='y'){
   this.ctxtObj.save();
    this.ctxtObj.fillStyle = "#00cece";
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 8;
   this.ctxtObj.beginPath();
   this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,ceLen,ci*gap-gap*0.5,ci*gap+gap*0.5,false);
   this.ctxtObj.stroke();
   this.ctxtObj.closePath();
   this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
    this.ctxtObj.rotate(i*gap);
   this.ctxtObj.fOnt= "20px bold 微软雅黑";
   this.ctxtObj.textAlign="start";
   this.ctxtObj.textBaseline="middle";
   this.ctxtObj.lineWidth = 2;
   this.ctxtObj.fillText(value + '年',eLen*1.03,0);
   this.ctxtObj.strokeText(value + '年',eLen*1.03,0);
   this.ctxtObj.restore();
  }else if(type=='M'){
   this.ctxtObj.save();
   this.ctxtObj.beginPath();
    this.ctxtObj.fillStyle = "#ce9b00";
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 7;
   this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,ceLen,ci*gap-gap*0.5,ci*gap+gap*0.5,false);
   this.ctxtObj.stroke();
   this.ctxtObj.closePath();
   this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
    this.ctxtObj.rotate(i*gap);
   this.ctxtObj.fOnt= "20px bold 微软雅黑";
   this.ctxtObj.textAlign="start";
   this.ctxtObj.textBaseline="middle";
   this.ctxtObj.lineWidth = 2;
   this.ctxtObj.fillText(value + '月',eLen*1.03,0);
   this.ctxtObj.strokeText(value + '月',eLen*1.03,0);
   this.ctxtObj.restore();
  }else if(type=='d'){
   this.ctxtObj.save();
   this.ctxtObj.beginPath();
    this.ctxtObj.fillStyle = "#bd01bd";
   this.ctxtObj.stroke;
   this.ctxtObj.lineWidth = 6;
   this.ctxtObj.arc(this.centerPoint.x,this.centerPoint.y,ceLen,ci*gap-gap*0.5,ci*gap+gap*0.5,false);
   this.ctxtObj.stroke();
   this.ctxtObj.closePath();
   this.ctxtObj.translate(this.centerPoint.x, this.centerPoint.y);
    this.ctxtObj.rotate(i*gap);
   this.ctxtObj.fOnt= "20px bold 微软雅黑";
   this.ctxtObj.textAlign="start";
   this.ctxtObj.textBaseline="middle";
   this.ctxtObj.lineWidth = 2;
   this.ctxtObj.fillText(value + '日',eLen*1.03,0);
   this.ctxtObj.strokeText(value + '日',eLen*1.03,0);
   this.ctxtObj.restore();
  }
  this.ctxtObj.restore();
 }
 animate = function(){
  var now = (+new Date);
  if (now - this.lastFpsUpdateTime > 60) {
   this.lastFpsUpdateTime = now;
   this.doDraw();
  }
  window.requestNextAnimationFrame(this.animate);
 }
 getCountDays = function (year,month) {
  var curDate = new Date();
  curDate.setFullYear(year);
  curDate.setMonth(month+1);
  curDate.setDate(0);
  return curDate.getDate();
 }
 getTimeNow = function(){
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  this.timeNow = date.getFullYear() 
    + seperator1 + (date.getMonth()+1+'').PadLeft(2,0)
    + seperator1 + (date.getDate()+'').PadLeft(2,0)
   + " " + (date.getHours()+'').PadLeft(2,0)
    + seperator2 + (date.getMinutes()+'').PadLeft(2,0)
   + seperator2 + (date.getSeconds()+'').PadLeft(2,0)
    + '.' +(date.getMilliseconds()+'').PadLeft(3,0);
 }
 // objects
}
var cwt = new calendarWithTime();
//=================================================
String.prototype.PadLeft = function(totalWidth, paddingChar)
{
 if ( paddingChar != null )
 {
 return this.PadHelper(totalWidth, paddingChar, false);
 } else {
 return this.PadHelper(totalWidth, ' ', false);
 }
}
String.prototype.PadRight = function(totalWidth, paddingChar)
{
 if ( paddingChar != null )
 {
 return this.PadHelper(totalWidth, paddingChar, true);
 } else {
 return this.PadHelper(totalWidth, ' ', true);
 }
}
String.prototype.PadHelper = function(totalWidth, paddingChar, isRightPadded)
{
 if ( this.length 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


推荐阅读
  • 浏览器作为我们日常不可或缺的软件工具,其背后的运作机制却鲜为人知。本文将深入探讨浏览器内核及其版本的演变历程,帮助读者更好地理解这一关键技术组件,揭示其内部运作的奥秘。 ... [详细]
  • Android开发:巧妙运用ViewStub写出类似Tab选项卡
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 在Android应用开发过程中,开发者经常遇到诸如CPU使用率过高、内存泄漏等问题。本文将介绍几种常用的命令及其应用场景,帮助开发者有效定位并解决问题。 ... [详细]
  • 为何Compose与Swarm之后仍有Kubernetes的诞生?
    探讨在已有Compose和Swarm的情况下,Kubernetes是如何以其独特的设计理念和技术优势脱颖而出,成为容器编排领域的领航者。 ... [详细]
  • 本文探讨了程序员这一职业的本质,认为他们是专注于问题解决的专业人士。文章深入分析了他们的日常工作状态、个人品质以及面对挑战时的态度,强调了编程不仅是一项技术活动,更是个人成长和精神修炼的过程。 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 本文介绍了SIP(Session Initiation Protocol,会话发起协议)的基本概念、功能、消息格式及其实现机制。SIP是一种在IP网络上用于建立、管理和终止多媒体通信会话的应用层协议。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • 在日常生活中,支付宝已成为不可或缺的支付工具之一。本文将详细介绍如何通过支付宝实现免费提现,帮助用户更好地管理个人财务,避免不必要的手续费支出。 ... [详细]
  • JUnit下的测试和suite
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 我的读书清单(持续更新)201705311.《一千零一夜》2006(四五年级)2.《中华上下五千年》2008(初一)3.《鲁滨孙漂流记》2008(初二)4.《钢铁是怎样炼成的》20 ... [详细]
  • 本文通过一个具体的案例,展示了如何使用 Python 爬虫技术从京东网站爬取手机的价格和参数。最近发布的 iPhone X 虽然价格昂贵,但不妨碍我们探索其他高性价比的国产手机。 ... [详细]
  • 无线鼠标应用:Remote Mouse
    Remote Mouse 是一款功能强大的无线鼠标软件,可将您的手机或平板设备变为遥控器,完美模拟键盘和鼠标操作,实现手机远程控制电脑。 ... [详细]
  • 本文介绍了Go语言中正则表达式的基本使用方法,并提供了一些实用的示例代码。 ... [详细]
  • Parallels Desktop for Mac 是一款功能强大的虚拟化软件,能够在不重启的情况下实现在同一台电脑上无缝切换和使用 Windows 和 macOS 系统中的各种应用程序。该软件不仅提供了高效稳定的性能,还支持多种高级功能,如拖放文件、共享剪贴板等,极大地提升了用户的生产力和使用体验。 ... [详细]
author-avatar
CL_LC的小屋花_344
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有