abcjs / examples /add-note-names.html
KEXEL's picture
Upload 337 files
af6912c verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="examples-styles.css"/>
<title>abcjs: Add Note Names</title>
<script src="../dist/abcjs-basic.js" type="text/javascript"></script>
<script type="text/javascript">
var abc = "T: Cooley's\n" +
"M: 4/4\n" +
"L: 1/8\n" +
"R: reel\n" +
"K: Emin\n" +
"|:D2|EB{c}BA B2 EB|~B2 AB dBAG|FDAD BDAD|FDAD dAFD|\n" +
"w: Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z\n" +
"EBBA B2 EB|B2 AB defg|afe^c dBAF|DEFD E2:|\n" +
"w: Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z\n" +
"|:gf|eB B2 efge|eB B2 gedB|A2 FA DAFA|A2 FA defg|\n" +
"w: Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z\n" +
"eB B2 eBgB|eB B2 defg|afe^c dBAF|DEFD E2:|\n" +
"w: Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z\n"
function load() {
ABCJS.renderAbc("paper", abc, { add_classes: true});
// This will get the groups of elements that represent all the notes
var notes = document.querySelectorAll(".abcjs-note")
for (var i = 0; i < notes.length; i++) {
var note = notes[i]
// This gets all the "important" elements for each note - this could be an accidental, the notehead, a stem, a decoration, etc.
var children = note.querySelectorAll("[data-name]")
var noteName;
for (var j = 0; j < children.length; j++) {
var child = children[j];
var name = child.getAttribute("data-name")
// All the note names are 1 or two characters. All the other stuff is longer, so this finds the notehead in the group.
if (name.length === 1 || name.length === 2) {
noteName = name
} else if (name === "lyric") {
// this finds the place to put the note name
var span = child.querySelector("tspan")
span.innerHTML = friendlyNoteName(noteName)
}
}
}
}
function friendlyNoteName(name) {
// The notes are in ABC format - so translate that to human readable
var acc = ''
if (name[0] === '_') acc = "b"
if (name[0] === '^') acc = "#"
var pitch = name[name.length-1]
return pitch.toUpperCase() + acc;
}
</script>
</head>
<body onload="load()">
<header>
<img src="https://paulrosen.github.io/abcjs/img/abcjs_comp_extended_08.svg" alt="abcjs logo">
<h1>Add note names</h1>
</header>
<div class="container">
<p>This will add note names underneath all the pitches. The trick is to create a dummy lyric line with "w: Z Z Z ..." then after the tune is rendered to replace the text with the note name. You can make the "w:" longer than the number of notes - any lyrics that are extra are just ignored. You can use two characters for each of the lyrics if you want to allow more space.</p>
<div id="paper"></div>
</div>
</body>
</html>