Javascript for viewing Humdrum digital scores online.
This page demonstrates a simple method of loading the verovio script, then displaying two static scores. The source code for a complete webpage containing the same examples is given further below.
Here is example HTML content to display the above notation. Try copying the
text in the black region below into a file ending in .htm
or .html
and
open it in your web brower (probably by double clicking on the file).
<html>
<head>
<title>Basic usage</title>
<script src="http://verovio-script.humdrum.org/scripts/verovio-toolkit-wasm.js"></script>
</head>
<body>
<h2> Twinkle, Twinkle, Little Star </h2>
<div style="padding-bottom:30px" id="notation-twinkle"></div>
<h2> Mary Had a Little Lamb</h2>
<div id="notation-mary"></div>
<script id="humdrum-twinkle" type="text/x-humdrum">
**kern
*clefG2
*M4/4
=1
4c
4c
4g
4g
=2
4a
4a
2g
=3
4f
4f
4e
4e
=4
4d
4d
2c;
==
*-
</script>
<script id="humdrum-mary" type="text/x-humdrum">
**kern
*clefG2
*M4/4
=1
4e
4d
4c
4d
=2
4e
4e
2e
=3
4d
4d
2d
=4
4e
4g
2g
=
*-
</script>
<script>
document.addEventListener("DOMContentLoaded", (event) => {
Module.onRuntimeInitialized = async _ => {
let tk = new verovio.toolkit();
let humdrumTwinkle = document.querySelector("#humdrum-twinkle").textContent.replace(/^\s+/, "");
let verovioOptions = {
scale: 40,
pageWidth: 1500,
pageHeight: 60000,
pageMarginTop: 0,
adjustPageHeight: 1
};
let svgTwinkle = tk.renderData(humdrumTwinkle, verovioOptions);
let elementTwinkle = document.querySelector("#notation-twinkle");
elementTwinkle.innerHTML = svgTwinkle;
let humdrumMary = document.querySelector("#humdrum-mary").textContent.replace(/^\s+/, "");
verovioOptions.scale = 60;
verovioOptions.pageWidth = 1000;
let svgMary = tk.renderData(humdrumMary, verovioOptions);
let elementMary = document.querySelector("#notation-mary");
elementMary.innerHTML = svgMary;
}
});
</script>
</body>
</html>
After Humdrum data is extracted from the embedded script
elements, any leading whitespace must be remove in order to use the
verovio auto-detection feature to identify the input data format
as Humdrum data. The javascript code .replace(/^\s+/, "")
removes any
leading whitespace, which occurs in the above exmaple since
there will be a leading newline in the textContent
of the Humdrum
script elements. If you do not want to strip leading whitespace, explicitly
set the input format with the verovio option inputFrom: "humdrum"
.