Skip to content

Instantly share code, notes, and snippets.

@fivesixty
Created June 19, 2010 22:28
Show Gist options
  • Save fivesixty/445356 to your computer and use it in GitHub Desktop.
Save fivesixty/445356 to your computer and use it in GitHub Desktop.
var Tween = (function () {
var animations = {}, nanim = 0;
var Tween = {};
Tween.step = function () {
var d = new Date();
var thisTick = d.getTime();
for (var a in animations) {
var anim = animations[a];
if (thisTick > (anim.start + anim.end)) {
delete animations[a];
anim.assign(anim.to);
if (anim.callback !== undefined) {
anim.callback();
}
nanim--;
} else {
anim.assign(Tween[anim.interpolation](anim.from, anim.to, (thisTick - anim.start) / anim.end));
}
}
if (nanim > 0) {
setTimeout(Tween.step, 20);
}
};
Tween.anim = function (identifier, object, variable, from, to, time, method, callback) {
var d = new Date();
var thisTick = d.getTime();
var t = {
assign: typeof object[variable] === "function" ?
function (value) {
object[variable](value);
} :
function (value) {
object[variable] = value;
},
start: thisTick,
end: time,
from: from,
to: to,
interpolation: method === undefined ? "linear" : method,
callback: callback
};
if (animations[identifier] === undefined) {
nanim++;
}
animations[identifier] = t;
if (nanim === 1) {
Tween.step();
}
};
Tween.linear = function (from, to, t) {
return (t * (to-from)) + from;
};
Tween.quadratic = function (from, to, t) {
return (t*t * (to-from)) + from;
};
Tween.invQuadratic = function (from, to, t) {
var u = 1-t;
return Tween.quadratic(to, from, u);
}
return Tween;
}());
var Watch = (function () {
document.write('<div id="watchPanel">&nbsp;</div>');
var watchDiv = document.getElementById("watchPanel"),
watchObj = undefined,
staticLink = false;
(function watchLoop() {
var html = "";
if (watchObj === undefined) {
html = "Watching Nothing.";
} else {
var watchVariable;
if (staticLink) {
watchVariable = eval(watchObj);
} else {
watchVariable = watchObj;
}
try {
html = JSON.stringify(watchVariable);
} catch (err) {
html = "Sorry, I can't watch circular structures.";
}
}
watchDiv.innerHTML = html;
setTimeout(watchLoop, 20);
}());
return function (object) {
staticLink = (typeof object !== "object");
watchObj = object;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment