From 4b10aeafc49abb34b598d891ed36d14b41f86d3d Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 28 Sep 2020 21:55:49 +0800 Subject: [PATCH] Add props, fix fret widths Add various props for fretboard display, also made fret widths realistic. --- .../Fretboard/Fretboard.stories.tsx | 18 ++ src/components/Fretboard/Fretboard.tsx | 238 ++++++++++++++---- 2 files changed, 205 insertions(+), 51 deletions(-) diff --git a/src/components/Fretboard/Fretboard.stories.tsx b/src/components/Fretboard/Fretboard.stories.tsx index d1fa215..08f8d66 100644 --- a/src/components/Fretboard/Fretboard.stories.tsx +++ b/src/components/Fretboard/Fretboard.stories.tsx @@ -8,3 +8,21 @@ export default { // By passing optional props to this story, you can control the props of the component when // you consume the story in a test. export const Default = (props?: Partial) => + +export const ReducedFrets = (props?: Partial) => + +export const FixedTunings = (props?: Partial) => + +export const Fretted = (props?: Partial) => + +export const Tunable = (props?: Partial) => + +export const LeftHanded = (props?: Partial) => + +export const Bass = (props?: Partial) => + +export const ExtendedGuitar = (props?: Partial) => + +// export const ExtendedBass = (props?: Partial) => + +export const WithCapo = (props?: Partial) => diff --git a/src/components/Fretboard/Fretboard.tsx b/src/components/Fretboard/Fretboard.tsx index a4d0e8e..6e1b077 100644 --- a/src/components/Fretboard/Fretboard.tsx +++ b/src/components/Fretboard/Fretboard.tsx @@ -1,17 +1,55 @@ -import React, { FC } from 'react' +import React, {ChangeEventHandler, FC} from 'react' import * as PropTypes from 'prop-types' const propTypes = { tunings: PropTypes.arrayOf(PropTypes.number), frets: PropTypes.number, leftHanded: PropTypes.bool, + fixedTunings: PropTypes.bool, + displayTunings: PropTypes.bool, + capoFret: PropTypes.number, + fretted: PropTypes.arrayOf(PropTypes.number), } export type Props = PropTypes.InferProps const PITCHES = 'C C# D D# E F F# G G# A A# B'.split(' ') -const Fretboard: FC = ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, leftHanded = false }) => { +const getPitchName = (n: number) => PITCHES[n % 12] + Math.floor(n / 12) + +const Fretboard: FC = ({ + tunings = [64, 59, 55, 50, 45, 40], + frets = 24, + leftHanded = false, + fixedTunings = false, + displayTunings = false, + capoFret = 0, + fretted = [], +}) => { + const [clientSide, setClientSide, ] = React.useState(false) + const [pitches, setPitches, ] = React.useState(tunings!.map(t => getPitchName(t!))) + + const updateStringPitch = (currentStringNumber: number): ChangeEventHandler => e => { + const { value } = e.target + setPitches(oldPitches => [ + ...oldPitches.slice(0, currentStringNumber), + getPitchName(Number(value)), + ...oldPitches.slice(currentStringNumber + 1), + ]) + } + + React.useEffect(() => { + setClientSide(true) + }, []) + + // React.useEffect(() => { + // if (!Array.isArray(tunings!)) { + // return + // } + // + // setPitches(() => tunings.map(t => getPitchName(t!))) + // }, [tunings]) + if (!Array.isArray(tunings!)) { return null } @@ -23,59 +61,80 @@ const Fretboard: FC = ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, style={{ border: '0.0625rem solid', boxSizing: 'border-box', - transform: leftHanded - ? 'perspective(6rem) rotateY(2deg) scaleX(1.375)' - : 'perspective(6rem) rotateY(-2deg) scaleX(1.375)', - transformOrigin: leftHanded ? 'left' : 'right', }} > - {strings!.map((pitches, stringNumber) => ( + {strings!.map((stringPitches, stringNumber) => (
-
- - - {PITCHES[tunings[stringNumber]! % 12]} - {Math.floor(tunings[stringNumber]! / 12)} - -
+ { + displayTunings + && ( +
+
+ + {pitches[stringNumber]} + + +
+
+ ) + }
= ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, style={{ position: 'relative', display: 'block', - width: '1rem', - height: '1rem', + width: '100%', + height: '100%', flexShrink: 0, padding: 0, border: 0, @@ -104,12 +163,40 @@ const Fretboard: FC = ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, backgroundColor: 'currentColor', }} /> + { + fretted![stringNumber] === 0 + && ( + + + + ) + }
- {pitches.map((_, i) => ( + {stringPitches.map((_, i) => (
= ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, >
= ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, {i % 12 === 11 && (
= ({ tunings = [64, 59, 55, 50, 45, 40], frets = 24, backgroundColor: 'currentColor', }} /> + { + fretted![stringNumber] === i + 1 + && ( + + + + ) + } + { + capoFret === (i + 1) && stringNumber === 0 && ( +
+
+
+ ) + }
))}