search
HomeWeb Front-endJS TutorialJavaScript implements clock ticking sound effect

下面一段代码给大家分享js实现时钟滴答声功能,具体代码如下所示:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
 <meta name="keywords" content="clock">
 <meta name="description" content="This is a clock">
 <title>Clock</title>
 </head>
 <body>
 <audio id="ticktock">
 <source = src="ticktock.mp3" type="audio/mp3">
 </audio>
 <script type="text/javascript">
 /*
 年(y)可以用 1-4 个占位符
 月(M)、日(d)、时(H,24时)、时(h,12时)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符
 毫秒(S)只能用 1 个占位符(是 1-3 位数字) 
 AM或PM只能用 1 个占位符(是 2 位英文)
 上午或下午(T)只能用 1 个占位符(是 2 位中文)
 星期(E)可以用 1-3 个占位符
 季度(q)只能用 1 个占位符(是 1 位数字)
 */
 Date.prototype.format = function(fmt) {
 var map = {
  "M+" : this.getMonth() + 1, //月
  "d+" : this.getDate(), //日
  "H+" : this.getHours(), //24时
  /*
  上午12时只代表当天上午的12时,下午的12时代表当天下午的12时,
  0时代表第二天的开始,即前面一天12时已过0时开始计算新一天的时间,
  虽然说时间上跟前面那一天下午12时重合,但日期已经发生更改,所以不能说0时就是12时
  */
  "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12,//12时
  "m+" : this.getMinutes(), //分
  "s+" : this.getSeconds(), //秒
  "S" : this.getMilliseconds(), //毫秒
  "t" : this.getHours() < 12 ? "AM" : "PM",
  "T" : this.getHours() < 12 ? "上午" : "下午",
 };
 var week = {
  "0" : "日",
  "1" : "一",
  "2" : "二",
  "3" : "三",
  "4" : "四",
  "5" : "五",
  "6" : "六",
 }
 var quarter = {
  "0" : "一",
  "1" : "二",
  "2" : "三",
  "3" : "四",
 }
 if(/(y+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
 }
 if(/(E+)/.test(fmt)) {
  var weekPreStr;
  switch(RegExp.$1.length) {
  case 1:
  weekPreStr = "";
  break;
  case 2:
  weekPreStr = "周";
  break;
  default:
  weekPreStr = "星期";
  break;
  }
  fmt = fmt.replace(RegExp.$1, weekPreStr + week[this.getDay()]);
 }
 if(/(q)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, quarter[Math.floor(this.getMonth() / 3)]);
 }
 for(var key in map) {
  if(new RegExp("(" + key + ")").test(fmt)) {
  fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? map[key] : ("00" + map[key]).substr((map[key]+"").length));
  }
 }
 return fmt;
 }
 </script>
 <script>
 var canvas = document.createElement("canvas");
 document.body.appendChild(canvas);
 var ctx = canvas.getContext("2d");
 var halfPI = Math.PI / 2;
 var doublePI = Math.PI * 2;
 //阴影级别
 var shadowBlur = 10;
 //阴影宽度
 var shadowWidth = 8;
 //阴影在X方向上的偏移
 var shadowOffsetX = 5;
 //阴影在Y方向上的便宜
 var shadowOffsetY = 5;
 //深色阴影
 var shadowDarkColor = "rgba(0,0,0,0.8)";
 //浅色阴影
 var shadowLightColor = "rgba(0,0,0,0.1)";
 //画布中心到边缘的内切圆半径
 var canvasRadius = 250;
 canvas.width = canvasRadius * 2;
 canvas.height = canvasRadius * 2;
 //获取画布中心的坐标
 var cx = canvasRadius;
 var cy = canvasRadius;
 //时钟外圈的贝塞尔花纹个数
 var bezierPatternCount = 36;
 //时钟外圈的贝塞尔花纹波峰处半径
 var bezierPeakRadius = canvasRadius - 10;
 //时钟外圈的贝塞尔花纹一半的角度
 var bezierHalfSpan = doublePI / bezierPatternCount / 2;
 //时钟外圈的贝塞尔花纹底部半径
 var bezierRadius = bezierPeakRadius - 20;
 //时钟外圈的贝塞尔花纹颜色
 var bezierPatternColor = "Plum";
 //时钟外圈半径
 var clockBorderRadius = bezierRadius - 10;
 //时钟外圈宽度
 var clockBorderWidth = 10;
 //时钟外圈颜色
 var clockBorderColor = "Aqua";
 //时钟外圈阴影半径
 var clockBorderShadowRadius = clockBorderRadius - shadowWidth + 1;
 //时钟整数时间刻度线宽
 var clockScaleWidth = 2;
 //时钟整数时间刻度外半径
 var clockScaleOuterRadius = clockBorderRadius - shadowWidth;
 //时钟整数时间刻度内半径
 var clockScaleInnerRadius = clockScaleOuterRadius - 20;
 //时钟刻度颜色
 var clockScaleColor = "Black";
 //时钟非整数时间处半径
 var clockScaleMiddleRadius = clockScaleOuterRadius - 10;
 //时钟数字半径
 var clockNumRadius = clockBorderShadowRadius - 40;
 //时钟数字字体
 var clockNumFont = "25px Arial";
 //时钟数字颜色
 var clockNumColor = "black";
 //数字日期距中心的垂直距离
 var digitalDateMarginCenter = 50;
 //数字日期颜色
 var digitalDateColor = "Black";
 //数字日期字体
 var digitalDateFont = "bold 18px Arial";
 //数字时间距中心的垂直距离
 var digitalTimeMarginCenter = 100;
 //数字时间颜色
 var digitalTimeColor = "white";
 //数字时间背景颜色
 var digitalTimeBgColor = "DarkSlateBlue";
 //数字时间字体
 var digitalTimeFont = "bold 25px Arial";
 //数字时间高度的一半
 var digitalTimeHeight = 40;
 //数字时间分隔线宽度
 var digitalTimeSpanLineWidth = 2;
 //时钟中心点内圆的半径
 var clockCenterInnerDotRadius = 7;
 //时钟中心点内圆的颜色
 var clockCenterInnerDotColor = "FireBrick";
 //时钟中心点外圆的半径
 var clockCenterOuterDotRadius = 10;
 //时钟中心点外圆的颜色
 var clockCenterOuterDotColor = "Maroon";
 //时针线宽
 var clockNeedleWidth = 5;
 //时针半径
 var clockHourNeedleRadius = clockBorderShadowRadius - 120;
 //时针颜色
 var clockHourNeedleColor = "DarkGreen";
 //分针半径
 var clockMinuteNeedleRadius = clockBorderShadowRadius - 80;
 //分针颜色
 var clockMinuteNeedleColor = "DarkSlateGray";
 //秒针半径
 var clockSecondNeedleRadius = clockBorderShadowRadius - 40;
 //秒针尾部半径
 var clockSecondNeedleBottomRadius = -20;
 //秒针颜色
 var clockSecondNeedleColor = "FireBrick";
 //画圆环
 function strokeCircle(cx, cy, r) {
 ctx.beginPath();
 ctx.arc(cx, cy, r, 0, doublePI);
 ctx.stroke();
 }
 //画圆
 function fillCircle(cx, cy, r) {
 ctx.beginPath();
 ctx.arc(cx, cy, r, 0, doublePI);
 ctx.fill();
 }
 //绘制线条
 function strokeLine(x1, y1, x2, y2) {
 ctx.beginPath();
 ctx.moveTo(x1, y1);
 ctx.lineTo(x2, y2);
 ctx.stroke();
 }
 //根据角度和半径计算圆上相应位置的坐标(最右侧为起始角度,顺时针方向为正)
 function circlePos(cx, cy, theta, radius) {
 var pos = {
  x: cx + radius * Math.cos(theta),
  y: cy + radius * Math.sin(theta),
 };
 return pos;
 }
 //在圆环上绘制刻度线
 function strokeCircleLine(cx, cy, theta, r1, r2) {
 var pos1 = circlePos(cx, cy, theta, r1);
 var pos2 = circlePos(cx, cy, theta, r2);
 strokeLine(pos1.x, pos1.y, pos2.x, pos2.y);
 }
 //设置默认阴影
 function setShadow(type) {
 ctx.lineWidth = shadowWidth;
 ctx.shadowBlur = shadowBlur;
 ctx.shadowOffsetX = shadowOffsetX;
 ctx.shadowOffsetY = shadowOffsetY;
 if(type === 1) {
  ctx.shadowColor = shadowLightColor;
 } else {
  ctx.shadowColor = shadowDarkColor;
 }
 }
 //取消阴影
 function clearShadow() {
 ctx.shadowColor = "rgba(0,0,0,0)";
 ctx.shadowBlur = 0;
 ctx.shadowOffsetX = 0;
 ctx.shadowOffsetY = 0;
 }
 //绘制时钟外圈的贝塞尔花纹
 function renderBezierPattern() {
 ctx.fillStyle = bezierPatternColor;
 ctx.beginPath();
 var theta = 0;
 //由于circlePos是顺时针方向正, 故圈也是顺时针方向
 var beginPos = circlePos(cx, cy, theta, bezierRadius);
 ctx.moveTo(beginPos.x, beginPos.y);
 while(theta < doublePI) {
  //贝塞尔曲线控制点
  var controlTheta = theta + bezierHalfSpan;
  var controlPos = circlePos(cx, cy, controlTheta, bezierPeakRadius);
  //贝塞尔曲线终止点
  var endTheta = controlTheta + bezierHalfSpan;
  var endPos = circlePos(cx, cy, endTheta, bezierRadius);
  ctx.quadraticCurveTo(controlPos.x, controlPos.y, endPos.x, endPos.y);
  theta = endTheta;
 }
 //绘制圆counterclockwise=false, 即默认是顺时针方向
 ctx.arc(cx, cy, clockBorderRadius, 0, doublePI, true);
 //注意: 两个相反方向的路径内部为填充范围
 ctx.fill();
 }
 //绘制时钟边框
 function renderClockBorder() {
 //画外框
 ctx.strokeStyle = clockBorderColor;
 ctx.lineWidth = clockBorderWidth;
 strokeCircle(cx, cy, clockBorderRadius);
 //画外框的内阴影
 ctx.strokeStyle = shadowLightColor;
 setShadow(1);
 strokeCircle(cx, cy, clockBorderShadowRadius);
 clearShadow();
 }
 //绘制时钟圆周上的数字和刻度部分
 function renderClockNums() {
 ctx.textAlign = "center";
 ctx.textBaseline = "middle";
 ctx.font = clockNumFont;
 var span = doublePI / 60;
 for(var i = 1, radian = -halfPI + span; i <= 60; i++, radian += span) {
  if(i % 5 == 0) {
  //绘制刻度
  ctx.strokeStyle = clockScaleColor;
  ctx.lineWidth = clockScaleWidth;
  strokeCircleLine(cx, cy, radian, clockScaleInnerRadius, clockScaleOuterRadius);
  //绘制数字
  var pos = circlePos(cx, cy, radian, clockNumRadius);
  var num = i / 5;
  ctx.fillStyle = clockNumColor;
  ctx.fillText(num, pos.x, pos.y);
  } else {
  ctx.strokeStyle = clockScaleColor;
  ctx.lineWidth = clockScaleWidth;
  strokeCircleLine(cx, cy, radian, clockScaleMiddleRadius, clockScaleOuterRadius);
  }
 }
 }
 //绘制数字时钟
 function renderDigital(date) {
 //绘制日期
 ctx.textAlign = "center";
 ctx.textBaseline = "middle";
 ctx.font = digitalDateFont;
 ctx.fillStyle = digitalDateColor;
 var text = date.format("yyyy年MM月dd日 EEE");
 ctx.fillText(text, cx, cy + digitalDateMarginCenter);
 //绘制时间
 ctx.font = digitalTimeFont;
 text = date.format(" HH mm ss ");
 ctx.fillStyle = digitalTimeBgColor;
 var textWidth = ctx.measureText(text).width;
 var textBgX = cx - textWidth / 2;
 var textBgY = cy + digitalTimeMarginCenter - digitalTimeHeight / 2;
 ctx.fillRect(textBgX, textBgY, textWidth, digitalTimeHeight);
 ctx.fillStyle = digitalTimeColor;
 ctx.fillText(text, cx, cy + digitalTimeMarginCenter);
 //绘制事件中间的分隔线
 ctx.lineWidth = digitalTimeSpanLineWidth;
 ctx.strokeStyle = digitalTimeColor;
 var textSpan = textWidth / 6;
 var leftLineX = cx - textSpan;
 strokeLine(leftLineX, textBgY, leftLineX, textBgY + digitalTimeHeight);
 var rightLineX = cx + textSpan;
 strokeLine(rightLineX, textBgY, rightLineX, textBgY + digitalTimeHeight);
 }
 //绘制时钟中心最下方红点
 function renderClockCenterOuterDot() {
 ctx.fillStyle = clockCenterOuterDotColor;
 fillCircle(cx, cy, clockCenterOuterDotRadius);
 }
 //绘制时钟中心最上方红点
 function renderClockCenterInnerDot() {
 ctx.fillStyle = clockCenterInnerDotColor;
 fillCircle(cx, cy, clockCenterInnerDotRadius);
 }
 //绘制时钟指针
 function renderClockNeedle(date) {
 var hourRadian = date.getHours() % 12 / 12 * doublePI - halfPI;
 var minuteRadian = date.getMinutes() / 60 * doublePI - halfPI;
 var secondRadian = date.getSeconds() / 60 * doublePI - halfPI;
 setShadow();
 ctx.lineCap = "round";
 ctx.lineWidth = clockNeedleWidth;
 ctx.strokeStyle = clockHourNeedleColor;
 strokeCircleLine(cx, cy, hourRadian, 0, clockHourNeedleRadius);
 ctx.strokeStyle = clockMinuteNeedleColor;
 strokeCircleLine(cx, cy, minuteRadian, 0, clockMinuteNeedleRadius);
 ctx.strokeStyle = clockSecondNeedleColor;
 strokeCircleLine(cx, cy, secondRadian, clockSecondNeedleBottomRadius, clockSecondNeedleRadius);
 ctx.lineCap = "square";
 clearShadow();
 }
 function render(date) {
 ctx.clearRect(0, 0, canvas.width, canvas.height);
 renderBezierPattern();
 renderClockBorder();
 renderClockNums();
 renderDigital(date);
 renderClockCenterOuterDot();
 renderClockNeedle(date);
 renderClockCenterInnerDot();
 }
 var lastTime = 0;
 var audio = document.getElementById("ticktock");
 function loop() {
 var date = new Date();
 var currentTime = date.getTime();
 if(currentTime - lastTime >= 1000) {
  lastTime = currentTime;
  //注意:这里设0非常关键,否则虽然会循环播放,但会从上一次暂停的地方开始播放,造成延迟
  audio.currentTime = 0;
  audio.play();
  render(date);
 }
 requestAnimationFrame(loop);
 }
 loop();
 </script>
 </body>
</html>

JavaScript implements clock ticking sound effect

以上所述是小编给大家介绍的JavaScript implements clock ticking sound effect,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

更多JavaScript implements clock ticking sound effect相关文章请关注PHP中文网!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software