Skip to content

Commit

Permalink
remove redundant postFormatter (use optional formattingFn instead), f…
Browse files Browse the repository at this point in the history
…ix bug, clean up code
  • Loading branch information
inorganik committed Apr 22, 2016
1 parent 11bd2cf commit 0353d8f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 279 deletions.
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ CountUp.js supports all browsers.

Simply include the countUp.js file in your project or install via npm or bower using the package name `countup.js`.

To contribute, please [read more here](contributing.md).
Before making a pull request, please [read this](#contributing). MIT License.

## Angular directive
Included is an angular module. Use the count-up attribute to quickly create an animation. It also integrates nicely with the angular-scroll-spy directive. The Angular directive only requires an `end-val` attribute, but will also accept `start-val`, `duration`, and `decimals`. `id` is not needed. You must include both countUp.js and the module to use the angular directive. **[Check out the angular demo](http://inorganik.github.io/angular-scroll-spy).**
Included is an angular module. Use the count-up attribute to quickly create an animation. It also integrates nicely with the angular-scroll-spy directive. The Angular directive only requires an `end-val` attribute, but will also accept `start-val`, `duration`, `decimals`, and `options`. `id` is not needed. You must include both countUp.js and the module to use the angular directive. **[Check out the angular demo](http://inorganik.github.io/angular-scroll-spy).**

## WordPress plugin
If you want a quick and easy way to use this on your WordPress site, try this plugin by [@4DMedia](https://twitter.com/4dMedia): [https://wordpress.org/plugins/countup-js/](https://wordpress.org/plugins/countup-js/)
Expand All @@ -26,7 +26,7 @@ Params:
- `endVal` = the value you want to arrive at
- `decimals` = (optional) number of decimal places in number, default 0
- `duration` = (optional) duration in seconds, default 2
- `options` = (see demo, optional) formatting/easing options object
- `options` = (optional, see demo) formatting/easing options object

Decimals, duration, and options can be left out to use the default values.

Expand All @@ -46,10 +46,10 @@ numAnim.start(function() {
})
```
#### Animating to large numbers
For large numbers, since the CountUp class has a long way to go in just a few seconds, the animation seems to abruptly stop. The solution is to subtract 100 from your endVal, then use the callback to invoke the `update` method which completes the animation with the same duration with a difference of only 100 to animate:
For large numbers, since CountUp has a long way to go in just a few seconds, the animation seems to abruptly stop. The solution is to subtract 100 from your `endVal`, then use the callback to invoke the `update` method which completes the animation with the same duration with a difference of only 100 to animate:
```js
var endVal = 9645.72;
var numAnim = new CountUp("targetElem", 0, endVal - 100);
var numAnim = new CountUp("targetElem", 0, endVal - 100, duration/2);
numAnim.start(function() {
numAnim.update(endVal);
});
Expand All @@ -64,7 +64,7 @@ Width angular-scroll-spy:
<h2 count-up id="numberAnimation" end-val="873.4" scroll-spy-event="elementFirstScrolledIntoView" scroll-spy></h2>
```

## Other methods:
#### Other methods:
Toggle pause/resume:

```js
Expand All @@ -84,24 +84,20 @@ var someValue = 1337;
numAnim.update(someValue);
```

## Custom easing:
#### Custom easing:

You can apply your custom easing function, which will receive standard 4 parameters necessary
to calculate Bezier curve:
You can optionally apply your custom easing function, which will receive 4 parameters necessary to calculate a Bezier curve:

- `t` (the current time);
- `b` (the beginning value);
- `c` (the change between the beginning and destination value);
- `c` (the difference between the beginning and destination value);
- `d` (the total time of the tween).

So, for instance, you could use bodies of easing functions from jQuery's [easing plugin](http://gsgd.co.uk/sandbox/jquery/easing/).
Pass in the body of the function and you're good to go :)
You could use any of Robert Penner's [easing functions](https://github.com/danro/jquery-easing/blob/master/jquery.easing.js). Just avoid using "bouncy" functions, because they cause counting in both directions

Just don't use any "bouncy" functions, as they tend to cause issues and are controlled not to exceed max value by the library.
If you don't specify a custom easing function, CountUp uses the default `easeOutExpo`.

If you don't specify a custom easing closure function, the plugin will fall back to the default `easeOutExpo`.

#### Example:
Example:

```js
var easeOutCubic = function(t, b, c, d) {
Expand All @@ -116,5 +112,11 @@ var demo = new CountUp("myTargetElement", 24.02, 94.62, 2, 2.5, options);
demo.start();
```

## Contributing <a name="contributing"></a>

Before you make a pull request, please be sure to follow these super simple instructions:

## MIT License
1. Do your work on the `countUp.js` and/or `angular-countUp.js` files in the root directory.
2. In Terminal, `cd` to the `countUp.js` directory.
3. Run `npm install`, which installs gulp and its dependencies.
4. Run `gulp`, which copies and minifies the .js files to the `dist` folder.
48 changes: 22 additions & 26 deletions angular-countUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* count-up attribute directive
*
* @param {number} startVal - (optional) The value you want to begin at, default 0
* @param {number} endVal - The value you want to arrive at
* @param {number} countUp - The value you want to arrive at
* @param {number} duration - (optional) Duration in seconds, default 2.
* @param {number} decimals - (optional) Number of decimal places in number, default 0
* @param {boolean} reanimateOnClick - (optional) Config if reanimate on click event, default true.
Expand All @@ -32,27 +32,24 @@
return {
restrict: 'A',
scope: {
startVal: "=?",
endVal: "=",
duration: "=?",
decimals: "=?",
reanimateOnClick: "=?",
startVal: '=?',
endVal: '=?',
duration: '=?',
decimals: '=?',
reanimateOnClick: '=?',
filter: '@',
options: "=?"
options: '=?'
},
link: function ($scope, $el, $attrs) {

var filterFunction = null;
var options = {};

if($scope.filter != null) {
filterFunction = createFilterFunction();
if ($scope.filter) {
var filterFunction = createFilterFunction();
options.formattingFn = filterFunction;
}

var options = {
postFormatter: filterFunction
};

if ($scope.options != null) {
if ($scope.options) {
angular.extend(options, $scope.options);
}

Expand All @@ -65,17 +62,17 @@
return function(value) {
var filterCallParams = [value];
Array.prototype.push.apply(filterCallParams, filterParams);
var value = $filter(filterName).apply(null, filterCallParams)
value = $filter(filterName).apply(null, filterCallParams);
return value;
}
};
}

function createCountUp(sta, end, dec, dur) {
sta = sta || 0;
if (isNaN(sta)) sta = Number(sta.match(/[\d\-\.]+/g).join('')); // strip non-numerical characters
end = end || 0;
if (isNaN(end)) end = Number(end.match(/[\d\-\.]+/g).join('')); // strip non-numerical characters
dur = Number(dur) || 2,
dur = Number(dur) || 2;
dec = Number(dec) || 0;

// construct countUp
Expand Down Expand Up @@ -122,19 +119,18 @@
}

$scope.$watch('endVal', function (newValue, oldValue) {
if (newValue == null || newValue === oldValue) {
if (newValue === null || newValue === oldValue) {
return;
}

if(countUp != null) {
countUp.update($scope.endVal);
if (countUp !== null) {
countUp.update($scope.endVal);
} else {
countUp = createCountUp($scope.startVal, $scope.endVal, $scope.decimals, $scope.duration);

animate();
countUp = createCountUp($scope.startVal, $scope.endVal, $scope.decimals, $scope.duration);
animate();
}
})
});
}
}
};
}]);
})(angular);
26 changes: 0 additions & 26 deletions contributing.md

This file was deleted.

Loading

0 comments on commit 0353d8f

Please sign in to comment.