Permalink
Browse files

- add unit test runner

- author tests
- fix copyright
- remake
  • Loading branch information...
1 parent 84e4bda commit a057abe8d108d86ff830fe4e03870f7143c9eeda @ded committed May 20, 2011
Showing with 270 additions and 104 deletions.
  1. +3 −0 .gitmodules
  2. +85 −60 morpheus.js
  3. +3 −4 morpheus.min.js
  4. +2 −3 src/copyright.js
  5. +39 −37 src/morpheus.js
  6. +1 −0 support/sink
  7. +137 −0 tests/tests.html
View
@@ -0,0 +1,3 @@
+[submodule "support/sink"]
+ path = support/sink
+ url = git://github.com/ded/sink-test.git
View
@@ -1,14 +1,12 @@
/*!
- * Morpheus - An animation framework
- * copyright Dustin Diaz 2011
- * https://github.com/ded/morpheus
+ * Morpheus - A Brilliant Animator
+ * https://github.com/ded/morpheus - (c) Dustin Diaz 2011
* License MIT
*/
!function (context, doc) {
var ie = /msie/i.test(navigator.userAgent),
hex = "0123456789abcdef",
- digit = /^-?[\d\.]+$/,
px = 'px',
unitless = { lineHeight: 1, zoom: 1, zIndex: 1, opacity: 1 },
getStyle = doc.defaultView && doc.defaultView.getComputedStyle ?
@@ -39,91 +37,118 @@
function (el, property) {
return el.style[camelize(property)];
+ },
+ RGBtoHex = function () {
+ function hx(n) {
+ n = parseInt(n, 10);
+ if (n === 0 || isNaN(n)) {
+ return '00';
+ }
+ n = Math.max(0, n);
+ n = Math.min(n, 255);
+ n = Math.round(n);
+ return hex.charAt((n - n % 16) / 16) + hex.charAt(n % 16);
+ }
+ return function (r, g, b) {
+ return hx(r) + hx(g) + hx(b);
};
+ }(),
+ toHex = function (c) {
+ var m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(c);
+ return (m ? '#' + RGBtoHex(m[1], m[2], m[3]) : c)
+ .replace(/#(\w)(\w)(\w)$/, '#$1$1$2$2$3$3'); // short to long
+ },
+ camelize = function (s) {
+ return s.replace(/-(.)/g, function (m, m1) {
+ return m1.toUpperCase();
+ });
+ };
- function toHex(c) {
- var m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(c);
- return (m ? '#' + (m[1] << 16 | m[2] << 8 | m[3]).toString(16) : c)
- .replace(/#(\w)(\w)(\w)$/, '#$1$1$2$2$3$3'); // from short to long
- }
-
- function camelize(s) {
- return s.replace(/-(.)/g, function (m, m1) {
- return m1.toUpperCase();
- });
+ function tween(duration, fn, done, ease, from, to) {
+ ease = ease || function (t) {
+ return Math.sin(t * Math.PI / 2)
+ };
+ var self = this,
+ time = duration || 1000,
+ diff = to - from,
+ start = new Date(),
+ timer = setTimeout(run, 5);
+
+ function run() {
+ var delta = new Date() - start;
+ if (delta > time) {
+ fn(to || 1);
+ done && done();
+ timer = null;
+ return;
+ }
+ to ?
+ fn((diff * ease(delta / time)) + from) :
+ fn(ease(delta / time));
+ setTimeout(run, 5);
+ }
}
+ function nextColor(pos, start, finish) {
+ var r = [], i;
+ for (i = 0; i < 6; i++) {
+ from = hex.indexOf(start[i]);
+ to = hex.indexOf(finish[i]);
+ r[i] = hex[Math.floor((to - from) * pos + from)];
+ }
+ return '#' + r.join('');
+ }
- function tween(duration, fn, done, ease, from, to) {
- ease = ease || function (t) {
- return Math.sin(t * Math.PI / 2)
- };
- var self = this,
- time = duration || 1000,
- diff = to - from,
- start = new Date(),
- timer = setTimeout(run, 5);
-
- function run() {
- var delta = new Date() - start;
- if (delta > time) {
- fn(to || 1);
- done && done();
- timer = null;
- return;
- }
- to ?
- fn((diff * ease(delta / time)) + from) :
- fn(ease(delta / time));
- setTimeout(run, 5);
- }
- }
+ function getVal(pos, options, begin, end, k, i, v) {
+ if (typeof begin[i][k] == 'string') {
+ return nextColor(pos, begin[i][k], end[i][k]);
+ } else {
+ v = (end[i][k] - begin[i][k]) * pos + begin[i][k];
+ !(k in unitless) && (v += px);
+ return v;
+ }
+ }
- tween.color = function (duration, from, to, fn, done, ease) {
- var start = toHex(from).slice(1),
- finish = toHex(to).slice(1);
- tween(duration, function (pos) {
- var r = [], i;
- for (i = 0; i < 6; i++) {
- from = hex.indexOf(start[i]);
- to = hex.indexOf(finish[i]);
- r[i] = hex[Math.floor((to - from) * pos + from)];
- }
- fn('#' + r.join(''));
- }, done, ease);
- }
+ function by(val, start, m, r, i) {
+ return (m = /^([+\-])=([\d\.]+)/.exec(val)) ?
+ (i = parseInt(m[2], 10)) && (r = (start + i)) && m[1] == '+' ?
+ r : start - i :
+ parseInt(val, 10);
+ }
function morpheus(elements, options) {
var els = elements ? (els = isFinite(elements.length) ? elements : [elements]) : [], i,
complete = options.complete,
duration = options.duration,
ease = options.easing,
- begin = [];
+ begin = [],
+ end = [];
delete options.complete;
delete options.duration;
delete options.easing;
// record beginning "from" state
for (i = els.length; i--;) {
begin[i] = {};
+ end[i] = {};
for (var k in options) {
- var v = options[k];
- digit.test(v) && !(k in unitless) && (v += px);
- begin[i][k] = parseInt(getStyle(els[i], k), 10);
+ var v = getStyle(els[i], k);
+ begin[i][k] = typeof options[k] == 'string' && options[k][0] == '#' ? toHex(v).slice(1) : parseFloat(v, 10);
+ end[i][k] = typeof options[k] == 'string' && options[k][0] == '#' ? toHex(options[k]).slice(1) : by(options[k], parseFloat(v, 10));
}
}
- // (p = camelize(k)) && digit.test(v) && !(p in unitless) && (v += px);
// one tween to rule them all
- tween(duration, function (pos) {
+ tween(duration, function (pos, v) {
for (i = els.length; i--;) {
for (var k in options) {
- var val = (options[k] - begin[i][k]) * pos + begin[i][k];
- els[i].style[k] = val + 'px';
+ v = getVal(pos, options, begin, end, k, i);
+ ie && k == 'opacity' ?
+ (els[i].filter = 'alpha(opacity=' + (v * 100) + ')') :
+ (els[i].style[camelize(k)] = v);
}
}
}, complete, ease);
-
}
typeof module !== 'undefined' && module.exports &&
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,6 +1,5 @@
/*!
- * Morpheus - An animation framework
- * copyright Dustin Diaz 2011
- * https://github.com/ded/morpheus
+ * Morpheus - A Brilliant Animator
+ * https://github.com/ded/morpheus - (c) Dustin Diaz 2011
* License MIT
*/
View
@@ -35,8 +35,10 @@
},
RGBtoHex = function () {
function hx(n) {
- n = parseInt(n);
- if (n == 0 || isNaN(n)) return '00';
+ n = parseInt(n, 10);
+ if (n === 0 || isNaN(n)) {
+ return '00';
+ }
n = Math.max(0, n);
n = Math.min(n, 255);
n = Math.round(n);
@@ -47,49 +49,49 @@
};
}(),
toHex = function (c) {
- var m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(c);
- return (m ? '#' + RGBtoHex(m[1], m[2], m[3]) : c)
- .replace(/#(\w)(\w)(\w)$/, '#$1$1$2$2$3$3'); // short to long
- },
+ var m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(c);
+ return (m ? '#' + RGBtoHex(m[1], m[2], m[3]) : c)
+ .replace(/#(\w)(\w)(\w)$/, '#$1$1$2$2$3$3'); // short to long
+ },
camelize = function (s) {
return s.replace(/-(.)/g, function (m, m1) {
return m1.toUpperCase();
});
};
function tween(duration, fn, done, ease, from, to) {
- ease = ease || function (t) {
- return Math.sin(t * Math.PI / 2)
- };
- var self = this,
- time = duration || 1000,
- diff = to - from,
- start = new Date(),
- timer = setTimeout(run, 5);
+ ease = ease || function (t) {
+ return Math.sin(t * Math.PI / 2)
+ };
+ var self = this,
+ time = duration || 1000,
+ diff = to - from,
+ start = new Date(),
+ timer = setTimeout(run, 5);
- function run() {
- var delta = new Date() - start;
- if (delta > time) {
- fn(to || 1);
- done && done();
- timer = null;
- return;
- }
- to ?
- fn((diff * ease(delta / time)) + from) :
- fn(ease(delta / time));
- setTimeout(run, 5);
- }
- }
+ function run() {
+ var delta = new Date() - start;
+ if (delta > time) {
+ fn(to || 1);
+ done && done();
+ timer = null;
+ return;
+ }
+ to ?
+ fn((diff * ease(delta / time)) + from) :
+ fn(ease(delta / time));
+ setTimeout(run, 5);
+ }
+ }
function nextColor(pos, start, finish) {
var r = [], i;
- for (i = 0; i < 6; i++) {
- from = hex.indexOf(start[i]);
- to = hex.indexOf(finish[i]);
- r[i] = hex[Math.floor((to - from) * pos + from)];
- }
- return '#' + r.join('');
+ for (i = 0; i < 6; i++) {
+ from = hex.indexOf(start[i]);
+ to = hex.indexOf(finish[i]);
+ r[i] = hex[Math.floor((to - from) * pos + from)];
+ }
+ return '#' + r.join('');
}
function getVal(pos, options, begin, end, k, i, v) {
@@ -103,7 +105,7 @@
}
function by(val, start, m, r, i) {
- return (m = /^([+-])=([\d\.]+)/.exec(val)) ?
+ return (m = /^([+\-])=([\d\.]+)/.exec(val)) ?
(i = parseInt(m[2], 10)) && (r = (start + i)) && m[1] == '+' ?
r : start - i :
parseInt(val, 10);
@@ -132,9 +134,9 @@
}
// one tween to rule them all
- tween(duration, function (pos, v, k) {
+ tween(duration, function (pos, v) {
for (i = els.length; i--;) {
- for (k in options) {
+ for (var k in options) {
v = getVal(pos, options, begin, end, k, i);
ie && k == 'opacity' ?
(els[i].filter = 'alpha(opacity=' + (v * 100) + ')') :
Submodule sink added at 5b8255
Oops, something went wrong.

0 comments on commit a057abe

Please sign in to comment.