Skip to content
OSMD
Language: English
Sponsor
All guides

Docs · OSMD 3.0

Options

The second argument of the constructor takes the options. Most of them change how the score is laid out and drawn:

options.js
import { OpenSheetMusicDisplay } from 'opensheetmusicdisplay';

const osmd = new OpenSheetMusicDisplay('score', {
  drawingParameters: 'compacttight', // less space around the music
  drawTitle: false,
  drawPartNames: false,
  pageFormat: 'A4_P', // A4 pages, portrait; the default is one endless page
  pageBackgroundColor: '#FFFFFF',
});
await osmd.load('my-score.musicxml');
osmd.render();

drawingParameters picks a preset: default, compact, compacttight (compact with smaller margins) and a few more. pageFormat takes A3_P, A4_P, A5_P, A6_P and Letter_P, each also with _L for landscape; the default, Endless, is one page as long as the music. With the SVG backend, each page is one <svg> element.

The full list is IOSMDOptions in the API reference, each option with its default.

Changing options later

set-options.js
osmd.setOptions({ darkMode: true, drawMeasureNumbers: false });
osmd.render();

setOptions() takes the same object as the constructor and changes only what you give it; the next render() shows it. A few options work only when they are set before load(), among them renderSingleHorizontalStaffline (the whole score on one line) and the default notehead color: their descriptions in the API reference say so.

Doing something after every render

on-render.jsOSMD 3.0
import { OpenSheetMusicDisplay } from 'opensheetmusicdisplay';

const osmd = new OpenSheetMusicDisplay('score', {
  // Every render draws new SVG elements: this runs after each one,
  // also after the renders autoResize starts.
  onRender: () => {
    for (const note of document.querySelectorAll('#score .vf-stavenote')) {
      note.addEventListener('click', () => note.classList.toggle('selected'));
    }
  },
});
await osmd.load('my-score.musicxml');
osmd.render();

A render replaces the SVG elements, so colors and event listeners you added are gone afterwards. After your own render() call you know when that happens, but OSMD also renders by itself: with autoResize when the width changes, and while it draws a score in batches. onRender, new in OSMD 3.0, runs after each of them. It gets { incremental, done }: whether a batch was drawn, and whether the whole score is drawn now.

Engraving rules

For finer control, osmd.EngravingRules holds the numbers the layout uses, such as PageLeftMargin, in OSMD’s units (10 pixels at zoom 1). OSMD 3.0 removed 64 rules that had no effect (pull request 2015): if your code sets one of them, TypeScript tells you after the update.

Next: Transposition