网页H5制作节拍器

本文介绍了一个使用前端代码实现的简单节拍器程序。通过HTML和JavaScript,该程序能够模拟节拍器的功能,固定60拍,点击按钮即可开始播放,并伴有图形和声音的变化。代码简洁且跨平台,适合音乐爱好者作为练习节拍的小工具。
摘要由CSDN通过智能技术生成

简介

节拍器像敲木鱼一样,有规律和节奏哒哒哒响,这个程序可以完美做到。

所有软件都能实现节拍器,网页无疑是跨平台的最佳方案。

前端代码

绘制节拍器界面,为了代码的简洁,只绘制了一个图,播放速度固定。

<!DOCTYPE HTML>
<html>
	<head>
		<style>
		section.tool{
			background: rgb(213, 253, 213);
			margin-top: 1vh;
			margin-left: 10%;
			width:40%;
			height:50%;
		}
		
		</style>
		</head>
	<body>
		<section class = "tool">
			<div class = 'tool1'>
				<div class = 'title'><h1>节拍器</h1></div>
				<div class="main">
					<div class = 'metronome'>		
						<img src="http://www.aljita.cn/static/head/head_58.png" id = 'image_me'/> 
					</div>
					<hr>
					<div class = 'metronome_play'>
						<input type = 'image'  src = 'http://www.aljita.cn/static/img/play.png' style="display:inline-block" onclick="play(this);" >&nbsp;&nbsp;&nbsp;&nbsp;点击开始</input>
					</div>
					<hr>
					
				</div>
			</div>
			
	</body>

逻辑代码

播放声音和动画,为了代码简洁,固定60拍。

	<script>
		let audio_context = null;
		
		audio_context_play = function(frequency, duration) 
		{				 		
			ctx = audio_context;
			let osc = ctx.createOscillator();	  
			let g = ctx.createGain();				
			osc.connect(g);					
			osc.type = 'sine';				
			osc.frequency.value =frequency;	 
			g.connect(ctx.destination);		 
			g.gain.value = 0;					
			g.gain.linearRampToValueAtTime(0.6,ctx.currentTime + 0.05);	
			g.gain.exponentialRampToValueAtTime(0.01 , ctx.currentTime + duration/2);	
			osc.start();						
			osc.stop(ctx.currentTime + duration/2);	
			
		}

		play_metronome = function(pos)
		{
			let play_heavy = false;
			let id = parseInt(Math.random()*105);
			let img_src = 'http://www.aljita.cn/static/head/head_'+ id       + '.png';
			if (pos % 4 == 1)
				play_heavy = true;
				
			if (play_heavy)
				audio_context_play(880, 0.28);
			else
				audio_context_play(440, 0.28);
			document.getElementById('image_me').src = img_src;
			
		}

		player_forever = function(pos)
		{
			pos += 1;
			play_metronome(pos);
			setTimeout(() => 
			{
				player_forever(pos);
			}, 1000);
			
		}
		play=function(obj)
		{
			obj.disabled = true;
			audio_context = new AudioContext();
			player_forever(0);
		};
		
	</script>

演示程序

我平时练习节拍的一个小道具。

安纶吉他:http://www.aljita.cn/tool

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值