remenissions Posted April 7, 2012 Share Posted April 7, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/ Share on other sites More sharing options...
remenissions Posted April 8, 2012 Author Share Posted April 8, 2012 Anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1335322 Share on other sites More sharing options...
remenissions Posted April 9, 2012 Author Share Posted April 9, 2012 Bump Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1335647 Share on other sites More sharing options...
remenissions Posted April 10, 2012 Author Share Posted April 10, 2012 ..... Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1335983 Share on other sites More sharing options...
remenissions Posted April 13, 2012 Author Share Posted April 13, 2012 Bump Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1336979 Share on other sites More sharing options...
Jessica Posted April 13, 2012 Share Posted April 13, 2012 Knock it off. Read the rules. Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1337081 Share on other sites More sharing options...
remenissions Posted April 15, 2012 Author Share Posted April 15, 2012 What do you mean? I am just trying to get some help correcting the timing on my script. I have made it very readable and just trying to find some one who might know how to help me fix the timing issue. Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1337449 Share on other sites More sharing options...
remenissions Posted April 18, 2012 Author Share Posted April 18, 2012 ............ Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1338329 Share on other sites More sharing options...
haku Posted April 19, 2012 Share Posted April 19, 2012 I don't have an answer for you, but why not just use jQuery (or another library) and use their animation handler? It will make your life much easier. Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1338790 Share on other sites More sharing options...
remenissions Posted April 20, 2012 Author Share Posted April 20, 2012 Blah, I've never liked Jquery. Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1338993 Share on other sites More sharing options...
remenissions Posted April 20, 2012 Author Share Posted April 20, 2012 Not that its bad at all I know its easy and simple, I just like to learn things myself. Rather learn how to do it then learn how to use their functions Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1338994 Share on other sites More sharing options...
haku Posted April 20, 2012 Share Posted April 20, 2012 I used to say the same thing - and did everything in pure javascript... until I learned jQuery. It's still good to know the basics, and occasionally I use them when I need them, but jQuery is extremely lightweight, and very handy. I've never looked back. Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1339122 Share on other sites More sharing options...
remenissions Posted April 21, 2012 Author Share Posted April 21, 2012 I'll take your word on it and give it a try. Ty Quote Link to comment https://forums.phpfreaks.com/topic/260522-animation-handler/#findComment-1339363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.