Skip to content

Commit

Permalink
Cache element
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmarabeas committed Jan 27, 2016
1 parent 809fb9a commit 0af12af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngFitText",
"version": "4.1.0",
"version": "4.1.1",
"main": [
"dist/ng-FitText.min.js"
],
Expand Down
2 changes: 1 addition & 1 deletion dist/ng-FitText.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-fittext",
"version": "4.1.0",
"version": "4.1.1",
"description": "An AngularJS directive for inflating web type",
"homepage": "https://github.com/patrickmarabeas/ng-FitText.js",
"bugs": "https://github.com/patrickmarabeas/ng-FitText.js/issues",
Expand Down
20 changes: 11 additions & 9 deletions src/ng-FitText.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* ng-FitText.js v4.1.0
* ng-FitText.js v4.1.1
* https://github.com/patrickmarabeas/ng-FitText.js
*
* Original jQuery project: https://github.com/davatron5000/FitText.js
Expand Down Expand Up @@ -39,6 +39,8 @@
angular.extend(config, fitTextConfig.config);

var parent = element.parent()
, domElem = element[0]
, domElemStyle = domElem.style
, computed = window.getComputedStyle(element[0], null)
, newlines = element.children().length || 1
, loadDelay = attrs.fittextLoadDelay || config.loadDelay
Expand All @@ -51,7 +53,7 @@
;

function calculate() {
var ratio = (calcSize * newlines) / element[0].offsetWidth / newlines;
var ratio = (calcSize * newlines) / domElem.offsetWidth / newlines;
return Math.max(
Math.min((parent[0].offsetWidth - 6) * ratio * compressor,
parseFloat(maxFontSize)
Expand All @@ -62,18 +64,18 @@

function resizer() {
// Don't calculate for elements with no width or height
if (element[0].offsetHeight * element[0].offsetWidth === 0)
if (domElem.offsetHeight * domElem.offsetWidth === 0)
return;

// Set standard values for calculation
element[0].style.fontSize = calcSize + 'px';
element[0].style.lineHeight = '1';
element[0].style.display = 'inline-block';
domElemStyle.fontSize = calcSize + 'px';
domElemStyle.lineHeight = '1';
domElemStyle.display = 'inline-block';

// Set usage values
element[0].style.fontSize = calculate() + 'px';
element[0].style.lineHeight = lineHeight;
element[0].style.display = display;
domElemStyle.fontSize = calculate() + 'px';
domElemStyle.lineHeight = lineHeight;
domElemStyle.display = display;
}

$timeout( function() { resizer() }, loadDelay);
Expand Down

0 comments on commit 0af12af

Please sign in to comment.