Jump to content

Animation Handler


remenissions

Recommended Posts

Hey guys, I'm trying to make an animation handler that I can use in any sittuation. It works, but the only problem I am having is that because the progress is determined by time it will not start at the starting point I give it, I tried to make a work around it but then it messes up the duration. For instance, if I want it to go from 200px to 400px in 1 second the time will keep going but the animation won't begin until it gets from 0 to 200px, which sets the timing off. I've messed with it for manyyyyyyyyyyyy hours trying to figure out how to get this to work. Just no luck yet.. Any help would be greatly appreciated!!!

 

/* Function : animate(element, control, startPos, endPos, duration, delta); */

function animation(curObj) {
	var start = new Date;		

	/* -Animation Interval- */
		var animation_Interval = setInterval(function() {

			/* -Determine current duration- */					
				var progress = (new Date - curObj.start)/curObj.duration;	

				document.getElementById("testObj").innerHTML = progress + "<br><br>:"+progress+":<br><br>" + document.getElementById("testObj").style.height;
			/* -/Determine current duration- */

			/* -Prevents over-animating- */
				if(progress > 1)
					progress = 1;
			/* -/Prevents over-animating- */

			/* -Animate element- */
				var delta = curObj.delta(progress);
				curObj.control(delta);
			/* -/Animate element- */				

			/* -End animation- */
				if(progress == 1) {
					clearInterval(animation_Interval);
					document.getElementById("testObj").innerHTML = progress + "<br><br>:"+progress+":<br><br>" + document.getElementById("testObj").style.height;
				}
			/* -/End animation- */

		}, curObj.delay || 10);
	/* -/Animation Interval- */

}

function animate(element, control, startPos, endPos, duration, delta) {
	animation({
		id: element,
		delay: 10, 
		start: new Date,
		end: endPos,
		duration: duration || 1000,
		delta: delta || linear,
		control_Id: control,
		control: function(delta) { 
			animation_Control(element, control, startPos, endPos, delta);
		}
	});

}

function animation_Control(element, control, startPos, endPos, delta) {

	/* -Determine increase or decrease && set temporary position- */
		var tempPos = startPos < endPos ?
				endPos*delta > startPos ?
					endPos*delta : 
						startPos : 
				(startPos - (startPos*delta)) > endPos ?
					startPos - (startPos*delta) : 
				endPos;  
	/* -/Determine increase or decrease && set temporary position- */

	/* -Set position- */ 
		var obj = document.getElementById(element);	 
		switch(control) {
			case "width":
				obj.style.width = tempPos + "px";
				break;
			case "height":
				obj.style.height = tempPos + "px";
				break;
			case "left":
				obj.style.left = tempPos + "px";
				break;
			case "right":
				obj.style.right = tempPos + "px";
				break;
			case "top":
				obj.style.top = tempPos + "px";
				break;
			case "bottom":
				obj.style.bottom = tempPos + "px";
				break;
			case "opacity":
				obj.style.opacity = tempPos/10;
				obj.style.filter = 'alpha(opacity=' + tempPos*10 + ')';
				break;
		}
	/* -/Set position- */ 

}

function linear(progress) {
  		return progress;
}

function quad(progress) {
	return Math.pow(progress, 2);
}

function circ(progress) {
   return 1 - Math.sin(Math.acos(progress));
}

function back(progress, x) {
   return Math.pow(progress, 2) * ((x + 1) * progress - x);
}

function bounce(progress) {
	for(var a = 0, b = 1, result; 1; a += b, b /= 2) {
    		if (progress >= (7 - 4 * a) / 11) {
      		return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2);
    		}
  		}
}

var bounceEaseOut = makeEaseOut(bounce);
function makeEaseOut(delta) {  
	return function(progress) {
    		return 1 - delta(1 - progress);
  		}	
}

<button onclick="animate('testObj', 'height', 100, 0, 1000);">Open</button>

<button onclick="animate('testObj', 'height', 350, 100, 1000, linear);">Close</button>

 

<div id="testObj" style="color: white; background-color: blue; width: 80px; height: 50px; position: absolute; left: 10px;"></div>

 

Link to comment
https://forums.phpfreaks.com/topic/260522-animation-handler/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.