Skip to content
OSMD
Language: English
Sponsor
All guides

Docs · OSMD 3.0

The cursor

The cursor marks a position in the score: one timestamp, with every note or rest that starts there in any part. It’s what a practice app moves along while someone plays, and what a player follows. Start from a rendered score (see Getting started):

cursor.js
osmd.cursor.show();
osmd.cursor.next();
const notes = osmd.cursor.NotesUnderCursor();
console.log(notes.map((note) => (note.isRest() ? 'rest' : note.Pitch.ToStringShort(3))));
osmd.cursor.reset();

show() draws the cursor at its position, next() and previous() move it one timestamp, reset() takes it back to the start, and hide() takes it off the page. NotesUnderCursor() gives the notes at the current position, rests included, which isRest() tells apart.

OSMD counts octaves three lower than scientific pitch notation: middle C is octave 1. Pitch.ToStringShort(3) adds the 3, so you get the names most other software uses, C4 for middle C.

Walking a whole score

walk.js
const cursor = osmd.cursor;
cursor.reset();
while (!cursor.Iterator.EndReached) {
  const measure = cursor.Iterator.CurrentMeasureIndex + 1;
  const pitches = cursor
    .NotesUnderCursor()
    .filter((note) => !note.isRest())
    .map((note) => note.Pitch.ToStringShort(3));
  console.log(measure, pitches.join(' '));
  cursor.next();
}

cursor.Iterator knows where the cursor is: EndReached after the last timestamp, CurrentMeasureIndex counting from 0. For a C major scale in two measures this logs 1 C4 up to 2 C5.

Scrolling along

The option followCursor scrolls the page at every move so the cursor sits in the middle of the view, also while it’s still visible. On a page that people scroll themselves while the cursor moves (a player, a practice view on a phone), that pulls the page back at every note. There, leave it off and scroll yourself only when the cursor leaves the view: osmd.cursor.cursorElement is the cursor’s image element, which you can measure with getBoundingClientRect().

More than one cursor

The option cursorsOptions takes a list, one entry per cursor, each with a type (0: the notes, 1: a thin line left of them, 3: the whole measure, and more), a color, an alpha and whether followCursor follows it. osmd.cursors holds them; osmd.cursor is the first.

Next: Options