-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathcreateLayout.svelte.ts
More file actions
125 lines (107 loc) · 4.01 KB
/
createLayout.svelte.ts
File metadata and controls
125 lines (107 loc) · 4.01 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { innerWidth, innerHeight } from 'svelte/reactivity/window';
type Sizes = { width: number; height: number };
const CANVAS_RATIO_TYPE_BREAK_POINTS = {
wideSquare: 1.3, // Min ratio of long width canvas, used for position FS UI elements
narrowSquare: 0.8, // GALAXY FOLD EXPAND RATIO IS 1.400390625
};
const CANVAS_SIZE_TYPE_BREAK_POINTS = {
smallMobile: 375, // Max size of small mobile layouts e.g. iPhone SE
mobile: 480, // Max size of common mobile layouts e.g. iPhone XR
tablet: 820, // Max size of tablets layouts, e.g. iPad Air
largeTablet: 1024, // Max size of large tablets layouts, e.g. iPad Pro
};
const getRatio = (value: Sizes) => value.width / (value.height || 1);
const STANDARD_MAIN_SIZES_MAP = {
desktop: { width: 1920, height: 1080 },
tablet: { width: 1920, height: 1920 },
landscape: { width: 1920, height: 1080 },
portrait: { width: 1080, height: 1920 },
};
type MainSizesMap = typeof STANDARD_MAIN_SIZES_MAP;
export const createLayout = (layoutOptions: {
backgroundRatio: {
normal: number;
portrait: number;
};
mainSizesMap: MainSizesMap;
}) => {
const canvasSizes = () => ({ width: innerWidth.current ?? 1, height: innerHeight.current ?? 1 }); // because of resizeTo: window
const canvasRatio = () => getRatio(canvasSizes());
const canvasRatioType = () => {
if (canvasRatio() >= CANVAS_RATIO_TYPE_BREAK_POINTS.wideSquare) return 'longWidth' as const;
if (canvasRatio() <= CANVAS_RATIO_TYPE_BREAK_POINTS.narrowSquare) return 'longHeight' as const;
return 'almostSquare' as const;
};
const canvasSizeType = () => {
const deviceWidth = Math.min(canvasSizes().width, canvasSizes().height);
if (deviceWidth <= CANVAS_SIZE_TYPE_BREAK_POINTS.smallMobile) return 'smallMobile' as const;
if (deviceWidth <= CANVAS_SIZE_TYPE_BREAK_POINTS.mobile) return 'mobile' as const;
if (deviceWidth <= CANVAS_SIZE_TYPE_BREAK_POINTS.tablet) return 'tablet' as const;
if (deviceWidth <= CANVAS_SIZE_TYPE_BREAK_POINTS.largeTablet) return 'largeTablet' as const;
return 'desktop' as const;
};
const layoutType = () => {
if (canvasRatioType() === 'almostSquare') return 'tablet' as const;
if (canvasRatioType() === 'longHeight') return 'portrait' as const;
if (canvasSizeType() === 'mobile' || canvasSizeType() === 'smallMobile')
return 'landscape' as const;
return 'desktop' as const;
};
const isStacked = () => ['portrait', 'almostSquare'].includes(layoutType());
const createMainLayout = (mainSizesMap: MainSizesMap) => () => {
const x = canvasSizes().width * 0.5;
const y = canvasSizes().height * 0.5;
const mainSizes = mainSizesMap[layoutType()];
const widthScale = canvasSizes().width / mainSizes.width;
const heightScale = canvasSizes().height / mainSizes.height;
const scale = Math.min(widthScale, heightScale);
return {
x,
y,
scale,
width: mainSizes.width,
height: mainSizes.height,
anchor: 0.5,
};
};
const mainLayout = createMainLayout(layoutOptions.mainSizesMap);
const mainLayoutStandard = createMainLayout(STANDARD_MAIN_SIZES_MAP);
const createBackgroundLayout = ({ scale, ratio }: { scale: number; ratio: number }) => {
const canvasRatio = getRatio(canvasSizes());
if (canvasRatio < ratio) {
return {
x: canvasSizes().width / 2,
y: canvasSizes().height / 2,
height: canvasSizes().height * scale,
};
}
return {
x: canvasSizes().width / 2,
y: canvasSizes().height / 2,
width: canvasSizes().width * scale,
};
};
const normalBackgroundLayout = ({ scale }: { scale: number }) =>
createBackgroundLayout({ scale, ratio: layoutOptions.backgroundRatio.normal });
const portraitBackgroundLayout = ({ scale }: { scale: number }) =>
createBackgroundLayout({ scale, ratio: layoutOptions.backgroundRatio.portrait });
const stateLayout = $state({
showLoadingScreen: true,
});
const stateLayoutDerived = {
canvasSizes,
canvasRatio,
canvasRatioType,
canvasSizeType,
layoutType,
isStacked,
mainLayout,
mainLayoutStandard,
normalBackgroundLayout,
portraitBackgroundLayout,
};
return {
stateLayout,
stateLayoutDerived,
};
};