|
6 | 6 | let { fretData } = $props(); |
7 | 7 |
|
8 | 8 | let containerRef = $state(null); |
9 | | - let width = $derived(containerRef.clientWidth); |
10 | | - let height = $derived(containerRef.clientHeight); |
| 9 | + let width = $state(0); |
| 10 | + let height = $state(0); |
11 | 11 |
|
12 | | - let fretX; |
13 | | - let strY; |
14 | 12 | let margin = { top: 20, right: 20, bottom: 20, left: 20 }; |
15 | 13 |
|
16 | | - // onMount(() => { |
17 | | - // width = containerRef.clientWidth; |
18 | | - // height = containerRef.clientHeight; |
| 14 | + let fretX = $derived( |
| 15 | + scaleBand() |
| 16 | + .domain(range(fretData.count)) |
| 17 | + .range([margin.left, width - margin.right]) |
| 18 | + .padding(0.1), |
| 19 | + ); |
19 | 20 |
|
20 | | - // fretX = scaleBand() |
21 | | - // .domain(range(fretData.count)) |
22 | | - // .range([margin.left, width - margin.right]) |
23 | | - // .padding(0.1); |
| 21 | + let strY = $derived( |
| 22 | + scalePoint() |
| 23 | + .domain(range(fretData.tuning.length)) |
| 24 | + .range([margin.top, height - margin.bottom]), |
| 25 | + ); |
24 | 26 |
|
25 | | - // strY = scalePoint() |
26 | | - // .domain(range(fretData.tuning.length)) |
27 | | - // .range([margin.top, height - margin.bottom]); |
28 | | - // }); |
| 27 | + onMount(() => { |
| 28 | + width = containerRef.clientWidth; |
| 29 | + height = containerRef.clientHeight; |
| 30 | + }); |
29 | 31 | </script> |
30 | 32 |
|
31 | | -<div bind:this={containerRef}> |
32 | | - {#if width && height} |
33 | | - <svg viewBox="0 0 {width} {height}"> </svg> |
| 33 | +<div bind:this={containerRef} class="h-full w-full"> |
| 34 | + {#if width} |
| 35 | + <svg viewBox="0 0 {width} {height}"> |
| 36 | + {#each fretX.domain() as fret} |
| 37 | + <g transform="translate({fretX(fret)}, {margin.top})"> |
| 38 | + <text class="fret-label">{fret}</text> |
| 39 | + <rect |
| 40 | + class="fret-line" |
| 41 | + x="0" |
| 42 | + y="0" |
| 43 | + width={1} |
| 44 | + height={height - margin.top - margin.bottom}></rect> |
| 45 | + </g> |
| 46 | + {/each} |
| 47 | + </svg> |
34 | 48 | {/if} |
35 | 49 | </div> |
| 50 | + |
| 51 | +<style> |
| 52 | + @reference "tailwindcss"; |
| 53 | +
|
| 54 | + .fret-label { |
| 55 | + @apply text-sm font-bold fill-gray-800 text-center; |
| 56 | + text-anchor: middle; |
| 57 | + dominant-baseline: ideographic; |
| 58 | + } |
| 59 | +
|
| 60 | + .fret-line { |
| 61 | + @apply fill-gray-200 stroke-0; |
| 62 | + } |
| 63 | +</style> |
0 commit comments