-
-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathoverlay-path.ts
More file actions
57 lines (56 loc) · 1.4 KB
/
overlay-path.ts
File metadata and controls
57 lines (56 loc) · 1.4 KB
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
interface OverlayPathParams {
height: number;
r?:
| number
| {
bottomLeft: number;
bottomRight: number;
topLeft: number;
topRight: number;
};
x?: number;
y?: number;
width: number;
}
/**
* Generates the svg path data for a rounded rectangle overlay
* @param dimension - Dimensions of rectangle.
* @param dimension.width - Width.
* @param dimension.height - Height.
* @param dimension.x - Offset from top left corner in x axis. default 0.
* @param dimension.y - Offset from top left corner in y axis. default 0.
* @param dimension.r - Corner Radius. Keep this smaller than half of width or height.
* @returns Rounded rectangle overlay path data.
*/
export function makeOverlayPath({
width,
height,
x = 0,
y = 0,
r = 0
}: OverlayPathParams) {
const { innerWidth: w, innerHeight: h } = window;
const {
topLeft = 0,
topRight = 0,
bottomRight = 0,
bottomLeft = 0
} = typeof r === 'number'
? { topLeft: r, topRight: r, bottomRight: r, bottomLeft: r }
: r;
return `M${w},${h}
H0
V0
H${w}
V${h}
Z
M${x + topLeft},${y}
a${topLeft},${topLeft},0,0,0-${topLeft},${topLeft}
V${height + y - bottomLeft}
a${bottomLeft},${bottomLeft},0,0,0,${bottomLeft},${bottomLeft}
H${width + x - bottomRight}
a${bottomRight},${bottomRight},0,0,0,${bottomRight}-${bottomRight}
V${y + topRight}
a${topRight},${topRight},0,0,0-${topRight}-${topRight}
Z`;
}