dat.gui/time/scrubber.js

870 lines
18 KiB
JavaScript
Raw Normal View History

2011-02-02 18:12:55 +00:00
GUI.Scrubber = function(controller, timer) {
var _this = this;
this.points = [];
2011-02-02 18:12:55 +00:00
this.timer = timer;
2011-02-08 15:40:38 +00:00
this.timer.scrubbers.push(this);
2011-02-02 18:12:55 +00:00
this.controller = controller;
this.controller.scrubber = this;
2011-02-02 20:51:58 +00:00
this.playing = false;
2011-02-08 15:40:38 +00:00
var previouslyHandled;
this.position = null;
2011-02-08 15:40:38 +00:00
this.getJSON = function() {
2011-02-08 15:40:38 +00:00
var pointArray = [];
for (var i in this.points) {
pointArray.push(this.points[i].getJSON());
2011-02-08 15:40:38 +00:00
}
var obj = {'points': pointArray};
return obj;
};
2011-02-02 20:51:58 +00:00
this.sort = function() {
this.points.sort(function(a,b) {
return a.time - b.time;
});
2011-02-08 15:40:38 +00:00
};
2011-02-02 20:51:58 +00:00
this.add = function(p) {
this.points.push(p);
this.sort();
2011-02-08 15:40:38 +00:00
};
2011-02-02 20:51:58 +00:00
var lastDown = 0;
2011-02-02 20:51:58 +00:00
this.controller.addChangeListener(function(newVal) {
if (!_this.playing) {
2011-02-08 15:40:38 +00:00
var v = newVal;
if (_this.controller.type == 'boolean') {
2011-02-08 15:40:38 +00:00
v = !v; // Couldn't tell you why I have to do this.
}
if (_this.timer.activePoint == null) {
2011-02-08 15:40:38 +00:00
_this.timer.activePoint = new GUI.ScrubberPoint(_this, _this.timer.playhead, v);
_this.add(_this.timer.activePoint);
_this.render();
} else {
2011-02-08 15:40:38 +00:00
_this.timer.activePoint.value = v;
}
2011-02-02 20:51:58 +00:00
}
2011-02-02 20:51:58 +00:00
});
2011-02-02 18:12:55 +00:00
this.domElement = document.createElement('div');
this.domElement.setAttribute('class', 'guidat-scrubber');
2011-02-02 18:12:55 +00:00
this.canvas = document.createElement('canvas');
this.domElement.appendChild(this.canvas);
2011-02-02 18:12:55 +00:00
this.g = this.canvas.getContext('2d');
2011-02-02 18:12:55 +00:00
var width;
var height;
2011-02-03 00:01:12 +00:00
var mx, pmx;
2011-02-08 15:40:38 +00:00
this.__defineGetter__('width', function() {
2011-02-02 18:12:55 +00:00
return width;
});
2011-02-08 15:40:38 +00:00
this.__defineGetter__('height', function() {
2011-02-02 18:12:55 +00:00
return height;
});
controller.domElement.insertBefore(this.domElement, controller.propertyNameElement.nextSibling);
this.render = function() {
// TODO: if visible ...
2011-02-08 15:40:38 +00:00
2011-02-02 18:12:55 +00:00
_this.g.clearRect(0, 0, width, height);
2011-02-08 15:40:38 +00:00
// Draw 0
if (_this.timer.windowMin < 0) {
var x = GUI.map(0, _this.timer.windowMin, _this.timer.windowMin+_this.timer.windowWidth, 0, width);
_this.g.fillStyle = '#000';
_this.g.fillRect(0, 0, x, height-1);
}
// Draw ticks
if (_this.timer.useSnap) {
_this.g.lineWidth = 1;
// TODO: That's just a damned nasty for loop.
2011-02-08 15:40:38 +00:00
for (var i = _this.timer.snap(_this.timer.windowMin); i < _this.timer.windowMin+_this.timer.windowWidth; i+= _this.timer.snapIncrement) {
if (i == 0) continue;
var x = Math.round(GUI.map(i, _this.timer.windowMin, _this.timer.windowMin+_this.timer.windowWidth, 0, width))+0.5;
if (i < 0) {
_this.g.strokeStyle = '#111111';
} else {
_this.g.strokeStyle = '#282828';
}
_this.g.beginPath();
_this.g.moveTo(x, 0);
_this.g.lineTo(x, height-1);
_this.g.stroke();
}
}
// Draw points
for (var i in _this.points) {
_this.points[i].update();
}
2011-02-08 15:40:38 +00:00
for (var i in _this.points) {
_this.points[i].render();
2011-02-02 18:12:55 +00:00
}
// Draw playhead
2011-02-08 15:40:38 +00:00
_this.g.strokeStyle = '#ff0024';
_this.g.lineWidth = 1;
2011-02-02 18:12:55 +00:00
var t = Math.round(GUI.map(_this.timer.playhead, _this.timer.windowMin, _this.timer.windowMin+_this.timer.windowWidth, 0, width))+0.5;
_this.g.beginPath();
_this.g.moveTo(t, 0);
_this.g.lineTo(t, height);
_this.g.stroke();
}
this.render();
var onResize = function() {
_this.canvas.width = width = _this.domElement.offsetWidth;
_this.canvas.height = height = _this.domElement.offsetHeight;
_this.position = GUI.getOffset(_this.canvas);
2011-02-02 18:12:55 +00:00
_this.render();
};
window.addEventListener('resize', function(e) {
onResize();
}, false);
var scrubPan = function() {
var t = _this.timer.playhead;
var tmin = _this.timer.windowMin + _this.timer.windowWidth/5;
var tmax = _this.timer.windowMin + _this.timer.windowWidth - _this.timer.windowWidth/5;
if (t < tmin) {
_this.timer.windowMin += GUI.map(t, _this.timer.windowMin, tmin, -_this.timer.windowWidth/50, 0);
}
if (t > tmax) {
_this.timer.windowMin += 0;
_this.timer.windowMin += GUI.map(t, tmax, _this.timer.windowMin+_this.timer.windowWidth, 0,_this.timer.windowWidth/50);
}
}
2011-02-08 15:40:38 +00:00
2011-02-02 20:51:58 +00:00
var scrub = function(e) {
var t = GUI.map(e.pageX, _this.position.left, _this.position.left+width, _this.timer.windowMin, _this.timer.windowMin+_this.timer.windowWidth);
2011-02-08 15:40:38 +00:00
_this.timer.playhead = _this.timer.snap(t);
scrubPan();
2011-02-03 00:01:12 +00:00
}
2011-02-08 15:40:38 +00:00
var pan = function(e) {
2011-02-03 00:01:12 +00:00
mx = e.pageX;
2011-02-08 15:40:38 +00:00
var t = GUI.map(mx - pmx, 0, width, 0, _this.timer.windowWidth);
_this.timer.windowMin -= t;
2011-02-03 00:01:12 +00:00
pmx = mx;
2011-02-08 15:40:38 +00:00
}
this.canvas.addEventListener('mousedown', function(e) {
// TODO: Detect right click and prevent that menu?
2011-02-08 15:40:38 +00:00
if (false) {
e.preventDefault();
document.addEventListener('mousemove', pan, false);
return false;
}
2011-02-08 15:40:38 +00:00
var thisDown = GUI.millis();
// Double click creates a keyframe
// TODO: You can double click to create a keyframe right on top of an existing keyframe.
// TODO: Make 300 a constant of some sort.
if (thisDown - lastDown < 300) {
var val = _this.controller.getValue();
if (_this.controller.type == 'boolean') {
val = !val;
}
_this.timer.activePoint = new GUI.ScrubberPoint(_this, _this.timer.playhead, val);
_this.timer.activePoint.update(); // Grab x and y
_this.timer.activePoint.onSelect();
_this.add(_this.timer.activePoint);
_this.render();
2011-02-08 15:40:38 +00:00
// A regular click COULD select a point ...
} else if (_this.timer.hoverPoint != null) {
2011-02-02 20:51:58 +00:00
if (_this.timer.activePoint != _this.timer.hoverPoint) {
if (_this.timer.activePoint != null) _this.timer.activePoint.onBlur();
_this.timer.activePoint = _this.timer.hoverPoint;
_this.timer.activePoint.onSelect();
}
_this.timer.playhead = _this.timer.snap(_this.timer.activePoint.time);
2011-02-03 00:01:12 +00:00
pmx = mx = e.pageX;
document.addEventListener('mousemove', _this.timer.activePoint.onDrag, false);
2011-02-03 00:01:12 +00:00
2011-02-08 15:40:38 +00:00
// Or we could just be trying to place the playhead/scrub.
2011-02-02 20:51:58 +00:00
} else {
if (_this.timer.activePoint != null) {
_this.timer.activePoint.onBlur();
}
2011-02-02 20:51:58 +00:00
_this.timer.activePoint = null;
2011-02-03 00:01:12 +00:00
_this.timer.hoverPoint = null;
2011-02-02 20:51:58 +00:00
scrub(e);
2011-02-08 15:40:38 +00:00
document.body.style.cursor = 'text';
_this.timer.pause();
2011-02-08 15:40:38 +00:00
pmx = mx = e.pageX;
2011-02-02 20:51:58 +00:00
document.addEventListener('mousemove', scrub, false);
_this.render();
}
lastDown = thisDown;
2011-02-02 20:51:58 +00:00
}, false);
this.canvas.addEventListener('mousewheel', function(e) {
2011-02-08 15:40:38 +00:00
e.preventDefault();
var dx = e.wheelDeltaX*4;
var dy = e.wheelDeltaY*4;
_this.timer.windowWidth -= dy;
_this.timer.windowMin += dy/2 - dx;
2011-02-08 15:40:38 +00:00
return false;
}, false);
this.canvas.addEventListener('mousemove', function(e) {
2011-02-02 20:51:58 +00:00
_this.timer.hoverPoint = null;
for (var i in _this.points) {
var cur = _this.points[i];
if (cur.isHovering(e.pageX-_this.position.left)) {
2011-02-02 20:51:58 +00:00
_this.timer.hoverPoint = cur;
}
}
2011-02-03 00:45:12 +00:00
if (_this.timer.hoverPoint == null) {
2011-02-08 15:40:38 +00:00
document.body.style.cursor = 'pointer';
2011-02-03 00:45:12 +00:00
} else {
2011-02-08 15:40:38 +00:00
document.body.style.cursor = 'auto';
2011-02-03 00:45:12 +00:00
}
2011-02-02 20:51:58 +00:00
_this.render();
});
document.addEventListener('mouseup', function() {
2011-02-08 15:40:38 +00:00
document.body.style.cursor = 'auto';
if (_this.timer.activePoint != null) {
document.removeEventListener('mousemove', _this.timer.activePoint.onDrag, false);
}
2011-02-02 20:51:58 +00:00
document.removeEventListener('mousemove', scrub, false);
2011-02-08 15:40:38 +00:00
document.removeEventListener('mousemove', pan, false);
2011-02-02 20:51:58 +00:00
}, false);
2011-02-02 18:12:55 +00:00
onResize();
this.timer.addPlayListener(this.render);
2011-02-08 15:40:38 +00:00
var handlePoint = function(point) {
if (point != previouslyHandled) {
previouslyHandled = point;
_this.controller.setValue(point.value);
}
};
2011-02-02 18:12:55 +00:00
var onPlayChange = function(curTime, prevTime) {
2011-02-08 15:40:38 +00:00
if (_this.points.length == 0) return;
2011-02-08 15:40:38 +00:00
_this.playing = true;
2011-02-08 15:40:38 +00:00
if (_this.controller.type == 'function') {
for (var i = 0; i < _this.points.length; i++) {
var t = _this.points[i].time;
if ((curTime > prevTime && prevTime < t && t < curTime) ||
(curTime < prevTime && prevTime > t && t > curTime)) {
_this.controller.getValue().call(this);
}
}
2011-02-08 15:40:38 +00:00
} else {
var prev = undefined, next = undefined;
// Find 'surrounding' points.
2011-02-08 15:40:38 +00:00
for (var i = 0; i < _this.points.length; i++) {
var t = _this.points[i].time;
2011-02-08 15:40:38 +00:00
if (t > curTime) {
if (i == 0) {
prev = null;
next = _this.points[i];
break;
} else {
2011-02-03 00:01:12 +00:00
2011-02-08 15:40:38 +00:00
prev = _this.points[i-1];
next = _this.points[i];
break;
}
2011-02-03 00:01:12 +00:00
}
2011-02-08 15:40:38 +00:00
}
2011-02-08 15:40:38 +00:00
if (next == undefined) {
prev = _this.points[_this.points.length-1];
next = null;
2011-02-02 18:12:55 +00:00
}
2011-02-08 15:40:38 +00:00
if (next != null & prev != null) {
2011-02-08 15:40:38 +00:00
if (_this.controller.type == 'number') {
var t = prev.tween(GUI.map(curTime, prev.time, next.time, 0, 1));
_this.controller.setValue(GUI.map(t, 0, 1, prev.value, next.value));
2011-02-08 15:40:38 +00:00
} else {
handlePoint(prev);
}
2011-02-08 15:40:38 +00:00
} else if (next != null) {
handlePoint(next);
} else if (prev != null) {
handlePoint(prev);
}
}
2011-02-02 18:12:55 +00:00
2011-02-02 20:51:58 +00:00
_this.playing = false;
2011-02-02 18:12:55 +00:00
};
this.timer.addPlayListener(onPlayChange);
this.timer.addWindowListener(this.render);
// Load saved points!!!!
if (timer.gui.json) {
var json = timer.gui.json.timer.scrubbers.splice(0, 1)[0];
for (var i in json.points) {
var p = json.points[i];
var pp = new GUI.ScrubberPoint(this, p.time, p.value);
if (p.tween) {
pp.tween = GUI.Easing[p.tween];
}
this.add(pp);
}
}
2011-02-02 18:12:55 +00:00
};
GUI.ScrubberPoint = function(scrubber, time, value) {
2011-02-03 00:01:12 +00:00
var _this = this;
2011-02-02 18:12:55 +00:00
var g = scrubber.g;
var timer = scrubber.timer;
var type = scrubber.controller.type;
2011-02-02 20:51:58 +00:00
var x, y;
this.hold = false;
2011-02-08 15:40:38 +00:00
var val;
this.__defineSetter__('value', function(v) {
2011-02-08 15:40:38 +00:00
val = v;
scrubber.render();
});
2011-02-02 20:51:58 +00:00
this.value = value;
2011-02-08 15:40:38 +00:00
this.__defineGetter__('value', function() {
2011-02-08 15:40:38 +00:00
return val;
});
this.__defineGetter__('x', function() {
return x;
});
this.__defineGetter__('y', function() {
return y;
});
2011-02-02 20:51:58 +00:00
var barSize = 4;
var rectSize = 5;
2011-02-02 20:51:58 +00:00
2011-02-08 15:40:38 +00:00
var c1 = '#ffd800';
var c2 = '#ff9000';
var positionTweenSelector = function() {
var tweenSelectorLeft = (scrubber.position.left + timer.activePoint.x) - timer.tweenSelector.offsetWidth/2;
var tweenSelectorTop = GUI.getOffset(scrubber.canvas, timer.gui.domElement).top + timer.activePoint.y - 25;
timer.tweenSelector.style.left = tweenSelectorLeft+'px';
timer.tweenSelector.style.top = tweenSelectorTop+'px';
}
this.onSelect = function() {
if (type == 'number') {
timer.showTweenSelector();
timer.tweenSelector.value = 'al';
positionTweenSelector();
}
}
this.onBlur = function() {
if (type == 'number') {
timer.hideTweenSelector();
}
}
this.onDrag = function(e) {
var t = GUI.map(e.pageX, scrubber.position.left, scrubber.position.left+scrubber.canvas.width, timer.windowMin, timer.windowMin+timer.windowWidth);
_this.time = timer.snap(t);
timer.playhead = timer.snap(t);
scrubber.sort();
_this.update();
if (type == 'number') {
positionTweenSelector();
}
}
this.getJSON = function() {
var obj = { 'value': _this.value, 'time': GUI.roundToDecimal(time,4) };
2011-02-08 15:40:38 +00:00
// TODO: save tweens
if (this.tween != GUI.Easing.Linear) {
for (var i in GUI.Easing) {
if (this.tween == GUI.Easing[i]) {
obj.tween = i;
}
}
}
2011-02-08 15:40:38 +00:00
return obj;
};
2011-02-02 18:12:55 +00:00
this.tween = GUI.Easing.Linear;
2011-02-02 18:12:55 +00:00
2011-02-03 00:01:12 +00:00
this.remove = function() {
scrubber.points.splice(scrubber.points.indexOf(this), 1);
scrubber.render();
};
2011-02-02 20:51:58 +00:00
this.isHovering = function(xx) {
return xx >= x-rectSize/2 && xx <= x+rectSize/2;
};
2011-02-02 18:12:55 +00:00
2011-02-08 15:40:38 +00:00
this.__defineGetter__('next', function() {
if (scrubber.points.length <= 1) {
return null;
2011-02-02 18:12:55 +00:00
}
var i = scrubber.points.indexOf(this);
if (i + 1 >= scrubber.points.length) {
return null;
}
2011-02-02 18:12:55 +00:00
return scrubber.points[i+1];
2011-02-02 20:51:58 +00:00
2011-02-02 18:12:55 +00:00
});
2011-02-08 15:40:38 +00:00
this.__defineGetter__('time', function() {
return time;
2011-02-02 18:12:55 +00:00
});
2011-02-08 15:40:38 +00:00
this.__defineSetter__('time', function(s) {
2011-02-03 00:01:12 +00:00
time = s;
});
2011-02-02 18:12:55 +00:00
this.update = function() {
2011-02-02 18:12:55 +00:00
2011-02-02 20:51:58 +00:00
x = GUI.map(time, timer.windowMin, timer.windowMin+timer.windowWidth, 0, 1);
x = Math.round(GUI.map(x, 0, 1, 0, scrubber.width));
2011-02-02 20:51:58 +00:00
y = scrubber.height/2;
if (scrubber.controller.type == 'number') {
y = GUI.map(_this.value, scrubber.controller.min, scrubber.controller.max, scrubber.height, 0);
}
}
this.render = function() {
if (x < 0 || x > scrubber.width) {
return;
}
if (GUI.hidden) {
return;
}
// TODO: if hidden because of scroll top.
2011-02-03 00:01:12 +00:00
if (scrubber.timer.activePoint == this) {
2011-02-08 15:40:38 +00:00
g.fillStyle = '#ffd800'; //
2011-02-03 00:01:12 +00:00
} else if (scrubber.timer.hoverPoint == this) {
2011-02-08 15:40:38 +00:00
g.fillStyle = '#999';
2011-02-03 00:01:12 +00:00
} else {
2011-02-08 15:40:38 +00:00
g.fillStyle = '#ccc';
2011-02-03 00:01:12 +00:00
}
switch (type) {
2011-02-08 15:40:38 +00:00
case 'boolean':
2011-02-03 03:53:46 +00:00
g.save();
g.translate(x, y-0.5);
2011-02-03 03:53:46 +00:00
if (this.value) {
g.strokeStyle = g.fillStyle;
g.lineWidth = barSize;
g.beginPath();
g.arc(0, 0, barSize, 0, Math.PI*2, false);
g.stroke();
2011-02-03 03:53:46 +00:00
} else {
g.rotate(Math.PI/4);
g.fillRect(-barSize/2, -barSize*3.5/2, barSize, barSize*3.5);
g.rotate(Math.PI/2);
g.fillRect(-barSize/2, -barSize*3.5/2, barSize, barSize*3.5);
}
g.restore();
break;
2011-02-08 15:40:38 +00:00
case 'number':
g.save();
var n = this.next;
if (n != null) {
g.lineWidth = 2;
2011-02-08 15:40:38 +00:00
g.strokeStyle='#222';
g.beginPath();
g.moveTo(x, y);
// TODO: Lines from points that may be outside of the window's scope.
if (_this.tween == GUI.Easing.Linear) {
g.lineTo(n.x, n.y);
} else {
for (var i = 0; i < 1; i+=0.01) {
var tx = GUI.map(i, 0, 1, x, n.x);
var ty = _this.tween(i);
ty = GUI.map(ty, 0, 1, y, n.y);
g.lineTo(tx, ty);
}
}
g.stroke();
}
g.translate(x, y);
g.rotate(Math.PI/4);
2011-02-03 00:01:12 +00:00
// g.fillStyle = c1;
g.fillRect(-rectSize/2, -rectSize/2, rectSize, rectSize);
g.restore();
break;
default:
g.save();
g.translate(x-barSize/2, 0);
2011-02-02 20:51:58 +00:00
//g.fillStyle = c1;
g.fillRect(0, 0, barSize/2, scrubber.height-1);
2011-02-02 20:51:58 +00:00
//g.fillStyle = c2;
g.fillRect(barSize/2, 0, barSize/2, scrubber.height-1);
g.restore();
2011-02-02 18:12:55 +00:00
}
2011-02-02 18:12:55 +00:00
}
}
GUI.Easing = {}
GUI.Easing.Linear = function ( k ) {
return k;
};
GUI.Easing.Hold = function(k) {
return 0;
}
GUI.Easing.QuadraticEaseIn = function ( k ) {
return k * k;
};
GUI.Easing.QuadraticEaseOut = function ( k ) {
return - k * ( k - 2 );
};
GUI.Easing.QuadraticEaseInOut = function ( k ) {
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
return - 0.5 * ( --k * ( k - 2 ) - 1 );
};
GUI.Easing.CubicEaseIn = function ( k ) {
return k * k * k;
};
GUI.Easing.CubicEaseOut = function ( k ) {
return --k * k * k + 1;
};
GUI.Easing.CubicEaseInOut = function ( k ) {
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k;
return 0.5 * ( ( k -= 2 ) * k * k + 2 );
};
GUI.Easing.QuarticEaseIn = function ( k ) {
return k * k * k * k;
};
GUI.Easing.QuarticEaseOut = function ( k ) {
return - ( --k * k * k * k - 1 );
}
GUI.Easing.QuarticEaseInOut = function ( k ) {
if ( ( k *= 2 ) < 1) return 0.5 * k * k * k * k;
return - 0.5 * ( ( k -= 2 ) * k * k * k - 2 );
};
//
GUI.Easing.QuinticEaseIn = function ( k ) {
return k * k * k * k * k;
};
GUI.Easing.QuinticEaseOut = function ( k ) {
return ( k = k - 1 ) * k * k * k * k + 1;
};
GUI.Easing.QuinticEaseInOut = function ( k ) {
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k * k * k;
return 0.5 * ( ( k -= 2 ) * k * k * k * k + 2 );
};
GUI.Easing.SinusoidalEaseIn = function ( k ) {
return - Math.cos( k * Math.PI / 2 ) + 1;
};
GUI.Easing.SinusoidalEaseOut = function ( k ) {
return Math.sin( k * Math.PI / 2 );
};
GUI.Easing.SinusoidalEaseInOut = function ( k ) {
return - 0.5 * ( Math.cos( Math.PI * k ) - 1 );
};
GUI.Easing.ExponentialEaseIn = function ( k ) {
return k == 0 ? 0 : Math.pow( 2, 10 * ( k - 1 ) );
};
GUI.Easing.ExponentialEaseOut = function ( k ) {
return k == 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1;
};
GUI.Easing.ExponentialEaseInOut = function ( k ) {
if ( k == 0 ) return 0;
if ( k == 1 ) return 1;
if ( ( k *= 2 ) < 1 ) return 0.5 * Math.pow( 2, 10 * ( k - 1 ) );
return 0.5 * ( - Math.pow( 2, - 10 * ( k - 1 ) ) + 2 );
};
GUI.Easing.CircularEaseIn = function ( k ) {
return - ( Math.sqrt( 1 - k * k ) - 1);
};
GUI.Easing.CircularEaseOut = function ( k ) {
return Math.sqrt( 1 - --k * k );
};
GUI.Easing.CircularEaseInOut = function ( k ) {
if ( ( k /= 0.5 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);
};
GUI.Easing.ElasticEaseIn = function( k ) {
var s, a = 0.1, p = 0.4;
if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
return - ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
};
GUI.Easing.ElasticEaseOut = function( k ) {
var s, a = 0.1, p = 0.4;
if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
};
GUI.Easing.ElasticEaseInOut = function( k ) {
var s, a = 0.1, p = 0.4;
if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
if ( ( k *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
return a * Math.pow( 2, -10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
};
GUI.Easing.BackEaseIn = function( k ) {
var s = 1.70158;
return k * k * ( ( s + 1 ) * k - s );
};
GUI.Easing.BackEaseOut = function( k ) {
var s = 1.70158;
return ( k = k - 1 ) * k * ( ( s + 1 ) * k + s ) + 1;
};
GUI.Easing.BackEaseInOut = function( k ) {
var s = 1.70158 * 1.525;
if ( ( k *= 2 ) < 1 ) return 0.5 * ( k * k * ( ( s + 1 ) * k - s ) );
return 0.5 * ( ( k -= 2 ) * k * ( ( s + 1 ) * k + s ) + 2 );
};
GUI.Easing.BounceEaseIn = function( k ) {
return 1 - GUI.Easing.BounceEaseOut( 1 - k );
};
GUI.Easing.BounceEaseOut = function( k ) {
if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
return 7.5625 * k * k;
} else if ( k < ( 2 / 2.75 ) ) {
return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
} else if ( k < ( 2.5 / 2.75 ) ) {
return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
} else {
return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
}
};
GUI.Easing.BounceEaseInOut = function( k ) {
if ( k < 0.5 ) return GUI.Easing.BounceEaseIn( k * 2 ) * 0.5;
return GUI.Easing.BounceEaseOut( k * 2 - 1 ) * 0.5 + 0.5;
};