Just a note in the process of making my own editor for the online components I’ve made for MIT's Music Fundamentals class.
In detecting note selections, the hardcoded “ - 10” in formatter.js was the hardest bug I found to debug in making a function that could figure out what note had been clicked in Vexflow if the canvas does not begin at left: 10. I’ve found that to put a piano brace I need left: 20, and because of the data format, I’ve been drawing additional measures as new staves placed after the first.
formatToStave: function(voices, stave, options) {
var justifyWidth = stave.getNoteEndX() - stave.getNoteStartX() - 10;
The 10 pixels needs to be compensated for in getting the x position of the note.
var vfn = new Vex.Flow.Note( … )
… do all your stave drawing, etc. then with nextTicks set to 0 at first...
var nextTicks = 0;
var fixVexflowPadding = 10;
… iterate over each note as vfn...
var nTicks = parseInt(vfn.ticks);
var formatterNote = formatter.tContexts.map[String(nextTicks)];
nextTicks += nTicks;
var actualXPositionFromCanvasStart = formatterNote.x + stave.start_x + stave.glyph_start_x - stave.x + fixVexflowPadding;
then that can be compared to the actual pixel clicked in the document.
// mouse event handler code from:
http://diveintohtml5.org/canvas.html
var xClick, yClick;
if (e.pageX != undefined && e.pageY != undefined) {
xClick = e.pageX;
yClick = e.pageY;
} else {
xClick = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
yClick = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
var offset = $(canvas).offset();
var xPx = (xClick - offset.left);
var yPx = (yClick - offset.top);
var lineSpacing = stave.options.spacing_between_lines_px;
var linesAboveStaff = stave.options.space_above_staff_ln;
var totalLines = (stave.options.num_lines - 1) + linesAboveStaff + stave.options.space_below_staff_ln;
var pixelScaling = totalLines * lineSpacing/$(canvas).height();
var yPxScaled = yPx * pixelScaling;
var notesFromTop = yPxScaled * 2 / lineSpacing;
var notesAboveFirstLine = Math.round(((storedVFStave.options.num_lines - 1 + linesAboveStaff) * 2 )- notesFromTop);
xPxScaled = xPx * pixelScaling;
then compare xPxScaled to actualXPositionFromCanvasStart +/– a window and the note can be identified.
this assumes that the canvas has not been distorted from the correct proportions. If it has then you’ll need a separate X and Y pixel scaling.
Hope that this helps others in their work!
P.S. — the winning bidder on the project bid $250.
Best,
Myke
> --
> ---
> You received this message because you are subscribed to the Google Groups "vexflow" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
vexflow+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
--- ---
Michael Scott Cuthbert
Homer A. Burnell Career Development Professor, MIT
Associate Professor of Music
+1 (413) 575-6024
cuth...@mit.edu http://www.trecento.com