How do I fix an issue with audio visualizer spectrum display on height decrease?How do I retrieve an HTML element's actual width and height?How to get image size (height & width) using JavaScript?How can I display a JavaScript object?HTML5 Audio Visualizer?Help with audio visualizerCan click event handlers in a Flash object react to click events delegated by JavaScript from another DOM element?Unsuccessful attempt in adapting audio-visualizer into codepen…need adviseAudio Visualizer with CanvasWhat kind of audio spectrum analyser is this?How to create a JavaScript Audio Visualizer?

What is the backup for a glass cockpit, if a plane loses power to the displays/controls?

How to safely discharge oneself

How does the probability of events change if an event does not occur

How to choose the correct exposure for flower photography?

Why didn't Daenerys' advisers suggest assassinating Cersei?

pwaS eht tirsf dna tasl setterl fo hace dorw

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

Print characters from list with a For-loop

If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?

Can I have a delimited macro with a literal # in the parameter text?

Addressing an email

Can the word crowd refer to just 10 people?

What should I wear to go and sign an employment contract?

Why does Taylor’s series “work”?

What city and town structures are important in a low fantasy medieval world?

DISTINCT NULL return single NULL in SQL Server

Why were early aviators' trousers flared at the thigh?

Why is python script running in background consuming 100 % CPU?

Have the writers and actors of Game Of Thrones responded to its poor reception?

Does the Aboleth have expertise in history and perception?

On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?

Can 2 light bulbs of 120V in series be used on 230V AC?

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario

Bash Read: Reading comma separated list, last element is missed



How do I fix an issue with audio visualizer spectrum display on height decrease?


How do I retrieve an HTML element's actual width and height?How to get image size (height & width) using JavaScript?How can I display a JavaScript object?HTML5 Audio Visualizer?Help with audio visualizerCan click event handlers in a Flash object react to click events delegated by JavaScript from another DOM element?Unsuccessful attempt in adapting audio-visualizer into codepen…need adviseAudio Visualizer with CanvasWhat kind of audio spectrum analyser is this?How to create a JavaScript Audio Visualizer?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to build a music sharing platform with an audio spectrum visualizer display in a canvas, everything works well when the canvas height is set to 350px but when reduced, the bars seem to be flying away, I tried to adjust everything I can from the javascript (main.js) but no progress, please help!



My HTML code with the adjusted canvas dimension is below:



<link rel="stylesheet" type="text/css" href="style.css">

<canvas id='canvas' width="450px" height="150px"></canvas>
<br>
<br>
<audio src="assets/sample.mp3" id="audio" controls> </audio>
<script src="main.js"></script>



This is the main.js javascript file



window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

window.onload = function()
var audio = document.getElementById('audio');
var ctx = new AudioContext();
var analyser = ctx.createAnalyser();
var audioSrc = ctx.createMediaElementSource(audio);
// we have to connect the MediaElementSource with the analyser
audioSrc.connect(analyser);
analyser.connect(ctx.destination);
// we could configure the analyser: e.g. analyser.fftSize (for further infos read the spec)
// analyser.fftSize = 64;
// frequencyBinCount tells you how many values you'll receive from the analyser
var frequencyData = new Uint8Array(analyser.frequencyBinCount);

// we're ready to receive some data!
var canvas = document.getElementById('canvas'),
cwidth = canvas.width,
cheight = canvas.height - 2,
meterWidth = 10, //width of the meters in the spectrum
gap = 2, //gap between meters
capHeight = 2,
capStyle = '#fff',
meterNum = 800 / (10 + 2), //count of the meters
capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
ctx = canvas.getContext('2d'),
gradient = ctx.createLinearGradient(0, 0, 0, 300);
gradient.addColorStop(1, '#0f0');
gradient.addColorStop(0.5, '#ff0');
gradient.addColorStop(0, '#f00');
// loop
function renderFrame()
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var step = Math.round(array.length / meterNum); //sample limited data from the total array
ctx.clearRect(0, 0, cwidth, cheight);
for (var i = 0; i < meterNum; i++)
var value = array[i * step];
if (capYPositionArray.length < Math.round(meterNum))
capYPositionArray.push(value);
;
ctx.fillStyle = capStyle;
//draw the cap, with transition effect
if (value < capYPositionArray[i])
ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);
else
ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);
capYPositionArray[i] = value;
;
ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter

requestAnimationFrame(renderFrame);

renderFrame();
audio.play();
;



I just need help on making sure the bars don't fly away when canvas is resized, thanks!










share|improve this question






























    0















    I am trying to build a music sharing platform with an audio spectrum visualizer display in a canvas, everything works well when the canvas height is set to 350px but when reduced, the bars seem to be flying away, I tried to adjust everything I can from the javascript (main.js) but no progress, please help!



    My HTML code with the adjusted canvas dimension is below:



    <link rel="stylesheet" type="text/css" href="style.css">

    <canvas id='canvas' width="450px" height="150px"></canvas>
    <br>
    <br>
    <audio src="assets/sample.mp3" id="audio" controls> </audio>
    <script src="main.js"></script>



    This is the main.js javascript file



    window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

    window.onload = function()
    var audio = document.getElementById('audio');
    var ctx = new AudioContext();
    var analyser = ctx.createAnalyser();
    var audioSrc = ctx.createMediaElementSource(audio);
    // we have to connect the MediaElementSource with the analyser
    audioSrc.connect(analyser);
    analyser.connect(ctx.destination);
    // we could configure the analyser: e.g. analyser.fftSize (for further infos read the spec)
    // analyser.fftSize = 64;
    // frequencyBinCount tells you how many values you'll receive from the analyser
    var frequencyData = new Uint8Array(analyser.frequencyBinCount);

    // we're ready to receive some data!
    var canvas = document.getElementById('canvas'),
    cwidth = canvas.width,
    cheight = canvas.height - 2,
    meterWidth = 10, //width of the meters in the spectrum
    gap = 2, //gap between meters
    capHeight = 2,
    capStyle = '#fff',
    meterNum = 800 / (10 + 2), //count of the meters
    capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
    ctx = canvas.getContext('2d'),
    gradient = ctx.createLinearGradient(0, 0, 0, 300);
    gradient.addColorStop(1, '#0f0');
    gradient.addColorStop(0.5, '#ff0');
    gradient.addColorStop(0, '#f00');
    // loop
    function renderFrame()
    var array = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(array);
    var step = Math.round(array.length / meterNum); //sample limited data from the total array
    ctx.clearRect(0, 0, cwidth, cheight);
    for (var i = 0; i < meterNum; i++)
    var value = array[i * step];
    if (capYPositionArray.length < Math.round(meterNum))
    capYPositionArray.push(value);
    ;
    ctx.fillStyle = capStyle;
    //draw the cap, with transition effect
    if (value < capYPositionArray[i])
    ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);
    else
    ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);
    capYPositionArray[i] = value;
    ;
    ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
    ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter

    requestAnimationFrame(renderFrame);

    renderFrame();
    audio.play();
    ;



    I just need help on making sure the bars don't fly away when canvas is resized, thanks!










    share|improve this question


























      0












      0








      0








      I am trying to build a music sharing platform with an audio spectrum visualizer display in a canvas, everything works well when the canvas height is set to 350px but when reduced, the bars seem to be flying away, I tried to adjust everything I can from the javascript (main.js) but no progress, please help!



      My HTML code with the adjusted canvas dimension is below:



      <link rel="stylesheet" type="text/css" href="style.css">

      <canvas id='canvas' width="450px" height="150px"></canvas>
      <br>
      <br>
      <audio src="assets/sample.mp3" id="audio" controls> </audio>
      <script src="main.js"></script>



      This is the main.js javascript file



      window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

      window.onload = function()
      var audio = document.getElementById('audio');
      var ctx = new AudioContext();
      var analyser = ctx.createAnalyser();
      var audioSrc = ctx.createMediaElementSource(audio);
      // we have to connect the MediaElementSource with the analyser
      audioSrc.connect(analyser);
      analyser.connect(ctx.destination);
      // we could configure the analyser: e.g. analyser.fftSize (for further infos read the spec)
      // analyser.fftSize = 64;
      // frequencyBinCount tells you how many values you'll receive from the analyser
      var frequencyData = new Uint8Array(analyser.frequencyBinCount);

      // we're ready to receive some data!
      var canvas = document.getElementById('canvas'),
      cwidth = canvas.width,
      cheight = canvas.height - 2,
      meterWidth = 10, //width of the meters in the spectrum
      gap = 2, //gap between meters
      capHeight = 2,
      capStyle = '#fff',
      meterNum = 800 / (10 + 2), //count of the meters
      capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
      ctx = canvas.getContext('2d'),
      gradient = ctx.createLinearGradient(0, 0, 0, 300);
      gradient.addColorStop(1, '#0f0');
      gradient.addColorStop(0.5, '#ff0');
      gradient.addColorStop(0, '#f00');
      // loop
      function renderFrame()
      var array = new Uint8Array(analyser.frequencyBinCount);
      analyser.getByteFrequencyData(array);
      var step = Math.round(array.length / meterNum); //sample limited data from the total array
      ctx.clearRect(0, 0, cwidth, cheight);
      for (var i = 0; i < meterNum; i++)
      var value = array[i * step];
      if (capYPositionArray.length < Math.round(meterNum))
      capYPositionArray.push(value);
      ;
      ctx.fillStyle = capStyle;
      //draw the cap, with transition effect
      if (value < capYPositionArray[i])
      ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);
      else
      ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);
      capYPositionArray[i] = value;
      ;
      ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
      ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter

      requestAnimationFrame(renderFrame);

      renderFrame();
      audio.play();
      ;



      I just need help on making sure the bars don't fly away when canvas is resized, thanks!










      share|improve this question
















      I am trying to build a music sharing platform with an audio spectrum visualizer display in a canvas, everything works well when the canvas height is set to 350px but when reduced, the bars seem to be flying away, I tried to adjust everything I can from the javascript (main.js) but no progress, please help!



      My HTML code with the adjusted canvas dimension is below:



      <link rel="stylesheet" type="text/css" href="style.css">

      <canvas id='canvas' width="450px" height="150px"></canvas>
      <br>
      <br>
      <audio src="assets/sample.mp3" id="audio" controls> </audio>
      <script src="main.js"></script>



      This is the main.js javascript file



      window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

      window.onload = function()
      var audio = document.getElementById('audio');
      var ctx = new AudioContext();
      var analyser = ctx.createAnalyser();
      var audioSrc = ctx.createMediaElementSource(audio);
      // we have to connect the MediaElementSource with the analyser
      audioSrc.connect(analyser);
      analyser.connect(ctx.destination);
      // we could configure the analyser: e.g. analyser.fftSize (for further infos read the spec)
      // analyser.fftSize = 64;
      // frequencyBinCount tells you how many values you'll receive from the analyser
      var frequencyData = new Uint8Array(analyser.frequencyBinCount);

      // we're ready to receive some data!
      var canvas = document.getElementById('canvas'),
      cwidth = canvas.width,
      cheight = canvas.height - 2,
      meterWidth = 10, //width of the meters in the spectrum
      gap = 2, //gap between meters
      capHeight = 2,
      capStyle = '#fff',
      meterNum = 800 / (10 + 2), //count of the meters
      capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
      ctx = canvas.getContext('2d'),
      gradient = ctx.createLinearGradient(0, 0, 0, 300);
      gradient.addColorStop(1, '#0f0');
      gradient.addColorStop(0.5, '#ff0');
      gradient.addColorStop(0, '#f00');
      // loop
      function renderFrame()
      var array = new Uint8Array(analyser.frequencyBinCount);
      analyser.getByteFrequencyData(array);
      var step = Math.round(array.length / meterNum); //sample limited data from the total array
      ctx.clearRect(0, 0, cwidth, cheight);
      for (var i = 0; i < meterNum; i++)
      var value = array[i * step];
      if (capYPositionArray.length < Math.round(meterNum))
      capYPositionArray.push(value);
      ;
      ctx.fillStyle = capStyle;
      //draw the cap, with transition effect
      if (value < capYPositionArray[i])
      ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);
      else
      ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);
      capYPositionArray[i] = value;
      ;
      ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
      ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter

      requestAnimationFrame(renderFrame);

      renderFrame();
      audio.play();
      ;



      I just need help on making sure the bars don't fly away when canvas is resized, thanks!







      javascript audio html5-canvas visualizer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 22:15









      JP4

      183515




      183515










      asked Mar 23 at 18:32









      PowerstonePowerstone

      12




      12






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317091%2fhow-do-i-fix-an-issue-with-audio-visualizer-spectrum-display-on-height-decrease%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317091%2fhow-do-i-fix-an-issue-with-audio-visualizer-spectrum-display-on-height-decrease%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript