Skip to content

Commit

Permalink
Refactor attribute calculations to be DRYer
Browse files Browse the repository at this point in the history
- Create `scaleInRange()` function to eliminate repeated calculations using the same formula.
- Rename `linear_scale_factor` to the more descriptive `width_ratio` (where value at `minWidth` would be 0 and at `maxWidth` would be 1), which might also be more sensible to use in easing-curve calculations in the future.
  • Loading branch information
tinymachine committed Feb 22, 2019
1 parent 7daf3a6 commit 934cf16
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/textblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ window.Textblock = function(textblocks) {
tb_maxw
); // cap current container width to minWidth and maxWidth settings

var linear_scale_factor =
(current_width_capped - tb_minw) / (tb_maxw - tb_minw);
var width_ratio = (current_width_capped - tb_minw) / (tb_maxw - tb_minw);

return {
fontSize: tb_minf + (tb_maxf - tb_minf) * linear_scale_factor,
lineHeight: tb_minl + (tb_maxl - tb_minl) * linear_scale_factor,
fontSize: scaleInRange(tb_minf, tb_maxf, width_ratio),
lineHeight: scaleInRange(tb_minl, tb_maxl, width_ratio),
fontVariationSettings:
'"wght" ' + (tb_ming + (tb_maxg - tb_ming) * linear_scale_factor)
'"wght" ' + scaleInRange(tb_ming, tb_maxg, width_ratio)
};
}
}
function scaleInRange(min, max, scale_factor) {
return min + (max - min) * scale_factor;
}
function prepBlockSettings(block) {
var defaultSettings = {
minWidth: 320,
Expand Down
2 changes: 1 addition & 1 deletion textblock.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 934cf16

Please sign in to comment.