使用canvas绘制时钟并通过class面向对象

使用canvas绘制时钟并通过class面向对象

1、思路分析

钟表可分为静止的刻度和动态的指针两大部分
由于指针具有动态性,必然需要定时器实时刷新清空并重绘
但刻度部分如果一起清空并重绘会降低性能
因此可以使用两个重叠在一起的canvas画板来分别绘制两个部分

2、绘制表盘步骤

2.1、首先获取第一个面板的上下文:

var canvas1=document.getElementById("canvas1");
var ctx1=canvas1.getContext("2d");

2.2、由于表盘是由多个圆盘和两种不同粗细的刻度组成的,因此先定义两个函数
定义一个画实心圆的函数

function drawDisk(radius,fillStyle){
    ctx1.beginPath()
    ctx1.arc(150,150,radius,0,2*Math.PI)
    ctx1.fillStyle=fillStyle
    ctx1.fill()
    ctx1.closePath()
}

定义一个画刻度的函数

function drawScale(count){
  ctx1.beginPath()
  for(var i=0; i<count; i++){
    ctx1.moveTo(150,150)
    ctx1.arc(150,150,100,i*2*Math.PI/count,(i+1)*2*Math.PI/count)
  }
  ctx1.stroke()
  ctx1.closePath()
}

2.4、画一个灰色的底盘
  drawDisk(100,"#ccc")
2.5、画60个细的刻度
  drawScale(60)
2.6、画一个白色的圆盘盖住细刻度的多余部分
  drawDisk(90,"white")
  
2.7、画12个粗的刻度
  drawScale(12)
2.8、画一个白色的圆盘盖住粗刻度的多余部分
  drawDisk(80,"white")
2.9、画一个黑色的小圆点作为钟表的中心
  drawDisk(5,"black")

3、绘制表针步骤

3.1、首先获取第二个面板的上下文:   var canvas2=document.getElementById("canvas2");   var ctx2=canvas2.getContext("2d"); 3.2、定义一个画针的函数   function drawHand(radius,deg,lineWidth){      ctx2.beginPath()      ctx2.moveTo(150,150)      ctx2.arc(150,150,radius,deg,deg)      ctx2.lineWidth=lineWidth      ctx2.lineCap= "round"      ctx2.stroke()      ctx2.closePath()   }3.3、开启定时器实时刷新钟表指针   setInterval(function(){      var d=new Date();      var h=d.getHours();      var m=d.getMinutes();      var s=d.getSeconds();      //console.log(h,m,s)      ctx2.clearRect(0,0,300,300)      drawHand(50,h*30*Math.PI/180+m*0.5*Math.PI/180-0.5*Math.PI,5)  //时针      drawHand(68,m*6*Math.PI/180-0.5*Math.PI,3) //分针      drawHand(80,s*6*Math.PI/180-0.5*Math.PI,2) //秒针   },1000)

4、面向对象封装思路

a.整个钟表可视为一个对象b.钟表有4个实心圆、2种刻度和3根指针作为钟表的9个属性c.钟表有一个绘制的方法d.实心圆、刻度、指针又可视为一个对象,它们也应该有一个绘制方法

5、封装对象

5.1、封装实心圆对象   class Disk {      /**      * 实心圆构造器      * @param [Object] ctx 画布上下文对象      * @param [Number] radius 实心圆半径      * @param [String] fillStyle 实心圆颜色样式      */      constructor(ctx = null, radius = 10, fillStyle = "red") {         this.ctx = ctx         this.radius = radius         this.fillStyle = fillStyle      }      /**      * 实心圆绘制方法      * @param [Number] cX 圆心X坐标      * @param [Number] cY 圆心Y坐标      */      drawDisk(cX, cY) {         this.ctx.beginPath()         this.ctx.arc(cX, cY, this.radius, 0, 2 * Math.PI)         this.ctx.fillStyle = this.fillStyle         this.ctx.fill()         this.ctx.closePath()      }   }5.2、封装刻度对象   class Scale {      /**      * 刻度构造器      * @param [Object] ctx 画布上下文对象      * @param [Number] count 刻度的个数      */      constructor(ctx = null, count = 600) {         this.ctx = ctx         this.count = count      }      /**      * 刻度绘制方法      * @param [Number] cX 刻度所在圆的圆心X坐标      * @param [Number] cY 刻度所在圆的圆心Y坐标      * @param [Number] radius 刻度所在圆的半径      */      drawScale(cX, cY, radius) {         this.ctx.beginPath()         for (var i = 0; i < this.count; i++) {            this.ctx.moveTo(cX, cY)            this.ctx.arc(cX, cY, radius, i * 2 * Math.PI / this.count, (i + 1) * 2 * Math.PI / this.count)         }         this.ctx.stroke()         this.ctx.closePath()      }   }5.3、封装表盘指针对象   class Hand {      /**      * 钟表指针类构造器      * @param [Object] ctx 画布上下文对象      * @param [Number] length 指针长度      * @param [Number] lineWidth 指针宽度      * @param [String] strokeStyle 指针颜色样式      */      constructor(ctx = null, length = 20, lineWidth = 2, strokeStyle = "black") {         this.ctx = ctx         this.length = length         this.lineWidth = lineWidth         this.strokeStyle = strokeStyle      }      /**      * 指针绘制方法      * @param cX      * @param cY      * @param deg      */      drawHand(cX, cY, deg) {         this.ctx.beginPath()         this.ctx.moveTo(cX, cY)         this.ctx.arc(cX, cY, this.length, deg, deg)         this.ctx.lineWidth = this.lineWidth         this.ctx.strokeStyle = this.strokeStyle         this.ctx.lineCap = "round"         this.ctx.stroke()         this.ctx.closePath()      }   }5.4、封装钟表对象   class Clock {      /**      * 钟表类构造器      * @param [Object] ctx1 指针画布上下文对象      * @param [Object] ctx2 表盘画布上下文对象      * @param [Number] cX 钟表圆心X左标      * @param [Number] cY 钟表圆心Y左标      * @param [Number] radius 钟表半径      */      constructor(ctx1 = null, ctx2 = null, cX = 100, cY = 100, radius = 100) {         this.ctx1 = ctx1         this.ctx2 = ctx2         this.cX = cX         this.cY = cY         this.radius = radius         this.bigDisk = new Disk(this.ctx1, this.radius, "#ccc")         this.midDisk = new Disk(this.ctx1, this.radius * 0.9, "white")         this.smallDisk = new Disk(this.ctx1, this.radius * 0.8, "white")         this.dotDisk = new Disk(this.ctx2, 5, "red")         this.thinScale = new Scale(this.ctx1, 60)   /*60个细刻度*/         this.wideScale = new Scale(this.ctx1, 12)   /*12个粗刻度*/         this.hHand = new Hand(this.ctx2, this.radius * 0.5, 6)         this.mHand = new Hand(this.ctx2, this.radius * 0.68, 4)         this.sHand = new Hand(this.ctx2, this.radius * 0.8, 2, ‘red‘)      }      /**      * 钟表绘制方法      */      drawClock() {         this.bigDisk.drawDisk(this.cX, this.cY)                    /*画底盘*/         this.thinScale.drawScale(this.cX, this.cY, this.radius)              /*画60个细的刻度*/         this.midDisk.drawDisk(this.cX, this.cY)                    /*画一个圆盘盖住细刻度的多余部分*/         this.wideScale.drawScale(this.cX, this.cY, this.radius)              /*画12个粗的刻度*/         this.smallDisk.drawDisk(this.cX, this.cY)                  /*画一个圆盘盖住粗刻度的多余部分*/         /**         * 钟表指针重绘函数         */         let renewHand=()=> {            var d = new Date()            var h = d.getHours()            var m = d.getMinutes()            var s = d.getSeconds()            //console.log(h,m,s)            this.ctx2.clearRect(this.cX - this.radius, this.cY - this.radius, this.radius * 2, this.radius * 2)            this.hHand.drawHand(this.cX, this.cY, h * 30 * Math.PI / 180 + m * 0.5 * Math.PI / 180 - 0.5 * Math.PI)    //时针            this.mHand.drawHand(this.cX, this.cY, m * 6 * Math.PI / 180 - 0.5 * Math.PI)   //分针            this.sHand.drawHand(this.cX, this.cY, s * 6 * Math.PI / 180 - 0.5 * Math.PI)   //秒针            this.dotDisk.drawDisk(this.cX, this.cY)                   //画一个小圆点作为钟表的中心         }         renewHand()         setInterval(renewHand, 1000)      }   }

6、实例化钟表对象

var canvas3=document.getElementById("canvas3");var ctx3=canvas3.getContext("2d");var canvas4=document.getElementById("canvas4");var ctx4=canvas4.getContext("2d");var clock = new Clock(ctx3,ctx4,150,150,100);clock.drawClock();这种绘制钟表的方法可以根据需求定制钟表的一些属性然后再调用绘制方法   clock.bigDisk.fillStyle="yellow"   clock.smallDisk.fillStyle="#3f3"   clock.sHand.strokeStyle="pink"   clock.mHand.strokeStyle="#a0aea0"   clock.hHand.strokeStyle="red"

原文地址:https://www.cnblogs.com/chuanzi/p/9609473.html

时间: 2024-08-06 17:21:57

使用canvas绘制时钟并通过class面向对象的相关文章

小任务之Canvas绘制时钟

背景图的绘制(大圆.数字.小圆点) 掌握基础知识:圆的绘制(arc方法),关于圆的弧度的计算,数学中关于sin cos的用法 圆的弧度为2*Math.PI 12个数字分得弧度每个为2*Math.PI/12 那么rad=i*2*Math.PI/12 x=Math.cos(rad)*所需要的长度(也就是半径-差值) y=Math.sin(rad)*所需要的长度(也就是半径-差值) 同理可得60个点的绘制 60个数字分得弧度每个rad=i*2*Math.PI/60 x=Math.cos(rad)*所需

canvas绘制时钟

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas绘制钟表</title> <style type="text/css"> #canvas{ /*border: 2px solid red;*/ display: block; margin: 0 auto; } </style> </

使用canvas绘制时钟

window.onload=function(){     var clock=document.getElementById("clock").getContext('2d');//通过获取canvas元素获取2d上下文     var width=clock.canvas.width;//得到画布的宽度     var height=clock.canvas.height;//得到花布的高度     var r=width/2;//得到将要画的时钟的半径     var prop=

HTML5 Canvas 绘制时钟

网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas. <canvas id="clock" width="300" height="300"></canvas> 下面是JS实现: 1.取得上下文: var clock = document.getElementById('cl

用canvas绘制一个简易时钟

在见识了html5中canvas的强大,笔者准备制作一个简易时钟. 下面就是成果啦,制作之前我们先分析一下,绘制一个时钟需要做哪些准备. 一 . 1.首先这个时钟分为表盘,指针(时针,分针,秒针)和数字三部分. 2.表盘是个圆,这个简单. 3.绘制指针时,需要先获取到系统时间,然后找到时间和角度的关系. 4.然后利用画圆函数,把起始和终点设为同一角度,即可画出射线(指针).  二. 接下来,我们再分析一下,绘制时钟需要用到的函数. 1.arc(x, y, r, start, stop) x, y

应用canvas绘制动态时钟--每秒自动动态更新时间

使用canvas绘制时钟 下文是部分代码,完整代码参照:https://github.com/lemoncool/canvas-clock,可直接下载. 首先看一下效果图:每隔一秒会动态更新时间 一.前期准备 1. HTML中准备一个容器存放画布,并为其设置width,height. <div> <canvas id="clock" height="200px" width="200px"></canvas>

绘制时钟

  <!doctype html>   <html lang="en">   <head>   <meta charset="UTF-8">   <title>Canvas绘制时钟Demo</title>   <style>   #myCanvas{   border: 1px solid;   }   </style>   </head>   <bod

js绘制时钟

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Canvas绘制时钟Demo</title> <style> #myCanvas{ border: 1px solid; } </style> </head> <body> <canvas id=&quo

封装 用canvas绘制直线的函数--面向对象

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用面向对象的思想 封装 在canvas绘制直线的函数</title> 6 </head> 7 <body> 8 <canvas id="cv"></canvas> 9 &