Skip to Content
JavaScriptFretboard DiagramsThe Fretboard class

The Fretboard class

The Fretboard class renders highly configurable fretboard diagrams in SVG format.

Check the complete Fretboard API reference for more details.

Installation

pnpm add @music-ui/fretboard

Usage

Rendering a fretboard requires a target element:

<div id="fretboard"></div>

and the following setup code:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600 }); fretboard.render();

Output:

Width behavior

The display option controls the <svg> element width behavior.

Allow horizontal overflow

display: "overflow" sets the <svg> width to the fretboard width (either the passed or the default one):

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ display: "overflow", }); fretboard.render();

Output:

Contained in the parent element

display: "contain" sets the <svg> width to min(100%, {width}px):

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ display: "contain", }); fretboard.render();

Output:

And with a smaller fretboard:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ display: "contain", fretCount: 5, width: 600, }); fretboard.render();

Output:

Fill the available space

display: "cover" sets the <svg> width to 100%:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ display: "cover", fretCount: 5, width: 600, }); fretboard.render();

Output:

Omitting the display option prevents from setting the <svg> element width property entirely, in case you want to control it from your CSS.

Displaying fretboard positions

An empty fretboard is not much useful. Let’s see how to display some positions on it and to customize their appearance:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600 }); // a good ole open C cowboy chord fretboard.setPositions([ { string: 5, fret: 3 }, { string: 4, fret: 2 }, { string: 2, fret: 1 }, ]); fretboard.render();

The Fretboard.setPositions methods accepts an array of fretboard positions.

A FretboardPosition can hold multiple information, the minimum requirement being the string and the fret keys like in the example above.

Displaying the position text

Let’s display the full C major arpeggio in first position:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600 }); fretboard.setPositions([ { string: 6, fret: 0 }, { string: 6, fret: 3 }, { string: 5, fret: 3 }, { string: 4, fret: 2 }, { string: 3, fret: 0 }, { string: 2, fret: 1 }, { string: 1, fret: 0 }, { string: 1, fret: 3 }, ]); fretboard.render();

Output:

That looks great! What about knowing which note corresponds to every position?

Let’s expand the position array with such information:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600 }); fretboard.setPositions([ { string: 6, fret: 0, note: "E" }, { string: 6, fret: 3, note: "G" }, { string: 5, fret: 3, note: "C" }, { string: 4, fret: 2, note: "E" }, { string: 3, fret: 0, note: "G" }, { string: 2, fret: 1, note: "C" }, { string: 1, fret: 0, note: "E" }, { string: 1, fret: 3, note: "G" }, ]); fretboard.render();

Output:

Exactly, that doesn’t change anything automatically. The fretboard constructor accepts a positionText function that helps us to achieve our goal:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600, positionText: (position) => position.note, }); fretboard.setPositions([ { string: 6, fret: 0, note: "E" }, { string: 6, fret: 3, note: "G" }, { string: 5, fret: 3, note: "C" }, { string: 4, fret: 2, note: "E" }, { string: 3, fret: 0, note: "G" }, { string: 2, fret: 1, note: "C" }, { string: 1, fret: 0, note: "E" }, { string: 1, fret: 3, note: "G" }, ]); fretboard.render();

That’s brilliant! positionText is a callback that exposes every position and allows us to return what we need.

In this example we compiled the position information manually. The @music-ui/fretboard package contains scale and arpeggio generation utilities.

Style the positions

Let’s highlight the root notes of the arpeggio with the Fretboard.style function:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600, positionText: (position) => position.note, }); fretboard.setPositions([ { string: 6, fret: 0, note: "E" }, { string: 6, fret: 3, note: "G" }, { string: 5, fret: 3, note: "C" }, { string: 4, fret: 2, note: "E" }, { string: 3, fret: 0, note: "G" }, { string: 2, fret: 1, note: "C" }, { string: 1, fret: 0, note: "E" }, { string: 1, fret: 3, note: "G" }, ]); fretboard.render(); fretboard.style({ fill: (position) => (position.note === "C" ? "orange" : "white"), });

Output:

The Fretboard.style functions accepts a key-value list of stylable properties and their corresponding values / function generating values.

Another approach is to use the filter property:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 5, width: 600, positionText: (position) => position.note, }); fretboard.setPositions([ { string: 6, fret: 0, note: "E" }, { string: 6, fret: 3, note: "G" }, { string: 5, fret: 3, note: "C" }, { string: 4, fret: 2, note: "E" }, { string: 3, fret: 0, note: "G" }, { string: 2, fret: 1, note: "C" }, { string: 1, fret: 0, note: "E" }, { string: 1, fret: 3, note: "G" }, ]); fretboard.render(); fretboard.style({ filter: (position) => position.note === "C", fill: "orange", });

Same output:

Rendering chord diagrams

The Fretboard class has a convenient renderChord method:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard({ fretCount: 3 }); fretboard.renderChord({ input: "x32010" });

Output:

See the chord diagrams page for more details.

Rendering scale diagrams

The Fretboard class has also, guess what, a renderScale method:

import { Fretboard } from "@music-ui/fretboard"; const fretboard = new Fretboard(); fretboard.renderScale({ root: "C", type: "major" });

Output:

See the scale diagrams page for more details.

Constructor parameters

The Fretboard constructor accepts the following parameters:

PropertyTypeDefaultDescription
elementstring | HTMLElement#fretboardThe container element or a valid CSS selector
display"contain" | "cover" | "overflow | undefinedundefinedThe width behavior
tuningstring[]["E2", "A2", "D3", "G3", "B3", "E4"]The instrument tuning
stringCountnumber6The amount of strings to render
stringWidthnumber | number[]1The string thickness
stringColorstring#666The string color
fretCountnumber15The amount of frets to render
fretWidthnumber1The fret width
fretColorstring#666The fret color
nutWidthnumber1The nut width
nutColorstring#666The nut color
middleFretWidthnumber3The middle fret width
middleFretColorstring#ff636cThe middle fret color
paddingTopnumber20The top padding around the svg element
paddingBottomnumber15The bottom padding around the svg element
paddingLeftnumber20The left padding around the svg element
paddingRightnumber20The right padding around the svg element
widthnumber960The desired element width
heightnumber150The desired element height
positionSizenumber20The fretboard position size
positionStrokeColorstring#555The fretboard position stroke color
positionStrokeWidthnumber2The fretboard position stroke width
positionFillstringwhiteThe fretboard position fill color
positionTextSizenumber12The fretboard position text size
positionText(position) => string() => ""The fretboard position text getter
disabledOpacitynumber0.9The opacity of the disabled frets
cropbooleanfalseIf true, renders just the frets with positions in them
fretPaddingLeftnumber0How many empty frets to display before the first rendered position
scaleFretsbooleantrueIf true, the fret will be scales logarithmically
showFretNumbersbooleantrueIf true, shows the fret numbers below the fretboard
fretNumbersHeightnumber40The fret numbers height
fretNumbersMarginnumber20The fret numbers top margin
fretNumbersColorstring#666The fret numbers color
fontstringArialThe fret numbers color
barresColorstring#666The barres color
highlightPaddingnumber10The highlight areas padding
highlightRadiusnumber10The highlight areas border radius
highlightStrokestringtransparentThe highlight areas stroke color
highlightFillstringdodgerblueThe highlight areas fill color
highlightBlendModestringcolor-burnThe highlight areas blend mode
Last updated on