Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use HTML5 to realize a moving graphic clock

Shulou Source: shulou.com Published: 2022-06-03 08:06:17 10月27日 Update

This article introduces the knowledge of "how to use HTML5 to achieve a moving graphic clock". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Using HTML5 to make clock

The code is as follows:

Html5 clock

Var Clock = function (canvas, options) {

This.canvas = canvas

This.ctx = this.canvas.getContext ("2d")

This.options = options

}

Clock.prototype = {

Constructor: Clock

DrawCircle: function () {

Var ctx = this.ctx

Ctx.strokeStyle = "black"

Ctx.arc (this.canvas.width / 2, this.canvas.height / 2,50,0,2 * Math.PI, false)

Ctx.stroke ()

}

DrawNum: function () {

Var ctx = this.ctx

Var angle = Math.PI * 2 / 12

For (var I = 1; I 12) {

Hour = hour 12

}

Minute = date.getMinutes ()

Second = date.getSeconds ()

Var b = minute * Math.PI / 30

Var c = second * Math.PI / 30

Var a = hour * Math.PI / 6 + Math.PI / 6 * minute / 60

Var minuteAngle = Math.PI * 2 / 3600

Var secondAngle = Math.PI * 2 / 60

Var hourAngle = Math.PI * 2 / 12 / 3600

Ctx.beginPath ()

Ctx.save ()

Ctx.translate (that.canvas.width / 2, that.canvas.height / 2)

Ctx.arc (0,0,3,0,2 * Math.PI, false)

Ctx.fill ()

Ctx.closePath ()

Ctx.beginPath ()

A + = hourAngle

Ctx.rotate (a)

Ctx.fillRect (- 2,-22, 4, 30)

Ctx.closePath ()

Ctx.beginPath ()

B + = minuteAngle

Ctx.rotate (b-a)

Ctx.fillRect (- 1.5,26,3,35)

Ctx.closePath ()

Ctx.beginPath ()

C + = secondAngle

Ctx.rotate (c-b)

Ctx.fillRect (- 1,-30, 2, 40)

Ctx.closePath ()

Ctx.restore ()

}

RePaint: function () {

This.drawPointer ()

This.drawCircle ()

This.drawNum ()

}

Tik: function () {

Var that = this

Var ctx = this.ctx

This.rePaint ()

Window.timer = setInterval (function () {

Ctx.clearRect (0,0, that.canvas.width, that.canvas.height)

That.rePaint ()

}, 1000)

}

}

Var options

Var clock = new Clock (document.getElementById ("canvas"), options)

Clock.tik ()

After saving, use the browser to run, you can see the circular clock moving around, let's try it.

This is the end of the introduction to "how to use HTML5 to implement a moving graphical clock". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Clock graphics content more knowledge practical learning next code dilemma circle reality situation article case browser look tape website industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Docker Linux Shulou Information