Skip to content

Commit

Permalink
Normalize wheel events across browsers (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanderkamp authored Jun 27, 2023
1 parent 76eec68 commit c7d0afa
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/client.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/client.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,34 @@ class DrawingApp {
view.removeAllListeners('drag');
if (type === 'pan') {
view.on('drag', actions.drag);
view.on('pinch', actions.pinch);
view.on('pinch', constrainedZoom);
view.on('rotate', actions.rotate);
view.off('drag', this.boundDraw);
} else {
view.off('drag', actions.drag);
view.off('pinch', actions.pinch);
view.off('pinch', constrainedZoom);
view.off('rotate', actions.rotate);
view.on('drag', this.boundDraw);
}
}

handleConnect({ view }) {
view.on('drag', actions.drag);
view.on('pinch', actions.pinch);
view.on('pinch', constrainedZoom);
view.on('rotate', actions.rotate);
this.setColor({ color: 'red', view });
this.setWidth({ width: 'medium', view });
}
}

function constrainedZoom(event) {
const targetScale = event.target.scale;
const deltaScale = event.scale;
if ((deltaScale > 1 && targetScale < 3) || (deltaScale < 1 && targetScale > 0.1)) {
actions.pinch(event);
}
}

// eslint-disable-next-line
const drawingApp = new DrawingApp();
drawingApp.initListeners();
Expand Down
4 changes: 4 additions & 0 deletions examples/drawing-app/static/drawing-app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
--color-two: #97ccf6;
}

html {
background-color: antiquewhite;
}

ul {
list-style: none;
padding: 0;
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"canvas-sequencer": "^3.0.6",
"core-js": "^3",
"express": "^4.17.1",
"normalize-wheel": "^1.0.1",
"socket.io": "^4.6.2",
"socket.io-client": "^4.6.2",
"westures": "^1.1.0"
Expand Down
14 changes: 10 additions & 4 deletions src/client/ClientController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { io } = require('socket.io-client');
const normalizeWheel = require('normalize-wheel');

const { constants, Message, NOP } = require('../shared.js');

Expand Down Expand Up @@ -333,6 +334,9 @@ class ClientController {
* Set up input event forwarding.
*/
setUpInputForwarding() {
// Prevent default gestures from the browser
this.canvas.style.touchAction = 'none';

// Forward pointer events
['pointerdown', 'pointermove', 'pointerup'].forEach((eventname) => {
this.canvas.addEventListener(eventname, (event) => {
Expand Down Expand Up @@ -390,10 +394,12 @@ class ClientController {
(event) => {
if (event.ctrlKey) {
event.preventDefault();
// Extract only the properties we care about
const { type, clientX, clientY, deltaY, deltaMode } = event;
const data = { type, clientX, clientY, deltaY, deltaMode };
this.socket.emit(Message.WHEEL, data);
const { clientX, clientY, deltaY } = event;
const { spinY } = normalizeWheel(event);
if (isFinite(spinY) && isFinite(deltaY)) {
const data = { clientX, clientY, deltaY, spinY };
this.socket.emit(Message.WHEEL, data);
}
}
},
{ passive: false }
Expand Down
5 changes: 3 additions & 2 deletions src/server/ServerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,13 @@ class ServerController {
* @param {WheelEvent} event - The event to forward.
*/
wheelEvent(event) {
const { clientX, clientY } = event;
const { clientX, clientY, deltaY, spinY } = event;
const target = this.view.group;
const view = this.view;
const group = this.view.group;
const device = this.device;
const scale = 1 - event.deltaY * 0.01;
let scaleChange = Math.abs(spinY) < Math.abs(deltaY) ? spinY : deltaY;
const scale = Math.max(Math.min(1 - scaleChange * 0.1, 1.1), 0.9);
const { x, y } = this.view.transformPoint(clientX, clientY);
this.application.messageHandler.scale({ x, y, target, view, group, device }, { scale });
}
Expand Down

0 comments on commit c7d0afa

Please sign in to comment.