Skip to content

Commit

Permalink
feat: add support for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasmafra committed Feb 2, 2024
1 parent fab6bb0 commit a2cf985
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions piano/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./svg/style.css">
<title>Piano Web</title>
</head>
<body>
<div id="container"></div>
Expand Down
11 changes: 9 additions & 2 deletions piano/svg/PianoSvg.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const strokeWidth = 1;

class PianoKeySvg {

pressed = false;

constructor(data, piano) {
this.data = data;
this.piano = piano;
Expand All @@ -41,17 +43,22 @@ class PianoKeySvg {
this.element.addEventListener('mousedown', () => this.onClickStart());
this.element.addEventListener('mouseup', () => this.onClickEnd());
this.element.addEventListener('mouseout', () => this.onClickEnd());
this.element.addEventListener('touchstart', () => this.onClickStart());
this.element.addEventListener('touchend', () => this.onClickEnd());
this.element.addEventListener('touchcancel', () => this.onClickEnd());
this.element.classList.add(this.data.typeClass);
}

onClickStart() {
if (this.piano.onKeyPress)
if (!this.pressed && this.piano.onKeyPress)
this.piano.onKeyPress(this);
this.pressed = true;
}

onClickEnd() {
if (this.piano.onKeyRelease)
if (this.pressed && this.piano.onKeyRelease)
this.piano.onKeyRelease(this);
this.pressed = false;
}

addHighlight() {
Expand Down

0 comments on commit a2cf985

Please sign in to comment.