VexFlow Sandbox

Experiment with VexFlow

by norihirosunada

HTML

<script src="https://npmcdn.com/vexflow/releases/vexflow-debug.js"></script>
<div id="hoge"></div>

JavaScript

VF = Vex.Flow;

// SVG rendererを作成し,"hoge"に貼り付ける.
var div = document.getElementById("hoge")
var renderer = new VF.Renderer(div, VF.Renderer.Backends.SVG);

// コンテキスト設定
renderer.resize(700, 1000);//表示サイズ
var context = renderer.getContext();
context.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");

//16部音符表示
var staves = [];
for(var row=0; row<2; row++){
	//五線譜位置・サイズ設定
  staves.push(new VF.Stave(10, 120+row*80, 600));
  staves[row].setContext(context).draw();
  staves[row].drawVerticalBar(308, true);
  var notes = [];
	for(var i=0; i<8; i++){
  	//10進法を2進法に変換
	  var binary = ('0000' + (row*8+i).toString(2)).slice(-4);
    //2進数を先頭から1つずつ読み込む
    for(var j=0; j<binary.length; j++){
    	//0なら16分休符,1なら16分音符を表示する
      if(binary[j] == '0'){ 
        notes.push(new VF.StaveNote({ keys: ["c/5"], duration: "16r"}));
      }else if(binary[j] == '1'){
        notes.push(new VF.StaveNote({ keys: ["c/5"], duration: "16"}));
      }
    } 
  } 
  //連桁設定
	var beams = VF.Beam.generateBeams(notes, {beam_rests: true});
	VF.Formatter.FormatAndDraw(context, staves[row], notes);
	beams.forEach(function(b) {b.setContext(context).draw()});
}