forked from ringw/vexflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrokes.js
223 lines (197 loc) · 7.12 KB
/
strokes.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// [VexFlow](http://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
// Author: Larry Kuhns
//
// ## Description
//
// This file implements the `Stroke` class which renders chord strokes
// that can be arpeggiated, brushed, rasquedo, etc.
Vex.Flow.Stroke = (function() {
function Stroke(type, options) {
if (arguments.length > 0) this.init(type, options);
}
Stroke.CATEGORY = "strokes";
Stroke.Type = {
BRUSH_DOWN: 1,
BRUSH_UP: 2,
ROLL_DOWN: 3, // Arpegiated chord
ROLL_UP: 4, // Arpegiated chord
RASQUEDO_DOWN: 5,
RASQUEDO_UP: 6
};
var Modifier = Vex.Flow.Modifier;
// ## Static Methods
// Arrange strokes inside `ModifierContext`
Stroke.format = function(strokes, state) {
var left_shift = state.left_shift;
var stroke_spacing = 0;
if (!strokes || strokes.length === 0) return this;
var str_list = [];
var i, str, shift;
for (i = 0; i < strokes.length; ++i) {
str = strokes[i];
var note = str.getNote();
var props;
if (note instanceof Vex.Flow.StaveNote) {
props = note.getKeyProps()[str.getIndex()];
shift = (props.displaced ? note.getExtraLeftPx() : 0);
str_list.push({ line: props.line, shift: shift, str: str });
} else {
props = note.getPositions()[str.getIndex()];
str_list.push({ line: props.str, shift: 0, str: str });
}
}
var str_shift = left_shift;
var x_shift = 0;
// There can only be one stroke .. if more than one, they overlay each other
for (i = 0; i < str_list.length; ++i) {
str = str_list[i].str;
shift = str_list[i].shift;
str.setXShift(str_shift + shift);
x_shift = Math.max(str.getWidth() + stroke_spacing, x_shift);
}
state.left_shift += x_shift;
return true;
};
// ## Prototype Methods
Vex.Inherit(Stroke, Modifier, {
init: function(type, options) {
Stroke.superclass.init.call(this);
this.note = null;
this.options = Vex.Merge({}, options);
// multi voice - span stroke across all voices if true
this.all_voices = 'all_voices' in this.options ?
this.options.all_voices : true;
// multi voice - end note of stroke, set in draw()
this.note_end = null;
this.index = null;
this.type = type;
this.position = Modifier.Position.LEFT;
this.render_options = {
font_scale: 38,
stroke_px: 3,
stroke_spacing: 10
};
this.font = {
family: "serif",
size: 10,
weight: "bold italic"
};
this.setXShift(0);
this.setWidth(10);
},
getPosition: function() { return this.position; },
addEndNote: function(note) { this.note_end = note; return this; },
draw: function() {
if (!this.context) throw new Vex.RERR("NoContext",
"Can't draw stroke without a context.");
if (!(this.note && (this.index != null))) throw new Vex.RERR("NoAttachedNote",
"Can't draw stroke without a note and index.");
var start = this.note.getModifierStartXY(this.position, this.index);
var ys = this.note.getYs();
var topY = start.y;
var botY = start.y;
var x = start.x - 5;
var line_space = this.note.stave.options.spacing_between_lines_px;
var notes = this.getModifierContext().getModifiers(this.note.getCategory());
var i;
for (i = 0; i < notes.length; i++) {
ys = notes[i].getYs();
for (var n = 0; n < ys.length; n++) {
if (this.note == notes[i] || this.all_voices) {
topY = Vex.Min(topY, ys[n]);
botY = Vex.Max(botY, ys[n]);
}
}
}
var arrow, arrow_shift_x, arrow_y, text_shift_x, text_y;
switch (this.type) {
case Stroke.Type.BRUSH_DOWN:
arrow = "vc3";
arrow_shift_x = -3;
arrow_y = topY - (line_space / 2) + 10;
botY += (line_space / 2);
break;
case Stroke.Type.BRUSH_UP:
arrow = "v11";
arrow_shift_x = 0.5;
arrow_y = botY + (line_space / 2);
topY -= (line_space / 2);
break;
case Stroke.Type.ROLL_DOWN:
case Stroke.Type.RASQUEDO_DOWN:
arrow = "vc3";
arrow_shift_x = -3;
text_shift_x = this.x_shift + arrow_shift_x - 2;
if (this.note instanceof Vex.Flow.StaveNote) {
topY += 1.5 * line_space;
if ((botY - topY) % 2 !== 0) {
botY += 0.5 * line_space;
} else {
botY += line_space;
}
arrow_y = topY - line_space;
text_y = botY + line_space + 2;
} else {
topY += 1.5 * line_space;
botY += line_space;
arrow_y = topY - 0.75 * line_space;
text_y = botY + 0.25 * line_space;
}
break;
case Stroke.Type.ROLL_UP:
case Stroke.Type.RASQUEDO_UP:
arrow = "v52";
arrow_shift_x = -4;
text_shift_x = this.x_shift + arrow_shift_x - 1;
if (this.note instanceof Vex.Flow.StaveNote) {
arrow_y = line_space / 2;
topY += 0.5 * line_space;
if ((botY - topY) % 2 === 0) {
botY += line_space / 2;
}
arrow_y = botY + 0.5 * line_space;
text_y = topY - 1.25 * line_space;
} else {
topY += 0.25 * line_space;
botY += 0.5 * line_space;
arrow_y = botY + 0.25 * line_space;
text_y = topY - line_space;
}
break;
}
// Draw the stroke
if (this.type == Stroke.Type.BRUSH_DOWN ||
this.type == Stroke.Type.BRUSH_UP) {
this.context.fillRect(x + this.x_shift, topY, 1, botY - topY);
} else {
if (this.note instanceof Vex.Flow.StaveNote) {
for (i = topY; i <= botY; i += line_space) {
Vex.Flow.renderGlyph(this.context, x + this.x_shift - 4,
i,
this.render_options.font_scale, "va3");
}
} else {
for (i = topY; i <= botY; i+= 10) {
Vex.Flow.renderGlyph(this.context, x + this.x_shift - 4,
i,
this.render_options.font_scale, "va3");
}
if (this.type == Vex.Flow.Stroke.Type.RASQUEDO_DOWN)
text_y = i + 0.25 * line_space;
}
}
// Draw the arrow head
Vex.Flow.renderGlyph(this.context, x + this.x_shift + arrow_shift_x, arrow_y,
this.render_options.font_scale, arrow);
// Draw the rasquedo "R"
if (this.type == Stroke.Type.RASQUEDO_DOWN ||
this.type == Stroke.Type.RASQUEDO_UP) {
this.context.save();
this.context.setFont(this.font.family, this.font.size, this.font.weight);
this.context.fillText("R", x + text_shift_x, text_y);
this.context.restore();
}
}
});
return Stroke;
}());