job w/ duration

This commit is contained in:
TreetopFlyer 2021-08-04 14:38:48 -04:00
parent cc6b18f401
commit 465f68d205

View File

@ -57,8 +57,14 @@ var Time =
Jobs:[], Jobs:[],
Stamp:false, Stamp:false,
Queue:false, Queue:false,
Loop:false,
Add:function(inJob) Add:function(inJob)
{ {
if(Time.Loop)
{
console.log("cannot modify queue while processing");
return;
}
if(!Time.Queue) if(!Time.Queue)
{ {
window.requestAnimationFrame(Time.Update); window.requestAnimationFrame(Time.Update);
@ -66,6 +72,19 @@ var Time =
Time.Queue = true; Time.Queue = true;
Time.Jobs.push(inJob); Time.Jobs.push(inJob);
}, },
Remove:function(inJob)
{
if(Time.Loop)
{
console.log("cannot modify queue while processing");
return;
}
var index = Time.Jobs.indexOf(inJob);
if(index > -1)
{
Time.Jobs.splice(index, 1);
}
},
Update:function(inTimestamp) Update:function(inTimestamp)
{ {
var delta; var delta;
@ -78,14 +97,16 @@ var Time =
delta = inTimestamp - Time.Stamp; delta = inTimestamp - Time.Stamp;
Time.Stamp = inTimestamp; Time.Stamp = inTimestamp;
Time.Loop = true;
for(i=0; i<Time.Jobs.length; i++) for(i=0; i<Time.Jobs.length; i++)
{ {
if(!Time.Jobs[i](delta)) if(Time.Jobs[i](delta) === false)
{ {
Time.Jobs.splice(i, 1); Time.Jobs.splice(i, 1);
i--; i--;
} }
} }
Time.Loop = false;
if(Time.Jobs.length > 0) if(Time.Jobs.length > 0)
{ {
@ -99,7 +120,7 @@ var Time =
} }
}; };
function JobConic(inFrom, inRange, inDuration, inHandler) function JobDuration(inDuration, inHandler, inDone)
{ {
var timeCurrent = 0; var timeCurrent = 0;
var timeLimit = inDuration*1000; var timeLimit = inDuration*1000;
@ -109,21 +130,28 @@ function JobConic(inFrom, inRange, inDuration, inHandler)
{ {
timeCurrent += inDelta; timeCurrent += inDelta;
timeMaxed = timeCurrent > timeLimit; timeMaxed = timeCurrent > timeLimit;
if(timeMaxed){ timeCurrent = timeLimit; } if(timeMaxed)
timeRelative = timeCurrent / timeLimit; {
inHandler(inFrom + inRange*Math.sqrt(1 - Math.pow(1-(timeRelative), 2))); inHandler(1);
return !timeMaxed; inDone();
return false;
}
return inHandler(timeCurrent / timeLimit);
}; };
} }
document.querySelector("nav").addEventListener("click", function(inEvent) document.querySelector("nav").addEventListener("click", function(inEvent)
{ {
var href = inEvent.target.getAttribute("href"); var domHtml = document.querySelector("html");
var html = document.querySelector("html"); var domGoal = document.querySelector(inEvent.target.getAttribute("href"));
var goal = document.querySelector(href).getBoundingClientRect().top; var posStart = domHtml.scrollTop - 200;
var posRange = domGoal.getBoundingClientRect().top;
var evtTick = function(inProgress){ domHtml.scrollTop = posStart + posRange*Math.sqrt(1 - Math.pow(1-(inProgress), 2)); };
var evtDone = function(){ Spy.Resume(); };
Time.Add( JobConic(html.scrollTop, goal, 0.4, function(inOutput){ html.scrollTop = inOutput; } ) ); Spy.Suspend();
Time.Add( JobDuration(0.4, evtTick, evtDone) );
inEvent.preventDefault(); inEvent.preventDefault();
}); });
@ -134,11 +162,23 @@ var Spy =
Attribute:"data-spy", Attribute:"data-spy",
Members:[], Members:[],
Defaults:[0, 1, 0, 1], Defaults:[0, 1, 0, 1],
Disabled:false,
Suspend:function()
{
Spy.Disabled = true;
},
Resume:function()
{
Spy.Disabled = false;
Spy.UpdateAll();
},
UpdateAll:function() UpdateAll:function()
{ {
var i, member, aabb, top, bottom, left, right; var i, member, aabb, top, bottom, left, right;
var visible; var visible;
if(Spy.Disabled){ return; }
for(i=0; i<Spy.Members.length; i++) for(i=0; i<Spy.Members.length; i++)
{ {
member = Spy.Members[i]; member = Spy.Members[i];