-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvexflowTest.html
171 lines (141 loc) · 4.76 KB
/
vexflowTest.html
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
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="">
<meta property="og:type" content="">
<meta property="og:url" content="">
<meta property="og:image" content="">
<meta name="theme-color" content="#fafafa">
</head>
<body>
<!-- Add your site or application content here -->
<button onclick="createNotes()">CREATE NOTES</button>
<button onclick="createMeasuresWidth()">CREATE MEASURES WIDTH</button>
<button onclick="runAllExceptNotes()">RUN ALL EXCEPT NOTES AND MEASURES WIDTH</button>
<button onclick="createStaves()">CREATE STAVES</button>
<button onclick="createBeams()">CREATE BEAMS</button>
<button onclick="drawStaves()">DRAW STAVES</button>
<button onclick="drawNotes()">DRAW NOTES</button>
<button onclick="drawBeams()">DRAW BEAMS</button>
<button onclick="scaleZoom()">SCALE</button>
<button onclick="resetAll()">CLEAR</button>
<div id="boo"></div>
<script src="js/vexflow-min.js"></script>
<script>
// HOWTO get absolute XPOS of note (do after formatting)
// console.log(notes[i].getAbsoluteX());
const VF = Vex.Flow;
let div = document.getElementById("boo")
let renderer = new VF.Renderer(div, VF.Renderer.Backends.SVG);
let formatter = new VF.Formatter();
div.style.width = "100%";
const TIMESIGNATUREWIDTH = 50
const screenWidth = div.offsetWidth
let context = renderer.getContext();
let scale = 1
context.scale(scale, scale);
let staveHeight = 0;
let measures = 30
let notes = []
let staves = []
let beams = []
let measuresWidth = []
let systems = 1
function resetAll() {
formatter = new VF.Formatter();
staves = []
beams = []
systems = 1
let svgContext = context.svg
while (svgContext.lastChild) {
svgContext.removeChild(svgContext.lastChild);
}
}
function createNotes() {
for (let i = 0; i < measures; i++) {
let phraseNotes = []
if (i % 3 === 0) {
for (let i = 1; i <= 16; i++) {
const note16 = new VF.StaveNote({keys: ["c/4"], duration: "16"})
phraseNotes.push(note16)
}
} else {
for (let i = 1; i <= 8; i++) {
const note8 = new VF.StaveNote({keys: ["c/4"], duration: "8"})
phraseNotes.push(note8)
}
}
notes.push(phraseNotes)
}
}
function createMeasuresWidth() {
for (let i = 0; i < measures; i++) {
let voice = new VF.Voice({num_beats: 4, beat_value: 4});
voice.addTickables(notes[i]);
formatter.joinVoices([voice]).format([voice]);
let width = formatter.getMinTotalWidth();
measuresWidth.push(width)
}
}
function createStaves() {
let previousMeasureWidth = 0;
let currentSystemHeight = 0;
for (let i = 0; i < measures; i++) {
let stave = new VF.Stave(previousMeasureWidth, currentSystemHeight, measuresWidth[i]);
if (staveHeight === 0) {
staveHeight = stave.height
}
previousMeasureWidth += measuresWidth[i];
if (previousMeasureWidth * scale > screenWidth) { // aka new system
systems += 1
currentSystemHeight += stave.height;
stave = new VF.Stave(0, currentSystemHeight, measuresWidth[i]);
previousMeasureWidth = measuresWidth[i]
}
staves.push(stave)
}
}
function createBeams() {
for (let i = 0; i < measures; i++) {
beams.push(VF.Beam.generateBeams(notes[i]))
}
}
function drawNotes() {
for (let i = 0; i < staves.length; i++) {
VF.Formatter.FormatAndDraw(context, staves[i], notes[i]);
}
}
function drawBeams() {
for (let i = 0; i < staves.length; i++) {
beams[i].forEach(function (b) {
b.setContext(context).draw()
})
}
}
function scaleZoom() {
resetAll()
scale = 2;
context.scale(scale, scale);
runAllExceptNotes()
}
function drawStaves() {
console.log(systems)
renderer.resize(div.offsetWidth, (scale * staveHeight) * systems + (scale * staveHeight));
for (let i = 0; i < staves.length; i++) {
staves[i].setContext(context).draw()
}
}
function runAllExceptNotes() {
createStaves()
createBeams()
drawStaves()
drawNotes()
drawBeams()
}
</script>
</body>
</html>