Skip to content

Commit 6a45bde

Browse files
feat: Add scaling strategy for components styles
1 parent a43b286 commit 6a45bde

File tree

11 files changed

+132
-72
lines changed

11 files changed

+132
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
55

66
## [2.1](../../releases/tag/v2.1)
77
- Add Game Over screen sounds and animations based on Sound
8+
- Add scaling strategy for components styles
89

910
## [2.0](../../releases/tag/v2.0)
1011
- Add Category Selection

src/Scaling.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// App scaling setup used for styles
2+
import { Dimensions } from 'react-native';
3+
const { width, height } = Dimensions.get('window');
4+
5+
//Guideline sizes are based on standard ~5" screen mobile device
6+
//const guidelineBaseWidth = 350;
7+
//const guidelineBaseHeight = 680;
8+
const guidelineBaseWidth = 500;
9+
const guidelineBaseHeight = 972;
10+
11+
const scale = size => width / guidelineBaseWidth * size;
12+
const verticalScale = size => height / guidelineBaseHeight * size;
13+
const moderateScale = (size, factor = 0.5) => size + ( scale(size) - size ) * factor;
14+
15+
export {scale, verticalScale, moderateScale};

src/components/AnswerStatus.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'react-native';
88
import { Font } from 'expo';
99
import LottieView from 'lottie-react-native';
10+
import { scale, moderateScale, verticalScale} from '../Scaling';
1011

1112
const STATUS_FONT = require('../../assets/fonts/BadaboomBB_Reg.ttf');
1213
const CORRECT_ANIMATION = require('../../assets/animations/433-checked-done.json');
@@ -104,14 +105,14 @@ const styles = StyleSheet.create({
104105
backgroundColor: 'rgba(255, 255, 255, 0.9)',
105106
},
106107
statusAnimation: {
107-
width: 300,
108-
height: 300
108+
width: scale(300),
109+
height: scale(300)
109110
},
110111
statusText: {
111112
fontFamily: "status-text",
112-
fontSize: 40,
113+
fontSize: moderateScale(40),
113114
textShadowRadius: 10,
114-
marginTop: -60
115+
marginTop: moderateScale(-60)
115116
},
116117
correctText: {
117118
color: '#00C871'

src/components/Button.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { Text, TouchableOpacity } from 'react-native';
3+
import { scale, moderateScale } from '../Scaling';
34

45
/**
56
* This object sets default values to the optional props.
@@ -37,20 +38,20 @@ const Button = ({ onPress, children, style }) => {
3738
const styles = {
3839
textStyle: {
3940
flex: 1,
40-
padding: 12,
41+
padding: scale(12),
4142
color: '#ffffff',
42-
fontSize: 24,
43+
fontSize: moderateScale(24),
4344
fontWeight:'normal',
4445
textAlign: 'center',
4546
textShadowColor:'#000000',
4647
textShadowOffset:{width: 2, height: 2},
4748
textShadowRadius:0,
4849
},
4950
buttonStyle: {
50-
height: 60,
51+
height: moderateScale(60),
5152
alignSelf: 'stretch',
52-
minHeight: 32,
53-
margin: 10,
53+
minHeight: moderateScale(32),
54+
margin: scale(10),
5455
backgroundColor: 'rgba(64, 64, 255, 0.8)',
5556
borderRadius: 8
5657
}

src/components/Question.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FlatList,
88
} from 'react-native';
99
import QuestionOptionItem from './QuestionOptionItem';
10+
import { scale, moderateScale, verticalScale} from '../Scaling';
1011

1112
/**
1213
* This object is used for type checking the props of the component.
@@ -77,16 +78,16 @@ const styles = StyleSheet.create({
7778
flex: 1,
7879
justifyContent: 'center',
7980
alignItems: 'center',
80-
marginLeft: 16,
81-
marginRight: 16,
81+
marginLeft: scale(16),
82+
marginRight: scale(16),
8283
},
8384

8485
questionData: {
85-
padding: 16,
86-
marginTop: 32,
87-
marginBottom: 32,
86+
padding: scale(16),
87+
marginTop: scale(32),
88+
marginBottom: scale(32),
8889
alignSelf: 'stretch',
89-
maxHeight: 280,
90+
maxHeight: verticalScale(280),
9091
borderWidth: 2,
9192
borderRadius: 8,
9293
borderColor: '#ffffff',
@@ -96,7 +97,7 @@ const styles = StyleSheet.create({
9697

9798
questionDescription: {
9899
color: '#000',
99-
fontSize: 20,
100+
fontSize: moderateScale(20),
100101
textAlign: 'center',
101102
},
102103

src/components/QuestionOptionItem.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
TouchableOpacity,
77
} from 'react-native';
88
import PropTypes from 'prop-types';
9+
import { scale, moderateScale } from '../Scaling';
910

1011
/**
1112
* This object is used for type checking the props of the component.
@@ -64,9 +65,9 @@ const styles = StyleSheet.create({
6465

6566
quizOptionDescription: {
6667
flex: 1,
67-
padding: 12,
68+
padding: scale(12),
6869
color: '#ffffff',
69-
fontSize: 24,
70+
fontSize: moderateScale(24),
7071
fontWeight:'normal',
7172
textAlign: 'center',
7273
textShadowColor:'#000000',

src/components/TriviaLoader.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'react-native';
88
import LottieView from 'lottie-react-native';
99
import Button from './Button';
10+
import { scale, moderateScale, verticalScale} from '../Scaling';
1011

1112
// Assets
1213
const BACKGROUND_IMAGE = require('../../assets/images/game_background.png');
@@ -100,28 +101,28 @@ const styles = StyleSheet.create({
100101
backgroundColor: 'rgba(255, 255, 255, 0.9)',
101102
},
102103
loaderAnimation: {
103-
width: 200,
104-
height: 200
104+
width: moderateScale(200),
105+
height: verticalScale(200)
105106
},
106107
errorAnimation: {
107-
width: 100,
108-
height: 100
108+
width: moderateScale(100),
109+
height: verticalScale(100)
109110
},
110111
loaderText: {
111-
fontSize: 30,
112+
fontSize: moderateScale(30),
112113
textShadowOffset: {width: -1, height: 1},
113114
textShadowRadius: 10,
114115
color: '#00AA38'
115116
},
116117
errorText: {
117-
fontSize: 30,
118+
fontSize: moderateScale(30),
118119
color: '#FF4423',
119120
textShadowOffset: {width: -1, height: 1},
120121
textShadowRadius: 10,
121-
marginBottom: 10,
122+
marginBottom: scale(10),
122123
},
123124
errorDescription: {
124-
fontSize: 20,
125+
fontSize: moderateScale(20),
125126
textAlign: 'center',
126127
marginBottom: 0,
127128
},

src/components/screens/GameOver.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import {
33
View,
44
Text,
5+
ScrollView,
56
StyleSheet,
67
ImageBackground,
78
} from 'react-native';
@@ -10,7 +11,7 @@ import { Audio, Font } from 'expo';
1011
import LottieView from 'lottie-react-native';
1112
import Button from '../Button';
1213
import { goToMainMenu, startGameSelection } from '../../actions';
13-
14+
import { scale, moderateScale, verticalScale} from '../../Scaling';
1415

1516
// Static assets
1617
const GOOD_ANIMATION = require('../../../assets/animations/677-trophy.json');
@@ -114,7 +115,10 @@ class GameOver extends React.Component {
114115
resizeMode="cover"
115116
>
116117
{(this.state.fontLoaded) &&
117-
<View style={styles.gameOverData}>
118+
<View style={styles.gameOverData}>
119+
<ScrollView>
120+
121+
<View style={styles.gameOverInternal}>
118122
<Text style={styles.gameOverTitle}>GAME OVER</Text>
119123
<LottieView
120124
style={styles.statusAnimation}
@@ -134,6 +138,8 @@ class GameOver extends React.Component {
134138
Back to Main Menu
135139
</Button>
136140
</View>
141+
</ScrollView>
142+
</View>
137143
}
138144
</ImageBackground>
139145
</View>
@@ -156,9 +162,9 @@ const styles = StyleSheet.create({
156162
alignItems: 'center'
157163
},
158164
gameOverData: {
159-
padding: 16,
160-
marginTop: 32,
161-
marginBottom: 32,
165+
padding: scale(16),
166+
marginTop: scale(32),
167+
marginBottom: scale(32),
162168
alignSelf: 'stretch',
163169
alignItems: 'center',
164170
borderWidth: 2,
@@ -167,42 +173,47 @@ const styles = StyleSheet.create({
167173
justifyContent: 'center',
168174
backgroundColor: 'rgba(255, 255, 255, 0.9)',
169175
},
176+
gameOverInternal: {
177+
alignSelf: 'stretch',
178+
alignItems: 'center',
179+
justifyContent: 'center',
180+
},
170181
gameOverMessage: {
171-
fontSize: 28,
182+
fontSize: moderateScale(28),
172183
textAlign: 'center',
173184
fontWeight: "900"
174185
},
175186
gameOverTitle: {
176187
fontFamily: "game-over-title",
177188
color: '#000000',
178189
textShadowOffset: {width: -1, height: 1},
179-
textShadowRadius: 10,
180-
fontSize: 78,
181-
marginBottom: -40,
190+
textShadowRadius: scale(10),
191+
fontSize: moderateScale(78),
192+
marginBottom: scale(-40),
182193
zIndex: 9999
183194
},
184195
gameScoreText: {
185196
fontFamily: 'game-over',
186197
fontWeight: "900",
187-
fontSize: 32,
188-
marginTop: 5,
189-
marginBottom: 5,
198+
fontSize: moderateScale(32),
199+
marginTop: scale(5),
200+
marginBottom: scale(5),
190201
},
191202
gameStatusText: {
192203
fontFamily: 'game-over',
193-
fontSize: 24,
204+
fontSize: moderateScale(24),
194205
fontWeight: "900",
195206
color: '#8f61f9',
196207
marginTop: 2,
197208
marginBottom: 2,
198209
},
199210
mainMenuButton: {
200-
marginBottom: 50,
211+
marginBottom: scale(50),
201212
backgroundColor: '#DC143C'
202213
},
203214
statusAnimation: {
204-
width: 200,
205-
height: 200
215+
width: scale(200),
216+
height: scale(200)
206217
},
207218
});
208219

src/components/screens/MainMenu.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { connect } from 'react-redux';
1010
import { Font } from 'expo';
1111
import Button from '../Button';
1212
import { startGameSelection } from '../../actions';
13+
import { scale, moderateScale, verticalScale} from '../../Scaling';
1314

1415
// Game background image
1516
const BACKGROUND_IMAGE = require('../../../assets/images/game_background.png');
@@ -93,20 +94,20 @@ const styles = StyleSheet.create({
9394
},
9495
gameTitleContainer: {
9596
flex: 1,
96-
marginTop: 60,
97+
marginTop: scale(60),
9798
alignSelf: 'center',
9899
justifyContent: 'flex-start',
99100
},
100101
gameTitle: {
101102
fontFamily: "game-title",
102103
color: '#000000',
103-
fontSize: 60
104+
fontSize: moderateScale(50)
104105
},
105106
playButton: {
106-
marginBottom: 10
107+
marginBottom: scale(10)
107108
},
108109
githubButton: {
109-
marginBottom: 50,
110+
marginBottom: scale(50),
110111
backgroundColor: '#DC143C'
111112
},
112113
imageBackground: {

0 commit comments

Comments
 (0)