UNPKG

vexflow

Version:

A JavaScript library for rendering music notation and guitar tablature

824 lines (608 loc) 26.4 kB
<!DOCTYPE html> <html> <head> <title>stavehairpin.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>stavehairpin.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.</p> <h2 id="description">Description</h2> <p>This class by Raffaele Viglianti, 2012 <a href="http://itisnotsound.wordpress.com/">http://itisnotsound.wordpress.com/</a></p> <p>This class implements hairpins between notes. Hairpins can be either Crescendo or Descrescendo.</p> <div class='highlight'><pre> <span class="hljs-keyword">import</span> { Vex } <span class="hljs-keyword">from</span> <span class="hljs-string">'./vex'</span>; <span class="hljs-keyword">import</span> { Element } <span class="hljs-keyword">from</span> <span class="hljs-string">'./element'</span>; <span class="hljs-keyword">import</span> { Modifier } <span class="hljs-keyword">from</span> <span class="hljs-string">'./modifier'</span>; <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StaveHairpin</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Element</span> </span>{ <span class="hljs-keyword">static</span> get type() { <span class="hljs-keyword">return</span> { <span class="hljs-attr">CRESC</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">DECRESC</span>: <span class="hljs-number">2</span>, }; } <span class="hljs-comment">/* Helper function to convert ticks into pixels. * Requires a Formatter with voices joined and formatted (to * get pixels per tick) * * options is struct that has: * * { * height: px, * y_shift: px, //vertical offset * left_shift_ticks: 0, //left horizontal offset expressed in ticks * right_shift_ticks: 0 // right horizontal offset expressed in ticks * } * **/</span> <span class="hljs-keyword">static</span> FormatByTicksAndDraw(ctx, formatter, notes, type, position, options) { <span class="hljs-keyword">const</span> ppt = formatter.pixelsPerTick; <span class="hljs-keyword">if</span> (ppt == <span class="hljs-literal">null</span>) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RuntimeError( <span class="hljs-string">'BadArguments'</span>, <span class="hljs-string">'A valid Formatter must be provide to draw offsets by ticks.'</span> ); } <span class="hljs-keyword">const</span> l_shift_px = ppt * options.left_shift_ticks; <span class="hljs-keyword">const</span> r_shift_px = ppt * options.right_shift_ticks; <span class="hljs-keyword">const</span> hairpin_options = { <span class="hljs-attr">height</span>: options.height, <span class="hljs-attr">y_shift</span>: options.y_shift, <span class="hljs-attr">left_shift_px</span>: l_shift_px, <span class="hljs-attr">right_shift_px</span>: r_shift_px }; <span class="hljs-keyword">new</span> StaveHairpin({ <span class="hljs-attr">first_note</span>: notes.first_note, <span class="hljs-attr">last_note</span>: notes.last_note, }, type) .setContext(ctx) .setRenderOptions(hairpin_options) .setPosition(position) .draw(); } <span class="hljs-comment">/** * Create a new hairpin from the specified notes. * * @constructor * @param {!Object} notes The notes to tie up. * @param {!Object} type The type of hairpin */</span> <span class="hljs-keyword">constructor</span>(notes, type) { <span class="hljs-comment">/** * Notes is a struct that has: * * { * first_note: Note, * last_note: Note, * } * **/</span> <span class="hljs-keyword">super</span>(); <span class="hljs-keyword">this</span>.setAttribute(<span class="hljs-string">'type'</span>, <span class="hljs-string">'StaveHairpin'</span>); <span class="hljs-keyword">this</span>.notes = notes; <span class="hljs-keyword">this</span>.hairpin = type; <span class="hljs-keyword">this</span>.position = Modifier.Position.BELOW; <span class="hljs-keyword">this</span>.render_options = { <span class="hljs-attr">height</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y_shift</span>: <span class="hljs-number">0</span>, <span class="hljs-comment">// vertical offset</span> left_shift_px: <span class="hljs-number">0</span>, <span class="hljs-comment">// left horizontal offset</span> right_shift_px: <span class="hljs-number">0</span>, <span class="hljs-comment">// right horizontal offset</span> }; <span class="hljs-keyword">this</span>.setNotes(notes); } setPosition(position) { <span class="hljs-keyword">if</span> (position === Modifier.Position.ABOVE || position === Modifier.Position.BELOW) { <span class="hljs-keyword">this</span>.position = position; } <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } setRenderOptions(options) { <span class="hljs-keyword">if</span> ( options.height != <span class="hljs-literal">null</span> &amp;&amp; options.y_shift != <span class="hljs-literal">null</span> &amp;&amp; options.left_shift_px != <span class="hljs-literal">null</span> &amp;&amp; options.right_shift_px != <span class="hljs-literal">null</span> ) { <span class="hljs-keyword">this</span>.render_options = options; } <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } <span class="hljs-comment">/** * Set the notes to attach this hairpin to. * * @param {!Object} notes The start and end notes. */</span> setNotes(notes) { <span class="hljs-keyword">if</span> (!notes.first_note &amp;&amp; !notes.last_note) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RuntimeError( <span class="hljs-string">'BadArguments'</span>, <span class="hljs-string">'Hairpin needs to have either first_note or last_note set.'</span> ); }</pre></div> <p>Success. Lets grab ‘em notes.</p> <div class='highlight'><pre> <span class="hljs-keyword">this</span>.first_note = notes.first_note; <span class="hljs-keyword">this</span>.last_note = notes.last_note; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } renderHairpin(params) { <span class="hljs-keyword">const</span> ctx = <span class="hljs-keyword">this</span>.checkContext(); <span class="hljs-keyword">let</span> dis = <span class="hljs-keyword">this</span>.render_options.y_shift + <span class="hljs-number">20</span>; <span class="hljs-keyword">let</span> y_shift = params.first_y; <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.position === Modifier.Position.ABOVE) { dis = -dis + <span class="hljs-number">30</span>; y_shift = params.first_y - params.staff_height; } <span class="hljs-keyword">const</span> l_shift = <span class="hljs-keyword">this</span>.render_options.left_shift_px; <span class="hljs-keyword">const</span> r_shift = <span class="hljs-keyword">this</span>.render_options.right_shift_px; ctx.beginPath(); <span class="hljs-keyword">switch</span> (<span class="hljs-keyword">this</span>.hairpin) { <span class="hljs-keyword">case</span> StaveHairpin.type.CRESC: ctx.moveTo(params.last_x + r_shift, y_shift + dis); ctx.lineTo(params.first_x + l_shift, y_shift + (<span class="hljs-keyword">this</span>.render_options.height / <span class="hljs-number">2</span>) + dis); ctx.lineTo(params.last_x + r_shift, y_shift + <span class="hljs-keyword">this</span>.render_options.height + dis); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> StaveHairpin.type.DECRESC: ctx.moveTo(params.first_x + l_shift, y_shift + dis); ctx.lineTo(params.last_x + r_shift, y_shift + (<span class="hljs-keyword">this</span>.render_options.height / <span class="hljs-number">2</span>) + dis); ctx.lineTo(params.first_x + l_shift, y_shift + <span class="hljs-keyword">this</span>.render_options.height + dis); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">default</span>:</pre></div> <p>Default is NONE, so nothing to draw</p> <div class='highlight'><pre> <span class="hljs-keyword">break</span>; } ctx.stroke(); ctx.closePath(); } draw() { <span class="hljs-keyword">this</span>.checkContext(); <span class="hljs-keyword">this</span>.setRendered(); <span class="hljs-keyword">const</span> firstNote = <span class="hljs-keyword">this</span>.first_note; <span class="hljs-keyword">const</span> lastNote = <span class="hljs-keyword">this</span>.last_note; <span class="hljs-keyword">const</span> start = firstNote.getModifierStartXY(<span class="hljs-keyword">this</span>.position, <span class="hljs-number">0</span>); <span class="hljs-keyword">const</span> end = lastNote.getModifierStartXY(<span class="hljs-keyword">this</span>.position, <span class="hljs-number">0</span>); <span class="hljs-keyword">this</span>.renderHairpin({ <span class="hljs-attr">first_x</span>: start.x, <span class="hljs-attr">last_x</span>: end.x, <span class="hljs-attr">first_y</span>: firstNote.getStave().y + firstNote.getStave().height, <span class="hljs-attr">last_y</span>: lastNote.getStave().y + lastNote.getStave().height, <span class="hljs-attr">staff_height</span>: firstNote.getStave().height, }); <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } }</pre></div> <div class="fleur">h</div> </div> </div> </body> </html>