diff --git a/README.md b/README.md index 8dd9d9ac..9831c17f 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,14 @@ The first step is to create a new theater instance. Available options are: name|default|description ----|-------|----------- -erase|`true`|Whether or not to play the erase animation before writing the following speech. +erase|`true`|Whether or not to play the erase animation before writing the next speech. autoplay|`true`|If true, plays the scenario automatically. Otherwise, you'll have to call `theater.play()` locale|`"detect"`|Customize keyboards, see [Keyboards & Localization](#keyboards--localization). +minSpeed|50|Minimum delay between each characters (the lower, the faster) +maxSpeed|600|Maximum delay between each characters (the greater, the slower) ```javascript -var theater = new TheaterJS({ erase: true, autoplay: true, locale: "detect" }); +var theater = new TheaterJS({ erase: true, autoplay: true, locale: "detect", minSpeed: 50, maxSpeed: 600 }); ``` @@ -440,6 +442,11 @@ The `emit` method accepts up to three arguments. The first being the "scope", th # Changelog +* 1.3.0 + * minSpeed and maxSpeed are now configurable + * default maxSpeed decreased to 600 (instead of 1000) + * mistakes are repeatable + * erase's speed varies just as "say" scenes * 1.2.2 Registration as a bower component `bower install theaterjs` * 1.2.1 Fix html support * 1.2.0 Adding support for html within the speeches diff --git a/bower.json b/bower.json index f02caa42..a93c9ef2 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "TheaterJS", - "version": "1.2.2", + "version": "1.3.0", "main": "build/theater.js", "license": "MIT", "ignore": [ diff --git a/build/theater.js b/build/theater.js index fa777e92..c54b0b8f 100644 --- a/build/theater.js +++ b/build/theater.js @@ -53,7 +53,7 @@ TheaterJS.prototype = { constructor: TheaterJS, - version: "1.2.0", + version: "1.3.0", keyboards: {}, diff --git a/build/theater.min.js b/build/theater.min.js index 411d5a87..1e153400 100644 --- a/build/theater.min.js +++ b/build/theater.min.js @@ -1 +1 @@ -!function(e,t){function r(e){var t=this,r={autoplay:!0,erase:!0,locale:"detect",minSpeed:50,maxSpeed:600};t.options=t.utils.merge(r,e||{}),"detect"===t.options.locale&&(t.options.locale=(window.navigator.languages||["en"])[0].split("-")[0]),t.keyboards[t.options.locale]||(t.options.locale="en"),t.utils.keyboard=t.keyboards[t.options.locale],t.events={},t.scene=-1,t.scenario=[],t.casting={},t.current={},t.state="ready"}r.prototype={constructor:r,version:"1.2.0",keyboards:{},set:function(e,t){var r=this,n=r.current.voice,i=r.current.html||[];return t[0]=r.utils.injectHTML(t[0],i),r.current.model=r.utils.injectHTML(e,i),r.utils.isFunction(n)?n.apply(r,t):n.innerHTML=r.current.model,r},getSayingSpeed:function(e){var t=this;t.utils.isNumber(e)||(e=0);var r=t.current.speed+e;r>1&&(r=1);var n=t.utils.randomFloat(r,1);return t.utils.getPercentageBetween(t.options.maxSpeed,t.options.minSpeed,n)},recover:function(e){var t=this,r=t.maxMistakes();return e>=r||t.utils.randomFloat(0,1)<=t.current.accuracy},maxMistakes:function(){var e=this,t=.8-e.current.accuracy;return 0>=t?1:10*t},isMistaking:function(){var e=this;return e.utils.randomFloat(0,.8)>e.current.accuracy},utils:{keyboard:{},copy:function(e){var t,r={};for(t in e)e.hasOwnProperty(t)&&(r[t]=e[t]);return r},isFunction:function(e){return"function"==typeof e},isString:function(e){return"string"==typeof e},isNumber:function(e){return"number"==typeof e},isObject:function(e){return e instanceof Object},stripHTML:function(e){return e.replace(/(<([^>]+)>)/gi,"")},mapHTML:function(e){for(var t,r,n=/<[^>]+>/gi,i=[],a=[];t=n.exec(e);)r={tagName:t[0],position:t.index},"/"===r.tagName.charAt(1)?r.opener=a.pop():"/"!==r.tagName.charAt(r.tagName.length-2)&&a.push(r),i.push(r);return i},injectHTML:function(e,t){for(var r,n=0,i=t.length;i>n;n++)r=t[n],r.positionn;n++){t=e[n];for(var a=0,s=t.length;s>a;a++)r[t[a]]={x:a,y:n}}return r},merge:function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e},getPercentageBetween:function(e,t,r){return e-e*r+t*r},randomCharNear:function(e){var t=this,r=t.mapKeyboard(t.keyboard),n=1,i=[],a=!!e.match(/[A-Z]/);e=e.toLowerCase();var s,o,c=r[e]||[];for(s in r)r.hasOwnProperty(s)&&s!==e&&(o=r[s],Math.abs(c.x-o.x)<=n&&Math.abs(c.y-o.y)<=n&&i.push(s));var u=i.length>0?i[t.randomNumber(0,i.length-1)]:t.randomChar();return a?u.toUpperCase():u},randomChar:function(){var e=this,t=e.keyboard.join("");return t.charAt(e.randomNumber(0,t.length-1))},randomNumber:function(e,t){return Math.floor(Math.random()*(t-e+1))+e},randomFloat:function(e,t){return Math.round(10*(Math.random()*(t-e)+e))/10},hasClass:function(e,t){return e.classList?e.classList.contains(t):new RegExp("(^| )"+t+"( |$)","gi").test(e.className)},addClass:function(e,t){e.classList?e.classList.add(t):e.className+=" "+t},removeClass:function(e,t){e.classList?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\b)"+t.split(" ").join("|")+"(\\b|$)","gi")," ")}},train:function(e){var t=this,r={experience:.6,voice:function(e){console.log(e)},model:""};return e=t.utils.merge(r,e),t.utils.isNumber(e.speed)||(e.speed=e.experience),t.utils.isNumber(e.accuracy)||(e.accuracy=e.experience),t.utils.isNumber(e.invincibility)||(e.invincibility=10*e.accuracy),e},describe:function(e,r,n){var i=this,a={name:e};return i.utils.isNumber(r)?a.experience=r:i.utils.isObject(r)&&(a=i.utils.merge(a,r)),void 0!==n&&(a.voice=i.utils.isString(n)?t.querySelector(n):n),i.casting[e]=i.train(a),i},write:function(){for(var e,t=this,r=Array.prototype.splice.apply(arguments,[0]),n=0,i=r.length;i>n;n++)if(e=r[n],t.utils.isString(e)){var a=e.split(":"),s=a.length>1&&"\\"!==a[0].charAt(a[0].length-1),o=s?a.shift().trim():null,c=a.join(":").replace(/\\:/g,":");s&&t.write({name:"actor",args:[o]}),t.options.erase&&s&&t.write({name:"erase"}),t.write({name:"say",args:[c,!s]})}else"number"==typeof e?t.write(0>e?{name:"erase",args:[e]}:{name:"wait",args:[e]}):"function"==typeof e?t.write({name:"call",args:[e]}):e instanceof Object&&t.scenario.push(e);return t.options.autoplay&&t.play(),t},play:function(e){var t=this;return e===!0&&(t.scene=-1),"ready"===t.state&&t.next(),t},on:function(e,t){var r=this;e=e.split(",");for(var n,i=0,a=e.length;a>i;i++)n=e[i]=e[i].trim(),(r.events[n]||(r.events[n]=[])).push(t);return r},emit:function(e,t,r){var n=this;n.utils.isString(t)?void 0!==t&&void 0===r&&(r=t):t=void 0;var i=e+(t?":"+t:"");return n.trigger(i,r).trigger("*",[i].concat(r)),n},trigger:function(e,t){var r=this,n=r.events[e]||[];t instanceof Array||(t=[t]);for(var i=0,a=n.length;a>i;i++)n[i].apply(r,[e].concat(t));return r},call:function(e,t){var r=this;return e.apply(r),t?r:r.next()},next:function(){var e=this,t=e.scenario[e.scene];if(t&&e.emit(t.name,"end",[t.name].concat(t.args)),e.scene+1>=e.scenario.length)e.state="ready";else{e.state="playing";var r=e.scenario[++e.scene];e.emit(r.name,"start",[r.name].concat(r.args)),e[r.name].apply(e,r.args)}return e},actor:function(e){var t=this;return t.current=t.casting[e],t.next()},say:function(e,t){var r,n,i=this,a=0,s=!1,o=i.current.invincibility;i.current.html=i.utils.mapHTML(e),e=i.utils.stripHTML(e),t?(n=i.current.model,r=i.current.model.length-1,e=n+e):(n=i.current.model="",r=-1);var c=setTimeout(function u(){var t,l,p=n.charAt(r);a>0&&(r>=e.length||s||i.recover(a))?(o=i.current.invincibility,s=!0,t=null,l=n=n.substr(0,r),a--,r--):(s=!1,r++,t=e.charAt(r),r0)&&a++,l=n+=t),i.set(l,[l,t,p,e]),a>0||re?n+1+e:0;setTimeout(function a(){var e=r.charAt(n),s=r.substr(0,--n);t.set(s,[s,null,e,s]),r=s,n>=i?setTimeout(a,t.getSayingSpeed(.2,!0)):t.next()},t.getSayingSpeed(.2))}return t},wait:function(e){var t=this;return setTimeout(function(){t.next()},e),t}},r.prototype.keyboards.en=["qwertyuiop","asdfghjkl","zxcvbnm"],r.prototype.keyboards.fr=["azertyuiop","qsdfghjklm","wxcvbn"],e.TheaterJS=r}(window,document); \ No newline at end of file +!function(e,t){function r(e){var t=this,r={autoplay:!0,erase:!0,locale:"detect",minSpeed:50,maxSpeed:600};t.options=t.utils.merge(r,e||{}),"detect"===t.options.locale&&(t.options.locale=(window.navigator.languages||["en"])[0].split("-")[0]),t.keyboards[t.options.locale]||(t.options.locale="en"),t.utils.keyboard=t.keyboards[t.options.locale],t.events={},t.scene=-1,t.scenario=[],t.casting={},t.current={},t.state="ready"}r.prototype={constructor:r,version:"1.3.0",keyboards:{},set:function(e,t){var r=this,n=r.current.voice,i=r.current.html||[];return t[0]=r.utils.injectHTML(t[0],i),r.current.model=r.utils.injectHTML(e,i),r.utils.isFunction(n)?n.apply(r,t):n.innerHTML=r.current.model,r},getSayingSpeed:function(e){var t=this;t.utils.isNumber(e)||(e=0);var r=t.current.speed+e;r>1&&(r=1);var n=t.utils.randomFloat(r,1);return t.utils.getPercentageBetween(t.options.maxSpeed,t.options.minSpeed,n)},recover:function(e){var t=this,r=t.maxMistakes();return e>=r||t.utils.randomFloat(0,1)<=t.current.accuracy},maxMistakes:function(){var e=this,t=.8-e.current.accuracy;return 0>=t?1:10*t},isMistaking:function(){var e=this;return e.utils.randomFloat(0,.8)>e.current.accuracy},utils:{keyboard:{},copy:function(e){var t,r={};for(t in e)e.hasOwnProperty(t)&&(r[t]=e[t]);return r},isFunction:function(e){return"function"==typeof e},isString:function(e){return"string"==typeof e},isNumber:function(e){return"number"==typeof e},isObject:function(e){return e instanceof Object},stripHTML:function(e){return e.replace(/(<([^>]+)>)/gi,"")},mapHTML:function(e){for(var t,r,n=/<[^>]+>/gi,i=[],a=[];t=n.exec(e);)r={tagName:t[0],position:t.index},"/"===r.tagName.charAt(1)?r.opener=a.pop():"/"!==r.tagName.charAt(r.tagName.length-2)&&a.push(r),i.push(r);return i},injectHTML:function(e,t){for(var r,n=0,i=t.length;i>n;n++)r=t[n],r.positionn;n++){t=e[n];for(var a=0,s=t.length;s>a;a++)r[t[a]]={x:a,y:n}}return r},merge:function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e},getPercentageBetween:function(e,t,r){return e-e*r+t*r},randomCharNear:function(e){var t=this,r=t.mapKeyboard(t.keyboard),n=1,i=[],a=!!e.match(/[A-Z]/);e=e.toLowerCase();var s,o,c=r[e]||[];for(s in r)r.hasOwnProperty(s)&&s!==e&&(o=r[s],Math.abs(c.x-o.x)<=n&&Math.abs(c.y-o.y)<=n&&i.push(s));var u=i.length>0?i[t.randomNumber(0,i.length-1)]:t.randomChar();return a?u.toUpperCase():u},randomChar:function(){var e=this,t=e.keyboard.join("");return t.charAt(e.randomNumber(0,t.length-1))},randomNumber:function(e,t){return Math.floor(Math.random()*(t-e+1))+e},randomFloat:function(e,t){return Math.round(10*(Math.random()*(t-e)+e))/10},hasClass:function(e,t){return e.classList?e.classList.contains(t):new RegExp("(^| )"+t+"( |$)","gi").test(e.className)},addClass:function(e,t){e.classList?e.classList.add(t):e.className+=" "+t},removeClass:function(e,t){e.classList?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\b)"+t.split(" ").join("|")+"(\\b|$)","gi")," ")}},train:function(e){var t=this,r={experience:.6,voice:function(e){console.log(e)},model:""};return e=t.utils.merge(r,e),t.utils.isNumber(e.speed)||(e.speed=e.experience),t.utils.isNumber(e.accuracy)||(e.accuracy=e.experience),t.utils.isNumber(e.invincibility)||(e.invincibility=10*e.accuracy),e},describe:function(e,r,n){var i=this,a={name:e};return i.utils.isNumber(r)?a.experience=r:i.utils.isObject(r)&&(a=i.utils.merge(a,r)),void 0!==n&&(a.voice=i.utils.isString(n)?t.querySelector(n):n),i.casting[e]=i.train(a),i},write:function(){for(var e,t=this,r=Array.prototype.splice.apply(arguments,[0]),n=0,i=r.length;i>n;n++)if(e=r[n],t.utils.isString(e)){var a=e.split(":"),s=a.length>1&&"\\"!==a[0].charAt(a[0].length-1),o=s?a.shift().trim():null,c=a.join(":").replace(/\\:/g,":");s&&t.write({name:"actor",args:[o]}),t.options.erase&&s&&t.write({name:"erase"}),t.write({name:"say",args:[c,!s]})}else"number"==typeof e?t.write(0>e?{name:"erase",args:[e]}:{name:"wait",args:[e]}):"function"==typeof e?t.write({name:"call",args:[e]}):e instanceof Object&&t.scenario.push(e);return t.options.autoplay&&t.play(),t},play:function(e){var t=this;return e===!0&&(t.scene=-1),"ready"===t.state&&t.next(),t},on:function(e,t){var r=this;e=e.split(",");for(var n,i=0,a=e.length;a>i;i++)n=e[i]=e[i].trim(),(r.events[n]||(r.events[n]=[])).push(t);return r},emit:function(e,t,r){var n=this;n.utils.isString(t)?void 0!==t&&void 0===r&&(r=t):t=void 0;var i=e+(t?":"+t:"");return n.trigger(i,r).trigger("*",[i].concat(r)),n},trigger:function(e,t){var r=this,n=r.events[e]||[];t instanceof Array||(t=[t]);for(var i=0,a=n.length;a>i;i++)n[i].apply(r,[e].concat(t));return r},call:function(e,t){var r=this;return e.apply(r),t?r:r.next()},next:function(){var e=this,t=e.scenario[e.scene];if(t&&e.emit(t.name,"end",[t.name].concat(t.args)),e.scene+1>=e.scenario.length)e.state="ready";else{e.state="playing";var r=e.scenario[++e.scene];e.emit(r.name,"start",[r.name].concat(r.args)),e[r.name].apply(e,r.args)}return e},actor:function(e){var t=this;return t.current=t.casting[e],t.next()},say:function(e,t){var r,n,i=this,a=0,s=!1,o=i.current.invincibility;i.current.html=i.utils.mapHTML(e),e=i.utils.stripHTML(e),t?(n=i.current.model,r=i.current.model.length-1,e=n+e):(n=i.current.model="",r=-1);var c=setTimeout(function u(){var t,l,p=n.charAt(r);a>0&&(r>=e.length||s||i.recover(a))?(o=i.current.invincibility,s=!0,t=null,l=n=n.substr(0,r),a--,r--):(s=!1,r++,t=e.charAt(r),r0)&&a++,l=n+=t),i.set(l,[l,t,p,e]),a>0||re?n+1+e:0;setTimeout(function a(){var e=r.charAt(n),s=r.substr(0,--n);t.set(s,[s,null,e,s]),r=s,n>=i?setTimeout(a,t.getSayingSpeed(.2,!0)):t.next()},t.getSayingSpeed(.2))}return t},wait:function(e){var t=this;return setTimeout(function(){t.next()},e),t}},r.prototype.keyboards.en=["qwertyuiop","asdfghjkl","zxcvbnm"],r.prototype.keyboards.fr=["azertyuiop","qsdfghjklm","wxcvbn"],e.TheaterJS=r}(window,document); \ No newline at end of file diff --git a/package.json b/package.json index c40b20a9..b8218288 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "TheaterJS", - "version": "1.2.2", + "version": "1.3.0", "description": "Typing effect mimicking human behavior.", "repository": { "type": "git", diff --git a/src/theater.js b/src/theater.js index fa777e92..c54b0b8f 100644 --- a/src/theater.js +++ b/src/theater.js @@ -53,7 +53,7 @@ TheaterJS.prototype = { constructor: TheaterJS, - version: "1.2.0", + version: "1.3.0", keyboards: {},