-
Notifications
You must be signed in to change notification settings - Fork 0
/
ketikin.min.js
1 lines (1 loc) · 1.57 KB
/
ketikin.min.js
1
const ketikin=(e,t)=>{fenceSpeed=(e=>e=100-(e=(e=e>100?100:e)<0?0:e)),getSeq=(e=>e.getAttribute("ketikin-seq")),getCursor=(e=>e.querySelector("#"+getSeq(e))||document.createElement("cursor")),animateCursor=(e=>{getCursor(e).animate([{opacity:.5}],{duration:1e3,iterations:1/0})}),removeCursor=(e=>{getCursor(e).remove()}),addCursor=(e=>{e.innerHTML=e.innerHTML+'<span id="{id}">|</span>'.replace("{id}",getSeq(e))}),addTypingChar=((e,t)=>{e.innerHTML=e.innerHTML+t}),swapTypingText=((e,t)=>{e.innerHTML=t}),type=((e,t,r)=>{setTimeout(()=>removeCursor(e)|addTypingChar(e,t)|addCursor(e),r)}),backSpace=((e,t,r)=>(setTimeout(()=>removeCursor(e)|swapTypingText(e,t)|addCursor(e),r),t.substring(0,t.length-1))),arrangeExecutionTime=((e,r)=>e+Math.floor(Math.random()*fenceSpeed(t.speed))+r),orchestrate=((e,t,r,o)=>{for(const r of t)type(e,r,o),o=arrangeExecutionTime(o,5);if(r){let r=5;for(o+=1e3;t;)t=backSpace(e,t,o),o=arrangeExecutionTime(o,r),r=Math.floor(.7*r);setTimeout(()=>swapTypingText(e,t)|addCursor(e),o)}return o}),playOrchestration=((e,t,r)=>{let o=0,n=!1;t.forEach((s,a)=>{n=a<t.length-1&&!r.loop||r.loop,o=orchestrate(e,s,n,o)+1e3}),r.loop?setTimeout(()=>playOrchestration(e,t,r),o):setTimeout(()=>animateCursor(e),o)}),setupOptions=(e=>Object.assign({texts:null,speed:0,loop:!1},e)),setupTexts=((e,t)=>(t.texts||[e.innerText]).filter(e=>e)),setupElement=(e=>(e.setAttribute("ketikin-seq","seq-"+Math.random().toString(36).substr(2)),e.innerHTML="",e)),document.querySelectorAll(e).forEach(e=>{const r=setupOptions(t),o=setupTexts(e,r);o.length>0&&playOrchestration(setupElement(e),o,r)})};