Skip to content

Commit

Permalink
Improve mobile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jverneaut committed Oct 7, 2024
1 parent 7c9be8a commit 63ca7b8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/synth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ const interpolated = { x: 0, y: 0 };

const distance = { x: 0, y: 0 };

window.addEventListener('mousemove', (e) => {
mousePos.x = e.clientX;
mousePos.y = e.clientY;
['touchstart', 'touchmove', 'mousemove'].forEach((event) => {
window.addEventListener(event, (e) => {
const x =
event === 'touchstart' || event === 'touchmove'
? e.touches[0].clientX
: e.clientX;
const y =
event === 'touchstart' || event === 'touchmove'
? e.touches[0].clientY
: e.clientY;

mousePos.x = x;
mousePos.y = y;
});
});

const mouse = document.createElement('div');
Expand Down Expand Up @@ -139,33 +150,37 @@ const interpolate = () => {
requestAnimationFrame(interpolate);
};

document.addEventListener('mousedown', () => {
if (!isPlaying) {
oscillators.forEach((osc) => {
if (!osc.started) {
osc.osc.start();
osc.started = true;
}
['touchstart', 'mousedown'].forEach((event) => {
document.addEventListener(event, () => {
if (!isPlaying) {
oscillators.forEach((osc) => {
if (!osc.started) {
osc.osc.start();
osc.started = true;
}

osc.osc.connect(lp);
osc.connected = true;
});
osc.osc.connect(lp);
osc.connected = true;
});

isPlaying = true;
}
isPlaying = true;
}
});
});

document.addEventListener('mouseup', () => {
if (isPlaying) {
oscillators.forEach((osc) => {
if (osc.connected) {
osc.osc.disconnect(lp);
osc.connected = false;
}
});

isPlaying = false;
}
['touchend', 'mouseup'].forEach((event) => {
document.addEventListener(event, () => {
if (isPlaying) {
oscillators.forEach((osc) => {
if (osc.connected) {
osc.osc.disconnect(lp);
osc.connected = false;
}
});

isPlaying = false;
}
});
});

requestAnimationFrame(interpolate);
9 changes: 9 additions & 0 deletions src/synth/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ html {

height: 100%;
background-size: calc(100% / 24);

position: fixed;
width: 100vw;
height: 100vh;
overflow: hidden;
}

.info {
Expand Down Expand Up @@ -46,3 +51,7 @@ html {
gap: 6px;
}
}

* {
box-sizing: border-box;
}

0 comments on commit 63ca7b8

Please sign in to comment.