dimanche 19 avril 2020

p5.js web editor problems with 2d array not defining and if loop causing freeze

first post.

I'm trying to code a fretboard which I will modify and add to over time but I'm having problems.

I can render the frets, strings and circles fine, even the notes on the circles, but getting the base note to show is a pain.

I have commented out the problem lines, near the bottom of the code. The if function causes p5.js to freeze, the 2d array doesn't seem to get defined even though I push an array into the empty array.

help?

   let stringNumber = 6;
let fretNumber = 22;
let notes = ["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"]
let baseNotes = [7, 0, 5, 10, 2, 7];
let fretboardWidth = 0;
let fretboardHeight = 0;
function setup() {
  createCanvas(600, 800);
}

function draw() {
  background(0,100,100);
  fretboardWidth = width - 100;
  fretboardHeight = height -100
  renderFrets();
  renderStrings();
  renderCircles();
  createNotes();
}

function renderFrets() {
  for(i=1; i <= fretNumber; i++){
   line(0, fretboardHeight / (fretNumber + 1) * i, fretboardWidth, fretboardHeight / (fretNumber + 1) * i);
  }
}

function renderStrings() {
  for(i=1; i <= stringNumber; i++){
   line(fretboardWidth / (stringNumber +1) * i, 0, fretboardWidth / (stringNumber +1) * i, fretboardHeight); 
  }
}

function renderCircles() {
  for(i = 1; i <= stringNumber; i++){
   for(j = 1; j <= fretNumber; j++){
     circle(fretboardWidth / (stringNumber+1) * i, fretboardHeight / (fretNumber+1) * j, 20); 
   }
  }
}

function createNotes() {
 let strings = [];
 let gstring = [];
  for(i=0; i < stringNumber; i++){
    for(j=0; j <= fretNumber; j++) {
      gstring.push(notes[(baseNotes[i] + j) % 12]);
    //  if(j=0){
     //   text(gstring[j], fretboardWidth / 7 * (i+1) - 4, fretboardHeight / (fretNumber+1) * j + 3);
     // }
       // else{
          text(gstring[j], fretboardWidth / 7 * (i+1) - 4, fretboardHeight / (fretNumber+1) * j + 3);
       // }

    }
    strings.push[gstring];
    gstring = [];  
  }
  text(strings[0][0], fretboardWidth / 7 * (i+1) - 4, 10);
}

Aucun commentaire:

Enregistrer un commentaire