Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cameras sample mobile friendly #325

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/sample/cameras/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ export default interface Input {
// InputHandler is a function that when called, returns the current Input state.
export type InputHandler = () => Input;

// createInputHandler returns an InputHandler by attaching event handlers to the window.
export function createInputHandler(window: Window): InputHandler {
// createInputHandler returns an InputHandler by attaching event handlers to the window and canvas.
export function createInputHandler(
window: Window,
canvas: HTMLCanvasElement
): InputHandler {
const digital = {
forward: false,
backward: false,
Expand Down Expand Up @@ -83,20 +86,22 @@ export function createInputHandler(window: Window): InputHandler {

window.addEventListener('keydown', (e) => setDigital(e, true));
window.addEventListener('keyup', (e) => setDigital(e, false));
window.addEventListener('mousedown', () => {

canvas.style.touchAction = 'pinch-zoom';
canvas.addEventListener('pointerdown', () => {
mouseDown = true;
});
window.addEventListener('mouseup', () => {
canvas.addEventListener('pointerup', () => {
mouseDown = false;
});
window.addEventListener('mousemove', (e) => {
mouseDown = (e.buttons & 1) !== 0;
canvas.addEventListener('pointermove', (e) => {
mouseDown = e.pointerType == 'mouse' ? (e.buttons & 1) !== 0 : true;
if (mouseDown) {
analog.x += e.movementX;
analog.y += e.movementY;
}
});
window.addEventListener(
canvas.addEventListener(
'wheel',
(e) => {
mouseDown = (e.buttons & 1) !== 0;
Expand Down
6 changes: 3 additions & 3 deletions src/sample/cameras/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
}

// The input handler
const inputHandler = createInputHandler(window);
const inputHandler = createInputHandler(window, canvas);

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

const TexturedCube: () => JSX.Element = () =>
const Cameras: () => JSX.Element = () =>
makeSample({
name: 'Cameras',
description: 'This example provides example camera implementations',
Expand All @@ -272,4 +272,4 @@ const TexturedCube: () => JSX.Element = () =>
filename: __filename,
});

export default TexturedCube;
export default Cameras;
Loading