UNPKG

vexflow

Version:

A JavaScript library for rendering music notation and guitar tablature

748 lines (531 loc) 23.7 kB
<!DOCTYPE html> <html> <head> <title>stavetempo.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" /> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div class="container"> <div class="page"> <div class="header"> <h1>stavetempo.js</h1> <div class="toc"> <h3>Table of Contents</h3> <ol> <li> <a class="source" href="accidental.html"> accidental.js </a> </li> <li> <a class="source" href="annotation.html"> annotation.js </a> </li> <li> <a class="source" href="articulation.html"> articulation.js </a> </li> <li> <a class="source" href="barnote.html"> barnote.js </a> </li> <li> <a class="source" href="beam.html"> beam.js </a> </li> <li> <a class="source" href="bend.html"> bend.js </a> </li> <li> <a class="source" href="boundingbox.html"> boundingbox.js </a> </li> <li> <a class="source" href="boundingboxcomputation.html"> boundingboxcomputation.js </a> </li> <li> <a class="source" href="canvascontext.html"> canvascontext.js </a> </li> <li> <a class="source" href="clef.html"> clef.js </a> </li> <li> <a class="source" href="clefnote.html"> clefnote.js </a> </li> <li> <a class="source" href="crescendo.html"> crescendo.js </a> </li> <li> <a class="source" href="curve.html"> curve.js </a> </li> <li> <a class="source" href="dot.html"> dot.js </a> </li> <li> <a class="source" href="easyscore.html"> easyscore.js </a> </li> <li> <a class="source" href="element.html"> element.js </a> </li> <li> <a class="source" href="factory.html"> factory.js </a> </li> <li> <a class="source" href="formatter.html"> formatter.js </a> </li> <li> <a class="source" href="fraction.html"> fraction.js </a> </li> <li> <a class="source" href="frethandfinger.html"> frethandfinger.js </a> </li> <li> <a class="source" href="ghostnote.html"> ghostnote.js </a> </li> <li> <a class="source" href="glyph.html"> glyph.js </a> </li> <li> <a class="source" href="glyphnote.html"> glyphnote.js </a> </li> <li> <a class="source" href="gracenote.html"> gracenote.js </a> </li> <li> <a class="source" href="gracenotegroup.html"> gracenotegroup.js </a> </li> <li> <a class="source" href="gracetabnote.html"> gracetabnote.js </a> </li> <li> <a class="source" href="index.html"> index.js </a> </li> <li> <a class="source" href="keymanager.html"> keymanager.js </a> </li> <li> <a class="source" href="keysignature.html"> keysignature.js </a> </li> <li> <a class="source" href="keysignote.html"> keysignote.js </a> </li> <li> <a class="source" href="modifier.html"> modifier.js </a> </li> <li> <a class="source" href="modifiercontext.html"> modifiercontext.js </a> </li> <li> <a class="source" href="multimeasurerest.html"> multimeasurerest.js </a> </li> <li> <a class="source" href="music.html"> music.js </a> </li> <li> <a class="source" href="note.html"> note.js </a> </li> <li> <a class="source" href="notehead.html"> notehead.js </a> </li> <li> <a class="source" href="notesubgroup.html"> notesubgroup.js </a> </li> <li> <a class="source" href="ornament.html"> ornament.js </a> </li> <li> <a class="source" href="parser.html"> parser.js </a> </li> <li> <a class="source" href="pedalmarking.html"> pedalmarking.js </a> </li> <li> <a class="source" href="raphaelcontext.html"> raphaelcontext.js </a> </li> <li> <a class="source" href="registry.html"> registry.js </a> </li> <li> <a class="source" href="renderer.html"> renderer.js </a> </li> <li> <a class="source" href="repeatnote.html"> repeatnote.js </a> </li> <li> <a class="source" href="smufl.html"> smufl.js </a> </li> <li> <a class="source" href="stave.html"> stave.js </a> </li> <li> <a class="source" href="stavebarline.html"> stavebarline.js </a> </li> <li> <a class="source" href="staveconnector.html"> staveconnector.js </a> </li> <li> <a class="source" href="stavehairpin.html"> stavehairpin.js </a> </li> <li> <a class="source" href="staveline.html"> staveline.js </a> </li> <li> <a class="source" href="stavemodifier.html"> stavemodifier.js </a> </li> <li> <a class="source" href="stavenote.html"> stavenote.js </a> </li> <li> <a class="source" href="staverepetition.html"> staverepetition.js </a> </li> <li> <a class="source" href="stavesection.html"> stavesection.js </a> </li> <li> <a class="source" href="stavetempo.html"> stavetempo.js </a> </li> <li> <a class="source" href="stavetext.html"> stavetext.js </a> </li> <li> <a class="source" href="stavetie.html"> stavetie.js </a> </li> <li> <a class="source" href="stavevolta.html"> stavevolta.js </a> </li> <li> <a class="source" href="stem.html"> stem.js </a> </li> <li> <a class="source" href="stemmablenote.html"> stemmablenote.js </a> </li> <li> <a class="source" href="stringnumber.html"> stringnumber.js </a> </li> <li> <a class="source" href="strokes.html"> strokes.js </a> </li> <li> <a class="source" href="svgcontext.html"> svgcontext.js </a> </li> <li> <a class="source" href="system.html"> system.js </a> </li> <li> <a class="source" href="tables.html"> tables.js </a> </li> <li> <a class="source" href="tabnote.html"> tabnote.js </a> </li> <li> <a class="source" href="tabslide.html"> tabslide.js </a> </li> <li> <a class="source" href="tabstave.html"> tabstave.js </a> </li> <li> <a class="source" href="tabtie.html"> tabtie.js </a> </li> <li> <a class="source" href="textbracket.html"> textbracket.js </a> </li> <li> <a class="source" href="textdynamics.html"> textdynamics.js </a> </li> <li> <a class="source" href="textnote.html"> textnote.js </a> </li> <li> <a class="source" href="tickable.html"> tickable.js </a> </li> <li> <a class="source" href="tickcontext.html"> tickcontext.js </a> </li> <li> <a class="source" href="timesignature.html"> timesignature.js </a> </li> <li> <a class="source" href="timesignote.html"> timesignote.js </a> </li> <li> <a class="source" href="tremolo.html"> tremolo.js </a> </li> <li> <a class="source" href="tuning.html"> tuning.js </a> </li> <li> <a class="source" href="tuplet.html"> tuplet.js </a> </li> <li> <a class="source" href="vex.html"> vex.js </a> </li> <li> <a class="source" href="vibrato.html"> vibrato.js </a> </li> <li> <a class="source" href="vibratobracket.html"> vibratobracket.js </a> </li> <li> <a class="source" href="voice.html"> voice.js </a> </li> <li> <a class="source" href="voicegroup.html"> voicegroup.js </a> </li> </ol> </div> </div> <p><a href="http://vexflow.com">VexFlow</a> - Copyright (c) Mohit Muthanna 2010. Author Radosaw Eichler 2012</p> <div class='highlight'><pre> <span class="hljs-keyword">import</span> { Flow } <span class="hljs-keyword">from</span> <span class="hljs-string">'./tables'</span>; <span class="hljs-keyword">import</span> { Modifier } <span class="hljs-keyword">from</span> <span class="hljs-string">'./modifier'</span>; <span class="hljs-keyword">import</span> { StaveModifier } <span class="hljs-keyword">from</span> <span class="hljs-string">'./stavemodifier'</span>; <span class="hljs-keyword">import</span> { Glyph } <span class="hljs-keyword">from</span> <span class="hljs-string">'./glyph'</span>; <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StaveTempo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StaveModifier</span> </span>{ <span class="hljs-keyword">static</span> get CATEGORY() { <span class="hljs-keyword">return</span> <span class="hljs-string">'stavetempo'</span>; } <span class="hljs-keyword">constructor</span>(tempo, x, shift_y) { <span class="hljs-keyword">super</span>(); <span class="hljs-keyword">this</span>.setAttribute(<span class="hljs-string">'type'</span>, <span class="hljs-string">'StaveTempo'</span>); <span class="hljs-keyword">this</span>.tempo = tempo; <span class="hljs-keyword">this</span>.position = Modifier.Position.ABOVE; <span class="hljs-keyword">this</span>.x = x; <span class="hljs-keyword">this</span>.shift_x = <span class="hljs-number">10</span>; <span class="hljs-keyword">this</span>.shift_y = shift_y; <span class="hljs-keyword">this</span>.font = { <span class="hljs-attr">family</span>: <span class="hljs-string">'times'</span>, <span class="hljs-attr">size</span>: <span class="hljs-number">14</span>, <span class="hljs-attr">weight</span>: <span class="hljs-string">'bold'</span>, }; <span class="hljs-keyword">this</span>.render_options = { <span class="hljs-attr">glyph_font_scale</span>: <span class="hljs-number">30</span>, <span class="hljs-comment">// font size for note</span> }; } getCategory() { <span class="hljs-keyword">return</span> StaveTempo.CATEGORY; } setTempo(tempo) { <span class="hljs-keyword">this</span>.tempo = tempo; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } setShiftX(x) { <span class="hljs-keyword">this</span>.shift_x = x; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } setShiftY(y) { <span class="hljs-keyword">this</span>.shift_y = y; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } draw(stave, shift_x) { <span class="hljs-keyword">const</span> ctx = stave.checkContext(); <span class="hljs-keyword">this</span>.setRendered(); <span class="hljs-keyword">const</span> options = <span class="hljs-keyword">this</span>.render_options;</pre></div> <p>FIXME: What does the ‘38’ mean? Why 38? Is that supposed to be the default font size for standard notation?</p> <div class='highlight'><pre> <span class="hljs-keyword">const</span> scale = options.glyph_font_scale / <span class="hljs-number">38</span>; <span class="hljs-keyword">const</span> name = <span class="hljs-keyword">this</span>.tempo.name; <span class="hljs-keyword">const</span> duration = <span class="hljs-keyword">this</span>.tempo.duration; <span class="hljs-keyword">const</span> dots = <span class="hljs-keyword">this</span>.tempo.dots; <span class="hljs-keyword">const</span> bpm = <span class="hljs-keyword">this</span>.tempo.bpm; <span class="hljs-keyword">const</span> font = <span class="hljs-keyword">this</span>.font; <span class="hljs-keyword">let</span> x = <span class="hljs-keyword">this</span>.x + <span class="hljs-keyword">this</span>.shift_x + shift_x; <span class="hljs-keyword">const</span> y = stave.getYForTopText(<span class="hljs-number">1</span>) + <span class="hljs-keyword">this</span>.shift_y; ctx.save(); <span class="hljs-keyword">if</span> (name) { ctx.setFont(font.family, font.size, font.weight); ctx.fillText(name, x, y); x += ctx.measureText(name).width; } <span class="hljs-keyword">if</span> (duration &amp;&amp; bpm) { ctx.setFont(font.family, font.size, <span class="hljs-string">'normal'</span>); <span class="hljs-keyword">if</span> (name) { x += ctx.measureText(<span class="hljs-string">' '</span>).width; ctx.fillText(<span class="hljs-string">'('</span>, x, y); x += ctx.measureText(<span class="hljs-string">'('</span>).width; } <span class="hljs-keyword">const</span> code = Flow.getGlyphProps(duration); x += <span class="hljs-number">3</span> * scale; Glyph.renderGlyph(ctx, x, y, options.glyph_font_scale, code.code_head); x += code.getWidth() * scale;</pre></div> <p>Draw stem and flags</p> <div class='highlight'><pre> <span class="hljs-keyword">if</span> (code.stem) { <span class="hljs-keyword">let</span> stem_height = <span class="hljs-number">30</span>; <span class="hljs-keyword">if</span> (code.beam_count) stem_height += <span class="hljs-number">3</span> * (code.beam_count - <span class="hljs-number">1</span>); stem_height *= scale; <span class="hljs-keyword">const</span> y_top = y - stem_height; ctx.fillRect(x - scale, y_top, scale, stem_height); <span class="hljs-keyword">if</span> (code.flag) { Glyph.renderGlyph(ctx, x, y_top, options.glyph_font_scale, code.code_flag_upstem, { <span class="hljs-attr">category</span>: <span class="hljs-string">'flag.staveTempo'</span> }); <span class="hljs-keyword">if</span> (!dots) x += <span class="hljs-number">6</span> * scale; } }</pre></div> <p>Draw dot</p> <div class='highlight'><pre> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; dots; i++) { x += <span class="hljs-number">6</span> * scale; ctx.beginPath(); ctx.arc(x, y + <span class="hljs-number">2</span> * scale, <span class="hljs-number">2</span> * scale, <span class="hljs-number">0</span>, <span class="hljs-built_in">Math</span>.PI * <span class="hljs-number">2</span>, <span class="hljs-literal">false</span>); ctx.fill(); } ctx.fillText(<span class="hljs-string">' = '</span> + bpm + (name ? <span class="hljs-string">')'</span> : <span class="hljs-string">''</span>), x + <span class="hljs-number">3</span> * scale, y); } ctx.restore(); <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } }</pre></div> <div class="fleur">h</div> </div> </div> </body> </html>