Skip to content

Commit

Permalink
feat: improved ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaybee18 committed Mar 12, 2024
1 parent 461500e commit 92cec68
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A piano learning webapp for midi devices
## Roadmap
### v0.1
- [ ] decent ui
- [x] decent ui
- [x] import midi
- [x] play midi

Expand Down
2 changes: 2 additions & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
body {
display: flex;
justify-content: center;

background-color: #323437;
}
</style>
</html>
16 changes: 11 additions & 5 deletions src/lib/components/noteView.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
width: 100%;
height: fit-content;
flex: 1;
background-color: yellow;
position: relative;
}

#notes {
width: 100%;
height: 100%;
display: block;

/*background-color: black;*/
}

#note-shadow {
background: linear-gradient(0deg, rgba(0,0,0,0) 0%, var(--background) 100%);
width: 100%;
height: 10%;
position: absolute;
}

#piano {
display: block;

border-radius: 30px;
}

.keys {
border: 1px dashed black;
box-sizing: border-box;

height: 200px;
background-color: blue;
}

.notes-wrapper {
Expand Down
18 changes: 12 additions & 6 deletions src/lib/components/noteView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import './noteView.css';
import { readMidiFile } from "$lib/notes";
import { Piano } from "$lib/piano";
import { PianoRoll } from "$lib/pianoRollCanvas";
import { PianoRoll } from "$lib/pianoRoll";
import { PianoKeys } from "$lib/pianoKeys";
const piano = new Piano(5);
Expand Down Expand Up @@ -58,16 +58,21 @@
}
};
const updateCanvasSize = (c: HTMLCanvasElement) => {
c.width = c.clientWidth;
c.height = c.clientHeight;
}
onMount(() => {
const pianoRollCanvas = document.getElementById("notes") as HTMLCanvasElement;
const pianoKeysCanvas = document.getElementById("piano") as HTMLCanvasElement;
const observer = new ResizeObserver((entries) => {
pianoRollCanvas.width = pianoRollCanvas.clientWidth;
pianoRollCanvas.height = pianoRollCanvas.clientHeight;
updateCanvasSize(pianoRollCanvas);
updateCanvasSize(pianoKeysCanvas);
pianoKeysCanvas.width = pianoKeysCanvas.clientWidth;
pianoKeysCanvas.height = pianoKeysCanvas.clientHeight;
const observer = new ResizeObserver((entries) => {
updateCanvasSize(pianoRollCanvas);
updateCanvasSize(pianoKeysCanvas);
pianoRoll.resize(pianoRollCanvas.width, pianoRollCanvas.height);
pianoRoll.draw();
Expand Down Expand Up @@ -103,6 +108,7 @@
Drop MIDI file here
</div>
</div>
<div id="note-shadow"></div>
<canvas id="notes"></canvas>
</div>
<canvas id="piano" class="keys"></canvas>
Expand Down
5 changes: 4 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ export const sharpKeyWidthFactor = 0.5;
export const MidiNoteOn = 159;
export const MidiNoteOff = 143;

export const pianoKeyOutlineWidth = 2;
export const pianoKeyOutlineWidth = 1;
export const sharpKeyHeightFactor = 0.6;

export const backgroundGrey = "#323437";
export const highlight = "#e2b714";
9 changes: 6 additions & 3 deletions src/lib/pianoKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export class PianoKeys {
public setCanvas(canvas: HTMLCanvasElement) {
this.canvas = canvas;
const { width, height } = canvas.getBoundingClientRect();
canvas.width = width;
canvas.height = height;
const dpi = window.devicePixelRatio;
const style_width = getComputedStyle(canvas).getPropertyValue("width").slice(0, -2);
const style_height = getComputedStyle(canvas).getPropertyValue("height").slice(0, -2);
canvas.setAttribute("width", (Number(style_width) * dpi).toString());
canvas.setAttribute("height", (Number(style_height) * dpi).toString());
this.width = width;
this.height = height;
}
Expand All @@ -35,7 +38,7 @@ export class PianoKeys {

this.canvas.width = width;
this.canvas.height = height;
ctx.translate(0.5, 0.5);
//ctx.translate(0.5, 0.5);

const numNaturalKeys = this.piano.octaves * 7 + 1;
const naturalKeyWidth = width / numNaturalKeys;
Expand Down
11 changes: 7 additions & 4 deletions src/lib/pianoRollCanvas.ts → src/lib/pianoRoll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sharpKeyWidthFactor } from "./constants";
import { highlight, sharpKeyWidthFactor } from "./constants";
import type { Note } from "./notes";
import type { Piano } from "./piano";

Expand Down Expand Up @@ -36,8 +36,11 @@ export class PianoRoll {
public setCanvas(canvas: HTMLCanvasElement) {
this.canvas = canvas;
const { width, height } = canvas.getBoundingClientRect();
canvas.width = width;
canvas.height = height;
const dpi = window.devicePixelRatio;
const style_width = getComputedStyle(canvas).getPropertyValue("width").slice(0, -2);
const style_height = getComputedStyle(canvas).getPropertyValue("height").slice(0, -2);
canvas.setAttribute("width", (Number(style_width) * dpi).toString());
canvas.setAttribute("height", (Number(style_height) * dpi).toString());
this.width = width;
this.height = height;

Expand Down Expand Up @@ -129,7 +132,7 @@ export class PianoRoll {

ctx.clearRect(0, 0, this.width, this.height);

ctx.fillStyle = "#0000ff";
ctx.fillStyle = highlight;
this.notes.forEach(note => {
const x = this.getNoteX(note.midiNumber);
const y = this.height - note.startTime * tickHeight + this.time;
Expand Down
13 changes: 12 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@
<div class="content">
<slot></slot>
</div>
<div class="footer">footer</div>
<div class="footer">
<div>
<div>
&copy 2024 Jan Bessler
</div>
<div>
<a href="https://github.com/Jaybee18/mfp">GitHub</a>
<a href="https://www.instagram.com/jaybes.mg/">Instagram</a>
<a href="https://twitter.com/JanBessler">X</a>
</div>
</div>
</div>
</div>
29 changes: 27 additions & 2 deletions src/routes/global.css
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
:root {
--background: #323437;
--text: #646669;
}

.main {
background-color: red;
width: 70%;
height: 100%;
display: flex;
flex-direction: column;

background-color: var(--background);
}

.header, .footer {
height: 100px;
min-height: 100px;
background-color: green;
display: flex;
flex-direction: column;
justify-content: center;

color: var(--text);
font-family: 'Courier New', Courier, monospace;
}

.footer > div {
display: flex;
flex-direction: row;
justify-content: space-between;
}

.footer > div a {
text-decoration: none;
color: var(--text);
margin: 0 5px;
}

.footer > div a:hover {
text-decoration: underline;
}

.content {
flex: 1;
overflow: hidden;
}

0 comments on commit 92cec68

Please sign in to comment.