This time I will bring you H5's CanvasHow to draw a music ring spectrogram. What are the precautions for H5Canvas to draw a music ring spectrogram. The following is a practical case. Let’s take a look.
Many of our friends at Station B must have seen the video of using the visual music player made by AE to play music. It looks very cool and exciting.
So today I will use Canvas to make a simple ring spectrogram.
Then~ ヾ(o・ω・)ノ Let’s get started!
1. First draw the effect of static
static effect
Drawing the static effect is very simple. We only need to start from one point and draw lines according to a certain angle. Then draw a circle to cover the line starting from the midpoint
<canvas id="wrap" height="800" width="800"></canvas><script> var wrap = document.getElementById("wrap"); var cxt = wrap.getContext("2d"); (function drawSpectrum() { cxt.clearRect(0, 0, wrap.width, wrap.height); //画线条 for (var i = 0; i < 360; i++) { var value = 8; cxt.beginPath(); cxt.lineWidth = 2; cxt.moveTo(300, 300); //R * cos (PI/180*一次旋转的角度数) ,-R * sin (PI/180*一次旋转的角度数) cxt.lineTo(Math.cos((i * 1) / 180 * Math.PI) * (200 + value) + 300, (- Math.sin((i * 1) / 180 * Math.PI) * (200 + value) + 300)); cxt.stroke(); } //画一个小圆,将线条覆盖 cxt.beginPath(); cxt.lineWidth = 1; cxt.arc(300, 300, 200, 0, 2 * Math.PI, false); cxt.fillStyle = "#fff"; cxt.stroke(); cxt.fill(); })();</script>
2. Call AudioAPI to draw the spectrum of music
Draw the spectrum of music
After the first step is completed , the second step is very simple, change the line length by calling AudioAPI to get audio changes.
Notice! ! ! The latest chrome browser may need to run on an http server~
You can refer to the article to draw the spectrogram of music (using Analyser node)
<input type="button" onclick="audio.play()" value="播放" /><input type="button" onclick="audio.pause()" value="暂停" /><canvas id="wrap" height="800" width="800"></canvas><script> var wrap = document.getElementById("wrap"); var cxt = wrap.getContext("2d"); //获取API var AudioContext = AudioContext || webkitAudioContext; var context = new AudioContext; //加载媒体 var audio = new Audio("demo.mp3"); //创建节点 var source = context.createMediaElementSource(audio); var analyser = context.createAnalyser(); //连接:source → analyser → destination source.connect(analyser); analyser.connect(context.destination); //创建数据 var output = new Uint8Array(360); (function drawSpectrum() { analyser.getByteFrequencyData(output);//获取频域数据 cxt.clearRect(0, 0, wrap.width, wrap.height); //画线条 for (var i = 0; i < 360; i++) { var value = output[i] / 8;//<===获取数据 cxt.beginPath(); cxt.lineWidth = 2; cxt.moveTo(300, 300); //R * cos (PI/180*一次旋转的角度数) ,-R * sin (PI/180*一次旋转的角度数) cxt.lineTo(Math.cos((i * 1) / 180 * Math.PI) * (200 + value) + 300, (- Math.sin((i * 1) / 180 * Math.PI) * (200 + value) + 300)); cxt.stroke(); } //画一个小圆,将线条覆盖 cxt.beginPath(); cxt.lineWidth = 1; cxt.arc(300, 300, 200, 0, 2 * Math.PI, false); cxt.fillStyle = "#fff"; cxt.stroke(); cxt.fill(); //请求下一帧 requestAnimationFrame(drawSpectrum); })();</script>
3. Ring left and right synchronous display
In fact, after step 2 of the internship, more than half of it has been completed, but careful friends will find that there is a lot of difference between the lines on the rightmost end point of the circle.
There are many ways to deal with it. We use one of the simple ways to deal with it, that is, to display it symmetrically.
<input type="button" onclick="audio.play()" value="播放" /><input type="button" onclick="audio.pause()" value="暂停" /><canvas id="wrap" height="550" width="800"></canvas><script> var wrap = document.getElementById("wrap"); var cxt = wrap.getContext("2d"); //获取API var AudioContext = AudioContext || webkitAudioContext; var context = new AudioContext; //加载媒体 var audio = new Audio("demo.mp3"); //创建节点 var source = context.createMediaElementSource(audio); var analyser = context.createAnalyser(); //连接:source → analyser → destination source.connect(analyser); analyser.connect(context.destination); //创建数据 var output = new Uint8Array(361); (function drawSpectrum() { analyser.getByteFrequencyData(output);//获取频域数据 cxt.clearRect(0, 0, wrap.width, wrap.height); //画线条 for (var i = 0; i < output.length; i++) { var value = output[i] / 10; //绘制左半边 cxt.beginPath(); cxt.lineWidth = 1; cxt.moveTo(300, 300); cxt.lineTo(Math.cos((i *0.5 + 90) / 180 * Math.PI) * (200 + value) + 300, (- Math.sin((i *0.5 + 90) / 180 * Math.PI) * (200 + value) + 300)); cxt.stroke(); //绘制右半边 cxt.beginPath(); cxt.lineWidth = 1; cxt.moveTo(300, 300); cxt.lineTo( (Math.sin((i *0.5) / 180 * Math.PI) * (200 + value) + 300),-Math.cos((i *0.5) / 180 * Math.PI) * (200 + value) + 300); cxt.stroke(); } //画一个小圆,将线条覆盖 cxt.beginPath(); cxt.lineWidth = 1; cxt.arc(300, 300, 200, 0, 2 * Math.PI, false); cxt.fillStyle = "#fff"; cxt.stroke(); cxt.fill(); //请求下一帧 requestAnimationFrame(drawSpectrum); })();</script>
4. Add a waveform graph
Finally let’s try to add a waveform graph in the circle
You can refer to the article to draw the waveform graph of music (using the Analyser node)
Example
<input type="button" onclick="audio.play()" value="播放" /><input type="button" onclick="audio.pause()" value="暂停" /><canvas id="wrap" height="550" width="800"></canvas><script> var wrap = document.getElementById("wrap"); var cxt = wrap.getContext("2d"); //获取API var AudioContext = AudioContext || webkitAudioContext; var context = new AudioContext; //加载媒体 var audio = new Audio("demo.mp3"); //创建节点 var source = context.createMediaElementSource(audio); var analyser = context.createAnalyser(); //连接:source → analyser → destination source.connect(analyser); analyser.connect(context.destination); //创建数据 var output = new Uint8Array(361); //计算出采样频率44100所需的缓冲区长度 var length = analyser.frequencyBinCount * 44100 / context.sampleRate | 0; //创建数据 var output2 = new Uint8Array(length); (function drawSpectrum() { analyser.getByteFrequencyData(output);//获取频域数据 cxt.clearRect(0, 0, wrap.width, wrap.height); //画线条 for (var i = 0; i < output.length; i++) { var value = output[i] / 10; //绘制左半边 cxt.beginPath(); cxt.lineWidth = 1; cxt.moveTo(300, 300); cxt.lineTo(Math.cos((i * 0.5 + 90) / 180 * Math.PI) * (200 + value) + 300, (- Math.sin((i * 0.5 + 90) / 180 * Math.PI) * (200 + value) + 300)); cxt.stroke(); //绘制右半边 cxt.beginPath(); cxt.lineWidth = 1; cxt.moveTo(300, 300); cxt.lineTo((Math.sin((i * 0.5) / 180 * Math.PI) * (200 + value) + 300), -Math.cos((i * 0.5) / 180 * Math.PI) * (200 + value) + 300); cxt.stroke(); } //画一个小圆,将线条覆盖 cxt.beginPath(); cxt.lineWidth = 1; cxt.arc(300, 300, 200, 0, 2 * Math.PI, false); cxt.fillStyle = "#fff"; cxt.stroke(); cxt.fill(); //将缓冲区的数据绘制到Canvas上 analyser.getByteTimeDomainData(output2); var height = 100, width = 400; cxt.beginPath(); for (var i = 0; i < width; i++) { cxt.lineTo(i + 100, 300 - (height / 2 * (output2[output2.length * i / width | 0] / 256 - 0.5))); } cxt.stroke(); //请求下一帧 requestAnimationFrame(drawSpectrum); })();</script>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to use canvas to make a useful graffiti drawing board
How to use s-xlsx to implement Excel File import and export (Part 2)
The above is the detailed content of How to use H5's Canvas to draw a music circular spectrogram. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

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
