File size: 7,192 Bytes
af6912c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!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: Annotating</title>
  <script src="../dist/abcjs-basic.js" type="text/javascript"></script>
  <script type="text/javascript">
    var abc = "T: New Exercise\n" +
      "M: C\n" +
      "L: 1/4\n" +
      "K: C\n" +
      'V:1 name="Alto\\n[species 2]\\n[Sop.]"\n'+
      '"^1"c2"^2β†’"G2|B2d2|B2"^5β†’\\n6β†’".(A2|C4)|\n' +
      'V:2 clef=treble-8 name="Tenor\\n[c.f.]\\n[Alt.]"\n' +
      '"^4. Consecutive leaps - - -"c4|e4|d4|c4|\n' +
      'w:H\n' +
      'w:F\n' +
      'w:I\n';

    function load() {
      ABCJS.renderAbc("paper", abc, { add_classes: true, staffwidth: 500 });
      annotate();
    }

    function annotate() {
      var red = "#FF3018";
      var yellow = "#C9B203";

      var els = document.querySelectorAll(".abcjs-annotation");
      var list = [];
      list[0] = { el: els[0], color: yellow};
      list[1] = { el: els[1], color: yellow};
      list[2] = { el: els[2], color: red};
      list[3] = { el: els[3], color: red};
      setFlagColors(list);

      list = [];
      list[0] = {
        el1: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m0.abcjs-n1"),
        pos1: "bottom",
        el2: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m1.abcjs-n0"),
        pos2: "top",
        color: red,
        text: "3"
      }
      list[1] = {
        el1: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m2.abcjs-n1"),
        pos1: "bottom",
        el2: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m3.abcjs-n0"),
        pos2: "top",
        color: yellow,
        text: "7"
      }
      list[2] = {
        el1: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v1.abcjs-m0.abcjs-n0"),
        pos1: "bottom",
        el2: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v1.abcjs-m1.abcjs-n0"),
        pos2: "top",
        color: red,
        text: "3"
      }
      list[3] = {
        el1: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v1.abcjs-m2.abcjs-n0"),
        pos1: "bottom",
        el2: document.querySelector(".abcjs-note.abcjs-l0.abcjs-v1.abcjs-m3.abcjs-n0"),
        pos2: "top",
        color: yellow,
        text: "7"
      }
      setConnectors(list);

      setWordsLine(0, [
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m0.abcjs-n0", text: "I" },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m1.abcjs-n0", text: "III7", background: yellow },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m2.abcjs-n0", text: "VII6" },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m2.abcjs-n1", text: "II", background: yellow },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m3.abcjs-n0", text: "I" },
      ] );

      setWordsLine(1, [
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m1.abcjs-n0", text: "8", color: yellow },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m2.abcjs-n1", text: "9\n10", color: yellow },
      ] );

      setWordsLine(2, [
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m0.abcjs-n1", text: "5", background: red },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m1.abcjs-n0", text: "5", background: red },
        { el: ".abcjs-note.abcjs-l0.abcjs-v0.abcjs-m3.abcjs-n0", text: "1", background: yellow },
      ] );
    }

    function setFlagColors(list) {
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        item.el.style = "fill:" + item.color + ";";
      }
    }

    function setConnectors(list) {
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        var el1 = item.el1.getBBox();
        var el2 = item.el2.getBBox();
        var x1 = el1.x + el1.width + 3;
        var y1 = item.pos1 === "top" ?  el1.y + 3 : el1.y + el1.height - 6;
        var x2 = el2.x - 3;
        var y2 = item.pos2 === "top" ?  el2.y + 3 : el2.y + el2.height;
        drawLine({ x1: x1, y1: y1, x2: x2, y2: y2, stroke: item.color });
        drawText({ text: item.text, x: x1 + (x2 - x1)/2, y: y1 + (y2 - y1)/2, fill: item.color })
      }
    }

    function setWordsLine(line, list) {
      var svg = document.querySelector("#paper svg");
      var place = document.querySelector(".abcjs-lyric");
      var pos = place.getBBox();
      var y = 20 + pos.y + line * 20;
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        var el = document.querySelector(item.el);
        var x = el.getBBox().x + el.getBBox().width/2;
        drawText({ x: x, y: y, fill: item.color, background: item.background, text: item.text });
      }
    }

    var svgNS = "http://www.w3.org/2000/svg";

    function drawLine(attr) {
      var svg = document.querySelector("#paper svg");
      var el = document.createElementNS(svgNS, 'line');
      el.setAttribute("x1", attr.x1);
      el.setAttribute("x2", attr.x2);
      el.setAttribute("y1", attr.y1);
      el.setAttribute("y2", attr.y2);
      el.setAttribute("stroke", attr.stroke);
      el.setAttribute("stroke-width", 2);
      svg.appendChild(el);
    }

    function drawText(attr) {
      var svg = document.querySelector("#paper svg");
      var el = document.createElementNS(svgNS, 'text');
      el.setAttribute("x", attr.x);
      el.setAttribute("y", attr.y);
      if (attr.fill) {
        el.setAttribute("fill", attr.fill);
        el.setAttribute("stroke", attr.fill);
      }
      el.textContent = attr.text;

      svg.appendChild(el);
    }

  </script>
  <style>
    .abcjs-slur {
            stroke-width: 2;
            stroke: red;
    }
    .abcjs-lyric {
      fill: none;
    }
    label {
      display:block;
    }
  </style>
</head>
<body onload="load()">
  <header>
    <img src="https://paulrosen.github.io/abcjs/img/abcjs_comp_extended_08.svg" alt="abcjs logo">
    <h1>Annotation</h1>
  </header>
  <div class="container">
    <p>
      This is a simple example of how elements can be found on the page and how
      annotations can be drawn.
    </p>
    <p>1) Note that annotations above the staves are written into the ABC notation using the syntax "^text".</p>
    <p>2) Note that room is created for annotations below the staves by creating a w: line and then setting its fill to none on the stylesheet.</p>
    <p>3) Note that the notes are retrieved by their unique class and their position is retrieved. When the music is changed,
    this will automatically move the drawn lines to the right place.</p>
    <div id="paper"></div>

    <label for="abc">ABC Notation:</label>
    <textarea id="abc" cols="50" rows="12" spellcheck="false">
T: New Exercise
M: C
L: 1/4
K: C
V: 1 name="Alto\n[species 2]\n[Sop.]"
"^1"c2"^2β†’"G2|B2d2|B2"^5β†’\n6β†’".(A2|C4)|
V: 2 clef=treble-8 name="Tenor\n[c.f.]\n[Alt.]"
"^4. Consecutive leaps - - -"c4|e4|d4|c4|
W: Set this line's fill to none to make space for annotations below the staff.
      </textarea>
    <p>Note that \ is a character which must be escaped in JavaScript.</p>
  </div>
</body>
</html>