vector-score 1.2.0 → 1.3.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
@@ -1,4 +1,6 @@
1
- ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/vector-score-icon.svg)
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/vector-score-icon.svg" alt="Vector Score Logo" />
3
+ </p>
2
4
 
3
5
  # Vector Score
4
6
  ![NPM Version](https://img.shields.io/npm/v/vector-score)
@@ -7,7 +9,8 @@
7
9
 
8
10
  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
11
 
10
- <br/>
12
+ [**See CSS class styling guide**](#Notes)
13
+
11
14
 
12
15
  ## Features
13
16
 
@@ -15,7 +18,7 @@ A lightweight, SVG-based TypeScript library for rendering simple musical notatio
15
18
  * Supports grand, treble, bass, and alto clefs.
16
19
  * Easy to add notes and provides justifying alignment functions.
17
20
  * Simple single line staff for display chords, notes, or scales.
18
- * [**Go to Music Staffs**](#Standard-Music-Staff)
21
+ * [**Go to Music Staffs**](#Grand-Staff)
19
22
 
20
23
  ### Render and Display Guitar Chords
21
24
  * Write explicitly which string, fret, and optionally finger to display on the diagram.
@@ -27,23 +30,6 @@ A lightweight, SVG-based TypeScript library for rendering simple musical notatio
27
30
  * Dedicated staff for rhythm exercises with customizable time signatures and bar handling. [**Go to Rhythm Staff**](#Rhythm-Staff)
28
31
  * Staff made to allow for 'endless' style of notes. [**Go to Scrolling Staff**](#Scrolling-Staff)
29
32
 
30
- <br />
31
-
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.
36
-
37
-
38
- <br />
39
-
40
- ## Installation
41
-
42
- ```bash
43
- npm i vector-score
44
- ```
45
-
46
- <br />
47
33
 
48
34
  ## Usage
49
35
 
@@ -67,24 +53,96 @@ const staff = new MusicStaff(container, {
67
53
  width: 400,
68
54
  scale: 1.2,
69
55
  spaceBelow: 1,
70
- keySignature: "Bb"
56
+ keySignature: "Bb",
71
57
  });
72
58
 
73
59
  // Draw a C Minor scale (quarter notes)
74
- // Format: NoteName + Accidental(optional) + Octave + Duration
75
60
  staff.drawNote(['C4q', 'D4q', 'Eb4q', 'F4q', 'G4q', 'Ab4q', 'Bb4q', 'C5q']);
61
+ staff.justifyNotes();
62
+ ```
76
63
 
77
- // Draw a C Chord
78
- staff.drawChord(['C4w', 'E4w', 'G4w']);
79
64
 
80
- // Evenly space all notes on the staff
81
- staff.justifyNotes();
65
+ ## Notes
66
+
67
+ ### CSS Classes & Theming
68
+ The GuitarChord diagram uses CSS classes for styling. All elements that can be targeted for theming are prefixed with `.vs-`.
69
+
70
+ #### General SVG Classes
71
+ * `svg.vs-svg-renderer-root`: The root SVG element.
72
+ * `g.vs-svg-renderer-parent`: The main parent group element containing all drawn elements.
73
+
74
+ #### Staff Theming
75
+ * `.vs-staff-layer`: The layer containing the staff lines, clef, and time signature.
76
+ * `.vs-notes-layer`: The layer containing all notes, chords, stems, and rests.
77
+ * `.vs-ui-layer`: Contains UI elements like the RhythmStaff beat tracker.
78
+ * `vs-rhythm-current-beat`: Targets the highlighting rectangle for the current beat in the RhythmStaff. Also possible to add in transition animation on `x` property.
79
+
80
+ #### Guitar Chord Theming
81
+ The `GuitarChord` class is entirely styled via CSS. Here are the specific layout targets:
82
+ * **Text & Labels:**
83
+ * `.vs-guitar-text`: Base class for all text elements in the diagram.
84
+ * `.vs-guitar-diagram-label`: The chord name label above the diagram.
85
+ * `.vs-guitar-fret-label`: The fret number indicator (e.g., "5fr").
86
+ * `.vs-guitar-fret-text`: The finger numbers inside the dots and barres.
87
+ * **Grid Lines:**
88
+ * `.vs-guitar-group-fret-lines`: The horizontal fret lines and the top nut.
89
+ * `.vs-guitar-group-string-lines`: The vertical string lines.
90
+ * **Dots & Markers:**
91
+ * `.vs-guitar-fret-muted`: The 'X' markers for muted strings above the nut.
92
+ * `.vs-guitar-fret-open`: The 'O' outline markers for open strings above the nut.
93
+ * `.vs-guitar-fret-dot`: The parent group for fretted dots.
94
+ * `.vs-guitar-fret-circle`: The actual SVG circle shape for a fretted note.
95
+ * **Barres:**
96
+ * `.vs-guitar-group-barre`: The group containing the elements for the barre line.
97
+ * `.vs-guitar-barre`: The rectangle element spanning across strings for barre chords.
98
+
99
+ #### Scrolling Staff Theming
100
+ * `g.vs-scrolling-notes-layer > g`: Targets the scrolling staff's notes, where a transition property can be added for smooth animation.
101
+
102
+
103
+ **Example: Custom Staff Theme Implementation**
104
+ ```css
105
+ /* Adds background color to root SVG element */
106
+ svg.vs-svg-renderer-root {
107
+ background-color: white;
108
+ }
109
+
110
+ /* Set base staff color */
111
+ .vs-staff-layer,
112
+ .vs-notes-layer {
113
+ color: #d4d4d8;
114
+ }
115
+
116
+ /* Animate current beat rect UI */
117
+ .vs-rhythm-current-beat {
118
+ transition: x 0.2s ease-in
119
+ }
82
120
  ```
83
121
 
84
- ### Resulting Staff
85
- ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/MusicStaffTrebleResult.svg)
122
+ **Example: Custom Guitar Chord Theme Implementation**
123
+ ```css
124
+ /* Set base grid colors */
125
+ .vs-guitar-group-fret-lines,
126
+ .vs-guitar-group-string-lines {
127
+ color: #d4d4d8;
128
+ }
129
+
130
+ /* Format the text labels */
131
+ .vs-guitar-text { fill: #1a1827; }
132
+ .vs-guitar-fret-text { fill: #ffffff; }
133
+
134
+ /* Style the open/muted markers */
135
+ .vs-guitar-fret-muted { color: #dc2626; }
136
+ .vs-guitar-fret-open { color: #3b82f6; }
137
+
138
+ /* Apply alternating fret dot colors */
139
+ .vs-guitar-fret-dot { color: oklch(50% 0.05 220); }
86
140
 
87
- <br/>
141
+ .vs-guitar-fret-dot:nth-child(odd of .vs-guitar-fret-dot) { color: oklch(60% 0.25 200); }
142
+
143
+ /* Style the barre */
144
+ .vs-guitar-barre { fill: #1e40af; }
145
+ ```
88
146
 
89
147
  ## Grand Staff
90
148
 
@@ -110,7 +168,6 @@ grandStaff.drawChord(["G3w", "C4w", "E4w"]);
110
168
  ### Resulting Staff
111
169
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/MusicStaffGrandResult.svg)
112
170
 
113
- <br/>
114
171
 
115
172
  ## Guitar Chords
116
173
 
@@ -124,11 +181,10 @@ import { GuitarChord } from 'vector-score';
124
181
  const grandStaff = new GuitarChord(container, {
125
182
  fretCount: 5,
126
183
  stringCount: 6,
127
- stringLabels: ["E", "A", "D", "G", "B", "E"],
128
- width: 300,
184
+ stringLabels: ["E", "A", "D", "G", "B", "E"], // Labels under each string, in order
185
+ inlineChordsAmount: 4, // Choose how many chords are in a single line
186
+ centerChords: true, // Centers the diagrams inside their rows
129
187
  scale: 1,
130
- color: "var(--font-color)",
131
- backgroundColor: "var(--bg-color)"
132
188
  });
133
189
 
134
190
  // C chord
@@ -137,9 +193,9 @@ guitarChordsSection.addChord("x32010", "032010", {
137
193
  });
138
194
 
139
195
  // 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]);
196
+ const frets = "687766";
197
+ const fingers = "142311";
198
+ const barres = guitarChordsSection.determineBarreOptions(frets, fingers, [6]);
143
199
 
144
200
  guitarChordsSection.addChord(frets, fingers, {
145
201
  label: "Bbmaj7",
@@ -149,7 +205,6 @@ guitarChordsSection.addChord(frets, fingers, {
149
205
  ### Result
150
206
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/GuitarChordsResult.svg)
151
207
 
152
- <br />
153
208
 
154
209
  ## Rhythm Staff
155
210
 
@@ -180,7 +235,6 @@ rhythm.incrementCurrentBeatUI();
180
235
  ### Resulting Staff
181
236
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/RhythmStaffResult.svg)
182
237
 
183
- <br />
184
238
 
185
239
  ## Scrolling Staff
186
240
 
@@ -225,7 +279,6 @@ scrollingStaff.queueNotes([
225
279
  ### Resulting Staff
226
280
  ![Alt Text](https://raw.githubusercontent.com/DrakeB1234/VectorScore/master/public/ScrollingStaffResult.webp)
227
281
 
228
- <br />
229
282
 
230
283
  ## Note String Syntax
231
284
 
@@ -247,7 +300,6 @@ Notes are defined using a specific string format parsed by the library:
247
300
  * `F#5q`: F Sharp, Octave 5, Quarter note
248
301
  * `Bb3e`: B Flat, Octave 3, Eighth note
249
302
 
250
- <br />
251
303
 
252
304
  ## Guitar String Syntax
253
305
 
@@ -274,7 +326,6 @@ Barre lines can be added dynamically using the built-in helper method, `determin
274
326
  * Pass in your fret string, finger string, and an array of the target frets you wish to barre.
275
327
  * 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
328
 
277
- <br />
278
329
 
279
330
  ## API Reference
280
331
 
@@ -290,7 +341,6 @@ Barre lines can be added dynamically using the built-in helper method, `determin
290
341
  | `changeChordByIndex(notes: string[], index: number)` | Replaces a chord at a specific index with a new chord. |
291
342
  | `destroy()` | Destroys internal arrays and elements |
292
343
 
293
- <br/>
294
344
 
295
345
  ### GuitarChord Class
296
346
 
@@ -304,7 +354,6 @@ Barre lines can be added dynamically using the built-in helper method, `determin
304
354
  | `destroy()` | Destroys internal arrays and elements. |
305
355
 
306
356
 
307
- <br/>
308
357
 
309
358
  ### RhythmStaff Class
310
359
 
@@ -318,7 +367,6 @@ Barre lines can be added dynamically using the built-in helper method, `determin
318
367
  | `resetCurrentBeatUI()` | Must be called if current beat goes over the total beats in the bar to reset its state |
319
368
  | `destroy()` | Destroys internal arrays and elements |
320
369
 
321
- <br/>
322
370
 
323
371
  ### ScrollingStaff Class
324
372
 
@@ -329,7 +377,6 @@ Barre lines can be added dynamically using the built-in helper method, `determin
329
377
  | `clearAllNote()` | Clears all notes on the staff |
330
378
  | `destroy()` | Destroys internal arrays and elements |
331
379
 
332
- <br />
333
380
 
334
381
  ## Configuration Options
335
382
 
@@ -341,17 +388,18 @@ Barre lines can be added dynamically using the built-in helper method, `determin
341
388
  * `spaceAbove`: Padding units above the staff (in staff line spaces).
342
389
  * `spaceBelow`: Padding units below the staff (in staff line spaces).
343
390
  * `noteStartX`: Position where notes start to draw.
344
- * `staffColor`: CSS color string for lines and notes.
345
- * `staffBackgroundColor`: CSS color string for background.
391
+ * `svgAutoFill`: Sets inline styles on root SVG element to allow for screen scaling (default: true).
392
+
346
393
 
347
394
  ### GuitarChordOptions
348
395
  * `stringCount`: Amount of strings to show in diagram, default is 6.
349
396
  * `fretCount`: Amount of frets to show in diagram, default is 5.
350
397
  * `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.
398
+ * `inlineChordsAmount`: Max amount of chords to display on a single line (default: 2).
399
+ * `centerChords`: Boolean to center the chord diagrams on their respective rows (default: true).
352
400
  * `scale`: Zoom factor (default: 1).
353
- * `color`: CSS color string for lines and notes.
354
- * `backgroundColor`: CSS color string for background.
401
+ * `width`: Total width of the SVG in pixels, overrides inlineChordAmount auto width calculation (default: undefined).
402
+ * `svgAutoFill`: Sets inline styles on root SVG element to allow for screen scaling (default: true).
355
403
 
356
404
  ### RhythmStaffOptions
357
405
  * `topNumber`: The top number of the time signature (e.g., 4 for 4/4 time).
@@ -367,6 +415,7 @@ Barre lines can be added dynamically using the built-in helper method, `determin
367
415
  * `staffColor`: CSS color string for lines and notes.
368
416
  * `staffBackgroundColor`: CSS color string for background.
369
417
  * `currentBeatUIColor`: CSS color string for current beat UI indicator.
418
+ * `svgAutoFill`: Sets inline styles on root SVG element to allow for screen scaling (default: true).
370
419
 
371
420
  ### ScrollingStaffOptions
372
421
  * `width`: Total width of the SVG in pixels.
@@ -378,3 +427,4 @@ Barre lines can be added dynamically using the built-in helper method, `determin
378
427
  * `staffColor`: CSS color string for lines and notes.
379
428
  * `staffBackgroundColor`: CSS color string for background.
380
429
  * `onNotesOut`: Callback function for when there are no more notes on the staff to advance.
430
+ * `svgAutoFill`: Sets inline styles on root SVG element to allow for screen scaling (default: true).
@@ -1,12 +1,13 @@
1
1
  import { GuitarBarreDef, GuitarChordDrawOptions } from '../types';
2
2
  export type GuitarChordOptions = {
3
3
  width?: number;
4
+ inlineChordsAmount?: number;
5
+ centerChords?: boolean;
4
6
  scale?: number;
5
7
  stringCount?: number;
6
8
  fretCount?: number;
7
9
  stringLabels?: string[];
8
- color?: string;
9
- backgroundColor?: string;
10
+ svgAutoFill?: boolean;
10
11
  };
11
12
  export default class GuitarChord {
12
13
  private svgRendererInstance;
@@ -26,7 +27,8 @@ export default class GuitarChord {
26
27
  * All config options are in the type GuitarChordOptions.
27
28
  */
28
29
  constructor(rootElementCtx: HTMLElement, options?: GuitarChordOptions);
29
- private drawStringDots;
30
+ private getDotsToHide;
31
+ private drawFretDots;
30
32
  private drawStringLabels;
31
33
  private drawBarre;
32
34
  private renderChordDiagram;
@@ -7,8 +7,7 @@ export type MusicStaffOptions = {
7
7
  keySignature?: string;
8
8
  spaceAbove?: number;
9
9
  spaceBelow?: number;
10
- staffColor?: string;
11
- staffBackgroundColor?: string;
10
+ svgAutoFill?: boolean;
12
11
  };
13
12
  export default class MusicStaff {
14
13
  private svgRendererInstance;
@@ -5,9 +5,7 @@ export type RhythmStaffOptions = {
5
5
  barsCount?: number;
6
6
  spaceAbove?: number;
7
7
  spaceBelow?: number;
8
- staffColor?: string;
9
- staffBackgroundColor?: string;
10
- currentBeatUIColor?: string;
8
+ svgAutoFill?: boolean;
11
9
  };
12
10
  export default class RhythmStaff {
13
11
  private rendererInstance;
@@ -4,55 +4,69 @@ type SVGRendererOptions = {
4
4
  width: number;
5
5
  height: number;
6
6
  scale: number;
7
- staffColor: string;
8
- staffBackgroundColor: string;
9
7
  useGlyphs: GlyphNames[];
8
+ svgAutoFill: boolean;
10
9
  };
11
- type LayerNames = 'staff' | 'notes' | 'ui';
12
10
  type DrawGlyphOptions = {
13
11
  yOffset?: number;
14
12
  xOffset?: number;
15
13
  };
14
+ type DrawLineOptions = {
15
+ strokeWidth?: number;
16
+ classes?: string | string[];
17
+ };
16
18
  type DrawRectOptions = {
17
19
  x?: number;
18
20
  y?: number;
19
21
  fill?: string;
20
22
  rx?: number;
23
+ classes?: string | string[];
21
24
  };
22
25
  type DrawCircleOptions = {
23
26
  filled?: boolean;
24
27
  fill?: string;
25
28
  strokeWidth?: number;
29
+ classes?: string | string[];
26
30
  };
27
31
  type DrawTextOptions = {
28
32
  fontSize?: number;
29
33
  anchor?: "start" | "middle" | "end";
34
+ baseline?: "central";
30
35
  fill?: string;
31
36
  fontWeight?: string;
37
+ classes?: string | string[];
32
38
  };
33
39
  export default class SVGRenderer {
34
40
  private rootElementRef;
35
41
  private svgElementRef;
36
42
  private parentGroupContainer;
37
- private musicStaffLayer;
38
- private musicNotesLayer;
39
- private musicUILayer;
43
+ private layers;
40
44
  private width;
41
45
  private scale;
42
46
  private totalYOffset;
43
47
  private totalHeight;
44
48
  constructor(rootElementCtx: HTMLElement, options: SVGRendererOptions);
45
49
  private makeGlyphDefs;
50
+ private addNamespacedClassesToElement;
51
+ /**
52
+ * Creates a layer that is appended to parent element.
53
+ * layerName arg is the key that is appended to the internal list of layers, which
54
+ * can be used in method getLayer() arg. Best to call on construction.
55
+ *
56
+ * @param {string} layerName
57
+ * @returns {SVGGElement}
58
+ */
59
+ createLayer(layerName: string): SVGGElement;
60
+ getLayer(layerName: string): SVGGElement | null;
46
61
  createGroup(className?: string): SVGGElement;
47
62
  commitElementsToDOM(elements: SVGElement[] | SVGElement, parent?: HTMLElement | SVGElement): void;
48
- getLayerByName(name: LayerNames): SVGGElement;
49
63
  addTotalRootSvgHeight(amount: number): void;
50
64
  setTotalRootSvgHeight(amount: number): void;
51
65
  addTotalRootSvgYOffset(amount: number): void;
52
66
  applySizingToRootSvg(): void;
53
67
  get rootSvgElement(): SVGElement;
54
68
  get parentGroupElement(): SVGElement;
55
- drawLine(x1: number, y1: number, x2: number, y2: number, parent: SVGElement): void;
69
+ drawLine(x1: number, y1: number, x2: number, y2: number, parent: SVGElement, options?: DrawLineOptions): void;
56
70
  drawRect(width: number, height: number, parent: SVGElement, options?: DrawRectOptions): SVGRectElement;
57
71
  drawGlyph(glyphName: GlyphNames, parent: SVGElement, options?: DrawGlyphOptions): void;
58
72
  drawCircle(cx: number, cy: number, radius: number, parent: SVGElement, options?: DrawCircleOptions): SVGCircleElement;
@@ -6,8 +6,7 @@ export type ScrollingStaffOptions = {
6
6
  staffType?: StaffTypes;
7
7
  spaceAbove?: number;
8
8
  spaceBelow?: number;
9
- staffColor?: string;
10
- staffBackgroundColor?: string;
9
+ svgAutoFill?: boolean;
11
10
  onNotesOut?: () => void;
12
11
  };
13
12
  export type NoteSequence = (string | string[])[];
@@ -25,14 +25,14 @@ export declare const GUITAR_DOT_RADIUS = 10;
25
25
  export declare const GUITAR_MARKER_RADIUS = 6;
26
26
  export declare const GUITAR_STRING_LABEL_OFFSET = 4;
27
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;
28
+ export declare const GUITAR_DIAGRAM_H_SPACING = 51;
29
+ export declare const GUITAR_DIAGRAM_V_SPACING = 12;
30
+ export declare const GUITAR_DIAGRAM_TOP_PADDING = 4;
30
31
  export declare const GUITAR_DIAGRAM_BOTTOM_PADDING = 1;
31
32
  export declare const GUITAR_NUT_THICKNESS = 4;
32
33
  export declare const GUITAR_NUT_X_OFFSET = 1;
33
34
  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;
35
+ export declare const GUITAR_FONT_BASE = 16;
36
+ export declare const GUITAR_FONT_SMALL = 12;
37
37
  export declare const GUITAR_LABEL_HEIGHT = 16;
38
- export declare const GUITAR_FRET_LABEL_OFFSET_X: number;
38
+ export declare const GUITAR_FRET_LABEL_OFFSET_X = 2;