Skip to content

Commit 14c11cb

Browse files
committed
Fix mouse coordinates
1 parent 4700e1b commit 14c11cb

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [0.1.1] - 2022-06-22
5+
- Fix mouse coordinates when canvas display dimensions is different from it's real dimnsions
6+
47
## [0.1.0] - 2022-06-21
58
- first beta release
69

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/media-stream-composer",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "api.video media stream composer",
55
"repository": {
66
"type": "git",

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class MediaStreamComposer {
119119
displaySettings: this.buildStreamDisplaySettings(streamId, stream.stream, newOptions)
120120
};
121121

122+
122123
this.cleanIndexes();
123124
}
124125

@@ -469,30 +470,31 @@ export class MediaStreamComposer {
469470
height: e.dragStart.streamHeight! * change,
470471
x: e.dragStart.x - e.dragStart.offsetX! + (e.dragStart.circleRadius! - newRadius),
471472
y: e.dragStart.y - e.dragStart.offsetY! + (e.dragStart.circleRadius! - newRadius),
473+
position: "fixed" as StreamPosition,
472474
};
473475
this.updateStream(e.dragStart.stream.id, newDisplaySettings);
474476
} else if (e.dragStart.locations?.indexOf("bottom") !== -1) {
475477
const height = e.dragStart.streamHeight! + e.y - e.dragStart.y;
476478
const width = e.dragStart.streamWidth! * height / e.dragStart.streamHeight!;
477479
const x = e.dragStart.x - e.dragStart.offsetX! - (width - e.dragStart.streamWidth!) / 2;
478-
this.updateStream(e.dragStart.stream.id, { height, width, x });
480+
this.updateStream(e.dragStart.stream.id, { position: "fixed", height, width, x });
479481
} else if (e.dragStart.locations?.indexOf("top") !== -1) {
480482
const height = e.dragStart.streamHeight! - (e.y - e.dragStart.y);
481483
const width = e.dragStart.streamWidth! * height / e.dragStart.streamHeight!;
482484
const y = e.dragStart.y - e.dragStart.offsetY! + (e.y - e.dragStart.y);
483485
const x = e.dragStart.x - e.dragStart.offsetX! - (width - e.dragStart.streamWidth!) / 2;
484-
this.updateStream(e.dragStart.stream.id, { height, y, x, width });
486+
this.updateStream(e.dragStart.stream.id, { position: "fixed", height, y, x, width });
485487
} else if (e.dragStart.locations?.indexOf("left") !== -1) {
486488
const width = e.dragStart.streamWidth! - (e.x - e.dragStart.x);
487489
const height = e.dragStart.streamHeight! * width / e.dragStart.streamWidth!;
488490
const x = e.dragStart.x - e.dragStart.offsetX! + (e.x - e.dragStart.x);
489491
const y = e.dragStart.y - e.dragStart.offsetY! - (height - e.dragStart.streamHeight!) / 2;
490-
this.updateStream(e.dragStart.stream.id, { width, x, height, y });
492+
this.updateStream(e.dragStart.stream.id, { position: "fixed", width, x, height, y });
491493
} else if (e.dragStart.locations?.indexOf("right") !== -1) {
492494
const width = e.dragStart.streamWidth! + (e.x - e.dragStart.x)
493495
const height = e.dragStart.streamHeight! * width / e.dragStart.streamWidth!;
494496
const y = e.dragStart.y - e.dragStart.offsetY! - (height - e.dragStart.streamHeight!) / 2;
495-
this.updateStream(e.dragStart.stream.id, { width, height, y });
497+
this.updateStream(e.dragStart.stream.id, { position: "fixed", width, height, y });
496498
}
497499
} else if (e.dragStart.stream.options.draggable && e.dragStart.locations?.indexOf("inside") !== -1) {
498500
this.updateStream(e.dragStart.stream.id, {

src/mouse-event-listener.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,30 @@ export default class MouseEventListener {
5555
constructor(canvas: HTMLCanvasElement, streams: { [id: string]: StreamDetails }) {
5656
this.streams = streams;
5757

58-
const fromMouseEvent = (e: MouseEvent) => ({ x: e.offsetX, y: e.offsetY });
59-
const fromTouchEvent = (e: TouchEvent, fct: ((e: { x: number, y: number }) => void)) => {
58+
const getDimensionsRatio = () => {
59+
return {
60+
widthRatio: canvas.clientWidth / canvas.width,
61+
heightRatio: canvas.clientHeight / canvas.height,
62+
}
63+
}
64+
65+
const fromMouseEvent = (e: MouseEvent) => {
66+
const dimensionsRatio = getDimensionsRatio();
67+
return {
68+
x: e.offsetX / dimensionsRatio.widthRatio,
69+
y: e.offsetY / dimensionsRatio.heightRatio,
70+
};
71+
};
72+
const fromTouchEvent = (e: TouchEvent, fct: ((e: Coordinates) => void)) => {
6073
if (!e.targetTouches[0]) {
6174
return;
6275
}
6376
const rect = (e.target as HTMLCanvasElement).getBoundingClientRect();
77+
const dimensionsRatio = getDimensionsRatio();
78+
6479
fct({
65-
x: e.targetTouches[0].pageX - rect.left,
66-
y: e.targetTouches[0].pageY - rect.top,
80+
x: (e.targetTouches[0].pageX - rect.left) * dimensionsRatio.widthRatio,
81+
y: (e.targetTouches[0].pageY - rect.top) * dimensionsRatio.heightRatio,
6782
});
6883
};
6984

@@ -110,31 +125,31 @@ export default class MouseEventListener {
110125
}
111126
this.dragStart = undefined;
112127
}
113-
private mouseDown(e: Coordinates) {
128+
private mouseDown(mouseCoordinates: Coordinates) {
114129
if (this.overStream) {
115130
this.dragStart = {
116131
...this.overStream,
117-
offsetX: e.x - this.overStream.stream.displaySettings.position.x,
118-
offsetY: e.y - this.overStream.stream.displaySettings.position.y,
132+
offsetX: mouseCoordinates.x - this.overStream.stream.displaySettings.position.x,
133+
offsetY: mouseCoordinates.y - this.overStream.stream.displaySettings.position.y,
119134
...(this.overStream.stream.options.mask === "circle" ? { circleRadius: this.overStream.stream.displaySettings.radius } : {}),
120135
streamWidth: this.overStream.stream.displaySettings.displayResolution.width,
121136
streamHeight: this.overStream.stream.displaySettings.displayResolution.height,
122-
x: e.x,
123-
y: e.y,
137+
x: mouseCoordinates.x,
138+
y: mouseCoordinates.y,
124139
hasMoved: false,
125140
};
126141
} else {
127142
this.dragStart = {
128-
x: e.x,
129-
y: e.y,
143+
x: mouseCoordinates.x,
144+
y: mouseCoordinates.y,
130145
hasMoved: false,
131146
};
132147
}
133148
}
134-
private mouseMove(e: Coordinates) {
149+
private mouseMove(mouseCoordinates: Coordinates) {
135150
if (this.dragStart) {
136151
this.onDragListeners.forEach(l => l({
137-
...e,
152+
...mouseCoordinates,
138153
dragStart: this.dragStart!,
139154
}));
140155
this.dragStart.hasMoved = true;
@@ -161,7 +176,7 @@ export default class MouseEventListener {
161176
if (stream.options.mask === "circle") {
162177
const centerX = x + stream.displaySettings.radius!;
163178
const centerY = y + stream.displaySettings.radius!;
164-
const distance = Math.sqrt(Math.pow(centerX - e.x, 2) + Math.pow(centerY - e.y, 2));
179+
const distance = Math.sqrt(Math.pow(centerX - mouseCoordinates.x, 2) + Math.pow(centerY - mouseCoordinates.y, 2));
165180
if (distance <= stream.displaySettings.radius!) {
166181
locations.push("inside");
167182
}
@@ -170,12 +185,12 @@ export default class MouseEventListener {
170185
}
171186
} else {
172187

173-
if (e.x > x && e.x < x + width && e.y > y && e.y < y + height) locations.push("inside");
188+
if (mouseCoordinates.x > x && mouseCoordinates.x < x + width && mouseCoordinates.y > y && mouseCoordinates.y < y + height) locations.push("inside");
174189

175-
if (e.x > x && e.x < x + width && e.y > y - 10 && e.y < y + 10) locations.push("top");
176-
if (e.x > x && e.x < x + width && e.y > y + height - 10 && e.y < y + height + 10) locations.push("bottom");
177-
if (e.y > y && e.y < y + height && e.x > x - 10 && e.x < x + 10) locations.push("left");
178-
if (e.y > y && e.y < y + height && e.x > x + width - 10 && e.x < x + width + 10) locations.push("right");
190+
if (mouseCoordinates.x > x && mouseCoordinates.x < x + width && mouseCoordinates.y > y - 10 && mouseCoordinates.y < y + 10) locations.push("top");
191+
if (mouseCoordinates.x > x && mouseCoordinates.x < x + width && mouseCoordinates.y > y + height - 10 && mouseCoordinates.y < y + height + 10) locations.push("bottom");
192+
if (mouseCoordinates.y > y && mouseCoordinates.y < y + height && mouseCoordinates.x > x - 10 && mouseCoordinates.x < x + 10) locations.push("left");
193+
if (mouseCoordinates.y > y && mouseCoordinates.y < y + height && mouseCoordinates.x > x + width - 10 && mouseCoordinates.x < x + width + 10) locations.push("right");
179194
}
180195

181196
if (locations.length > 0 && stream.displaySettings.index! > index) {
@@ -190,7 +205,7 @@ export default class MouseEventListener {
190205

191206
this.onMoveListeners.forEach(l => l({
192207
...(this.overStream ? this.overStream! : {}),
193-
...e
208+
...mouseCoordinates
194209
}));
195210
}
196211
}

0 commit comments

Comments
 (0)