-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey-utils.js
69 lines (65 loc) · 1.78 KB
/
key-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { getBoundingBox } from "@the-via/reader";
export const CSSVarObject = {
keyWidth: 80,
keyXSpacing: 8,
keyHeight: 80,
keyYSpacing: 8,
keyXPos: 80 + 8,
keyYPos: 80 + 8,
};
export const getColor = (color) => {
switch (color) {
case "accent": {
return "MistyRose";
}
case "alpha": {
return "Snow";
}
case "mod": {
return "GainsBoro";
}
default: {
return "yellow";
}
}
};
export function calculatePointPosition({
x = 0,
y = 0,
r = 0,
rx = 0,
ry = 0,
w = 0,
h = 0,
}) {
// We express the radians in counter-clockwise form, translate the point by the origin, rotate it, then reverse the translation
const rRadian = (r * (2 * Math.PI)) / 360;
const cosR = Math.cos(rRadian);
const sinR = Math.sin(rRadian);
const originX = CSSVarObject.keyXPos * rx;
const originY = CSSVarObject.keyYPos * ry;
const xPos =
CSSVarObject.keyXPos * x +
(w * CSSVarObject.keyWidth) / 2 +
((w - 1) * CSSVarObject.keyXSpacing) / 2;
const yPos =
CSSVarObject.keyYPos * y +
(h * CSSVarObject.keyHeight) / 2 +
((h - 1) * CSSVarObject.keyYSpacing) / 2;
const transformedXPos =
xPos * cosR - yPos * sinR - originX * cosR + originY * sinR + originX;
const transformedYPos =
xPos * sinR + yPos * cosR - originX * sinR - originY * cosR + originY;
return [transformedXPos, transformedYPos];
}
export const calculateKeyboardFrameDimensions = (keys) => {
const boundingBoxes = keys.map(getBoundingBox);
const minX = Math.min(...boundingBoxes.map((b) => b.xStart));
const minY = Math.min(...boundingBoxes.map((b) => b.yStart));
const width = Math.max(...boundingBoxes.map((b) => b.xEnd)) - minX;
const height = Math.max(...boundingBoxes.map((b) => b.yEnd)) - minY;
return {
width,
height,
};
};