Guest User

Untitled

a guest
Apr 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // Tweener Like snippet
  2. function Tween3(item, opt) {
  3. var TIME = 10, TM_EXP = /(\+)?\$([\#\d])/g, actions = [],
  4. easing = function(t, b, c, d){return c*t/d + b;},
  5. T = {time:1,onComplete:1,transition:1,delay:1};
  6. var sets = [], delay = opt.delay*1000 || 0, start = new Date()*1 + delay, time = (opt.time||1) * 1000;
  7. for (var k in opt) if (!T[k]) {
  8. var set = opt[k], from = set.from || parseFloat(item[k]) || 0, tmpl = set.tmpl || '-$#';
  9. sets.push({key:k, from:from, to:set.to, tmpl:tmpl});
  10. }
  11. var act = {item:item,sets:sets,time:time,start:start,delay:delay,easing:opt.transition || easing};
  12. if (opt.onComplete) act.onComplete = opt.onComplete;
  13. actions.push(act);
  14. if (actions.length === 1) loop();
  15. function loop(){
  16. var now = new Date()*1;
  17. for (var i = actions.length; i--;) {
  18. var act = actions[i], duration = now - act.start;
  19. if (duration < 0) continue;
  20. for (var j = 0, L = act.sets.length; j < L; ++j) {
  21. var set = act.sets[j], val = act.easing(duration, set.from, set.to - set.from, act.time);
  22. act.item[set.key] = set.tmpl.replace(TM_EXP,
  23. function(m, p, m1){return p && val < 0 ? 0 : (m1 == '#' ? val : val.toFixed(m1));});
  24. }
  25. if (duration > act.time) {
  26. for (var j = 0; j < L; ++j) {item[sets[j].key] = sets[j].tmpl.replace(TM_EXP, sets[j].to);}
  27. if (typeof act.onComplete == 'function') act.onComplete(item);
  28. actions.splice(i, 1);
  29. }
  30. }
  31. if (actions.length) setTimeout(loop, TIME);
  32. }
  33. }
Add Comment
Please, Sign In to add comment