Skip to content

Commit 6c61474

Browse files
author
Oskar Widmark
committed
fix: enable drawing on mobile devices
1 parent d005d79 commit 6c61474

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/App.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,18 @@ class App extends React.Component<Props> {
454454
<canvas
455455
className="App-canvas"
456456
ref={this.canvasController.canvasRef}
457-
onMouseDown={(event) =>
457+
onPointerDown={(event) =>
458458
this.startDrawOnCanvas(event.clientX, event.clientY)
459459
}
460-
onMouseMove={(event) =>
460+
onPointerMove={(event) =>
461461
this.drawOnCanvas(event.clientX, event.clientY)
462462
}
463-
onMouseUp={this.canvasController.endDraw}
464-
onMouseLeave={this.canvasController.endDraw}
463+
onPointerUp={this.canvasController.endDraw}
464+
onPointerLeave={(event) => {
465+
this.drawOnCanvas(event.clientX, event.clientY);
466+
this.canvasController.endDraw();
467+
}}
468+
style={{ touchAction: 'none' }}
465469
/>
466470
</div>
467471
<SideDrawer isOpen={this.state.areSettingsOpen}>

src/AppWithSound.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ export function AppWithSound(): React.ReactElement {
2020

2121
const isPortrait = useIsPortrait();
2222

23-
if (isPortrait) {
24-
return <RotationRequest />;
25-
}
26-
2723
return (
28-
<App
29-
playSound={play}
30-
stopSounds={stop}
31-
soundType={soundType}
32-
setSoundType={setSoundType}
33-
setVolume={setVolume}
34-
/>
24+
<>
25+
{isPortrait && <RotationRequest />}
26+
<App
27+
playSound={play}
28+
stopSounds={stop}
29+
soundType={soundType}
30+
setSoundType={setSoundType}
31+
setVolume={setVolume}
32+
/>
33+
</>
3534
);
3635
}

src/canvas-controller.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ export class CanvasController {
222222
this.drawAll(arr);
223223
};
224224

225+
indexInBounds = (i: number) => {
226+
return i >= 0 && i < this.context.columnNbr;
227+
};
228+
229+
boundDrawHeight = (y: number) => {
230+
return Math.min(Math.max(y, 0), this.context.columnNbr - 1);
231+
};
232+
225233
getDrawData = (mouseX: number, mouseY: number): DrawData[] => {
226234
if (!this.isDrawing) return [];
227235

@@ -254,25 +262,29 @@ export class CanvasController {
254262
let curHeight = this.prevDrawHeight;
255263
for (
256264
let i = this.prevDrawIndex + indexIncr;
257-
i !== colIndex;
265+
i !== colIndex && this.indexInBounds(i);
258266
i += indexIncr
259267
) {
260268
curHeight +=
261269
(colHeight - this.prevDrawHeight) /
262270
Math.abs(colIndex - this.prevDrawIndex);
271+
curHeight = this.boundDrawHeight(curHeight);
263272
drawData.push({
264273
index: i,
265274
value: Math.floor(curHeight),
266275
});
267276
}
268277
}
269278

270-
drawData.push({
271-
index: colIndex,
272-
value: colHeight,
273-
});
274-
this.prevDrawIndex = colIndex;
275-
this.prevDrawHeight = colHeight;
279+
if (this.indexInBounds(colIndex)) {
280+
drawData.push({
281+
index: colIndex,
282+
value: this.boundDrawHeight(colHeight),
283+
});
284+
this.prevDrawIndex = colIndex;
285+
this.prevDrawHeight = colHeight;
286+
}
287+
276288
return drawData;
277289
};
278290

@@ -433,6 +445,7 @@ export class CanvasController {
433445
};
434446

435447
endDraw = () => {
448+
console.log('endDraw');
436449
this.isDrawing = false;
437450
this.prevDrawIndex = null;
438451
this.prevDrawHeight = null;

0 commit comments

Comments
 (0)