Skip to content

Commit

Permalink
Improved build; resolved #26
Browse files Browse the repository at this point in the history
  • Loading branch information
broadsw0rd committed Sep 4, 2017
1 parent b40ebc9 commit 83d0465
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 150 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Dynamica is a low-level animation engine library, which provides smooth and exac

## Features

- Designed with performance in mind
- Simple - [191 LOC](https://github.com/broadsw0rd/dynamica/blob/master/dist/dynamica.js#L191)
- Lightweight - [2.5 KB](https://github.com/broadsw0rd/dynamica/blob/master/dist/dynamica.min.js)
- Well tested - [100% code coverage](https://coveralls.io/github/broadsw0rd/dynamica?branch=master)
- Built for any environment - HTML, CSS, Canvas, React, etc...
- Designed with performance in mind and tested with [IRHydra](http://mrale.ph/irhydra/2/)

## Install

Expand All @@ -49,7 +49,7 @@ npm install --save dynamica

or

Download [dev](https://rawgit.com/broadsw0rd/dynamica/1.1.0/dist/dynamica.js) or [prod](https://rawgit.com/broadsw0rd/dynamica/1.1.0/dist/dynamica.min.js) version and put it in your html
Download [dev](https://rawgit.com/broadsw0rd/dynamica/master/dist/dynamica.umd.js) or [prod](https://rawgit.com/broadsw0rd/dynamica/master/dist/dynamica.min.js) version and put it in your html

```html
<script src="vendor/dynamica.min.js"></script>
Expand Down
226 changes: 104 additions & 122 deletions dist/dynamica.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Animation = factory());
}(this, (function () { 'use strict';

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};


Expand All @@ -14,22 +8,22 @@ function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}

var performanceNow = createCommonjsModule(function (module) {
var performanceNow$1 = createCommonjsModule(function (module) {
// Generated by CoffeeScript 1.12.2
/* istanbul ignore next */
(function () {
(function() {
var getNanoSeconds, hrtime, loadTime, moduleLoadTime, nodeLoadTime, upTime;

if (typeof performance !== "undefined" && performance !== null && performance.now) {
module.exports = function () {
if ((typeof performance !== "undefined" && performance !== null) && performance.now) {
module.exports = function() {
return performance.now();
};
} else if (typeof process !== "undefined" && process !== null && process.hrtime) {
module.exports = function () {
} else if ((typeof process !== "undefined" && process !== null) && process.hrtime) {
module.exports = function() {
return (getNanoSeconds() - nodeLoadTime) / 1e6;
};
hrtime = process.hrtime;
getNanoSeconds = function () {
getNanoSeconds = function() {
var hr;
hr = hrtime();
return hr[0] * 1e9 + hr[1];
Expand All @@ -38,156 +32,144 @@ var performanceNow = createCommonjsModule(function (module) {
upTime = process.uptime() * 1e9;
nodeLoadTime = moduleLoadTime - upTime;
} else if (Date.now) {
module.exports = function () {
module.exports = function() {
return Date.now() - loadTime;
};
loadTime = Date.now();
} else {
module.exports = function () {
module.exports = function() {
return new Date().getTime() - loadTime;
};
loadTime = new Date().getTime();
}

}).call(commonjsGlobal);


});

var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};

function noop() {}
function noop () {}

function id(value) {
return value;
function id (value) {
return value
}

function indexOf(array, item) {
function indexOf (array, item) {
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
return i;
return i
}
}
return -1;
return -1
}

var Animation = function () {
Animation.add = function add(animation) {
Animation.instances.push(animation);
};

Animation.remove = function remove(animation) {
var idx = indexOf(Animation.instances, animation);
if (idx !== -1) {
Animation.instances.splice(idx, 1);
}
};

Animation.animate = function animate(time) {
var animations = Animation.instances.concat();
for (var i = 0, animation; i < animations.length; i++) {
animation = animations[i];
animation.animate(time);
}
};
var Animation = function Animation (options) {
var ref = options || {};
var duration = ref.duration;
var handler = ref.handler;
var ease = ref.ease;
var onstart = ref.onstart;
var oncancel = ref.oncancel;
var oncomplete = ref.oncomplete;

if (isNaN(duration)) {
throw Error('`duration` should be defined, check https://github.com/broadsw0rd/dynamica#api')
}

function Animation(options) {
classCallCheck(this, Animation);
this.startTime = 0;

var _ref = options || {},
duration = _ref.duration,
handler = _ref.handler,
ease = _ref.ease,
onstart = _ref.onstart,
oncancel = _ref.oncancel,
oncomplete = _ref.oncomplete;
this.duration = Number(duration);
this.handler = handler || noop;
this.ease = ease || id;

if (isNaN(duration)) {
throw Error('`duration` should be defined, check https://github.com/broadsw0rd/dynamica#api');
}
this.onstart = onstart || noop;
this.oncancel = oncancel || noop;
this.oncomplete = oncomplete || noop;

this.startTime = 0;
this.next = [];
this._started = false;
};

this.duration = Number(duration);
this.handler = handler || noop;
this.ease = ease || id;
Animation.add = function add (animation) {
Animation.instances.push(animation);
};

this.onstart = onstart || noop;
this.oncancel = oncancel || noop;
this.oncomplete = oncomplete || noop;
Animation.remove = function remove (animation) {
var idx = indexOf(Animation.instances, animation);
if (idx !== -1) {
Animation.instances.splice(idx, 1);
}
};

this.next = [];
this._started = false;
Animation.animate = function animate (time) {
var animations = Animation.instances.concat();
for (var i = 0, animation; i < animations.length; i++) {
animation = animations[i];
animation.animate(time);
}
};

Animation.prototype.start = function start() {
var startTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : performanceNow();

Animation.add(this);
this._started = true;
this.startTime = startTime;
this.onstart && this.onstart();
};

Animation.prototype.animate = function animate(time) {
var t = (time - this.startTime) / this.duration;
if (t < 1) {
this.handler(this.ease(t));
} else {
this.complete(time);
}
};
Animation.prototype.start = function start (startTime) {
if ( startTime === void 0 ) startTime = performanceNow$1();

Animation.prototype.complete = function complete() {
var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : performanceNow();
Animation.add(this);
this._started = true;
this.startTime = startTime;
this.onstart && this.onstart();
};

Animation.prototype.animate = function animate (time) {
var t = (time - this.startTime) / this.duration;
if (t < 1) {
this.handler(this.ease(t));
} else {
this.complete(time);
}
};

if (!this.started()) return;
Animation.prototype.complete = function complete (time) {
var this$1 = this;
if ( time === void 0 ) time = performanceNow$1();

this.remove();
this.handler(1);
this.oncomplete && this.oncomplete();
for (var i = 0, next; i < this.next.length; i++) {
next = this.next[i];
next.start(Math.min(this.startTime + this.duration, time));
next.animate(time);
}
};
if (!this.started()) { return }

Animation.prototype.remove = function remove() {
Animation.remove(this);
this._started = false;
};
this.remove();
this.handler(1);
this.oncomplete && this.oncomplete();
for (var i = 0, next; i < this.next.length; i++) {
next = this$1.next[i];
next.start(Math.min(this$1.startTime + this$1.duration, time));
next.animate(time);
}
};

Animation.prototype.cancel = function cancel() {
if (!this.started()) return;
Animation.prototype.remove = function remove () {
Animation.remove(this);
this._started = false;
};

this.remove();
this.oncancel && this.oncancel();
};
Animation.prototype.cancel = function cancel () {
if (!this.started()) { return }

Animation.prototype.queue = function queue(animation) {
this.next.push(animation);
};
this.remove();
this.oncancel && this.oncancel();
};

Animation.prototype.dequeue = function dequeue(animation) {
var idx = indexOf(this.next, animation);
if (idx !== -1) {
this.next.splice(idx, 1);
}
};
Animation.prototype.queue = function queue (animation) {
this.next.push(animation);
};

Animation.prototype.started = function started() {
return this._started;
};
Animation.prototype.dequeue = function dequeue (animation) {
var idx = indexOf(this.next, animation);
if (idx !== -1) {
this.next.splice(idx, 1);
}
};

return Animation;
}();
Animation.prototype.started = function started () {
return this._started
};

Animation.instances = [];

return Animation;

})));
export default Animation;
2 changes: 1 addition & 1 deletion dist/dynamica.min.js

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

Loading

0 comments on commit 83d0465

Please sign in to comment.