Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Centralize render loop, because speed.

  • Loading branch information...
commit 2cbbe55bada7b01a73233922a5ae24b10dc6559a 1 parent 486b028
@danro authored
Showing with 63 additions and 7 deletions.
  1. +31 −3 morpheus.js
  2. +1 −1  morpheus.min.js
  3. +31 −3 src/morpheus.js
View
34 morpheus.js
@@ -79,9 +79,37 @@
function (callback) {
win.setTimeout(function () {
callback(+new Date())
- }, 10)
+ }, 11) // these go to eleven
}
}()
+ , has = function (array, elem, i) {
+ if (Array.prototype.indexOf) return array.indexOf(elem)
+ for (i=0; i<array.length; ++i) {
+ if (array[i] === elem) return i
+ }
+ }
+ , children = []
+ , rendering = false
+ , render = function (t) {
+ var i, found
+ for (i = children.length; i--;) {
+ children[i](t);
+ found = true;
+ }
+ rendering = found && frame(render)
+ }
+ , live = function (f) {
+ children.push(f)
+ if (!rendering) render()
+ }
+ , die = function (f) {
+ var i, rest, index = has(children, f)
+ if (index >= 0) {
+ rest = children.slice(index+1)
+ children.length = index
+ children.push.apply(children, rest)
+ }
+ }
function parseTransform(style, base) {
var values = {}, m
@@ -144,13 +172,14 @@
, start = +new Date()
, stop = 0
, end = 0
- frame(run)
+ live(run)
function run(t) {
var delta = t - start
if (delta > time || stop) {
to = isFinite(to) ? to : 1
stop ? end && fn(to) : fn(to)
+ die(run)
return done && done.apply(self)
}
// if you don't specify a 'to' you can use tween as a generic delta tweener
@@ -158,7 +187,6 @@
isFinite(to) ?
fn((diff * ease(delta / time)) + from) :
fn(ease(delta / time))
- frame(run)
}
return {
stop: function (jump) {
View
2  morpheus.min.js
@@ -3,4 +3,4 @@
* https://github.com/ded/morpheus - (c) Dustin Diaz 2011
* License MIT
*/
-!function(a,b){typeof define=="function"?define(b):typeof module!="undefined"?module.exports=b():this[a]=b()}("morpheus",function(){function q(a,b){var c={},d;if(d=a.match(h))c.rotate=A(d[1],b?b.rotate:null);if(d=a.match(i))c.scale=A(d[1],b?b.scale:null);if(d=a.match(j))c.skewx=A(d[1],b?b.skewx:null),c.skewy=A(d[3],b?b.skewy:null);if(d=a.match(k))c.translatex=A(d[1],b?b.translatex:null),c.translatey=A(d[3],b?b.translatey:null);return c}function r(a){var b="";return"rotate"in a&&(b+="rotate("+a.rotate+"deg) "),"scale"in a&&(b+="scale("+a.scale+") "),"translatex"in a&&(b+="translate("+a.translatex+"px,"+a.translatey+"px) "),"skewx"in a&&(b+="skew("+a.skewx+"deg,"+a.skewy+"deg)"),b}function s(a,b,c){return"#"+(1<<24|a<<16|b<<8|c).toString(16).slice(1)}function t(a){var b=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(a);return(b?s(b[1],b[2],b[3]):a).replace(/#(\w)(\w)(\w)$/,"#$1$1$2$2$3$3")}function u(a){return a.replace(/-(.)/g,function(a,b){return b.toUpperCase()})}function v(a){return typeof a=="function"}function w(a,b,c,d,e,f){function m(a){var n=a-j;if(n>g||k)return f=isFinite(f)?f:1,k?l&&b(f):b(f),c&&c.apply(h);isFinite(f)?b(i*d(n/g)+e):b(d(n/g)),p(m)}d=v(d)?d:B.easings[d]||function(a){return Math.sin(a*Math.PI/2)};var g=a||1e3,h=this,i=f-e,j=+(new Date),k=0,l=0;return p(m),{stop:function(a){k=1,l=a,a||(c=null)}}}function x(a,b){var c=a.length,d=[],e,f;for(e=0;e<c;++e)d[e]=[a[e][0],a[e][1]];for(f=1;f<c;++f)for(e=0;e<c-f;++e)d[e][0]=(1-b)*d[e][0]+b*d[parseInt(e+1,10)][0],d[e][1]=(1-b)*d[e][1]+b*d[parseInt(e+1,10)][1];return[d[0][0],d[0][1]]}function y(a,b,c){var d=[],e,f,g,h;for(e=0;e<6;e++)g=Math.min(15,parseInt(b.charAt(e),16)),h=Math.min(15,parseInt(c.charAt(e),16)),f=Math.floor((h-g)*a+g),f=f>15?15:f<0?0:f,d[e]=f.toString(16);return"#"+d.join("")}function z(a,b,c,d,e,f,g){if(e=="transform"){g={};for(var h in c[f][e])g[h]=h in d[f][e]?Math.round(((d[f][e][h]-c[f][e][h])*a+c[f][e][h])*1e3)/1e3:c[f][e][h];return g}return typeof c[f][e]=="string"?y(a,c[f][e],d[f][e]):(g=Math.round(((d[f][e]-c[f][e])*a+c[f][e])*1e3)/1e3,e in l||(g+=b[f][e]||"px"),g)}function A(a,b,c,d,e){return(c=f.exec(a))?(e=parseFloat(c[2]))&&b+(c[1]=="+"?1:-1)*e:parseFloat(a)}function B(a,b){var c=a?c=isFinite(a.length)?a:[a]:[],d,f=b.complete,h=b.duration,i=b.easing,j=b.bezier,k=[],l=[],p=[],s=[],y,B;delete b.complete,delete b.duration,delete b.easing,delete b.bezier,j&&(y=b.left,B=b.top,delete b.right,delete b.bottom,delete b.left,delete b.top);for(d=c.length;d--;){k[d]={},l[d]={},p[d]={};if(j){var C=o(c[d],"left"),D=o(c[d],"top"),E=[A(v(y)?y(c[d]):y||0,parseFloat(C)),A(v(B)?B(c[d]):B||0,parseFloat(D))];s[d]=v(j)?j(c[d],E):j,s[d].push(E),s[d].unshift([parseInt(C,10),parseInt(D,10)])}for(var F in b){var G=o(c[d],F),H,I=v(b[F])?b[F](c[d]):b[F];if(typeof I=="string"&&e.test(I)&&!e.test(G)){delete b[F];continue}k[d][F]=F=="transform"?q(G):typeof I=="string"&&e.test(I)?t(G).slice(1):parseFloat(G),l[d][F]=F=="transform"?q(I,k[d][F]):typeof I=="string"&&I.charAt(0)=="#"?t(I).slice(1):A(I,parseFloat(G)),typeof I=="string"&&(H=I.match(g))&&(p[d][F]=H[1])}}return w.apply(c,[h,function(a,e,f){for(d=c.length;d--;){j&&(f=x(s[d],a),c[d].style.left=f[0]+"px",c[d].style.top=f[1]+"px");for(var g in b)e=z(a,p,k,l,g,d),g=="transform"?c[d].style[m]=r(e):g=="opacity"&&!n?c[d].style.filter="alpha(opacity="+e*100+")":c[d].style[u(g)]=e}},f,i])}var a=this,b=document,c=window,d=b.documentElement,e=/^rgb\(|#/,f=/^([+\-])=([\d\.]+)/,g=/^(?:[\+\-]=)?\d+(?:\.\d+)?(%|in|cm|mm|em|ex|pt|pc|px)$/,h=/rotate\(((?:[+\-]=)?([\-\d\.]+))deg\)/,i=/scale\(((?:[+\-]=)?([\d\.]+))\)/,j=/skew\(((?:[+\-]=)?([\-\d\.]+))deg, ?((?:[+\-]=)?([\-\d\.]+))deg\)/,k=/translate\(((?:[+\-]=)?([\-\d\.]+))px, ?((?:[+\-]=)?([\-\d\.]+))px\)/,l={lineHeight:1,zoom:1,zIndex:1,opacity:1,transform:1},m=function(){var a=b.createElement("a").style,c=["webkitTransform","MozTransform","OTransform","msTransform","Transform"],d;for(d=0;d<c.length;d++)if(c[d]in a)return c[d]}(),n=function(){return typeof b.createElement("a").style.opacity!="undefined"}(),o=b.defaultView&&b.defaultView.getComputedStyle?function(a,c){c=c=="transform"?m:c;var d=null,e=b.defaultView.getComputedStyle(a,"");return e&&(d=e[u(c)]),a.style[c]||d}:d.currentStyle?function(a,b){b=u(b);if(b=="opacity"){var c=100;try{c=a.filters["DXImageTransform.Microsoft.Alpha"].opacity}catch(d){try{c=a.filters("alpha").opacity}catch(e){}}return c/100}var f=a.currentStyle?a.currentStyle[b]:null;return a.style[b]||f}:function(a,b){return a.style[u(b)]},p=function(){return c.requestAnimationFrame||c.webkitRequestAnimationFrame||c.mozRequestAnimationFrame||c.oRequestAnimationFrame||c.msRequestAnimationFrame||function(a){c.setTimeout(function(){a(+(new Date))},10)}}();return B.tween=w,B.getStyle=o,B.bezier=x,B.transform=m,B.parseTransform=q,B.formatTransform=r,B.easings={},B})
+!function(a,b){typeof define=="function"?define(b):typeof module!="undefined"?module.exports=b():this[a]=b()}("morpheus",function(){function w(a,b){var c={},d;if(d=a.match(h))c.rotate=G(d[1],b?b.rotate:null);if(d=a.match(i))c.scale=G(d[1],b?b.scale:null);if(d=a.match(j))c.skewx=G(d[1],b?b.skewx:null),c.skewy=G(d[3],b?b.skewy:null);if(d=a.match(k))c.translatex=G(d[1],b?b.translatex:null),c.translatey=G(d[3],b?b.translatey:null);return c}function x(a){var b="";return"rotate"in a&&(b+="rotate("+a.rotate+"deg) "),"scale"in a&&(b+="scale("+a.scale+") "),"translatex"in a&&(b+="translate("+a.translatex+"px,"+a.translatey+"px) "),"skewx"in a&&(b+="skew("+a.skewx+"deg,"+a.skewy+"deg)"),b}function y(a,b,c){return"#"+(1<<24|a<<16|b<<8|c).toString(16).slice(1)}function z(a){var b=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(a);return(b?y(b[1],b[2],b[3]):a).replace(/#(\w)(\w)(\w)$/,"#$1$1$2$2$3$3")}function A(a){return a.replace(/-(.)/g,function(a,b){return b.toUpperCase()})}function B(a){return typeof a=="function"}function C(a,b,c,d,e,f){function m(a){var n=a-j;if(n>g||k)return f=isFinite(f)?f:1,k?l&&b(f):b(f),v(m),c&&c.apply(h);isFinite(f)?b(i*d(n/g)+e):b(d(n/g))}d=B(d)?d:H.easings[d]||function(a){return Math.sin(a*Math.PI/2)};var g=a||1e3,h=this,i=f-e,j=+(new Date),k=0,l=0;return u(m),{stop:function(a){k=1,l=a,a||(c=null)}}}function D(a,b){var c=a.length,d=[],e,f;for(e=0;e<c;++e)d[e]=[a[e][0],a[e][1]];for(f=1;f<c;++f)for(e=0;e<c-f;++e)d[e][0]=(1-b)*d[e][0]+b*d[parseInt(e+1,10)][0],d[e][1]=(1-b)*d[e][1]+b*d[parseInt(e+1,10)][1];return[d[0][0],d[0][1]]}function E(a,b,c){var d=[],e,f,g,h;for(e=0;e<6;e++)g=Math.min(15,parseInt(b.charAt(e),16)),h=Math.min(15,parseInt(c.charAt(e),16)),f=Math.floor((h-g)*a+g),f=f>15?15:f<0?0:f,d[e]=f.toString(16);return"#"+d.join("")}function F(a,b,c,d,e,f,g){if(e=="transform"){g={};for(var h in c[f][e])g[h]=h in d[f][e]?Math.round(((d[f][e][h]-c[f][e][h])*a+c[f][e][h])*1e3)/1e3:c[f][e][h];return g}return typeof c[f][e]=="string"?E(a,c[f][e],d[f][e]):(g=Math.round(((d[f][e]-c[f][e])*a+c[f][e])*1e3)/1e3,e in l||(g+=b[f][e]||"px"),g)}function G(a,b,c,d,e){return(c=f.exec(a))?(e=parseFloat(c[2]))&&b+(c[1]=="+"?1:-1)*e:parseFloat(a)}function H(a,b){var c=a?c=isFinite(a.length)?a:[a]:[],d,f=b.complete,h=b.duration,i=b.easing,j=b.bezier,k=[],l=[],p=[],q=[],r,s;delete b.complete,delete b.duration,delete b.easing,delete b.bezier,j&&(r=b.left,s=b.top,delete b.right,delete b.bottom,delete b.left,delete b.top);for(d=c.length;d--;){k[d]={},l[d]={},p[d]={};if(j){var t=o(c[d],"left"),u=o(c[d],"top"),v=[G(B(r)?r(c[d]):r||0,parseFloat(t)),G(B(s)?s(c[d]):s||0,parseFloat(u))];q[d]=B(j)?j(c[d],v):j,q[d].push(v),q[d].unshift([parseInt(t,10),parseInt(u,10)])}for(var y in b){var E=o(c[d],y),H,I=B(b[y])?b[y](c[d]):b[y];if(typeof I=="string"&&e.test(I)&&!e.test(E)){delete b[y];continue}k[d][y]=y=="transform"?w(E):typeof I=="string"&&e.test(I)?z(E).slice(1):parseFloat(E),l[d][y]=y=="transform"?w(I,k[d][y]):typeof I=="string"&&I.charAt(0)=="#"?z(I).slice(1):G(I,parseFloat(E)),typeof I=="string"&&(H=I.match(g))&&(p[d][y]=H[1])}}return C.apply(c,[h,function(a,e,f){for(d=c.length;d--;){j&&(f=D(q[d],a),c[d].style.left=f[0]+"px",c[d].style.top=f[1]+"px");for(var g in b)e=F(a,p,k,l,g,d),g=="transform"?c[d].style[m]=x(e):g=="opacity"&&!n?c[d].style.filter="alpha(opacity="+e*100+")":c[d].style[A(g)]=e}},f,i])}var a=this,b=document,c=window,d=b.documentElement,e=/^rgb\(|#/,f=/^([+\-])=([\d\.]+)/,g=/^(?:[\+\-]=)?\d+(?:\.\d+)?(%|in|cm|mm|em|ex|pt|pc|px)$/,h=/rotate\(((?:[+\-]=)?([\-\d\.]+))deg\)/,i=/scale\(((?:[+\-]=)?([\d\.]+))\)/,j=/skew\(((?:[+\-]=)?([\-\d\.]+))deg, ?((?:[+\-]=)?([\-\d\.]+))deg\)/,k=/translate\(((?:[+\-]=)?([\-\d\.]+))px, ?((?:[+\-]=)?([\-\d\.]+))px\)/,l={lineHeight:1,zoom:1,zIndex:1,opacity:1,transform:1},m=function(){var a=b.createElement("a").style,c=["webkitTransform","MozTransform","OTransform","msTransform","Transform"],d;for(d=0;d<c.length;d++)if(c[d]in a)return c[d]}(),n=function(){return typeof b.createElement("a").style.opacity!="undefined"}(),o=b.defaultView&&b.defaultView.getComputedStyle?function(a,c){c=c=="transform"?m:c;var d=null,e=b.defaultView.getComputedStyle(a,"");return e&&(d=e[A(c)]),a.style[c]||d}:d.currentStyle?function(a,b){b=A(b);if(b=="opacity"){var c=100;try{c=a.filters["DXImageTransform.Microsoft.Alpha"].opacity}catch(d){try{c=a.filters("alpha").opacity}catch(e){}}return c/100}var f=a.currentStyle?a.currentStyle[b]:null;return a.style[b]||f}:function(a,b){return a.style[A(b)]},p=function(){return c.requestAnimationFrame||c.webkitRequestAnimationFrame||c.mozRequestAnimationFrame||c.oRequestAnimationFrame||c.msRequestAnimationFrame||function(a){c.setTimeout(function(){a(+(new Date))},11)}}(),q=function(a,b,c){if(Array.prototype.indexOf)return a.indexOf(b);for(c=0;c<a.length;++c)if(a[c]===b)return c},r=[],s=!1,t=function(a){var b,c;for(b=r.length;b--;)r[b](a),c=!0;s=c&&p(t)},u=function(a){r.push(a),s||t()},v=function(a){var b,c,d=q(r,a);d>=0&&(c=r.slice(d+1),r.length=d,r.push.apply(r,c))};return H.tween=C,H.getStyle=o,H.bezier=D,H.transform=m,H.parseTransform=w,H.formatTransform=x,H.easings={},H})
View
34 src/morpheus.js
@@ -74,9 +74,37 @@
function (callback) {
win.setTimeout(function () {
callback(+new Date())
- }, 10)
+ }, 11) // these go to eleven
}
}()
+ , has = function (array, elem, i) {
+ if (Array.prototype.indexOf) return array.indexOf(elem)
+ for (i=0; i<array.length; ++i) {
+ if (array[i] === elem) return i
+ }
+ }
+ , children = []
+ , rendering = false
+ , render = function (t) {
+ var i, found
+ for (i = children.length; i--;) {
+ children[i](t);
+ found = true;
+ }
+ rendering = found && frame(render)
+ }
+ , live = function (f) {
+ children.push(f)
+ if (!rendering) render()
+ }
+ , die = function (f) {
+ var i, rest, index = has(children, f)
+ if (index >= 0) {
+ rest = children.slice(index+1)
+ children.length = index
+ children.push.apply(children, rest)
+ }
+ }
function parseTransform(style, base) {
var values = {}, m
@@ -139,13 +167,14 @@
, start = +new Date()
, stop = 0
, end = 0
- frame(run)
+ live(run)
function run(t) {
var delta = t - start
if (delta > time || stop) {
to = isFinite(to) ? to : 1
stop ? end && fn(to) : fn(to)
+ die(run)
return done && done.apply(self)
}
// if you don't specify a 'to' you can use tween as a generic delta tweener
@@ -153,7 +182,6 @@
isFinite(to) ?
fn((diff * ease(delta / time)) + from) :
fn(ease(delta / time))
- frame(run)
}
return {
stop: function (jump) {
Please sign in to comment.
Something went wrong with that request. Please try again.