「VexFlow 小節の繰り返し」の版間の差分

提供:yonewiki
(同じ利用者による、間の21版が非表示)
16行目: 16行目:


 いったんまた、これまでの累積の音符を消しましょう。
 いったんまた、これまでの累積の音符を消しましょう。
 前の1,2,4小節の繰り返しはちょうどいいフォントが提供されていますが、それ以外の繰り返し小節数は重ねるためのパーツを合わせて使うみたいなんですけど、文字列を繋げただけでは以下の図のようになるので、メンドクサイことになります。すこしづつつめないといけないです。メンドクサイので綺麗に並べる作業はやらないことにしました。


 
 
38行目: 41行目:
const Instrument2 = new Position(0, 0);
const Instrument2 = new Position(0, 0);
const Stave2Pos = new Position(0, 0);
const Stave2Pos = new Position(0, 0);
const Stave3Pos = new Position(0, 0);
const Stave6Pos = new Position(0, 0);
const Stave10Pos = new Position(0, 0);
const Stave11Pos = new Position(0, 0);
(function(){
(function(){
const {
const {
119行目: 126行目:
Instrument1.y = stave1.y + 90;  
Instrument1.y = stave1.y + 90;  


const notes1 = [
  new StaveNote({ keys: ["c/4"], duration: "4" }),
  new StaveNote({ keys: ["d/4"], duration: "4" }),
  new StaveNote({ keys: ["e/4"], duration: "4" }),
  new StaveNote({ keys: ["f/4"], duration: "4" })
];
var stave1Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave1Voice.setStrict(false);
stave1Voice.addTickables(notes1);
var formatter1 = new Vex.Flow.Formatter().joinVoices([stave1Voice]);
formatter1.formatToStave([stave1Voice], stave1);
stave1Voice.draw(ctx, stave1);


const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
131行目: 152行目:
stave3.setMeasure(3);
stave3.setMeasure(3);
stave3.draw();
stave3.draw();
Stave3Pos.x = stave3.x;
Stave3Pos.y = stave3.y;


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
167行目: 191行目:
stave6.draw();
stave6.draw();


Stave6Pos.x = stave6.x;
Stave6Pos.y = stave6.y;


const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
197行目: 223行目:
stave10.draw();
stave10.draw();


Stave10Pos.x = stave10.x;
Stave10Pos.y = stave10.y;


const stave11 = new Stave(stave10.width + stave10.x, stave9.y, nStaveWidth).setContext(ctx);
const stave11 = new Stave(stave10.width + stave10.x, stave9.y, nStaveWidth).setContext(ctx);
202行目: 230行目:
stave11.draw();
stave11.draw();


Stave11Pos.x = stave11.x;
Stave11Pos.y = stave11.y;


const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);
const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);
254行目: 284行目:
// テキスト要素の作成
// テキスト要素の作成
var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText2.setAttribute('id', 'rest9'); // テキスト要素にIDを付ける
newText2.setAttribute('id', 'rest2'); // テキスト要素にIDを付ける
newText2.setAttribute('x', 0);
newText2.setAttribute('x', 0);
newText2.setAttribute('y', 0);
newText2.setAttribute('y', 0);
266行目: 296行目:


(function(){
(function(){
     var textWidth = document.getElementById('rest9').getBBox().width;
     var textWidth = document.getElementById('rest2').getBBox().width;
     var textHeight = document.getElementById('rest9').getBBox().height;
     var textHeight = document.getElementById('rest2').getBBox().height;


     // クレシェンドの幅に応じて縦方向のスケーリング
     // クレシェンドの幅に応じて縦方向のスケーリング
273行目: 303行目:
     var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。
     var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。


     var StavePos_x = Stave2Pos.x + 120;
     var StavePos_x = Stave2Pos.x + 170;
     var StavePos_y = Stave2Pos.y + 60;
     var StavePos_y = Stave2Pos.y + 60;


284行目: 314行目:


// クレシェンド記号のフォント設定
// クレシェンド記号のフォント設定
var fontSize9_1 = 40; // フォントサイズ
var fontSize3 = 40; // フォントサイズ
var fontFamily9_1 = "Bravura, Arial"; // Bravuraフォントファミリー
var fontFamily3 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent9_1 = "\uE081\uE089";//複数小節休符 // 符頭E0A4
var textContent3 = "\uE501";//複数小節休符 // 符頭E0A4
 
// テキスト要素の作成
var newText3 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText3.setAttribute('id', 'rest3'); // テキスト要素にIDを付ける
newText3.setAttribute('x', 0);
newText3.setAttribute('y', 0);
newText3.setAttribute('font-family', fontFamily3);
newText3.setAttribute('font-size', fontSize3);
newText3.setAttribute('fill', 'black');
newText3.textContent = textContent3;
 
// SVG要素にテキストを追加
svgElement.appendChild(newText3);
 
(function(){
    var textWidth = document.getElementById('rest3').getBBox().width;
    var textHeight = document.getElementById('rest3').getBBox().height;
 
    // クレシェンドの幅に応じて縦方向のスケーリング
    var horizontalScale = 1;//crescendo9Width / textWidth;
    var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。
 
    var StavePos_x = Stave3Pos.x + 335;
    var StavePos_y = Stave3Pos.y + 60;
 
    newText3.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');
 
    // クレシェンド記号をSVG要素に追加する
    svgElement.appendChild(newText3);
})();
 
// クレシェンド記号のフォント設定
var fontSize6 = 40; // フォントサイズ
var fontFamily6 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent6 = "\uE502";//複数小節休符 // 符頭E0A4
 
// テキスト要素の作成
var newText6 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText6.setAttribute('id', 'rest6'); // テキスト要素にIDを付ける
newText6.setAttribute('x', 0);
newText6.setAttribute('y', 0);
newText6.setAttribute('font-family', fontFamily6);
newText6.setAttribute('font-size', fontSize6);
newText6.setAttribute('fill', 'black');
newText6.textContent = textContent6;
 
// SVG要素にテキストを追加
svgElement.appendChild(newText6);
 
(function(){
    var textWidth = document.getElementById('rest6').getBBox().width;
    var textHeight = document.getElementById('rest6').getBBox().height;
 
    // クレシェンドの幅に応じて縦方向のスケーリング
    var horizontalScale = 1;//crescendo9Width / textWidth;
    var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。
 
    var StavePos_x = Stave6Pos.x + 325;
    var StavePos_y = Stave6Pos.y + 60;
 
    newText6.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');
 
    // クレシェンド記号をSVG要素に追加する
    svgElement.appendChild(newText6);
})();
 
// クレシェンド記号のフォント設定
var fontSize10 = 40; // フォントサイズ
var fontFamily10 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent10 = "\uE503\uE504\uE504\uE504\uE504\uE505";//複数小節休符 // 符頭E0A4


// テキスト要素の作成
// テキスト要素の作成
var newText9_1 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
var newText10 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText9_1.setAttribute('id', 'rest9_1'); // テキスト要素にIDを付ける
newText10.setAttribute('id', 'rest10'); // テキスト要素にIDを付ける
newText9_1.setAttribute('x', 0);
newText10.setAttribute('x', 0);
newText9_1.setAttribute('y', 0);
newText10.setAttribute('y', 0);
newText9_1.setAttribute('font-family', fontFamily9_1);
newText10.setAttribute('font-family', fontFamily10);
newText9_1.setAttribute('font-size', fontSize9_1);
newText10.setAttribute('font-size', fontSize10);
newText9_1.setAttribute('fill', 'black');
newText10.setAttribute('fill', 'black');
newText9_1.textContent = textContent9_1;
newText10.textContent = textContent10;


// SVG要素にテキストを追加
// SVG要素にテキストを追加
svgElement.appendChild(newText9_1);
svgElement.appendChild(newText10);


(function(){
(function(){
     var textWidth = document.getElementById('rest9_1').getBBox().width;
     var textWidth = document.getElementById('rest10').getBBox().width;
     var textHeight = document.getElementById('rest9_1').getBBox().height;
     var textHeight = document.getElementById('rest10').getBBox().height;


     // クレシェンドの幅に応じて縦方向のスケーリング
     // クレシェンドの幅に応じて縦方向のスケーリング
309行目: 409行目:
     var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。
     var verticalScale = 1;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。


     var StavePos_x = Stave2Pos.x + 205;
     var StavePos_x = Stave10Pos.x + 325;
     var StavePos_y = Stave2Pos.y + 40;
     var StavePos_y = Stave10Pos.y + 60;


     newText9_1.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');
     newText10.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');


     // クレシェンド記号をSVG要素に追加する
     // クレシェンド記号をSVG要素に追加する
     svgElement.appendChild(newText9_1);
     svgElement.appendChild(newText10);
})();
})();


function insertTextToSVG(options) {
    var textContent = options.textContent;
    var strID = options.strID;
    var Stave_x = options.Stave_x;
    var Stave_y = options.Stave_y;
    var Stave_x_Offset = options.Stave_x_Offset || 0;
    var Stave_y_Offset = options.Stave_y_Offset || 0;
    var StaveWidth = options.StaveWidth || 0;
    var StaveHeight = options.StaveHeight || 0;
    var StaveVerticalScale = options.StaveVerticalScale || 0;
    var fontSize = options.fontSize || 40;
    var fontFamily = options.fontFamily || "Bravura, Arial";
    var shift_x = options.shift_x || [0];
    var offset_sum = 0;
    shift_x.forEach(function(offset, index) {
        offset_sum = offset_sum + offset;
        var newText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
        newText.setAttribute('id', strID + '_' + index); // テキスト要素にIDを付ける
        newText.setAttribute('x', 0);
        newText.setAttribute('y', 0);
        newText.setAttribute('font-family', fontFamily);
        newText.setAttribute('font-size', fontSize);
        newText.setAttribute('fill', 'black');
        if (Array.isArray(textContent)) {
          newText.textContent = textContent[index];
        }
        else{
          newText.textContent = textContent;
        }
        (function() {
            var textWidth = newText.getBBox().width;
            var textHeight = newText.getBBox().height;
            var horizontalScale;
            if (typeof Stave10Width === 'undefined' || Stave10Width == 0) {
                horizontalScale = 1;
            } else {
                horizontalScale = StaveWidth / textWidth;
            }
            var verticalScale;
            if (typeof StaveHeight === 'undefined' || StaveHeight == 0) {
                verticalScale = 1;
            } else if (StaveVerticalScale != 0) {
                verticalScale = StaveVerticalScale;
            } else {
                verticalScale = StaveHeight / textHeight;
            }
            var StavePos_x = Stave_x + Stave_x_Offset + offset_sum;
            var StavePos_y = Stave_y + Stave_y_Offset;
            newText.setAttribute('transform', 'translate(' + StavePos_x + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');
        })();
        svgElement.appendChild(newText);
    });
}
var options = {
    textContent: ["\uE503", "\uE504", "\uE504", "\uE504", "\uE504", "\uE504", "\uE504", "\uE504", "\uE504", "\uE505"], // 必須文字列
    strID: "rest11", // 必須
    Stave_x: Stave11Pos.x,
    Stave_y: Stave11Pos.y,
    Stave_x_Offset: 305, // オプション、デフォルトは0
    Stave_y_Offset: 60, // オプション、デフォルトは0
    StaveWidth: 0, // オプション、デフォルトは0
    StaveHeight: 0, // オプション、デフォルトは0
    StaveVerticalScale: 0, // オプション、デフォルトは0
    fontSize: 40, // オプション、デフォルトは40
    fontFamily: "Bravura, Arial", // オプション、デフォルトは"Bravura, Arial"
    shift_x: [0, 0, 10, 10, 10, 10, 10, 10, 10, 15] // xのオフセット値配列
};
insertTextToSVG(options);


})();
})();
643行目: 817行目:
stave11.draw();
stave11.draw();


Stave11Pos.x = stave11.x;
Stave11Pos.y = stave11.y;


const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);
const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);

2024年3月27日 (水) 23:57時点における版

VexFlow 使い方に戻る。

概要

 小節の繰り返し(Bar Repeat)はVexFlowでは描画する術は提供されていません。なので、この記事を作る必要はないのですが、じゃあどうやって実現するの?っていうのもあるとおもうので、最低限のアンサーを示そうとする管理人の粋なはからいにより存在する記事です。


 そうです。このVexFlowの記事をずっと読んでくれている人にはわかると思います。例の奥の手を発動する感じです。うまくできるかは見守っていただければと思います。時間かかるだろうな。この記事がいろいろ変化している間は格闘中だと思って下さい。


 え?なんか同じ文章を前の項目でも見た?そうなんす。そうなんす。また大変な表記ナンス。なんす?ってなんす?


 いったんまた、これまでの累積の音符を消しましょう。


 前の1,2,4小節の繰り返しはちょうどいいフォントが提供されていますが、それ以外の繰り返し小節数は重ねるためのパーツを合わせて使うみたいなんですけど、文字列を繋げただけでは以下の図のようになるので、メンドクサイことになります。すこしづつつめないといけないです。メンドクサイので綺麗に並べる作業はやらないことにしました。

 

小節の繰り返し


 コードは以下のとおりです。


<div id="yonet202403Mid_Output01"></div>
<script>
///////////////////////
(function(){
class Position {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}
var VF = Vex.Flow;
const Instrument1 = new Position(0, 0);
const Instrument2 = new Position(0, 0);
const Stave9Pos = new Position(0, 0);
(function(){
const {
  Factory,
  Stave,
  StaveNote,
  Formatter,
  Voice,
  StaveTie,
  BarLine,
  Beam,
  Dot,
  StaveConnector,
  Modifier,
  RenderContext,
  TextDynamics,
  Articulation,
  TextNote,
  Crescendo,
  Renderer
} = Vex.Flow;

let nscale = 0.64;
let nwidth = 1040;
let nheight = 720;

let nHeadMargin = 70;
let nStaveWidth = 350;

const f = new Factory({ 
  renderer: { elementId: 'yonet202403Mid_Output01', width: nwidth, height: nheight * nscale }
  });
const ctx = f.getContext();
ctx.scale(nscale, nscale);

const defaultStyle = {
  lineWidth: ctx.lineWidth,
  strokeStyle: ctx.strokeStyle,
};



for (let i = 0; i <= nwidth * (1 / nscale); i += 10) {
  ctx.beginPath();
  ctx.moveTo(0 + i, 0);
  ctx.lineTo(0 + i, nheight);
  if (i % 100 === 0){
    ctx.setLineWidth(1);
    ctx.setStrokeStyle('rgba(200, 200, 200, 0.8)');
  }
  else{
    ctx.setLineWidth(0.5);
    ctx.setStrokeStyle('rgba(230, 230, 230, 0.8)');
  }
  ctx.stroke();
}

for (let i = 0; i <= nheight; i += 10) {
  ctx.beginPath();
  ctx.moveTo(0, i);
  ctx.lineTo(nwidth * (1 / nscale), i);
  if (i % 100 === 0){
    ctx.setLineWidth(1);
    ctx.setStrokeStyle('rgba(200, 200, 200, 0.8)');
  }
  else{
    ctx.setLineWidth(0.5);
    ctx.setStrokeStyle('rgba(230, 230, 230, 0.8)');
  }
  ctx.stroke();
}

ctx.lineWidth = defaultStyle.lineWidth;
ctx.strokeStyle = defaultStyle.strokeStyle;

const stave1 = new Stave(80, 150, nHeadMargin + nStaveWidth).setContext(ctx);
stave1.setClef("treble").setContext(ctx);
stave1.setTimeSignature('4/4').setContext(ctx);
stave1.setMeasure(1);
stave1.draw();

Instrument1.x = stave1.x - 30;
Instrument1.y = stave1.y + 90; 

const gracenotes1 = [
  new VF.GraceNote({ keys: ['b/4'], duration: '8', slash: true })
];

const notes1 = [
  new StaveNote({ keys: ["c/4", "e/4", "g/4"], duration: "4" }),
  new StaveNote({ keys: ["d/4", "f/4", "a/4"], duration: "4" }),
  new StaveNote({ keys: ["e/4", "g/4", "b/4"], duration: "4" }),
  new StaveNote({ keys: ["f/4", "a/4", "c/5"], duration: "4" })
];

notes1[0].addModifier(new VF.Accidental({type:'#'}), 0);
notes1[0].addModifier(new VF.GraceNoteGroup({notes: gracenotes1, slur: true}), 0);//Vex.Flow.Modifier.Position.LEFT

var stave1Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave1Voice.setStrict(false);
stave1Voice.addTickables(notes1);

var formatter1 = new Vex.Flow.Formatter().joinVoices([stave1Voice]);
formatter1.formatToStave([stave1Voice], stave1);
stave1Voice.draw(ctx, stave1);

const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
stave2.setMeasure(2);
stave2.setContext(ctx).draw();

const gracenotes2 = [{ keys: ['d/4'], duration: '8', slash: false }].map(f.GraceNote.bind(f));

const notes2 = [
        f.StaveNote({ keys: ['c/4'], duration: '4', auto_stem: true }),
        f.StaveNote({ keys: ["d/4"], duration: "4" }),
        f.StaveNote({ keys: ["e/4"], duration: "4" }),
        f.StaveNote({ keys: ["f/4"], duration: "4" })
];
notes2[0].addModifier(f.GraceNoteGroup({ notes: gracenotes2, slur: true }), 0);
const stave2Voice = f.Voice({ num_beats: 4, beat_value: 4 }).setStrict(false).addTickables(notes2);
var formatter2 = new VF.Formatter().joinVoices([stave2Voice]);
formatter2.formatToStave([stave2Voice], stave2);
f.draw();


const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
stave3.setMeasure(3);
stave3.draw();


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
stave4.setMeasure(4);
stave4.draw();

const notes4 = [
  new StaveNote({ keys: ["c/4", "e/4", "g/4", "b/4"], duration: "4" }),
  new StaveNote({ keys: ["d/4", "f/4", "a/4", "c/5"], duration: "4" }),
  new StaveNote({ keys: ["e/4", "g/4", "b/4", "d/5"], duration: "4" }),
  new StaveNote({ keys: ["f/4", "a/4", "c/5", "e/5"], duration: "4" })
];

var stave4Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave4Voice.addTickables(notes4);

var formatter4 = new Vex.Flow.Formatter().joinVoices([stave4Voice]);
formatter4.format([stave4Voice], nStaveWidth);

stave4Voice.draw(ctx, stave4);

const stave5 = new Stave(stave1.x, stave1.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
stave5.addClef("treble").setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.addKeySignature('E').setContext(ctx);
stave5.setMeasure(5);
stave5.draw();

var connector = new VF.StaveConnector(stave1, stave5);
connector.setType(VF.StaveConnector.type.SINGLE);
connector.setContext(ctx);
connector.draw();

const gracenotes5 = [
  new VF.GraceNote({ keys: ['f/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['g/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['b/5'], duration: '16', slash: false }),
];

const notes5 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["c/6"], duration: "2", stem_direction: -1 })
];

notes5[2].addModifier(new VF.GraceNoteGroup(gracenotes5, true).beamNotes(), 0);

var stave5Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave5Voice.setStrict(false);
stave5Voice.addTickables(notes5);

var formatter5 = new Vex.Flow.Formatter().joinVoices([stave5Voice]);
formatter5.format([stave5Voice], nStaveWidth);

stave5Voice.draw(ctx, stave5);

function dotted(staveNote, noteIndex = -1) {
  if (noteIndex < 0) {
    Dot.buildAndAttach([staveNote], {
      all: true
    });
  } 
  else {
    for(noteIndexValue of noteIndex){
      Dot.buildAndAttach([staveNote], {
        index: noteIndexValue
      });
    }
  }
  return staveNote;
}

const stave6 = new Stave(stave5.width + stave5.x, stave5.y, nStaveWidth).setContext(ctx);
stave6.setMeasure(6);
stave6.draw();

const notes6 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1  }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1  }),
  dotted(new StaveNote({ keys: ["f/5"], duration: "4d", stem_direction: -1  })),
  new StaveNote({ keys: ["e/5"], duration: "16", stem_direction: -1  }),
  new StaveNote({ keys: ["f/5"], duration: "16", stem_direction: -1  })
];
//const notes6beam = notes6[3].concat(notes6[4]);
const beam6 = new VF.Beam([notes6[3],notes6[4]]);

var stave6Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave6Voice.addTickables(notes6);

var formatter6 = new Vex.Flow.Formatter().joinVoices([stave6Voice]);
formatter6.format([stave6Voice], nStaveWidth);

stave6Voice.draw(ctx, stave6);
beam6.setContext(ctx).draw();

const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
stave7.setMeasure(7);
stave7.draw();

const gracenotes7 = [
  new VF.GraceNote({ keys: ['e/6'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['f/6'], duration: '16', slash: false })
];

const notes7 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["g/6"], duration: "2", stem_direction: -1 })
];

notes7[2].addModifier(new VF.GraceNoteGroup(gracenotes7, true).beamNotes(), 0);

var stave7Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave7Voice.setStrict(false);
stave7Voice.addTickables(notes7);

var formatter7 = new Vex.Flow.Formatter().joinVoices([stave7Voice]);
formatter7.format([stave7Voice], nStaveWidth);

stave7Voice.draw(ctx, stave7);

const stave8 = new Stave(stave7.width + stave7.x, stave5.y, nStaveWidth).setContext(ctx);
stave8.setMeasure(8);
stave8.draw();

const notes8 = [
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["f/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "2", stem_direction: -1  })
];

const beam8_1 = new VF.Beam([notes8[0],notes8[1]]);
const beam8_2 = new VF.Beam([notes8[2],notes8[3]]);

var stave8Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave8Voice.addTickables(notes8);

var formatter8 = new Vex.Flow.Formatter().joinVoices([stave8Voice]);
formatter8.format([stave8Voice], nStaveWidth);

stave8Voice.draw(ctx, stave8);
beam8_1.setContext(ctx).draw();
beam8_2.setContext(ctx).draw();

const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
stave9.setMeasure(9);
stave9.draw();

var connector9 = new VF.StaveConnector(stave5, stave9);
connector9.setType(VF.StaveConnector.type.SINGLE);
connector9.setContext(ctx);
connector9.draw();

var connector9_2 = new VF.StaveConnector(stave5, stave9);
connector9_2.setType(VF.StaveConnector.type.BRACKET);
connector9_2.setContext(ctx);
connector9_2.draw();

Instrument2.x = stave1.x - 30;
Instrument2.y = stave5.y + ((stave9.y - stave5.y) / 2) + 90; 

Stave9Pos.x = stave9.x;
Stave9Pos.y = stave9.y;

const stave10 = new Stave(stave9.width + stave9.x, stave9.y, nStaveWidth).setContext(ctx);
stave10.setMeasure(10);
stave10.draw();


const stave11 = new Stave(stave10.width + stave10.x, stave9.y, nStaveWidth).setContext(ctx);
stave11.setMeasure(11);
stave11.draw();

Stave11Pos.x = stave11.x;
Stave11Pos.y = stave11.y;

const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);
stave12.setMeasure(12);
stave12.draw();

})();

const svgElement2 = document.querySelector('#yonet202403Mid_Output01 svg');
svgElement2.style.backgroundColor = '#F5F5F5';
var svgContainer = document.getElementById('yonet202403Mid_Output01');
var svgElement = svgContainer.querySelector('svg');

var newText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText.setAttribute('x', '100');
newText.setAttribute('y', '100');
newText.setAttribute('font-family', 'Hiragino Kaku Gothic Pro, メイリオ, MS Gothic, sans-serif');
newText.setAttribute('font-size', '50');
newText.setAttribute('fill', 'black');
newText.textContent = 'TestTitle - subTitle - lyrics: yo-net, music: yo-net';
svgElement.appendChild(newText);

var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText2.setAttribute('x', Instrument1.x);
newText2.setAttribute('y', Instrument1.y);
newText2.setAttribute('font-family', 'Bravura, VexFlow, Arial, sans-serif');
newText2.setAttribute('font-size', '20pt');
newText2.setAttribute('fill', 'black');
newText2.textContent = 'Vocal';
var textGroup2 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup2.setAttribute('transform', 'rotate(-90 ' + Instrument1.x + ' ' + Instrument1.y + ')');
textGroup2.appendChild(newText2);
svgElement.appendChild(textGroup2);

var newText3 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText3.setAttribute('x', Instrument2.x);
newText3.setAttribute('y', Instrument2.y);
newText3.setAttribute('font-family', 'Bravura, VexFlow, Arial, sans-serif');
newText3.setAttribute('font-size', '20pt');
newText3.setAttribute('fill', 'black');
newText3.textContent = 'E.Guitar';
var textGroup3 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup3.setAttribute('transform', 'rotate(-90 ' + Instrument2.x + ' ' + Instrument2.y + ')');
textGroup3.appendChild(newText3);
svgElement.appendChild(textGroup3);

//フォント設定
var fontSize9 = 40; // フォントサイズ
var fontFamily9 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent9 = "\uE4EF\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F0\uE4F1";//複数小節休符 E4EFが始まり、E4F0が途中、E4F1が終了記号// 符頭はE0A4

// テキスト要素の作成
var newText9 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText9.setAttribute('id', 'rest9'); // テキスト要素にIDを付ける
newText9.setAttribute('x', 0);
newText9.setAttribute('y', 0);
newText9.setAttribute('font-family', fontFamily9);
newText9.setAttribute('font-size', fontSize9);
newText9.setAttribute('fill', 'black');
newText9.textContent = textContent9;

// SVG要素にテキストを追加
svgElement.appendChild(newText9);

(function(){
    var textWidth = document.getElementById('rest9').getBBox().width;
    var textHeight = document.getElementById('rest9').getBBox().height;

    //スケーリング
    var horizontalScale = 1;
    var verticalScale = 1;

    var StavePos_x = Stave9Pos.x + 120;
    var StavePos_y = Stave9Pos.y + 60;

    newText9.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');

    //SVG要素に追加する
    svgElement.appendChild(newText9);
})();


// 複数小節休符番号記号のフォント設定
var fontSize9_1 = 40; // フォントサイズ
var fontFamily9_1 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent9_1 = "\uE081\uE089";//複数小節休符番号 // 符頭E0A4

// テキスト要素の作成
var newText9_1 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText9_1.setAttribute('id', 'rest9_1'); // テキスト要素にIDを付ける
newText9_1.setAttribute('x', 0);
newText9_1.setAttribute('y', 0);
newText9_1.setAttribute('font-family', fontFamily9_1);
newText9_1.setAttribute('font-size', fontSize9_1);
newText9_1.setAttribute('fill', 'black');
newText9_1.textContent = textContent9_1;

// SVG要素にテキストを追加
svgElement.appendChild(newText9_1);

(function(){
    var textWidth = document.getElementById('rest9_1').getBBox().width;
    var textHeight = document.getElementById('rest9_1').getBBox().height;

    // スケーリング
    var horizontalScale = 1;
    var verticalScale = 1;

    var StavePos_x = Stave9Pos.x + 205;
    var StavePos_y = Stave9Pos.y + 40;

    newText9_1.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');

    // SVG要素に追加する
    svgElement.appendChild(newText9_1);
})();


})();
</script>


 VexFlowの技術とは関係のない記事になってしまいましたが、またひとつ奥の手の使い方を示せましたね。


 newText9というSVGエレメントのテキストタグを生成するようなオブジェクトを作り、


  • var newText9 = document.createElementNS('http://www.w3.org/2000/svg', 'text');


 一度生成してから、そのサイズとかを取得したいので、


  • newText9.setAttribute('id', 'rest9');


 と、Textタグにidエレメントとその値rest9を定義します。


 最初の作成位置は座標 x = 0, y = 0 にします。拡大縮小処理を後でやると、原点(0 , 0)を中心に拡大するので、ちょっと離れたところに置いただけでも、2倍にするだけでも画面の真ん中に置いたものは画面の外側に移動してしまいますので、原点に配置した状態で拡大します。


  • newText9.setAttribute('x', 0); newText9.setAttribute('y', 0);


 そして、font-familyのフォント名とfont-sizeのフォントサイズを指定します。フォントサイズ40くらいがぴったりはまりますね。拡大縮小はあまり使わない方がいいので、あとで、縦横比を調整する程度で考えておきましょう。


  • newText9.setAttribute('font-family', fontFamily9); newText9.setAttribute('font-size', fontSize9);


 フォントの塗りは黒を念のため指定。


  • newText9.setAttribute('fill', 'black');


 テキストの内容を以下のメンバ変数に設定します。


  • newText9.textContent


 複数小節の記号はSMuFLフォントでE4EFが始まり、E4F0が途中、E4F1が終了を形成するので文字列のようにつなげると、幅が調節できます。始まりと終了だけを両サイドにおけばうまく描けるようになっています。便利ですね。


  • svgElement.appendChild(newText9_1);


 で予め取得していたsvgタグの中にnewText9_1が保持しているタグは挿入されます。


  • var svgContainer = document.getElementById('yonet202403Mid_Output01');
  • var svgElement = svgContainer.querySelector('svg');


 で、予め取得されていますね。でもこのままでは、挿入されたタグは原点にテキストを描画するので、何も表示されない可能性もあります。


 以下のような即時実行関数によって


(function(){
    newText9.setAttribute('transform', 'translate(' + StavePos_x  + ',' + StavePos_y + ') scale(' + horizontalScale + ',' + verticalScale + ')');

    // SVG要素に追加する
    svgElement.appendChild(newText9_1);
}();


 差し込んだタグを更新します。newText9という一つのタグを追従しているから更新されるように見えるのです。エレメントの移動は同じ場所なので、位置も変わらずにtransformエレメントがtranslate(…) scale(…)という値を伴って追加されます。そういうものなんですね。


 同じ要領で、何小節休むのかと言う意味の19も追加しています。大きな太字の数字は拍子(Time Signature)表示に使う数字のフォントを利用しています。しっくりくる奴ですよね。コードポイントのE080~E089までが下一桁の数字と表示される数字とが連動する形になっています。

 

VexFlow 使い方に戻る。