Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing requestAnimationFrame for effecincy #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions emile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the terms of the MIT license.

(function(emile, container){

var parseEl = document.createElement('div'),
props = ('backgroundColor borderBottomColor borderBottomWidth borderLeftColor borderLeftWidth '+
'borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth bottom color fontSize '+
Expand Down Expand Up @@ -32,7 +33,17 @@
css = parseEl.childNodes[0].style;
while(i--) if(v = css[props[i]]) rules[props[i]] = parse(v);
return rules;
}
}

// request animation step
var req_frame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 1000 / 60);
};

container[emile] = function(el, style, opts, after){
el = typeof el == 'string' ? document.getElementById(el) : el;
Expand All @@ -41,11 +52,14 @@
prop, current = {}, start = +new Date, dur = opts.duration||200, finish = start+dur, interval,
easing = opts.easing || function(pos){ return (-Math.cos(pos*Math.PI)/2) + 0.5; };
for(prop in target) current[prop] = parse(comp[prop]);
interval = setInterval(function(){
on_animate = function(){
var time = +new Date, pos = time>finish ? 1 : (time-start)/dur;
for(prop in target)
el.style[prop] = target[prop].f(current[prop].v,target[prop].v,easing(pos)) + target[prop].u;
if(time>finish) { clearInterval(interval); opts.after && opts.after(); after && setTimeout(after,1); }
},10);
if(time>finish) { opts.after && opts.after(); after && setTimeout(after,1); }
else { req_frame( on_animate, el ); }
};

req_frame( on_animate, el );
}
})('emile', this);
})('emile', this);
3 changes: 2 additions & 1 deletion emile.min.js

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