Skip to content

Commit f742f58

Browse files
beaufortfrancoisben-clayton
authored andcommitted
Make cameras sample mobile friendly
1 parent f7f813c commit f742f58

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/sample/cameras/input.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ export default interface Input {
2727
// InputHandler is a function that when called, returns the current Input state.
2828
export type InputHandler = () => Input;
2929

30-
// createInputHandler returns an InputHandler by attaching event handlers to the window.
31-
export function createInputHandler(window: Window): InputHandler {
30+
// createInputHandler returns an InputHandler by attaching event handlers to the window and canvas.
31+
export function createInputHandler(
32+
window: Window,
33+
canvas: HTMLCanvasElement
34+
): InputHandler {
3235
const digital = {
3336
forward: false,
3437
backward: false,
@@ -83,20 +86,22 @@ export function createInputHandler(window: Window): InputHandler {
8386

8487
window.addEventListener('keydown', (e) => setDigital(e, true));
8588
window.addEventListener('keyup', (e) => setDigital(e, false));
86-
window.addEventListener('mousedown', () => {
89+
90+
canvas.style.touchAction = 'pinch-zoom';
91+
canvas.addEventListener('pointerdown', () => {
8792
mouseDown = true;
8893
});
89-
window.addEventListener('mouseup', () => {
94+
canvas.addEventListener('pointerup', () => {
9095
mouseDown = false;
9196
});
92-
window.addEventListener('mousemove', (e) => {
93-
mouseDown = (e.buttons & 1) !== 0;
97+
canvas.addEventListener('pointermove', (e) => {
98+
mouseDown = e.pointerType == 'mouse' ? (e.buttons & 1) !== 0 : true;
9499
if (mouseDown) {
95100
analog.x += e.movementX;
96101
analog.y += e.movementY;
97102
}
98103
});
99-
window.addEventListener(
104+
canvas.addEventListener(
100105
'wheel',
101106
(e) => {
102107
mouseDown = (e.buttons & 1) !== 0;

src/sample/cameras/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
1717
}
1818

1919
// The input handler
20-
const inputHandler = createInputHandler(window);
20+
const inputHandler = createInputHandler(window, canvas);
2121

2222
// The camera types
2323
const initialCameraPosition = vec3.create(3, 2, 5);
@@ -245,7 +245,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
245245
requestAnimationFrame(frame);
246246
};
247247

248-
const TexturedCube: () => JSX.Element = () =>
248+
const Cameras: () => JSX.Element = () =>
249249
makeSample({
250250
name: 'Cameras',
251251
description: 'This example provides example camera implementations',
@@ -272,4 +272,4 @@ const TexturedCube: () => JSX.Element = () =>
272272
filename: __filename,
273273
});
274274

275-
export default TexturedCube;
275+
export default Cameras;

0 commit comments

Comments
 (0)