├── .gitignore ├── History.md ├── Makefile ├── Readme.md ├── component.json ├── examples ├── array.html ├── ball.html ├── circle.html ├── dom.html ├── interactive.html └── rect.html ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | components 2 | build 3 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.2.0 / 2015-03-16 3 | ================== 4 | 5 | * update "emitter" to v1.2.0 6 | * use "component-clone" and "component-type" for browsers support (#5, @cristiandouce) 7 | * update component.json (#4, @pgherveou) 8 | * package: add browserify mappings (#7, @dfcreative) 9 | * package: add "repository" field (#3) 10 | 11 | 1.1.0 / 2013-05-29 12 | ================== 13 | 14 | * add Tween#stop(). Closes #2 15 | * fix final update to complete the tween. Closes #1 16 | 17 | 1.0.0 / 2013-05-27 18 | ================== 19 | 20 | * pin deps 21 | * add package.json 22 | * add interactive example 23 | 24 | 0.1.0 / 2012-11-10 25 | ================== 26 | 27 | * add array tweening support 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | build: components index.js 3 | @component build --dev 4 | 5 | components: component.json 6 | @component install --dev 7 | 8 | clean: 9 | rm -fr build components template.js 10 | 11 | tween.js: 12 | @component build --standalone tween \ 13 | && mv build/build.js $@ \ 14 | && rm -fr build 15 | 16 | .PHONY: clean tween.js 17 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # tween 3 | 4 | Motion tween component using [ease](https://github.com/component/ease). 5 | 6 | ## Installation 7 | 8 | $ component install component/tween 9 | 10 | ## Example 11 | 12 | ```js 13 | var Tween = require('tween'); 14 | var raf = require('raf'); 15 | var button = document.querySelector('button'); 16 | 17 | var tween = Tween({ rotate: 0, opacity: 0 }) 18 | .ease('out-bounce') 19 | .to({ rotate: 360, opacity: 1 }) 20 | .duration(800); 21 | 22 | tween.update(function(o){ 23 | button.style.opacity = o.opacity; 24 | button.style.webkitTransform = 'rotate(' + (o.rotate | 0) + 'deg)'; 25 | }); 26 | 27 | tween.on('end', function(){ 28 | animate = function(){}; 29 | }); 30 | 31 | function animate() { 32 | raf(animate); 33 | tween.update(); 34 | } 35 | 36 | animate(); 37 | ``` 38 | 39 | ## API 40 | 41 | ### Tween(obj:Object|Array) 42 | 43 | Initialize a new `Tween` with `obj`. 44 | 45 | ### Tween#reset() 46 | 47 | Reset the tween. 48 | 49 | ### Tween#to(obj:Object|Array) 50 | 51 | Tween to `obj` and reset internal state. 52 | 53 | tween.to({ x: 50, y: 100 }) 54 | 55 | ### Tween#duration(ms:Number) 56 | 57 | Set duration to `ms` [500]. 58 | 59 | ### Tween#ease(fn:String|Function) 60 | 61 | Set easing function to `fn`. 62 | 63 | tween.ease('in-out-sine') 64 | 65 | ### Tween#update(fn:Function) 66 | 67 | Set update function to `fn` or 68 | when no argument is given this performs 69 | a "step". 70 | 71 | ### Tween#stop() 72 | 73 | Immediately stop the tween and emit "stop" and end" events. `tween.stopped` 74 | is then marked as `true`. 75 | 76 | ## License 77 | 78 | MIT 79 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tween", 3 | "repo": "component/tween", 4 | "description": "Motion tween engine using 'ease'", 5 | "version": "1.2.0", 6 | "keywords": [ 7 | "tween", 8 | "tweening", 9 | "animation" 10 | ], 11 | "dependencies": { 12 | "component/clone": "0.2.2", 13 | "component/ease": "1.0.0", 14 | "component/emitter": "1.2.0", 15 | "component/type": "1.1.0" 16 | }, 17 | "development": { 18 | "component/raf": "*", 19 | "component/autoscale-canvas": "*" 20 | }, 21 | "license": "MIT", 22 | "scripts": [ 23 | "index.js" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /examples/array.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/ball.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 65 | -------------------------------------------------------------------------------- /examples/circle.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 46 | -------------------------------------------------------------------------------- /examples/dom.html: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 14 | 15 | 16 | 42 | -------------------------------------------------------------------------------- /examples/interactive.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/rect.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var Emitter = require('emitter'); 7 | var clone = require('clone'); 8 | var type = require('type'); 9 | var ease = require('ease'); 10 | 11 | /** 12 | * Expose `Tween`. 13 | */ 14 | 15 | module.exports = Tween; 16 | 17 | /** 18 | * Initialize a new `Tween` with `obj`. 19 | * 20 | * @param {Object|Array} obj 21 | * @api public 22 | */ 23 | 24 | function Tween(obj) { 25 | if (!(this instanceof Tween)) return new Tween(obj); 26 | this._from = obj; 27 | this.ease('linear'); 28 | this.duration(500); 29 | } 30 | 31 | /** 32 | * Mixin emitter. 33 | */ 34 | 35 | Emitter(Tween.prototype); 36 | 37 | /** 38 | * Reset the tween. 39 | * 40 | * @api public 41 | */ 42 | 43 | Tween.prototype.reset = function(){ 44 | this.isArray = 'array' === type(this._from); 45 | this._curr = clone(this._from); 46 | this._done = false; 47 | this._start = Date.now(); 48 | return this; 49 | }; 50 | 51 | /** 52 | * Tween to `obj` and reset internal state. 53 | * 54 | * tween.to({ x: 50, y: 100 }) 55 | * 56 | * @param {Object|Array} obj 57 | * @return {Tween} self 58 | * @api public 59 | */ 60 | 61 | Tween.prototype.to = function(obj){ 62 | this.reset(); 63 | this._to = obj; 64 | return this; 65 | }; 66 | 67 | /** 68 | * Set duration to `ms` [500]. 69 | * 70 | * @param {Number} ms 71 | * @return {Tween} self 72 | * @api public 73 | */ 74 | 75 | Tween.prototype.duration = function(ms){ 76 | this._duration = ms; 77 | return this; 78 | }; 79 | 80 | /** 81 | * Set easing function to `fn`. 82 | * 83 | * tween.ease('in-out-sine') 84 | * 85 | * @param {String|Function} fn 86 | * @return {Tween} 87 | * @api public 88 | */ 89 | 90 | Tween.prototype.ease = function(fn){ 91 | fn = 'function' == typeof fn ? fn : ease[fn]; 92 | if (!fn) throw new TypeError('invalid easing function'); 93 | this._ease = fn; 94 | return this; 95 | }; 96 | 97 | /** 98 | * Stop the tween and immediately emit "stop" and "end". 99 | * 100 | * @return {Tween} 101 | * @api public 102 | */ 103 | 104 | Tween.prototype.stop = function(){ 105 | this.stopped = true; 106 | this._done = true; 107 | this.emit('stop'); 108 | this.emit('end'); 109 | return this; 110 | }; 111 | 112 | /** 113 | * Perform a step. 114 | * 115 | * @return {Tween} self 116 | * @api private 117 | */ 118 | 119 | Tween.prototype.step = function(){ 120 | if (this._done) return; 121 | 122 | // duration 123 | var duration = this._duration; 124 | var now = Date.now(); 125 | var delta = now - this._start; 126 | var done = delta >= duration; 127 | 128 | // complete 129 | if (done) { 130 | this._from = this._to; 131 | this._update(this._to); 132 | this._done = true; 133 | this.emit('end'); 134 | return this; 135 | } 136 | 137 | // tween 138 | var from = this._from; 139 | var to = this._to; 140 | var curr = this._curr; 141 | var fn = this._ease; 142 | var p = (now - this._start) / duration; 143 | var n = fn(p); 144 | 145 | // array 146 | if (this.isArray) { 147 | for (var i = 0; i < from.length; ++i) { 148 | curr[i] = from[i] + (to[i] - from[i]) * n; 149 | } 150 | 151 | this._update(curr); 152 | return this; 153 | } 154 | 155 | // objech 156 | for (var k in from) { 157 | curr[k] = from[k] + (to[k] - from[k]) * n; 158 | } 159 | 160 | this._update(curr); 161 | return this; 162 | }; 163 | 164 | /** 165 | * Set update function to `fn` or 166 | * when no argument is given this performs 167 | * a "step". 168 | * 169 | * @param {Function} fn 170 | * @return {Tween} self 171 | * @api public 172 | */ 173 | 174 | Tween.prototype.update = function(fn){ 175 | if (0 == arguments.length) return this.step(); 176 | this._update = fn; 177 | return this; 178 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "component-tween", 3 | "description": "Motion tween engine using 'ease'", 4 | "version": "1.2.0", 5 | "keywords": [ 6 | "tween", 7 | "tweening", 8 | "animation" 9 | ], 10 | "dependencies": { 11 | "component-clone": "0.2.2", 12 | "component-emitter": "1.2.0", 13 | "component-type": "1.1.0", 14 | "ease-component": "1.0.0" 15 | }, 16 | "browser": { 17 | "clone": "component-clone", 18 | "emitter": "component-emitter", 19 | "type": "component-type", 20 | "ease": "ease-component" 21 | }, 22 | "license": "MIT", 23 | "component": { 24 | "scripts": { 25 | "tween/index.js": "index.js" 26 | } 27 | }, 28 | "repository": { 29 | "type": "git", 30 | "url": "https://github.com/component/tween.git" 31 | } 32 | } 33 | --------------------------------------------------------------------------------