VexFlow Documentation

repository·master·Indexed 24 days ago

https://github.com/0xfe/vexflow

An open-source JavaScript library for rendering music notation and guitar tablature. VexFlow supports HTML Canvas and SVG outputs and is compatible with browser environments and Node.js. It provides a high-level EasyScore API for rapid creation and a low-level Native API for granular control. The library includes support for multiple music engraving fonts such as Bravura, Petaluma, Gonville, and Leland.

Tokens
6.4K
Snippets
11
Records
49
Agent score
38%

What's inside VexFlow

  1. Compare VexFlow images using two working copies

    master

    If you need to compare two different local versions of VexFlow (for example, your own branch vs. a pull request branch), use this method:

    1. Generate Images for Both: In each separate working copy, run grunt and then grunt generate:current.
    2. Prepare for Comparison: You will now have two vexflow/build/images/current/ folders.
    3. Compare: Open index.html in your local browser. Drag one current/ folder onto the left side of the compare tool and the other current/ folder onto the right side.

    Tip: To avoid managing two current/ folders, you can rename one to reference/ and move it into the other working copy's vexflow/build/images/ directory. Then, drag the combined images/ folder onto the compare tool as described in the single working copy method.

  2. Use the EasyScore high-level API

    master

    EasyScore is a high-level API designed for quickly creating music notation. It uses a Factory to manage the renderer, score, and system.

    To use it, ensure you have a container element (e.g., <div id="output"></div>) in your HTML. The Factory handles the connection between the renderer and the musical elements.

    const { Factory, EasyScore, System } = Vex.Flow;
    
    const vf = new Factory({
      renderer: { elementId: 'output', width: 500, height: 200 },
    });
    
    const score = vf.EasyScore();
    const system = vf.System();
    
    system
      .addStave({
        voices: [
          score.voice(score.notes('C#5/q, B4, A4, G#4', { stem: 'up' })),
          score.voice(score.notes('C#4/h, C#4', { stem: 'down' })),
        ],
      })
      .addClef('treble')
      .addTimeSignature('4/4');
    
    vf.draw();
  3. Use Roboto Slab font in CSS

    master

    To use the Roboto Slab font (weight 500, Latin subset) in your project, include the following @font-face declaration in your CSS. This configuration uses Google Fonts' hosted WOFF2 and WOFF files.

    /* latin */
    @font-face {
      font-family: 'Roboto Slab';
      font-style: normal;
      font-weight: 500;
      font-display: swap;
      src: url(https://fonts.gstatic.com/s/robotoslab/v16/BngbUXZYTXPIvIBgJJSb6s3BzlRRfKOFbvjovoSmb2RjV9Ku1Q.woff2) format('woff2'),
        url(https://fonts.gstatic.com/s/robotoslab/v16/BngbUXZYTXPIvIBgJJSb6s3BzlRRfKOFbvjovoSmb2RlV9Su1cai.woff) format('woff');
      unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC,
        U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
    }
  4. Generate SMuFL fonts

    master

    To add a new SMuFL glyph to VexFlow, first add the SMuFL code along with the Gonville backup code to config/valid_codes.json.

    Then, follow these steps to generate the glyph files:

    1. Generate Gonville glyphs: Convert the OTF to a SMuFL-compatible OTF, then generate the TypeScript glyph file.
    2. Generate Bravura glyphs: Generate the TypeScript glyph file from a Bravura OTF.
    3. Generate Petaluma glyphs: Generate the TypeScript glyph file from a Petaluma OTF.
    4. Generate Gonville glyphs: Generate the TypeScript glyph file directly from the Gonville OTF.
    # Generate Gonville glyphs
    $ ./gonville2smufl.py @/gonville/Gonville-18_20200703.otf @/gonville/GonvilleSmufl.otf
    $ node fontgen_smufl.js @/gonville/GonvilleSmufl.otf ../../src/fonts/gonville_glyphs.ts
    
    # Generate Bravura glyphs
    $ node fontgen_smufl.js @/bravura/Bravura_1.392.otf ../../src/fonts/bravura_glyphs.ts
    
    # Generate Petaluma glyphs
    $ node fontgen_smufl.js @/petaluma/Petaluma_1.065.otf ../../src/fonts/petaluma_glyphs.ts
    
    # Generate Gonville glyphs
    $ node fontgen_gonville.js @/gonville/Gonville-18_20200703.otf ../../src/fonts/gonville_glyphs.ts
  5. Run VexFlow ES Module demos using a web server

    master

    To run demonstrations that use VexFlow with ES Modules, you must serve the files via a web server rather than opening them directly from the file system.

    1. Navigate to the main vexflow/ folder in your terminal.
    2. Start a local web server using npx http-server.
    3. Access the ES module demo by visiting http://127.0.0.1:8080/demos/modules/module.html in your browser.
    $ npx http-server
  6. Load fonts asynchronously with vexflow-core.js

    master

    If you are using vexflow-core.js along with individual font bundles (e.g., vexflow-font-bravura.js, vexflow-font-petaluma.js, etc.), you must load the fonts before rendering. The Vex.Flow.setMusicFont(...) function is asynchronous in this configuration and returns a Promise.

    IMPORTANT: The default font stack is empty, so you must call setMusicFont before rendering your score.

  7. Install VexFlow via CDN or npm

    master

    Via CDN

    For web pages, you can include VexFlow using a <script> tag. It is recommended to specify a version number to ensure stability.

    <script src="https://cdn.jsdelivr.net/npm/vexflow@4.2.2/build/cjs/vexflow.js"></script>
    <script>
      // YOUR CODE GOES HERE
    </script>

    Via npm

    If you are using a module bundler, install VexFlow using npm:

    npm install vexflow
    npm install vexflow
  8. Generate text metrics for a text font

    master

    To create text metrics for a custom font (e.g., an .otf file):

    1. Run fontgen_text.js with your font file and the desired output TypeScript filename.
    2. Register the generated metrics using TextFormatter.registerInfo(...) to make them available for your module.
    $ node fontgen_text.js MyFont.otf myfont_glyphs.ts
  9. Run the Web Worker demos

    master

    The worker/ directory contains examples demonstrating how to use web workers to render multiple scores simultaneously. To run these demos:

    1. Navigate to the vexflow/ folder.
    2. Start a web server using npx http-server.
    3. Open your browser and navigate to http://127.0.0.1:8080/demos/worker/.
    npx http-server
  10. Use statically bundled font modules

    master
    VexFlow provides individual font bundles that statically include only a specific font. When using these bundles (e.g., vexflow-bravura.js, vexflow-gonville.js, vexflow-petaluma.js, or vexflow-leland.js), you do not need to call Vex.Flow.setMusicFont(...) as the font is already bundled.