AndreP-git commited on
Commit
1074451
·
1 Parent(s): e1cfc81

Update resize

Browse files
Files changed (2) hide show
  1. app.js +35 -0
  2. index.html +15 -11
app.js CHANGED
@@ -202,6 +202,20 @@ window.onload = function()
202
 
203
  renderFormula();
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  loadPreset();
206
 
207
  }
@@ -823,6 +837,27 @@ function formatEnergy(v)
823
 
824
 
825
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
826
  // Renders every element carrying a data-tex attribute
827
  function renderFormula()
828
  {
 
202
 
203
  renderFormula();
204
 
205
+ fitFormula();
206
+ document.fonts.ready.then(fitFormula);
207
+
208
+ // Refit only when the width changes (fitting itself changes the height)
209
+ let formulaWidth=0;
210
+ new ResizeObserver(entries=>{
211
+ const width=entries[0].contentRect.width;
212
+ if(width!==formulaWidth)
213
+ {
214
+ formulaWidth=width;
215
+ fitFormula();
216
+ }
217
+ }).observe(document.querySelector(".formula"));
218
+
219
  loadPreset();
220
 
221
  }
 
837
 
838
 
839
 
840
+ // Shrinks the formula so it always fits on one line.
841
+ // All its inner sizes are in em, so the width scales linearly with font-size.
842
+ function fitFormula()
843
+ {
844
+
845
+ const formula=document.querySelector(".formula");
846
+
847
+ // Back to the CSS size, then scale down only if it overflows
848
+ formula.style.fontSize="";
849
+
850
+ const available=formula.clientWidth;
851
+ const needed=formula.scrollWidth;
852
+
853
+ if(needed>available)
854
+ {
855
+ const base=parseFloat(getComputedStyle(formula).fontSize);
856
+ formula.style.fontSize=(base*available/needed*0.98)+"px";
857
+ }
858
+
859
+ }
860
+
861
  // Renders every element carrying a data-tex attribute
862
  function renderFormula()
863
  {
index.html CHANGED
@@ -329,33 +329,37 @@
329
  font-size: 1.0625rem;
330
  }
331
 
 
 
332
  .formula {
333
  flex: 1;
334
  display: flex;
335
- align-items: stretch;
336
- align-content: center;
337
- flex-wrap: wrap;
338
- gap: 10px;
339
  font-size: 1.125rem;
 
 
340
  }
341
 
342
- .formula > span {
343
- align-self: center;
344
  }
345
 
346
  .plus {
347
- font-size: 1.25rem;
348
  color: var(--muted);
349
  }
350
 
351
  .term {
 
352
  display: flex;
353
  flex-direction: column;
354
  align-items: center;
355
  justify-content: space-between;
356
- gap: 6px;
357
- padding: 10px 14px 8px;
358
- border-radius: 10px;
359
  border: 1.5px solid var(--c);
360
  background-color: color-mix(in srgb, var(--c) 16%, white);
361
  }
@@ -382,7 +386,7 @@
382
  }
383
 
384
  .termLabel {
385
- font-size: 0.6875rem;
386
  font-weight: 600;
387
  letter-spacing: 0.02em;
388
  color: color-mix(in srgb, var(--c) 55%, black);
 
329
  font-size: 1.0625rem;
330
  }
331
 
332
+ /* Always a single line: everything inside is sized in em, and
333
+ fitFormula() in app.js lowers font-size when the line would overflow */
334
  .formula {
335
  flex: 1;
336
  display: flex;
337
+ align-items: center;
338
+ flex-wrap: nowrap;
339
+ gap: 0.55em;
 
340
  font-size: 1.125rem;
341
+ white-space: nowrap;
342
+ overflow: hidden;
343
  }
344
 
345
+ .formula > * {
346
+ flex-shrink: 0;
347
  }
348
 
349
  .plus {
350
+ font-size: 1.11em;
351
  color: var(--muted);
352
  }
353
 
354
  .term {
355
+ align-self: stretch;
356
  display: flex;
357
  flex-direction: column;
358
  align-items: center;
359
  justify-content: space-between;
360
+ gap: 0.33em;
361
+ padding: 0.55em 0.78em 0.45em;
362
+ border-radius: 0.55em;
363
  border: 1.5px solid var(--c);
364
  background-color: color-mix(in srgb, var(--c) 16%, white);
365
  }
 
386
  }
387
 
388
  .termLabel {
389
+ font-size: 0.61em;
390
  font-weight: 600;
391
  letter-spacing: 0.02em;
392
  color: color-mix(in srgb, var(--c) 55%, black);