-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.ts
81 lines (70 loc) · 1.91 KB
/
size.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
import { G2View } from '../runtime';
type Size = {
width: number;
height: number;
depth?: number;
};
const parseInt10 = (d: string) => (d ? parseInt(d) : 0);
/**
* @description Get the element's bounding size.
* @param container dom element.
* @returns the element width and height
*/
export function getContainerSize(container: HTMLElement): Size {
// size = width/height - padding.
const style = getComputedStyle(container);
const wrapperWidth = container.clientWidth || parseInt10(style.width);
const wrapperHeight = container.clientHeight || parseInt10(style.height);
const widthPadding =
parseInt10(style.paddingLeft) + parseInt10(style.paddingRight);
const heightPadding =
parseInt10(style.paddingTop) + parseInt10(style.paddingBottom);
return {
width: wrapperWidth - widthPadding,
height: wrapperHeight - heightPadding,
};
}
/**
* @description Calculate the real canvas size by view options.
*/
export function getBBoxSize(options: G2View): Size {
const {
height,
width,
padding = 0,
paddingLeft = padding,
paddingRight = padding,
paddingTop = padding,
paddingBottom = padding,
margin = 0,
marginLeft = margin,
marginRight = margin,
marginTop = margin,
marginBottom = margin,
inset = 0,
insetLeft = inset,
insetRight = inset,
insetTop = inset,
insetBottom = inset,
} = options;
// @todo Add this padding to theme.
// 30 is default size for padding, which defined in runtime.
const maybeAuto = (padding) => (padding === 'auto' ? 20 : padding);
const finalWidth =
width -
maybeAuto(paddingLeft) -
maybeAuto(paddingRight) -
marginLeft -
marginRight -
insetLeft -
insetRight;
const finalHeight =
height -
maybeAuto(paddingTop) -
maybeAuto(paddingBottom) -
marginTop -
marginBottom -
insetTop -
insetBottom;
return { width: finalWidth, height: finalHeight };
}