-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
243 lines (216 loc) · 6.27 KB
/
index.html
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html>
<head>
<title>Calculadora de font-size responsivo</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<style type="text/css">
* {
margin: 0;
padding: 0;
border: none;
}
body {
font-family: sans-serif;
margin: 30px;
}
p {
margin: 16px 0;
}
form {
margin: 10px 0;
}
fieldset {
margin: 10px 0;
}
input {
padding: 5px;
border: 1px solid #ccc;
width: 70px;
}
input[name=breakpoint] {
margin-right: 10px;
}
.button {
width: 20px;
height: 20px;
border: 1px solid #aaa;
background-color: #ccc;
cursor: pointer;
border-radius: 100%;
font-weight: bold;
display: inline-block;
text-align: center;
}
.minus:after {
content: '-';
}
.plus:after {
content: '+';
}
.calculate {
display: inline-block;
padding: 10px 15px;
width: auto;
height: auto;
border-radius: 0;
margin: 20px 0;
}
#css {
border: 1px solid #bbb;
background-color: #eee;
padding: 30px;
}
</style>
</head>
<body>
<h1>Tamanho responsivo para CSS</h1>
<p>
Para entender melhor o funcionamento, leia <a href="https://medium.com/@euharrison/trigonometria-do-font-size-a104c95fa9a">https://medium.com/@euharrison/trigonometria-do-font-size-a104c95fa9a</a>
</p>
<br>
<h2>Função para sass</h2>
<p><pre>
@function calc-responsive($value1, $width1, $value2, $width2) {
$x1: $width1;
$x2: $width2;
$y1: $value1;
$y2: $value2;
$a: ($y2 - $y1) / ($x2 - $x1);
$b: ($x2 * $y1 - $x1 * $y2) / ($x2 - $x1);
@return calc(#{$a*100vw} + #{$b*1px});
}
</pre></p>
<p>Aplicando, teremos:</p>
<pre>
body {
font-size: calc-responsive(16, 320, 30, 1920);
}
</pre>
<br>
<br>
<h2>Calculadora</h2>
<p>Útil para analisar o gráfico que gera e criar breakpoints</p>
<form>
<fieldset>
font-size: <input type="number" name="fontSize" value="16">
breakpoint: <input type="number" name="breakpoint" value="320">
<div class="button minus"></div>
</fieldset>
<fieldset>
font-size: <input type="number" name="fontSize" value="18">
breakpoint: <input type="number" name="breakpoint" value="768">
<div class="button minus"></div>
</fieldset>
<fieldset>
font-size: <input type="number" name="fontSize" value="22">
breakpoint: <input type="number" name="breakpoint" value="1360">
<div class="button minus"></div>
</fieldset>
<fieldset>
font-size: <input type="number" name="fontSize" value="30">
breakpoint: <input type="number" name="breakpoint" value="1920">
<div class="button minus"></div>
</fieldset>
</form>
<div class="button plus"></div>
<br>
<div class="button calculate">CALCULATE</div>
<div id="graph"></div>
<pre><div id="css"></div></pre>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<script>
var graph = Morris.Line({
element: 'graph',
xkey: 'windowWidth',
ykeys: ['fontSize'],
labels: ['font-size'],
xLabelFormat: function (x) { return x.src.windowWidth+'px'; },
yLabelFormat: function (y) { return y+'px'; },
parseTime: false,
});
function setupMinus() {
$('.minus').click(function() {
$(this).parent().remove();
});
}
setupMinus();
$('.plus').click(function() {
var fieldset = '';
fieldset += '<fieldset> ';
fieldset += 'font-size: <input type="number" name="fontSize" value="30"> ';
fieldset += 'breakpoint: <input type="number" name="breakpoint" value="1920"> ';
fieldset += '<div class="button minus"></div> ';
fieldset += '</fieldset>';
$('form').append(fieldset);
setupMinus();
});
$('.calculate').click(calculate);
calculate();
function calculate() {
var equacoes = [];
// Input
var breakpoints = [];
var fontSizes = [];
$('fieldset').each(function(){
breakpoints.push( parseInt($('[name=breakpoint]', this).val()) );
fontSizes.push( parseInt($('[name=fontSize]', this).val()) );
})
// Equacoes
for (var i = 0; i < breakpoints.length-1; i++) {
// y = ax + b
// font-size = a(vw) + b
var x1 = breakpoints[i];
var x2 = breakpoints[i+1];
var y1 = fontSizes[i];
var y2 = fontSizes[i+1];
var a = (y2-y1) / (x2-x1);
var b = (x2*y1 - x1*y2) / (x2 - x1);
equacoes[i] = {
a: a,
b: b,
minWidth: x1,
maxWidth: x2,
}
};
// Graph
var minWidth = breakpoints[0];
var maxWidth = breakpoints[breakpoints.length-1];
var step = 20;
var totalSteps = (maxWidth-minWidth)/step + 1;
var data = [];
for (var i = 0; i < totalSteps; i++) {
var windowWidth = minWidth+(i*step);
var equacao = getEquacao(windowWidth);
data[i] = {
windowWidth: windowWidth,
fontSize: equacao.a*windowWidth + equacao.b,
};
};
graph.setData(data);
function getEquacao(windowWidth) {
for (var i = 0; i < equacoes.length; i++) {
if (windowWidth < equacoes[i].maxWidth) {
return equacoes[i];
}
};
return equacoes[equacoes.length-1];
}
// CSS
// no font-size, multiplicar por 100 pois a unidade vw usa valores de 0-100, ao invés de 0-1
var css = '';
css += 'font-size: calc('+(equacoes[0].a*100)+'vw + '+equacoes[0].b+'px);\n\n';
for (var i = 1; i < equacoes.length; i++) {
css += '@media (min-width: '+equacoes[i].minWidth+'px) {\n';
css += ' font-size: calc('+(equacoes[i].a*100)+'vw + '+equacoes[i].b+'px);\n';
css += '}\n';
}
css += '@media (min-width: '+breakpoints[breakpoints.length-1]+'px) {\n';
css += ' font-size: '+fontSizes[fontSizes.length-1]+'px;\n';
css += '}\n';
document.getElementById('css').innerHTML = css;
}
</script>
</body>
</html>