Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize scalable clock with HTML5

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve a scalable clock in HTML5". In daily operation, I believe many people have doubts about how to achieve a scalable clock in HTML5. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the question of "how to achieve a scalable clock in HTML5". Next, please follow the editor to study!

The specific code is as follows:

Canvas_time div {text-align: center; margin-top: 250px;} # clock {border: 1px solid # cccccc;} var dom = document.getElementById ('clock'); var ctx = dom.getContext (' 2d'); var width = ctx.canvas.width;var height = ctx.canvas.height;var r = width/2; var rem = width/200;function drawBackground () {ctx.save () / / Store current environment variables Ctx.translate (r _ r); / / reset coordinates to r _ r ctx.beginPath (); / / start a path ctx.lineWidth = 10 minutes; / / set lineweight 10 Ctx.arc (0,0, r-ctx.lineWidth / 2,0,2 * Math.PI, false); ctx.stroke (); var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; / / define the array ctx.font = 18, 12, 1, 2; ctx.textAlign = "center"; ctx.textBaseline = "middle" HourNumbers.forEach (function (number, I) {var rad = 2 * Math.PI / 12 * i; var x = Math.cos (rad) * (r-30*rem); var y = Math.sin (rad) * (r-30*rem); ctx.fillText (number, x, y);}); for (var I = 0; I < 60) ) {var rad = 2 * Math.PI / 60 * i; var x = Math.cos (rad) * (r-18*rem); var y = Math.sin (rad) * (r-18*rem); ctx.beginPath (); if (I% 5 = 0) {ctx.arc (x, y, 2*rem, 0,2 * Math.PI, false) Ctx.fillStyle = "# 000";} else {ctx.arc (x, y, 2*rem, 0,2 * Math.PI, false); ctx.fillStyle = "# ccc";} ctx.fill ();}} function drawHour (hour, minute) {ctx.save (); ctx.beginPath () Var rad = 2 * Math.PI / 12 * hour; var mrad = 2 * Math.PI / 12 / 60 * minute; ctx.rotate (rad + mrad); ctx.lineWidth = 6 years remand; ctx.lineCap = "round"; ctx.moveTo (0, 10*rem); ctx.lineTo (0,-r / 2); ctx.stroke (); ctx.restore () } function drawMinute (minute) {ctx.save (); ctx.beginPath (); var rad = 2 * Math.PI / 60 * minute; ctx.rotate (rad); ctx.lineWidth = 334 remand; ctx.lineCap = "round"; ctx.moveTo (0, 10*rem); ctx.lineTo (0,-r + 30*rem); ctx.stroke () Ctx.restore ();} function drawSecond (second) {ctx.save (); ctx.beginPath (); ctx.fillStyle = 'red'; var rad = 2 * Math.PI / 60 * second; ctx.rotate (rad); ctx.moveTo (- 2*rem, 20*rem); ctx.lineTo (2*rem, 20*rem) Ctx.lineTo (1,-r + 16*rem); ctx.lineTo (- 1,-r + 16*rem); ctx.fill (); ctx.restore ();} function drawDot () {ctx.beginPath (); ctx.fillStyle ='# fff'; ctx.arc (0,0, 3*rem, 0,2 * Math.PI, false); ctx.fill () } function draw01 () {ctx.clearRect (0,0, width, height); var now = new Date (); var hour = now.getHours (); var minute = now.getMinutes (); var second = now.getSeconds (); drawBackground (); drawHour (hour, minute); drawMinute (minute); drawSecond (second); drawDot () Ctx.restore ();} draw01 (); setInterval (draw01, 1000); at this point, the study on "how HTML5 implements a scalable clock" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report