首页 > 解决方案 > p5.j​​s 嵌套 For 循环不迭代

问题描述

我正在尝试为音乐可视化项目创建一些测量声音频率的音阶。它们旨在以 2x2 网格模式显示 4 个不同的频率(低音、低中、高中和高音)。我快到了我有我的矩形,但测量和显示频率本身的指针仅针对顶行 x 和不是最下面一行我对 JavaScript 很陌生,所以我确信它可能是我所缺少的非常简单的东西。

    // draw the plots to the screen
    this.draw = function() {
        //create an array amplitude values from the fft.
        var spectrum = fourier.analyze();
        //iterator for selecting frequency bin.
        var currentBin = 0;
        push();
        fill('#f0f2d2');
        //nested for loop to place plots in 2*2 grid.
        for(var i = 0; i < this.plotsDown; i++) {
            for(var j = 0; j < this.plotsAcross; j++) {

                //calculate the size of the plots
                var x = this.pad * j * 10;
                var y = height/20 * i * 10;
                var w = (width - this.pad) / this.plotsAcross;
                var h = (height - this.pad) / this.plotsDown;

                //draw a rectangle at that location and size
                rect(x, y, w, h);

                //add on the ticks
                this.ticks((x + w/2), h, this.frequencyBins[i])

                var energy = fourier.getEnergy(this.frequencyBins[currentBin]);

                //add the needle
                this.needle(energy, (x + w/2), h)

                currentBin++;
            }
        }

        pop();
    };

标签: javascriptfor-loopnestedp5.js

解决方案


推荐阅读