javascript canvas encapsulated dynamic clock

  • 2021-08-28 19:02:30
  • OfStack

In this paper, we share the specific code of canvas encapsulated dynamic clock for your reference, the specific contents are as follows


<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>canvas Drawing dynamic clock </title>
 <style>
 #clock {
  display: block;
  margin: 30px auto;
 }
 </style>
</head>

<body>
 <canvas id="clock" width="200" height="200"></canvas>
 <script>

 function canvasClock(canvasClockObj) {
  return (function (canvasClockObj) {
  var ctx = canvasClockObj.dom.getContext('2d')
  let width = ctx.canvas.width
  let height = ctx.canvas.height
  let r = width > height ? height / 2 : width / 2

  //  Draw Background Panel 
  function drawBackground() {
   //  Draw outer ring 
   ctx.save() //  Save the current canvas state before starting each time, so as not to move the canvas and affect subsequent drawing 
   ctx.translate(r, r) //  Set the starting point to the center of the circle 
   ctx.beginPath() //  You must start every time you start drawing 1 Strip path 
   ctx.lineWidth = 10 //  Set the width of the line to be drawn 
   ctx.strokeStyle = canvasClockObj.outerRing
   ctx.arc(0, 0, r - ctx.lineWidth / 2, 0, 2 * Math.PI, false) //  Painting 1 A whole circle 
   ctx.stroke() //  Stroke a circle 
   ctx.strokeStyle = '#000'
   //  Drawing minutes   And   Hours 
   var minuteNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
   minuteNumbers.map(function (number, i) {
   var rad = 2 * Math.PI / 60 * i
   var x = Math.cos(rad) * (r - 17) //  Object for each minute x Axial coordinates 
   var y = Math.sin(rad) * (r - 17) //  Object for each minute y Axial coordinates 
   ctx.beginPath() //  You must start every time you start drawing 1 Strip path 
   ctx.fillStyle = '#ccc'
   ctx.arc(x, y, 2, 0, 2 * Math.PI, false)
   ctx.fill()
   })
   var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]
   hourNumbers.map(function (number, i) {
   var rad = 2 * Math.PI / 12 * i
   var x = Math.cos(rad) * (r - 30)
   var y = Math.sin(rad) * (r - 30)
   var x1 = Math.cos(rad) * (r - 17)
   var y1 = Math.sin(rad) * (r - 17)
   ctx.beginPath() //  You must start every time you start drawing 1 Strip path 
   ctx.fillStyle = canvasClockObj.hourColor ? canvasClockObj.hourColor :'#000' //  Settings   The color of the hour 
   ctx.textAlign = 'center' //  Center the text left and right 
   ctx.textBaseline = 'middle' //  Center the text up and down 
   ctx.font = 14 + 'px Arial'
   ctx.fillText(number, x, y)
   ctx.arc(x1, y1, 2, 0, 2 * Math.PI, false)
   ctx.fill()
   })
  }
  drawBackground()

  //  Draw the center of a circle 
  function drawDot() {
   ctx.beginPath()
   ctx.fillStyle = '#fff'
   ctx.lineWidth = 1
   ctx.arc(0, 0, 3, 2 * Math.PI, false)
   ctx.fill()
  }
  //  Draw an hour hand 
  function drawHour(hour, minute) {
   ctx.save()
   ctx.beginPath()
   var hrad = Math.PI / 12 * hour * 2
   var mrad = Math.PI / 12 / 60 * minute * 2
   ctx.rotate(hrad + mrad)
   ctx.lineWidth = 4
   ctx.moveTo(0, 10)
   ctx.lineTo(0, -r / 2.5)
   ctx.lineCap = 'round'
   ctx.stroke()
   ctx.restore()
  }
  //  Draw the minute hand 
  function drawMinute(minute, second) {
   ctx.save()
   ctx.beginPath()
   var mrad = Math.PI / 60 * minute * 2
   var srad = Math.PI / 60 / 60 * second * 2
   ctx.rotate(srad + mrad)
   ctx.lineWidth = 0.5
   ctx.lineJoin = 'round'
   ctx.fillStyle = '#000'
   ctx.moveTo(2, 10)
   ctx.lineTo(0, -r / 1.7)
   ctx.lineTo(-2, 10)
   ctx.lineTo(2, 10)
   ctx.lineCap = 'round'
   ctx.fill()
   ctx.restore()
  }
  //  Draw the second hand 
  function drawSecond(second) {
   ctx.save()
   ctx.beginPath()
   var srad = Math.PI / 30 * second
   ctx.rotate(srad)
   ctx.lineWidth = 0.5
   ctx.lineJoin = 'round'
   ctx.fillStyle = canvasClockObj.secondHand ? canvasClockObj.secondHand : '#f00'
   ctx.moveTo(2, 10)
   ctx.lineTo(0, -r / 1.2)
   ctx.lineTo(-2, 10)
   ctx.lineTo(2, 10)
   ctx.lineCap = 'round'
   ctx.fill()
   ctx.restore()
  }
  //  Move the pointer 
  function draw() {
   ctx.translate(-r, -r)
   ctx.clearRect(0, 0, width, height)
   var now = new Date()
   var hour = now.getHours()
   var minute = now.getMinutes()
   var second = now.getSeconds()
   drawBackground()        // Draw a disk background 
   drawHour(hour, minute);       // Draw an hour hand 
   drawMinute(minute,second);        // Draw the minute hand 
   drawSecond(second);        // Draw the second hand 
   drawDot();           // Drawing origin  
  }
  draw()
  setInterval(draw, 1000);
  })(canvasClockObj)
 }
 canvasClock({
  dom:document.getElementById('clock'), //  Required:  canvas Node 
  // outerRing:'purple', //  Outer ring color   Default value:  #000 
  // hourColor:'skyblue', //  The color of the hour   Default value  #000
  // secondHand:'yellow' //  The color of the second hand   Default value:  #f00
 })
 </script>
</body>

</html>

More JavaScript clock effects click to view: JavaScript clock effects topic


Related articles: