function animation(duration,from,to,handler){
const dis=to-from;
const speed=dis/duration;
const startTime=Date.now();
let value=from;
handler(value);
function _run(){
const now=Date.now();
const time=now-startTime;
if(time>=duration){
value=to;
return
}
const d=time*speed;
value=from+d;
handler(value);
requestAnimationFrame(_run);
}
requestAnimationFrame(_run);
}
function debounce(fun,delay){
let timerId=null;
return function(...args){
timerId && clearTimeout(timerId);
timerId=setTimeout(()=>{
fun.apply(this,args)
},delay)
}
}
function paralleltask(tasks,parallelCount){
return new Promise(resolve=>{
if(tasks.length===0){
resolve();
return;
}
let nextIndex=0;
let taskFinish=0;
function _run(){
const task=tasks[nextIndex];
nextIndex++;
task().then(()=>{
taskFinish++;
if(nextIndex<tasks.length){
_run();
}else{
if(taskFinish===tasks.length){
resolve();
}
}
})
}
for(let i=0;i<parallelCount && i<tasks.length;i++){
_run();
}
})
}
function groupby(arr,generatekey){
const newObj={};
for(let item of arr){
const key=generatekey(item);
if(!newObj[key]){
newObj[key]=[]
}
newObj[key].push(item)
}
return newObj
}
function parallelRendertask(taskdatas,renderTask){
if(taskdatas.length===0){
return;
}
let i=0;
function _run(){
if(i===taskdatas.length){
return;
}
requestIdleCallback(idle=>{
while(idle.timeRemaining()>0 && i<taskdatas.length){
//做相应的渲染任务
renderTask(taskdatas[i]);
i++;
}
_run;
})
}
_run();
}