@@ -6,7 +6,7 @@ const VF = Vex.Flow;
66// Drawing basic score elements
77// ============================
88
9- function drawSlice ( slice , staffType , scale , useIDs , styles ) {
9+ function drawSlice ( slice , staffType , scale , id_pfx , styles ) {
1010 let div = createSvgElt ( "g" ) ;
1111 let renderer = new VF . Renderer ( div , VF . Renderer . Backends . SVG ) ;
1212 //renderer.resize(500, 500);
@@ -27,12 +27,11 @@ function drawSlice(slice, staffType, scale, useIDs, styles) {
2727 } ) ;
2828
2929 // add IDs
30- if ( useIDs ) {
31- for ( let i = 0 ; i < notes . length ; i ++ ) {
32- chord . _noteHeads [ i ] . attrs . id = notes [ i ] . id ;
33- }
34- chord . attrs . id = `slice${ slice . id } -${ clef } ` ;
30+ for ( let i = 0 ; i < notes . length ; i ++ ) {
31+ chord . _noteHeads [ i ] . attrs . id = notes [ i ] . id ;
3532 }
33+ chord . attrs . id = `${ id_pfx } slice${ slice . id } -${ clef } ` ;
34+
3635
3736 // add style attributes
3837 if ( styles ) {
@@ -107,7 +106,7 @@ function drawStaff(width, staffType) {
107106 return elt ;
108107}
109108
110- function drawSystem ( slices , totalWidth , scale , staffType , useIDs , styles = null ) {
109+ function drawSystem ( slices , totalWidth , scale , staffType , id_pfx , styles = null ) {
111110 let container = createSvgElt ( "g" ) ;
112111
113112 // draw staff
@@ -127,7 +126,7 @@ function drawSystem(slices, totalWidth, scale, staffType, useIDs, styles = null)
127126 "translate(" + slice . x + " 0) scale(" + scale + " " + scale + ")" ,
128127 ) ;
129128 sliceG . setAttribute ( "id" , "slice-" + slice . id ) ;
130- sliceG . appendChild ( drawSlice ( slice , staffType , scale , useIDs , styles ) ) ;
129+ sliceG . appendChild ( drawSlice ( slice , staffType , scale , id_pfx , styles ) ) ;
131130
132131 // apply slice styles
133132 if ( styles !== null ) {
@@ -157,12 +156,71 @@ function drawSystem(slices, totalWidth, scale, staffType, useIDs, styles = null)
157156// Drawing a score
158157// ===============
159158
159+ function drawScoreEdge ( container , edge , passing ) {
160+ // compute coordinates
161+ const bbleft = getNoteBBox ( edge . left . id , container ) ;
162+ const bbright = getNoteBBox ( edge . right . id , container ) ;
163+ let x1 = bbleft . x + bbleft . width + markerWidth + 2 ;
164+ let y1 = bbleft . y + bbleft . height / 2 ;
165+ let x2 = bbright . x - markerWidth - 2 ;
166+ let y2 = bbright . y + bbright . height / 2 ;
167+
168+ // compute
169+ let line = createSvgElt ( "line" ) ;
170+ line . setAttribute ( "x1" , x1 ) ;
171+ line . setAttribute ( "y1" , y1 ) ;
172+ line . setAttribute ( "x2" , x2 ) ;
173+ line . setAttribute ( "y2" , y2 ) ;
174+
175+ let classes = "pv-score-edge" ;
176+ if ( passing ) {
177+ line . setAttribute ( "stroke-dasharray" , "6,3" ) ;
178+ classes += " pv-passing" ;
179+ } else {
180+ classes += " pv-regular" ;
181+ }
182+
183+ line . setAttribute ( "class" , classes ) ;
184+
185+ return line ;
186+ } ;
187+
160188export const drawScore =
161- ( slices ) => ( staffType ) => ( totalWidth ) => ( scale ) => {
162- let system = drawSystem ( slices , totalWidth , scale , staffType , false ) ;
189+ ( slices ) => ( transitions ) => ( staffType ) => ( totalWidth ) => ( scale ) => {
190+ // draw system and notes
191+ let system = drawSystem ( slices , totalWidth , scale , staffType , "score-" ) ;
163192 let container = createSvgElt ( "g" ) ;
164193 container . appendChild ( system . staff ) ;
165194 container . appendChild ( system . notes ) ;
195+
196+ // make sure that the container is attached to the DOM (required for computing bboxes)
197+ let fakesvg = createSvgElt ( "svg" ) ;
198+ fakesvg . appendChild ( container ) ;
199+ document . body . appendChild ( fakesvg ) ;
200+
201+ // draw edges
202+ transitions . forEach ( transition => {
203+ let transElt = createSvgElt ( "g" ) ;
204+ transElt . setAttribute ( "id" , `score-transition-${ transition . id } ` ) ;
205+ transElt . setAttribute ( "class" , "pv-score-transition" ) ;
206+
207+ transition . regular . forEach ( ( edge ) => {
208+ transElt . appendChild (
209+ drawScoreEdge ( container , edge , false ) ,
210+ ) ;
211+ } ) ;
212+ transition . passing . forEach ( ( edge ) => {
213+ transElt . appendChild (
214+ drawScoreEdge ( container , edge , true ) ,
215+ ) ;
216+ } ) ;
217+ container . appendChild ( transElt ) ;
218+ } ) ;
219+
220+ // remove container from DOM
221+ fakesvg . remove ( ) ;
222+ container . remove ( ) ;
223+
166224 return container ;
167225 } ;
168226
@@ -371,7 +429,7 @@ export const drawGraph = (graph) => (totalWidth) => (scale) => {
371429 let levelOffset = graph . styles . staffType == "grand" ? 150 : 80 ;
372430 for ( let level = minLevel ; level <= graph . maxd ; level ++ ) {
373431 let levelSlices = graph . slices . filter ( ( s ) => s . depth == level ) ;
374- let levelG = drawSystem ( levelSlices , totalWidth , scale , graph . styles . staffType , true , graph . styles ) ;
432+ let levelG = drawSystem ( levelSlices , totalWidth , scale , graph . styles . staffType , "" , graph . styles ) ;
375433 let transform = `translate(0 ${ ( level - minLevel ) * levelOffset } )` ;
376434 levelG . notes . setAttribute ( "transform" , transform ) ;
377435 levelG . staff . setAttribute (
@@ -417,7 +475,7 @@ export const drawGraph = (graph) => (totalWidth) => (scale) => {
417475 } ) ;
418476
419477 // draw score
420- let score = drawScore ( graph . surfaceSlices ) ( graph . styles . staffType ) ( totalWidth ) ( scale ) ;
478+ let score = drawScore ( graph . surfaceSlices ) ( graph . surfaceTransitions ) ( graph . styles . staffType ) ( totalWidth ) ( scale ) ;
421479 score . setAttribute (
422480 "transform" ,
423481 `translate(0 ${ ( graph . maxd - minLevel + 1 ) * levelOffset } )` ,
0 commit comments