Skip to content

Commit ddc096c

Browse files
committed
attempt at a very hamfisted fix for multitouch
1 parent 1dbbba1 commit ddc096c

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

assets/js/fullkeyboardtest.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
setPedal,
88
pausePlaying,
99
stopMidiPlaying,
10-
userMovesSeekBar
10+
userMovesSeekBar,
11+
destroyRect
1112
} from "./piano/pianoPlayer.js";
1213
import { setReferencesToElements, resizeVisualizerCanvas } from "./visualizer/visualizer.js";
1314

@@ -25,12 +26,19 @@ window.addEventListener("load", function() {
2526
});
2627
}, {once: true});
2728

28-
window.addEventListener("pointerdown", function() {
29+
keyboard.addEventListener("pointerdown", function() {
2930
state.mouseDown = true;
3031
});
3132

32-
window.addEventListener("pointerup", function() {
33+
keyboard.addEventListener("pointerup", function() {
3334
state.mouseDown = false;
35+
console.log("keyboard pointer up");
36+
37+
state.visualizerRects.forEach(visualizerRect => {
38+
if (visualizerRect.length > 0) {
39+
visualizerRect.forEach(rect => destroyRect(rect));
40+
}
41+
});
3442
});
3543

3644
// attach event listeners to each key of the piano

assets/js/piano/pianoPlayer.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ export function startPlaying(i, key, color = "red", gain = 1, skipDrawing = fals
123123
const rect = drawRect(state.rects[i], 10000, color);
124124
state.visualizerRects[i].push({
125125
rect: rect,
126-
createdAt: Date.now()
126+
createdAt: Date.now(),
127+
i: i,
128+
color: color
127129
});
128130
}
129131
}
@@ -137,21 +139,10 @@ export function stopPlaying(i, key, color = "red", skipDestroy = false) {
137139
key.style.fill = key.dataset.fill;
138140
key.setAttribute("class", "unpressed");
139141
state.currentlyHeldDownKeys[i] = false;
140-
141-
// console.log(state.visualizerRects);
142142

143143
if (! skipDestroy) {
144144
const rectObj = state.visualizerRects[i].shift();
145-
const rect = rectObj.rect;
146-
const duration = Date.now() - rectObj.createdAt;
147-
148-
const replacementRect = drawRect(state.rects[i], duration, color, {yPos: visualizerHeight - (visualizerHeight * (duration / 2000))});
149-
150-
setTimeout(function() {
151-
replacementRect.parentNode.removeChild(replacementRect);
152-
}, duration + 2000);
153-
154-
rect.parentNode.removeChild(rect);
145+
destroyRect(rectObj);
155146
}
156147

157148
if (!state.pedal) {
@@ -165,6 +156,18 @@ export function stopPlaying(i, key, color = "red", skipDestroy = false) {
165156
}
166157
}
167158

159+
export function destroyRect(rectObj){
160+
const duration = Date.now() - rectObj.createdAt;
161+
162+
const replacementRect = drawRect(state.rects[rectObj.i], duration, rectObj.color, {yPos: visualizerHeight - (visualizerHeight * (duration / 2000))});
163+
164+
setTimeout(function() {
165+
replacementRect.parentNode.removeChild(replacementRect);
166+
}, duration + 2000);
167+
168+
rectObj.rect.parentNode.removeChild(rectObj.rect);
169+
}
170+
168171
function noteStop(note, noteBufferSource, endingVolume = 0.1, noteFadeDuration = CONSTANTS.noteFade) {
169172
try {
170173
note.gain.gain.setTargetAtTime(endingVolume, state.audioContext.currentTime, noteFadeDuration);

0 commit comments

Comments
 (0)