VexFlow Sandbox
Experiment with VexFlow
by ronyeh
HTML
<script src="https://unpkg.com/[email protected]/build/cjs/vexflow-debug.js"></script>
<div id="output"></div>
JavaScript
const {
Renderer,
Stave,
StaveNote,
Voice,
Formatter
} = Vex.Flow;
// Create an SVG renderer and attach it to the DIV element named "boo".
const div = document.getElementById('output');
const renderer = new Renderer(div, Renderer.Backends.SVG);
// Configure the rendering context.
renderer.resize(500, 500);
const context = renderer.getContext();
// Create a stave of width 400 at position 10, 40 on the canvas.
const stave = new Stave(10, 40, 400);
// Add a clef and time signature.
stave.addClef('treble').addTimeSignature('4/4');
// Connect it to the rendering context and draw!
stave.setContext(context).draw();
// Create the notes
const notes = [
new StaveNote({
keys: ['c/5'],
duration: 'q'
}),
new StaveNote({
keys: ['d/4'],
duration: 'q'
}),
new StaveNote({
keys: ['b/4'],
duration: 'qr'
}),
new StaveNote({
keys: ['c/4', 'e/4', 'g/4'],
duration: 'q'
}),
];
const notes2 = [new StaveNote({
keys: ['c/4'],
duration: 'w'
})];
// Create a voice in 4/4 and add above notes
const voices = [
new Voice({
num_beats: 4,
beat_value: 4
}).addTickables(notes),
new Voice({
num_beats: 4,
beat_value: 4
}).addTickables(notes2),
];
// Format and justify the notes to 400 pixels.
new Formatter().joinVoices(voices).format(voices, 350);
// Render voices.
voices.forEach(function(v) {
v.draw(context, stave);
});