Skip to content

Commit e1b9bd2

Browse files
committed
feat: add mm properties to AnalyzeResult
1 parent e34cf40 commit e1b9bd2

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ console.log(result.elements);
5353
// height: 18, // Element height in CSS pixels
5454
// left: 32, // X position from left edge
5555
// top: 200.875, // Y position from top edge
56-
// tapSuccessRate: 0.8497559260608007 // Success rate (0-1, where 1 is 100%)
56+
// widthMm: 22.69..., // Element width in millimeters
57+
// heightMm: 2.98..., // Element height in millimeters
58+
// tapSuccessRate: 0.84... // Success rate (0-1, where 1 is 100%)
5759
// }
5860
// ]
5961

src/tappy.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ export class Tappy {
1717
const html = await this.adapter.getHtml();
1818

1919
const resultElements = elements.map((element) => {
20-
const mmWidth = pixelToMm(element.width, device.ppi, device.scaleFactor);
21-
const mmHeight = pixelToMm(element.height, device.ppi, device.scaleFactor);
22-
const sigmaX = Math.sqrt(0.0091 * mmWidth ** 2 + 1.0949);
23-
const sigmaY = Math.sqrt(0.0149 * mmHeight ** 2 + 0.9414);
20+
const widthMm = pixelToMm(element.width, device.ppi, device.scaleFactor);
21+
const heightMm = pixelToMm(
22+
element.height,
23+
device.ppi,
24+
device.scaleFactor,
25+
);
26+
const sigmaX = Math.sqrt(0.0091 * widthMm ** 2 + 1.0949);
27+
const sigmaY = Math.sqrt(0.0149 * heightMm ** 2 + 0.9414);
2428
const tapSuccessRate =
25-
erf(mmWidth / (2 * Math.sqrt(2) * sigmaX)) *
26-
erf(mmHeight / (2 * Math.sqrt(2) * sigmaY));
29+
erf(widthMm / (2 * Math.sqrt(2) * sigmaX)) *
30+
erf(heightMm / (2 * Math.sqrt(2) * sigmaY));
2731

2832
return {
2933
...element,
34+
widthMm: widthMm,
35+
heightMm: heightMm,
3036
tapSuccessRate: tapSuccessRate,
3137
};
3238
});

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ export interface Adapter {
8383
*/
8484
export type AnalyzeResult = {
8585
device: Device;
86-
elements: (TappableElement & { tapSuccessRate: number })[];
86+
elements: (TappableElement & {
87+
widthMm: number;
88+
heightMm: number;
89+
tapSuccessRate: number;
90+
})[];
8791
screenshot: string;
8892
html: string;
8993
};

test/tappy.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ describe("Tappy", () => {
4646

4747
expect(result.elements[0]).toBeDefined();
4848
expect(result.elements[1]).toBeDefined();
49+
expect(result.elements[0]).toHaveProperty("widthMm");
50+
expect(result.elements[1]).toHaveProperty("widthMm");
51+
expect(result.elements[0]).toHaveProperty("heightMm");
52+
expect(result.elements[1]).toHaveProperty("heightMm");
4953
expect(result.elements[0]).toHaveProperty("tapSuccessRate");
5054
expect(result.elements[1]).toHaveProperty("tapSuccessRate");
5155

0 commit comments

Comments
 (0)