You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2 line
10 KiB

  1. (function(){(function(){for(var t=0,e=["ms","moz","webkit","o"],i=0;i<e.length&&!window.requestAnimationFrame;++i)window.requestAnimationFrame=window[e[i]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[e[i]+"CancelAnimationFrame"]||window[e[i]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(s,o){var r=new Date().getTime(),a=Math.max(0,16-(r-t)),h=window.setTimeout(function(){s(r+a)},a);return t=r+a,h}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(s){clearTimeout(s)})})(),window.Typewriter=function(t,e){if(this._settings={cursorAnimationPaused:!1,opacityIncreasing:!1,currentOpacity:1,delayedQue:[],delayItemsCount:0,eventQue:[],calledEvents:[],eventRunning:!1,timeout:!1,delayExecution:!1,fps:.06,typingFrameCount:0,stringToTypeHTMLArray:[],currentTypedCharacters:[],typing:!1,usedIDs:[],charAmountToDelete:!1,userOptions:{},eventLoopRerun:0},!t)return console.error("Please choose an DOM element so that type writer can display itself.");if(typeof e!="object")return console.error("Typewriter only accepts the options as an object.");this._settings.userOptions=e,this.default_options={strings:!1,cursorClassName:"typewriter-cursor",cursor:"|",animateCursor:!0,blinkSpeed:50,typingSpeed:"natural",deleteSpeed:"natural",charSpanClassName:"typewriter-char",wrapperClassName:"typewriter-wrapper",loop:!1,autoStart:!1,devMode:!1},this.options=this._setupOptions(e),this.el=t,this._setupTypwriterWrapper(),this._startCursorAnimation(),this.options.autoStart===!0&&this.options.strings&&this.typeOutAllStrings()};var n=window.Typewriter.prototype;n.stop=function(){return this._addToEventQue(this._stopEventLoop),this},n.start=function(){return this._startEventLoop(),this},n.rerun=function(){return this._addToEventQue(this._rerunCalledEvents),this},n.typeString=function(t){return!t||typeof t!="string"?console.error("Please enter a string as the paramater."):(t=this._getCharacters(t),this._addToEventQue([this._typeCharacters,[t]]),this)},n.deleteAll=function(){return this._addToEventQue([this._deleteChars,["all"]]),this},n.deleteChars=function(t){return this._addToEventQue([this._deleteChars,[t]]),this},n.pauseFor=function(t){return this._addToEventQue([this._pauseFor,[t]]),this},n.typeOutAllStrings=function(){var t=this._getStringsAsCharsArray();if(t.length===1)this._typeCharacters(t[0]);else for(var e=0,i=t.length;e<i;e++)this._addToEventQue([this._typeCharacters,[t[e]]]),this.pauseFor(this._randomInteger(1500,2500)),this.deleteAll(),this.pauseFor(this._randomInteger(1500,2500));return this},n.changeSettings=function(t){return!t&&typeof t!="object"?console.error("Typewriter will only accept an object as the settings."):(this._addToEventQue([this._changeSettings,[JSON.stringify(t)]]),this)},n.changeBlinkSpeed=function(t){return!t&&typeof t!="number"?console.error("Please enter a number for the new blink speed."):(this.changeSettings({blinkSpeed:t}),this)},n.changeTypingSpeed=function(t){return!t&&typeof t!="number"?console.error("Please enter a number for the new typing speed."):(this.changeSettings({typingSpeed:t}),this)},n.changeDeleteSpeed=function(t){return!t&&typeof t!="number"?console.error("Please enter a number for the new delete speed."):(this.changeSettings({changeDeleteSpeed:t}),this)},n._rerunCalledEvents=function(){0<this._settings.currentTypedCharacters.length?(this.deleteAll(),this._resetEventLoop("rerunCalledEvents")):(this._settings.eventQue=this._settings.calledEvents,this._settings.calledEvents=[],this.options=this._setupOptions(this._settings.userOptions),this._settings.usedIDs=[],this.charAmountToDelete=!1,this._startEventLoop())},n._deleteChars=function(t){return t&&(this._settings.charAmountToDelete=t),this._deletingCharIdsAnimation=window.requestAnimationFrame(this._deletingCharAnimationFrame.bind(this)),this},n._pauseFor=function(t){var e=this;e._settings.eventRunning=!0,setTimeout(function(){e._resetEventLoop("pauseFor")},t)},n._changeSettings=function(t){this.options=this._setupOptions(JSON.parse(t[0])),this._resetEventLoop("changeSettings"),this.options.devMode&&console.log("New settings",this.options)},n._deletingCharAnimationFrame=function(){var t=this,e=this.options.deleteSpeed,i=t.options.wrapperClassName,s=t._settings.currentTypedCharacters,o=t._settings.charAmountToDelete;if(!t._settings.charAmountToDelete||t._settings.charAmountToDelete===0||s===0)return t._resetEventLoop("deletingCharAnimationFrame"),!0;e=="natural"&&(e=t._randomInteger(50,150)),o=="all"&&(o=s.length,t._settings.charAmountToDelete=o),setTimeout(function(){if(t._settings.charAmountToDelete){var r=s.length-1,a=s[r];t._settings.currentTypedCharacters.splice(r,1),(r=document.getElementById(a))&&(t.el.querySelector("."+i).removeChild(r),t._settings.charAmountToDelete=o-1,t.options.devMode&&console.log("Deleted char with ID",a))}t._deletingCharIdsAnimation=window.requestAnimationFrame(t._deletingCharAnimationFrame.bind(t))},e)},n._setupOptions=function(t){var e={},i;for(i in this.default_options)e[i]=this.default_options[i];if(this._settings.userOptions)for(i in this._settings.userOptions)e[i]=this._settings.userOptions[i];for(i in t)e[i]=t[i];return e},n._addToEventQue=function(t){this._settings.eventQue.push(t),0<this._settings.eventQue.length&&!this._settings.eventRunning&&this.options.autoStart&&this._startEventLoop()},n._startEventLoop=function(){if(this.options.devMode&&console.log("Event loop started."),!this._settings.eventRunning){if(0<this._settings.eventQue.length){this.eventLoopRerun=0;var t=this._settings.eventQue[0];typeof t=="function"?(this._settings.eventRunning=!0,this._settings.calledEvents.push(t),this._settings.eventQue.splice(0,1),t.call(this),this.options.devMode&&console.log("Event started.")):t instanceof Array&&typeof t[0]=="function"&&t[1]instanceof Array&&(this._settings.eventRunning=!0,this._settings.calledEvents.push(t),this._settings.eventQue.splice(0,1),t[0].call(this,t[1]),this.options.devMode&&console.log("Event started."))}this._eventQueAnimation=window.requestAnimationFrame(this._startEventLoop.bind(this))}if(!this._settings.eventRunning&&0>=this._settings.eventQue.length){var e=this;e._stopEventLoop(),setTimeout(function(){e.options.loop&&(e.eventLoopRerun++,e.options.devMode&&console.log("Before Loop State",e._settings),4<e.eventLoopRerun?(console.error("Maximum amount of loop retries reached."),e._stopEventLoop()):(e.options.devMode&&console.log("Looping events."),e._rerunCalledEvents()))},1e3)}},n._resetEventLoop=function(t){t=t||"Event",this._settings.eventRunning=!1,this._startEventLoop(),this.options.devMode&&console.log(t,"Finished")},n._stopEventLoop=function(){window.cancelAnimationFrame(this._eventQueAnimation),this.options.devMode&&console.log("Event loop stopped.")},n._setupTypwriterWrapper=function(){var t=this.options.wrapperClassName,e=document.createElement("span");e.className=t,this.el.innerHTML="",this.el.appendChild(e)},n._typeCharacters=function(t){return this._settings.stringToTypeHTMLArray=this._convertCharsToHTML(t),this._typingAnimation=window.requestAnimationFrame(this._typingAnimationFrame.bind(this,t.length)),this},n._typingAnimationFrame=function(t){var e=this,i=this.options.typingSpeed,s=e.options.wrapperClassName;if(e._settings.stringToTypeHTMLArray.length==0)return window.cancelAnimationFrame(e._typingAnimation),this._resetEventLoop("typingAnimationFrame"),!0;i=="natural"&&(i=this._randomInteger(50,150)),setTimeout(function(){var o=e._settings.stringToTypeHTMLArray[0];e.el.querySelector("."+s).appendChild(o.el),e._settings.currentTypedCharacters.push(o.id),e._settings.stringToTypeHTMLArray.splice(0,1),e._typingAnimation=window.requestAnimationFrame(e._typingAnimationFrame.bind(e,t)),e.options.devMode&&console.log("Typed",o)},i)},n._convertCharsToHTML=function(t){var e=[],i=this.options.charSpanClassName;t=t[0];for(var s=0,o=t.length;s<o;s++){var r=document.createElement("span"),a=this._generateUniqueID();r.id=a,r.className=i+" typewriter-item-"+s,r.innerHTML=t[s],e.push({id:a,el:r})}return e},n._getCharacters=function(t){return typeof t!="string"?!1:t.split("")},n._getStringsAsCharsArray=function(){var t=typeof this.options.strings=="string";if(!(this.options.strings instanceof Array))return t?[this.options.strings.split("")]:console.error("Typewriter only accepts strings or an array of strings as the input.");for(var t=[],e=0,i=this.options.strings.length;e<i;e++){var s=this._getCharacters(this.options.strings[e]);if(!s){console.error("Please enter only strings.");break}t.push(s)}return t},n._cursorAnimationFrame=function(){if(!this._settings.cursorAnimationPaused){var t=.001*this.options.blinkSpeed,e=this.el.querySelector(".typewriter-cursor");this._settings.opacityIncreasing==1&&(1<=this._settings.currentOpacity&&(this._settings.opacityIncreasing=!1,this._settings.currentOpacity=1),this._settings.currentOpacity+=t),this._settings.opacityIncreasing==0&&(0>=this._settings.currentOpacity&&(this._settings.opacityIncreasing=!0,this._settings.currentOpacity=0),this._settings.currentOpacity-=t),e.style.opacity=this._settings.currentOpacity,this._cursorAnimation=window.requestAnimationFrame(this._cursorAnimationFrame.bind(this))}},n._startCursorAnimation=function(){var t=this.options.cursor,e=this.options.cursorClassName,i=document.createElement("span");i.className=e,i.innerHTML=t,this.el.appendChild(i),this.options.animateCursor&&(this._cursorAnimation=window.requestAnimationFrame(this._cursorAnimationFrame.bind(this)))},n._pauseCursorAnimation=function(){this._settings.cursorAnimationPaused||(window.cancelAnimationFrame(this._cursorAnimation),this._settings.cursorAnimationPaused=!0)},n._restartCursorAnimation=function(){if(!this._settings.cursorAnimationPaused)return console.error("Cursor animation is already running.");this._settings.cursorAnimationPaused=!1,this._cursorAnimation=window.requestAnimationFrame(this._cursorAnimationFrame.bind(this))},n._randomInteger=function(t,e){return Math.floor(Math.random()*(e-t+1))+t},n._randomID=function(){for(var t="",e=0;e<this._randomInteger(5,15);e++)t+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".charAt(Math.floor(62*Math.random()));return t},n._generateUniqueID=function(){var t=this._randomID();return this._settings.usedIDs.indexOf(t)==-1?(this._settings.usedIDs.push(t),t):this._generateUniqueID.call(this)}})();