VexFlow 楽器名記載

提供:yonewiki

VexFlow 使い方に戻る。

概要

 スコア譜のように小節の最初に楽器名を記載する方法ついて説明します。縦書きができるといいのですが、できなさそうな感じです。でも、この後の曲名記載の技術を使えばまぁできるんでしょうね。知恵を絞ってVexFlowの中の人の斜め上くらいをいきたいですね。


 Staveのメンバ関数であるsetTextで機能を提供しています。第一引数に文字列リテラルを指定し、第二引数にVF.Modifier.Position.LEFTのような表示位置引数と第三引数にX, Y方向へのオフセットを付与します。 {shift_x: 0, shift_y: 0} のような感じです。


 あるいは、StaveConnectorのメンバ関数であるsetTextで機能を提供しています。第一引数に文字列リテラルを付与するだけです。以下のとおりの表示になります。


 Staveのメンバ関数であるsetTextの第二引数は以下のような値になっています。


コード 表示内容
Modifier.Position.LEFT 小節の左にテキストを追加
Modifier.Position.RIGHT 小節の右にテキストを追加
Modifier.Position.ABOVE 小節の上にテキストを追加
Modifier.Position.BELOW 小節の下にテキストを追加


 

楽器名の記述

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

<div id="yonet202403Mid_Output01"></div>
<script>
var VF = Vex.Flow;
(function(){
const {
  Factory,
  Stave,
  StaveNote,
  Formatter,
  Voice,
  StaveTie,
  BarLine,
  StaveConnector,
  Modifier,
  Renderer
} = Vex.Flow;

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

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(130, 50, nHeadMargin + nStaveWidth).setContext(ctx);
stave1.setClef("treble").setContext(ctx);
stave1.setTimeSignature('4/4').setContext(ctx);
stave1.setSection("A", 0);
stave1.setMeasure(1);
stave1.setText("Vocal", VF.Modifier.Position.LEFT, {shift_y: 0});
//stave1.setBegBarType(4);//BarLine.type.REPEAT_BEGIN
stave1.setBegBarType(VF.Barline.type.REPEAT_BEGIN);//BarLine.type.REPEAT_BEGIN
stave1.draw();

//SINGLE:1, DOUBLE:2, END:3, REPEAT_BEGIN:4, REPEAT_END:5, REPEAT_BOTH:6, NONE:7

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

Formatter.FormatAndDraw(ctx, stave1, notes1);

const tie1 = new StaveTie({
  first_note: notes1[1],
  last_note: notes1[2],
  first_indices: [0],
  last_indices: [0],
});

const tie2 = new StaveTie({
  first_note: notes1[2],
  last_note: notes1[3],
  first_indices: [0],
  last_indices: [0],
});

tie1.setContext(ctx).draw();
tie2.setContext(ctx).draw();


const stave2 = new Stave(stave1.width + stave1.x, stave1.y, nStaveWidth).setContext(ctx);
stave2.setEndBarType(VF.Barline.type.REPEAT_BOTH);
stave2.setMeasure(2);
stave2.draw();


const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
stave3.setEndBarType(VF.Barline.type.NONE);
stave3.setSection("B", 0);
stave3.setMeasure(3);
stave3.draw();


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
stave4.setBegBarType(VF.Barline.type.NONE);
stave4.setEndBarType(VF.Barline.type.REPEAT_END);
stave4.setMeasure(4);
stave4.draw();


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.setSection("C", 0);
stave5.setMeasure(5);
stave5.setEndBarType(VF.Barline.type.SINGLE);//規定値(省略可能)

stave5.draw();

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

const notes5 = [
  new StaveNote({ keys: ["a/4"], duration: "q" }),
  new StaveNote({ keys: ["b/4"], duration: "q" }),
  new StaveNote({ keys: ["c/5"], duration: "q" }),
  new StaveNote({ keys: ["d/5"], duration: "q" }),
];

Formatter.FormatAndDraw(ctx, stave5, notes5);

const tie3 = new StaveTie({
  first_note: notes5[1],
  last_note: notes5[2],
  first_indices: [0],
  last_indices: [0],
});

const tie4 = new StaveTie({
  first_note: notes5[2],
  last_note: notes5[3],
  first_indices: [0],
  last_indices: [0],
});

tie3.setContext(ctx).draw();
tie4.setContext(ctx).draw();

const stave6 = new Stave(stave5.width + stave5.x, stave5.y, nStaveWidth).setContext(ctx);
stave6.setEndBarType(VF.Barline.type.DOUBLE);
stave6.setMeasure(6);

stave6.draw();

const notes6 = [
  new StaveNote({ keys: ["e/5"], duration: "q" }),
  new StaveNote({ keys: ["f/5"], duration: "q" }),
  new StaveNote({ keys: ["g/5"], duration: "q" }),
  new StaveNote({ keys: ["a/5"], duration: "q" }),
];

Formatter.FormatAndDraw(ctx, stave6, notes6);

const tie5 = new StaveTie({
  first_note: notes5[3],
  last_note: notes6[0],
  first_indices: [0],
  last_indices: [0],
});

tie5.setContext(ctx).draw();


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


const stave8 = new Stave(stave7.width + stave7.x, stave5.y, nStaveWidth).setContext(ctx);
stave8.setMeasure(8);
stave8.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.setText('E.Guitar');
connector9_2.draw();


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();


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

})();

const svgElement = document.querySelector('#Output svg');
svgElement.style.backgroundColor = '#F5F5F5';
</script>


StaveConnectorというクラスを生成するときにコンストラクタの引数として、線を引く、2つの小節つまりはStaveオブジェクトを設定します。


第1引数: 一つ目のStaveオブジェクト


第2引数: 二つ目のStaveオブジェクト


という感じで指定するだけです。これで線を引きたい小節同士を指定したことになります。そして、StaveConnectorのメンバ関数setTypeとsetContextとdrawをよびだして描画が完結します。


setTypeの引数にはさまざまな線の種類に関する設定ができます。


コード 表示内容
StaveConnector.type.SINGLE 小節の左に細線
StaveConnector.type.SINGLE_LEFT 小節の左に細線※SINGLEと全く同じ
StaveConnector.type.SINGLE_RIGHT 小節の右に細線
StaveConnector.type.DOUBLE 小節の左に二重線(小節線と離れて左に太線1本)
StaveConnector.type.THIN_DOUBLE 小節の左に細い二重線(小節線と離れて左に細線1本)
StaveConnector.type.BOLD_DOUBLE_LEFT 小節の左に太い二重線(小節線も上下で繋げて左に太線1本)
StaveConnector.type.BOLD_DOUBLE_RIGHT 小節の右に太い二重線(小節線も上下で繋げて右に太線1本)
StaveConnector.type.BRACE 小節の左に { のようなカッコ線
StaveConnector.type.BRACKET 小節の左に [ のようなカッコ線。例示のもの
StaveConnector.type.NONE 接続線なし


 テキストの追加がこのように小節を中心に好きなだけオフセットを入れて好きなように挿入できるということは、なんでも表示させようと思えば表示できるということでしょう。テキストの倍率とフォントの指定とフォントの作成ができればの話ですが。


 

VexFlow 使い方に戻る。