vector-score 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,41 +5,49 @@
5
5
  [![Bundlephobia](https://img.shields.io/bundlephobia/minzip/vector-score)](https://bundlephobia.com/package/vector-score)
6
6
  ![NPM Downloads](https://img.shields.io/npm/d18m/vector-score)
7
7
 
8
- A lightweight, SVG-based TypeScript library for rendering musical staves, notes, and rhythm patterns in the browser.
8
+ A lightweight, SVG-based TypeScript library for rendering simple musical notation, rhythm staves, and guitar chords. Designed for simple displaying musical information for musical oriented web applications.
9
+
10
+ <br/>
9
11
 
10
12
  ## Features
11
13
 
12
- * **Multiple Staff Types**: Supports Treble, Bass, Alto, and Grand staves (MusicStaff and ScrollingStaff).
13
- * [**Music Staff**](#Standard-Music-Staff):Standard music staff for notation.
14
- * [**Rhythm Staff**](#Rhythm-Staff): Dedicated staff for rhythm exercises with customizable time signatures and bar handling.
15
- * [**Scrolling Staff**](#Scrolling-Staff) Staff made to allow for 'endless' style of notes.
16
- * **SVG Rendering**: Scalable Vector graphics suitable for any screen size.
17
- * **Flexible Note Input**: Simple string-based syntax for defining notes.
14
+ ### Rendering Standard Musical Notation
15
+ * Supports grand, treble, bass, and alto clefs.
16
+ * Easy to add notes and provides justifying alignment functions.
17
+ * Simple single line staff for display chords, notes, or scales.
18
+ * [**Go to Music Staffs**](#Standard-Music-Staff)
18
19
 
19
- ## Installation
20
+ ### Render and Display Guitar Chords
21
+ * Write explicitly which string, fret, and optionally finger to display on the diagram.
22
+ * Supports explicity barre chords.
23
+ * Label each string below diagram, useful for showing tuning of chord.
24
+ * [**Go to Guitar Chords**](#Guitar-Chords)
20
25
 
21
- ```bash
22
- npm i vector-score
23
- ````
26
+ ### Extra Classes
27
+ * Dedicated staff for rhythm exercises with customizable time signatures and bar handling. [**Go to Rhythm Staff**](#Rhythm-Staff)
28
+ * Staff made to allow for 'endless' style of notes. [**Go to Scrolling Staff**](#Scrolling-Staff)
24
29
 
25
- ## Development
30
+ <br />
26
31
 
27
- To start the development server with a playground:
32
+ ## Notes
33
+
34
+ * Main targeting class for css is 'vs-svg-renderer-parent'
35
+ * Could be useful if needing to add in specific colors or styling to any SVG element rendered.
28
36
 
29
- ```bash
30
- npm run dev
31
- ```
32
37
 
33
- To build the library for production:
38
+ <br />
39
+
40
+ ## Installation
34
41
 
35
42
  ```bash
36
- npm run build
43
+ npm i vector-score
37
44
  ```
38
45
 
46
+ <br />
47
+
39
48
  ## Usage
40
49
 
41
50
  ### 1. Setup HTML
42
-
43
51
  Create a container element in your HTML where the staff will be rendered.
44
52
 
45
53
  ```html
@@ -47,8 +55,7 @@ Create a container element in your HTML where the staff will be rendered.
47
55
  ```
48
56
 
49
57
  ### 2. Import and Initialize
50
-
51
- ### Standard Music Staff
58
+ Import desired class (MusicStaff, GuitarChord, etc.). Declare variable with reference to container element. Pass in options for specific class (options are typed).
52
59
 
53
60
  ```typescript
54
61
  import { MusicStaff } from 'vector-score';
@@ -73,10 +80,17 @@ staff.drawChord(['C4w', 'E4w', 'G4w']);
73
80
  // Evenly space all notes on the staff
74
81
  staff.justifyNotes();
75
82
  ```
76
- #### Resulting Staff
83
+
84
+ ### Resulting Staff
77
85
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/MusicStaffTrebleResult.svg)
78
86
 
79
- ### Grand Staff
87
+ <br/>
88
+
89
+ ## Grand Staff
90
+
91
+ See [**Note String Syntax**](#Note-String-Syntax) to see how to write notes on music staff (i.e. ['G4q', 'E4h', 'C4w', "A3h", "F3h"] )
92
+
93
+ See [**MusicStaffOptions**](#MusicStaffOptions) to see how configuration options during class instantiation.
80
94
 
81
95
  ```typescript
82
96
  import { MusicStaff } from 'vector-score';
@@ -93,10 +107,51 @@ grandStaff.drawNote(['G4q', 'E4h', 'C4w', "A3h", "F3h"]);
93
107
 
94
108
  grandStaff.drawChord(["G3w", "C4w", "E4w"]);
95
109
  ```
96
- #### Resulting Staff
110
+ ### Resulting Staff
97
111
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/MusicStaffGrandResult.svg)
98
112
 
99
- ### Rhythm Staff
113
+ <br/>
114
+
115
+ ## Guitar Chords
116
+
117
+ See [**Guitar String Syntax**](#Guitar-String-Syntax) to see how to write **frets**, **fingers**, and **barre lines** on Guitar Chord diagrams (i.e., `"x32010"`, `"032010"`).
118
+
119
+ See [**GuitarChordOptions**](#GuitarChordOptions) to see how configuration options during class instantiation.
120
+
121
+ ```typescript
122
+ import { GuitarChord } from 'vector-score';
123
+
124
+ const grandStaff = new GuitarChord(container, {
125
+ fretCount: 5,
126
+ stringCount: 6,
127
+ stringLabels: ["E", "A", "D", "G", "B", "E"],
128
+ width: 300,
129
+ scale: 1,
130
+ color: "var(--font-color)",
131
+ backgroundColor: "var(--bg-color)"
132
+ });
133
+
134
+ // C chord
135
+ guitarChordsSection.addChord("x32010", "032010", {
136
+ label: "C",
137
+ });
138
+
139
+ // Bbmaj7 Barre Chord (Automatically calculates the starting fret and barre positioning)
140
+ const frets = "x13231";
141
+ const fingers = "013241";
142
+ const barres = guitarChordsSection.determineBarreOptions(frets, fingers, [1]);
143
+
144
+ guitarChordsSection.addChord(frets, fingers, {
145
+ label: "Bbmaj7",
146
+ barres: barres
147
+ });
148
+ ```
149
+ ### Result
150
+ ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/GuitarChordsResult.svg)
151
+
152
+ <br />
153
+
154
+ ## Rhythm Staff
100
155
 
101
156
  ```typescript
102
157
  import { RhythmStaff } from 'vector-score';
@@ -122,10 +177,12 @@ rhythm.drawNote(['q', 'q']);
122
177
  // Increment the UI to show the first beat in the bar
123
178
  rhythm.incrementCurrentBeatUI();
124
179
  ```
125
- #### Resulting Staff
180
+ ### Resulting Staff
126
181
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/RhythmStaffResult.svg)
127
182
 
128
- ### Scrolling Staff
183
+ <br />
184
+
185
+ ## Scrolling Staff
129
186
 
130
187
  ```typescript
131
188
  import { ScrollingStaff } from 'vector-score';
@@ -165,9 +222,11 @@ scrollingStaff.queueNotes([
165
222
 
166
223
  // The button event listener calls 'advanceNotes()' to move the notes over, one step at a time.
167
224
  ```
168
- #### Resulting Staff
225
+ ### Resulting Staff
169
226
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/ScrollingStaffResult.webp)
170
227
 
228
+ <br />
229
+
171
230
  ## Note String Syntax
172
231
 
173
232
  Notes are defined using a specific string format parsed by the library:
@@ -188,6 +247,35 @@ Notes are defined using a specific string format parsed by the library:
188
247
  * `F#5q`: F Sharp, Octave 5, Quarter note
189
248
  * `Bb3e`: B Flat, Octave 3, Eighth note
190
249
 
250
+ <br />
251
+
252
+ ## Guitar String Syntax
253
+
254
+ Chords are defined using two continuous strings: one for **frets** and one for **fingers**. The length of both strings must match the configured string count of the diagram (default is 6). Notes are written in ***string order***, with the first character representing the lowest string (e.g., low E in standard tuning).
255
+
256
+ ### Frets String format: `[xX\da-zA-Z]+`
257
+ * `x` or `X`: Muted string.
258
+ * `0`: Open string.
259
+ * `1-9`: Fretted at the specified fret.
260
+ * `a-z` / `A-Z`: Base-36 alphanumeric encoding for double-digit frets (e.g., `a` = 10, `b` = 11, `c` = 12).
261
+
262
+ ### Fingers String format: `[\d]+`
263
+ * `0`: No finger labeled.
264
+ * `1-9`: Finger number to display on the dot.
265
+
266
+ **Example:**
267
+ * `guitarChordsSection.addChord("x32010", "032010")`: Displays an open C major chord.
268
+ * The lowest string is muted (`x`) and has no finger label (`0`).
269
+ * The A string is played at the 3rd fret (`3`) with the 3rd finger (`3`).
270
+ * *Note: The `startFret` option is now automatically calculated based on the lowest fretted note if left undefined.*
271
+
272
+ ### Barre Lines
273
+ Barre lines can be added dynamically using the built-in helper method, `determineBarreOptions(frets, fingers, barreFrets)`.
274
+ * Pass in your fret string, finger string, and an array of the target frets you wish to barre.
275
+ * The method will automatically calculate the stretch (`fromString` to `toString`) based on the matching finger numbers and return a definition array to pass directly into your `addChord` options.
276
+
277
+ <br />
278
+
191
279
  ## API Reference
192
280
 
193
281
  ### MusicStaff Class
@@ -202,6 +290,22 @@ Notes are defined using a specific string format parsed by the library:
202
290
  | `changeChordByIndex(notes: string[], index: number)` | Replaces a chord at a specific index with a new chord. |
203
291
  | `destroy()` | Destroys internal arrays and elements |
204
292
 
293
+ <br/>
294
+
295
+ ### GuitarChord Class
296
+
297
+ | Method | Description |
298
+ | :--- | :--- |
299
+ | `addChord(frets: string, fingers: string, options?: GuitarChordDrawOptions)` | Draws notes on the diagram using string configurations. Options include manual `startFret`, `label`, and `barres`. |
300
+ | `modifyChordByIndex(frets: string, fingers: string, chordIndex: number, options?: GuitarChordDrawOptions)` | Modifies the chord at the specified index with new definitions and options. |
301
+ | `determineBarreOptions(frets: string, fingers: string, barreFrets: number[])` | Automatically calculates barre line dimensions based on fingering and returns `GuitarBarreDef[]` to be used in chord options. |
302
+ | `removeChordByIndex(chordIndex: number)` | Removes the chord diagram at the specified index and recalculates layout. |
303
+ | `clearAllChords()` | Removes all chords in the container. |
304
+ | `destroy()` | Destroys internal arrays and elements. |
305
+
306
+
307
+ <br/>
308
+
205
309
  ### RhythmStaff Class
206
310
 
207
311
  | Method | Description |
@@ -214,6 +318,8 @@ Notes are defined using a specific string format parsed by the library:
214
318
  | `resetCurrentBeatUI()` | Must be called if current beat goes over the total beats in the bar to reset its state |
215
319
  | `destroy()` | Destroys internal arrays and elements |
216
320
 
321
+ <br/>
322
+
217
323
  ### ScrollingStaff Class
218
324
 
219
325
  | Method | Description |
@@ -223,6 +329,8 @@ Notes are defined using a specific string format parsed by the library:
223
329
  | `clearAllNote()` | Clears all notes on the staff |
224
330
  | `destroy()` | Destroys internal arrays and elements |
225
331
 
332
+ <br />
333
+
226
334
  ## Configuration Options
227
335
 
228
336
  ### MusicStaffOptions
@@ -236,6 +344,15 @@ Notes are defined using a specific string format parsed by the library:
236
344
  * `staffColor`: CSS color string for lines and notes.
237
345
  * `staffBackgroundColor`: CSS color string for background.
238
346
 
347
+ ### GuitarChordOptions
348
+ * `stringCount`: Amount of strings to show in diagram, default is 6.
349
+ * `fretCount`: Amount of frets to show in diagram, default is 5.
350
+ * `stringLabels`: Labels to show under each string in each chord diagram. Provided string values will display in order of strings in this array value.
351
+ * `width`: Total width of the SVG in pixels.
352
+ * `scale`: Zoom factor (default: 1).
353
+ * `color`: CSS color string for lines and notes.
354
+ * `backgroundColor`: CSS color string for background.
355
+
239
356
  ### RhythmStaffOptions
240
357
  * `topNumber`: The top number of the time signature (e.g., 4 for 4/4 time).
241
358
  * `barsCount`: Number of measures to draw.
@@ -0,0 +1,106 @@
1
+ import { GuitarBarreDef, GuitarChordDrawOptions } from '../types';
2
+ export type GuitarChordOptions = {
3
+ width?: number;
4
+ scale?: number;
5
+ stringCount?: number;
6
+ fretCount?: number;
7
+ stringLabels?: string[];
8
+ color?: string;
9
+ backgroundColor?: string;
10
+ };
11
+ export default class GuitarChord {
12
+ private svgRendererInstance;
13
+ private options;
14
+ private chordEntries;
15
+ private cursorX;
16
+ private cursorY;
17
+ private readonly diagramWidth;
18
+ private readonly diagramHeight;
19
+ private readonly gridTopY;
20
+ private readonly rowHeight;
21
+ /**
22
+ * Creates an instance of a GuitarChord diagram renderer.
23
+ *
24
+ * @param rootElementCtx - The element (div) reference that the chord diagram(s) will be appended to.
25
+ * @param options - Optional configuration. Can adjust total width, scale, string/fret count, and coloring.
26
+ * All config options are in the type GuitarChordOptions.
27
+ */
28
+ constructor(rootElementCtx: HTMLElement, options?: GuitarChordOptions);
29
+ private drawStringDots;
30
+ private drawStringLabels;
31
+ private drawBarre;
32
+ private renderChordDiagram;
33
+ private relayoutChords;
34
+ /**
35
+ * Adds a new chord diagram. Diagrams are placed left-to-right in the order added, wrapping to a
36
+ * new row automatically once the configured width is exceeded.
37
+ *
38
+ * @param frets - A string, with each value being ordered low-to-high (e.g. first position in string 0 is the low E string)
39
+ * * `x` - muted string
40
+ * * `0` - open string
41
+ * * `1...n` - fretted at the given absolute fret number (e.g. `3`)
42
+ * @param fingers - A string, with each value being ordered low-to-high (e.g. first position in string 0 is the low E string)
43
+ * * `0` - No finger labeled
44
+ * * `1...n` - Finger labeled as number provided
45
+ *
46
+ * @param options - Optional per-chord settings: `startFret` (default `1`, for diagrams higher up the neck)
47
+ * and `label` (a chord name drawn above the diagram).
48
+ *
49
+ * @returns The index of the newly added chord, for later use with the CRUD-by-index methods.
50
+ *
51
+ * @throws {Error} If fret or finger's string length doesn't match the configured string count.
52
+ *
53
+ * @example
54
+ * // Draw an open C major chord
55
+ * guitarChord.addChord("x32010", "032010", { label: "C" });
56
+ */
57
+ addChord(frets: string, fingers: string, options?: GuitarChordDrawOptions): number;
58
+ /**
59
+ * Replaces the chord diagram at the specified index with a new one, in place.
60
+ *
61
+ * @param frets - A string, with each value being ordered low-to-high (e.g. first position in string 0 is the low E string)
62
+ * * `x` - muted string
63
+ * * `0` - open string
64
+ * * `1...n` - fretted at the given absolute fret number (e.g. `3`)
65
+ * @param fingers - A string, with each value being ordered low-to-high (e.g. first position in string 0 is the low E string)
66
+ * * `0` - No finger labeled
67
+ * * `1...n` - Finger labeled as number provided
68
+ *
69
+ * @param chordIndex - The index of the chord to replace (as returned by addChord).
70
+ *
71
+ * @param options - Optional per-chord settings, see addChord.
72
+ *
73
+ * @returns void
74
+ *
75
+ * @throws {Error} If the index is out of bounds, or if chordDef is invalid (see addChord).
76
+ *
77
+ * @example
78
+ * // Change chord at index 0 to a D major chord
79
+ * guitarChord.modifyChordByIndex("xx0232", "000132", 0, { label: "D" });
80
+ *
81
+ */
82
+ modifyChordByIndex(frets: string, fingers: string, chordIndex: number, options?: GuitarChordDrawOptions): void;
83
+ /**
84
+ * Removes the chord diagram at the specified index. Remaining chords are re-flowed to close the gap.
85
+ * @param chordIndex - The index of the chord to remove.
86
+ * @returns void
87
+ * @throws {Error} If the index is out of bounds.
88
+ */
89
+ removeChordByIndex(chordIndex: number): void;
90
+ /**
91
+ * Removes all chord diagrams and resets internal positioning.
92
+ * @returns void
93
+ */
94
+ clearAllChords(): void;
95
+ /**
96
+ * * Used to automatically determine options for creating barre lines.
97
+ * * The returned value then can be used in addChord and modifyChord methods.
98
+ * @returns GuitarBarreDef[]
99
+ */
100
+ determineBarreOptions(frets: string, fingers: string, barreFrets: number[]): GuitarBarreDef[];
101
+ /**
102
+ * Removes the root svg element and cleans up arrays.
103
+ * @returns void
104
+ */
105
+ destroy(): void;
106
+ }
@@ -17,6 +17,18 @@ type DrawRectOptions = {
17
17
  x?: number;
18
18
  y?: number;
19
19
  fill?: string;
20
+ rx?: number;
21
+ };
22
+ type DrawCircleOptions = {
23
+ filled?: boolean;
24
+ fill?: string;
25
+ strokeWidth?: number;
26
+ };
27
+ type DrawTextOptions = {
28
+ fontSize?: number;
29
+ anchor?: "start" | "middle" | "end";
30
+ fill?: string;
31
+ fontWeight?: string;
20
32
  };
21
33
  export default class SVGRenderer {
22
34
  private rootElementRef;
@@ -35,6 +47,7 @@ export default class SVGRenderer {
35
47
  commitElementsToDOM(elements: SVGElement[] | SVGElement, parent?: HTMLElement | SVGElement): void;
36
48
  getLayerByName(name: LayerNames): SVGGElement;
37
49
  addTotalRootSvgHeight(amount: number): void;
50
+ setTotalRootSvgHeight(amount: number): void;
38
51
  addTotalRootSvgYOffset(amount: number): void;
39
52
  applySizingToRootSvg(): void;
40
53
  get rootSvgElement(): SVGElement;
@@ -42,6 +55,8 @@ export default class SVGRenderer {
42
55
  drawLine(x1: number, y1: number, x2: number, y2: number, parent: SVGElement): void;
43
56
  drawRect(width: number, height: number, parent: SVGElement, options?: DrawRectOptions): SVGRectElement;
44
57
  drawGlyph(glyphName: GlyphNames, parent: SVGElement, options?: DrawGlyphOptions): void;
58
+ drawCircle(cx: number, cy: number, radius: number, parent: SVGElement, options?: DrawCircleOptions): SVGCircleElement;
59
+ drawText(text: string, x: number, y: number, parent: SVGElement, options?: DrawTextOptions): SVGTextElement;
45
60
  destroy(): void;
46
61
  }
47
62
  export {};
@@ -17,3 +17,22 @@ export declare const HALF_NOTE_LEDGER_LINE_WIDTH = 12.5;
17
17
  export declare const CHORD_MAX_CONSECUTIVE_ACCIDENTALS = 3;
18
18
  export declare const staffParams: Record<StaffTypes, StaffParams>;
19
19
  export declare const durationBeatValueMap: Record<Durations, number>;
20
+ export declare const GUITAR_STRING_COUNT_DEFAULT = 6;
21
+ export declare const GUITAR_FRET_COUNT_DEFAULT = 5;
22
+ export declare const GUITAR_STRING_SPACING = 22;
23
+ export declare const GUITAR_FRET_SPACING = 30;
24
+ export declare const GUITAR_DOT_RADIUS = 10;
25
+ export declare const GUITAR_MARKER_RADIUS = 6;
26
+ export declare const GUITAR_STRING_LABEL_OFFSET = 4;
27
+ export declare const GUITAR_STRING_LABEL_HEIGHT: number;
28
+ export declare const GUITAR_DIAGRAM_H_SPACING = 40;
29
+ export declare const GUITAR_DIAGRAM_V_SPACING = 16;
30
+ export declare const GUITAR_DIAGRAM_BOTTOM_PADDING = 1;
31
+ export declare const GUITAR_NUT_THICKNESS = 4;
32
+ export declare const GUITAR_NUT_X_OFFSET = 1;
33
+ export declare const GUITAR_NUT_SPACE_ABOVE: number;
34
+ export declare const GUITAR_LABEL_FONT_SIZE = 16;
35
+ export declare const GUITAR_FINGER_FONT_SIZE = 14;
36
+ export declare const GUITAR_FRET_LABEL_FONT_SIZE = 14;
37
+ export declare const GUITAR_LABEL_HEIGHT = 16;
38
+ export declare const GUITAR_FRET_LABEL_OFFSET_X: number;
@@ -0,0 +1,5 @@
1
+ import { GuitarStringState } from '../types';
2
+ export declare function createStateStrings(frets: string[], fingers: string[]): GuitarStringState[];
3
+ export declare function calculateStartFret(fretParts: string[], fretCount: number): number;
4
+ export declare function parseFretsEntry(frets: string): string[];
5
+ export declare function parseFingersEntry(fingers: string): string[];
package/dist/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export { default as MusicStaff } from './classes/MusicStaff';
2
2
  export { default as RhythmStaff } from './classes/RhythmStaff';
3
3
  export { default as ScrollingStaff } from './classes/ScrollingStaff';
4
+ export { default as GuitarChord } from './classes/GuitarChord';
4
5
  export type { MusicStaffOptions } from './classes/MusicStaff';
5
6
  export type { RhythmStaffOptions } from './classes/RhythmStaff';
6
7
  export type { ScrollingStaffOptions } from './classes/ScrollingStaff';
8
+ export type { GuitarChordOptions } from './classes/GuitarChord';
7
9
  export type { StaffTypes } from './types';
10
+ export type { GuitarChordDrawOptions, GuitarBarreDef } from './types';
package/dist/types.d.ts CHANGED
@@ -14,3 +14,18 @@ export type KeySignatureDef = {
14
14
  type: AccidentalType;
15
15
  count: number;
16
16
  };
17
+ export type GuitarStringState = {
18
+ fret: string;
19
+ finger: string;
20
+ };
21
+ export type GuitarBarreDef = {
22
+ fret: number;
23
+ fromString: number;
24
+ toString: number;
25
+ finger?: number;
26
+ };
27
+ export type GuitarChordDrawOptions = {
28
+ startFret?: number;
29
+ label?: string;
30
+ barres?: GuitarBarreDef[];
31
+ };