Skip to content

Commit dafbe4b

Browse files
authored
Add files via upload
1 parent c9d41af commit dafbe4b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ export const randomInt = (min = 0, max = 1) => {
1616

1717
};
1818

19+
// converts screen point to canvas point
20+
21+
export const screenToCanvas = (cnv, x, y) => {
22+
23+
const rect = cnv.getBoundingClientRect(), // abs. size of element
24+
25+
scaleX = cnv.width / rect.width, // relationship bitmap vs. element for X
26+
27+
scaleY = cmv.height / rect.height; // relationship bitmap vs. element for Y
28+
29+
return {
30+
31+
x: (x - rect.left) * scaleX, // scale mouse coordinates after they have
32+
33+
y: (y - rect.top) * scaleY // been adjusted to be relative to element
34+
35+
}
36+
37+
};
38+
1939
// array stuff
2040

2141
export const unique = arr => [...new Set(arr)];

0 commit comments

Comments
 (0)