From 788e755657186d4f567739abe093607d17309e62 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Mon, 27 Jul 2020 07:48:50 -0400 Subject: [PATCH] random accidentals --- app.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index aacf96c..19b3b48 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,18 @@ const randomNote = () => { return `${pitch}/${octave}`; } +const returnNewNote = () => { + const newNote = new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }) + if(Math.random() > 0.5){ + const accidentals = ['b', '#'] + const randomAccidentalIndex = Math.floor(Math.random()*accidentals.length); + const accidental = accidentals[randomAccidentalIndex]; + newNote.addAccidental(0, new VF.Accidental(accidental)); + } + + return newNote; +} + const drawNotes = (measureNum) => { const staveWidth = 550; // Create a stave at position 10, 40 of width 400 on the canvas. @@ -36,17 +48,17 @@ const drawNotes = (measureNum) => { stave.setContext(context).draw(); const notes = [ - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), + returnNewNote(), + returnNewNote(), ]; const notes2 = [ - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), + returnNewNote(), + returnNewNote(), ]; const notes3 = [ - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), - new VF.StaveNote({clef: "treble", keys: [randomNote()], duration: "8" }), + returnNewNote(), + returnNewNote(), + returnNewNote(), ]; const allNotes = notes.concat(notes2).concat(notes3);