Skip to content
This repository has been archived by the owner on Apr 8, 2021. It is now read-only.

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nof1000 committed Mar 27, 2016
0 parents commit fd274e7
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
lib-cov
*.html
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.html

node_modules
dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2016 Denis Maslennikov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Tactween
A simple core-based tweening to create animations

## Install
CDN:
**coming soon**

Download:
- releases: [HERE](https://github.com/nof1000/tactween/releases/ "Releases")
- bower: `bower install tactween`
- npm: `npm install tactween`

## Usage

Include `tactween.js` into your page:
```html
<script src="./tactween.js"></script>
```

And let's animate anything!
```javascript
// from 0 to 100
tactween(100, {
duration: 1500,
change: function(v) {
progress.style.width = v.value + '%';
}
})
```

## Reference
### tactween(props, options);
- `props` - is an (number, array, object) the value(s) you want to animate.
- `options` - is an object to setup the animation.
- `duration` - is in milliseconds (default: `1000`)
- `timing` - is an function to describe how to intermediate values calculated (default: `(fraction) => { return fraction }`}
- `before` - (**optional**) is called before animation start
- `change` - (**optional**) is called at every change
- `complete` - (**optional**) is the completion

## Examples
### Properties
```javascript
// Number
tactween(1000, {
change: function(v) { milliseconds = v.value; }
});

// Array may comprise only two elements
tactween([-100, 100], {
change: function(v) { milliseconds = v.value; }
});

// Object
tactween({
r: [0, 255],
g: [0, 255],
b: [0, 255]
}, {
change: function(v) {
document.body.style.background = (
'rgb(' + (v.r ^ 0) + ',' + (v.g ^ 0) + ',' + (v.b ^ 0) + ')';
);
}
});
```

### Timing
```javascript

// Linear
tactween(1000, {
timing: function(fraction) { return fraction; },
change: function(v) { anything = v.value; }
});

// Quad
tactween(1000, {
timing: function(fraction) { return Math.pow(fraction, 2); },
change: function(v) { anything = v.value; }
});

// Circ
tactween(1000, {
timing: function(fraction) { return 1 - Min.sin(Math.acos(fraction)); },
change: function(v) { anything = v.value; }
});

// etc...
```

## LICENSE
[MIT](./LICENSE "The MIT License")
32 changes: 32 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "tactween",
"description": "A simple core-based tweening to create animations",
"main": "./dist/tactween.min.js",
"homepage": "https://github.com/nof1000/tactween",
"license": "MIT",
"authors": [
"Denis Maslennikov <[email protected]> (http://nofach.com)"
],
"keywords": [
"requestAnimationFrame",
"fundamental",
"animation",
"tweening",
"tween",
"time",
"core"
],
"moduleType": [
"amd",
"globals",
"node"
],
"ignore": [
"**/.*",
"*.html",
"node_modules",
"bower_components",
"gulpfile.js",
"package.json"
]
}
76 changes: 76 additions & 0 deletions dist/tactween.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['tactween'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
} else {
root.tn = root.tactween = factory(root);
}
}(this, function (window) {
'use strict';

var isArray = function(arr) {
return ('isArray' in Array) ?
Array.isArray(arr)
:
(Object.prototype.toString.call(arr) == '[object Array]');
};

var now = (function(){
var offset = Date.now();
return ('performance' in window) ?
(function() { return performance.now(); })
:
(function() { return Date.now() - offset; });
})();

// For cross-runtime (browser, nodejs, etc...);
var setFrame = window.requestAnimationFrame || function(callback) {
setTimeout(function() {
callback(now());
}, 20);
};

var Tactween = function(props, options) {
props = props || {};
options = options || {};

var duration = options.duration || 1000;
var timing = options.timing || (function(fraction) { return fraction; });
var frameUID = 0;
var values = {};
var value = 0;
var start = now();

if (typeof props == 'number') {
props = { value: [0, props] };
} else if (isArray(props)) {
if (!(props.length == 2)) throw 'Array may comprise only two elements.';
props = { value: props };
} else if (typeof props != 'object') {
throw 'Props may comprise only Number, Array or Object.';
}

if (options.before) options.before();

frameUID = setFrame(function frame(time){
var fraction = (time - start) / duration;
if (fraction > 1) fraction = 1;

value = timing(fraction);
for (var prop in props) {
values[prop] = props[prop][0] + (
value * (props[prop][1] - props[prop][0])
);
}

if (options.change) options.change(values);
if ((fraction == 1) && options.complete) options.complete(values);
if (fraction < 1) frameUID = setFrame(frame);
});

return { tactween: Tactween, more: Tactween, uid: frameUID };
};

return Tactween;
}));
1 change: 1 addition & 0 deletions dist/tactween.min.js

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

15 changes: 15 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');

gulp.task('default', function() {
return gulp.src('./src/tactween.js')
.pipe(gulp.dest('./dist'))
.pipe(rename('./tactween.min.js'))
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});

gulp.watch('./src/*.js', function(event) {
gulp.run('default');
});
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/tactween.js');
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "tactween",
"title": "Tactween",
"description": "A simple core-based tweening to create animations",
"version": "0.1.0",
"main": "index.js",
"author": "Denis Maslennikov <[email protected]> (http://nofach.com)",
"license": "MIT",
"keywords": [
"requestAnimationFrame",
"fundamental",
"animation",
"tweening",
"tween",
"time",
"core"
],
"repository": {
"type": "git",
"url": "https://github.com/nof1000/tactween"
},
"bugs": {
"url": "https://github.com/nof1000/tactween/issues"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.5.3"
}
}
76 changes: 76 additions & 0 deletions src/tactween.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['tactween'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
} else {
root.tn = root.tactween = factory(root);
}
}(this, function (window) {
'use strict';

var isArray = function(arr) {
return ('isArray' in Array) ?
Array.isArray(arr)
:
(Object.prototype.toString.call(arr) == '[object Array]');
};

var now = (function(){
var offset = Date.now();
return ('performance' in window) ?
(function() { return performance.now(); })
:
(function() { return Date.now() - offset; });
})();

// For cross-runtime (browser, nodejs, etc...);
var setFrame = window.requestAnimationFrame || function(callback) {
setTimeout(function() {
callback(now());
}, 20);
};

var Tactween = function(props, options) {
props = props || {};
options = options || {};

var duration = options.duration || 1000;
var timing = options.timing || (function(fraction) { return fraction; });
var frameUID = 0;
var values = {};
var value = 0;
var start = now();

if (typeof props == 'number') {
props = { value: [0, props] };
} else if (isArray(props)) {
if (!(props.length == 2)) throw 'Array may comprise only two elements.';
props = { value: props };
} else if (typeof props != 'object') {
throw 'Props may comprise only Number, Array or Object.';
}

if (options.before) options.before();

frameUID = setFrame(function frame(time){
var fraction = (time - start) / duration;
if (fraction > 1) fraction = 1;

value = timing(fraction);
for (var prop in props) {
values[prop] = props[prop][0] + (
value * (props[prop][1] - props[prop][0])
);
}

if (options.change) options.change(values);
if ((fraction == 1) && options.complete) options.complete(values);
if (fraction < 1) frameUID = setFrame(frame);
});

return { tactween: Tactween, more: Tactween, uid: frameUID };
};

return Tactween;
}));

0 comments on commit fd274e7

Please sign in to comment.