diff --git a/src/gest.js b/src/gest.js index 3ed1457..b55d811 100644 --- a/src/gest.js +++ b/src/gest.js @@ -1,7 +1,7 @@ /* * @name: gest.js * @description: gest.js is a webcam based gesture recognition library that helps developers make webpages more immersive - * @version: 0.5.0 + * @version: 0.5.1 * @author: Hadi Michael (http://hadi.io) * @acknowledgements: gest.js is an extension of work started by William Wu (https://github.com/wvvvw) * @license: MIT License @@ -600,7 +600,9 @@ window.gest = (function (window) { if (!gestIsInitialised || !userHasAskedToStart) { return false; } if (video) { video.src = ''; } - return !!stream.stop(); + + stream.getTracks()[0].stop(); + return true; }; /* @public */ diff --git a/src/gest.min.js b/src/gest.min.js index e1d7d39..906c6f2 100644 --- a/src/gest.min.js +++ b/src/gest.min.js @@ -1,10 +1,10 @@ /* * @name: gest.js * @description: gest.js is a webcam based gesture recognition library that helps developers make webpages more immersive - * @version: 0.5.0 + * @version: 0.5.1 * @author: Hadi Michael (http://hadi.io) * @acknowledgements: gest.js is an extension of work started by William Wu (https://github.com/wvvvw) * @license: MIT License */ - window.gest=function(e){"use strict";navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;var t={framerate:25,videoCompressionRate:4,sensitivity:80,skinFilter:false,debug:{state:false,canvas:null,context:null}},n=false,r=false,i,s,o,u,a=function(){function t(){m.removeEventListener("DOMContentLoaded",document,t);m.removeEventListener("load",e,t);if(l()){n=true}if(r&&n){return e.gest.start()}return false}if(a.prototype._singletonInstance){return a.prototype._singletonInstance}a.prototype._singletonInstance=this;if(document.readyState==="complete"){t.call()}else{m.addEventListener("DOMContentLoaded",document,t);m.addEventListener("load",e,t)}return true},f=function(e){var t=m.createCustomEvent("gest",document);t.direction=e.direction||null;t.up=e.up||false;t.down=e.down||false;t.left=e.left||false;t.right=e.right||false;t.error=e.error||null;m.fireEvent(t)},l=function(){s=document.createElement("video");o=document.createElement("canvas");if(!!s.canPlayType&&!!(o.getContext&&o.getContext("2d"))&&!!navigator.getUserMedia){s.width=300;s.height=225;s.setAttribute("style","visibility: hidden;");document.body.appendChild(s);o.setAttribute("style","width: 300px; display: none;");document.body.appendChild(o);u=o.getContext("2d")}else{c(0);return false}return true},c=function(e,n){var r;switch(e){case 0:r={code:e,message:"gest.js can't run in your browser :("};break;case 1:r={code:e,message:"gest.js could not start."};break;case 2:r={code:e,message:"gest.js has already started."};break;case 10:r={code:e,message:"DEEENIED! gest.js needs webcam access.",obj:n};break;case 11:r={code:e,message:"A constraint specified is not supported by the web-browser.",obj:n};break;case 12:r={code:e,message:"No media tracks of the type specified in the constraints are found.",obj:n};break;case 13:r={code:e,message:"Couldn't get access to webcam.",obj:n};break;default:r=null;break}if(t.debug.state){console.error(r.message)}f({error:r})},h=function(e,n){try{u.drawImage(s,0,0,e,n);var r=u.getImageData(0,0,e,n);if(t.skinFilter){d.get(p.apply(r),t.sensitivity,e,n)}else{d.get(r,t.sensitivity,e,n)}}catch(i){if(i.name==="NS_ERROR_NOT_AVAILABLE"){return false}else{throw i}}},p={huemin:0,huemax:.1,satmin:.3,satmax:1,valmin:.4,valmax:1,rgb2hsv:function(e,t,n){e=e/255;t=t/255;n=n/255;var r=Math.max(e,t,n),i=Math.min(e,t,n),s,o,u=r,a=r-i;if(r===0){o=0}else{o=a/r}if(r==i){s=0}else{switch(r){case e:s=(t-n)/a+(tthis.huemin&&l[0].59&&l[0]<1)&&l[1]>this.satmin&&l[1]this.valmin&&l[2]=0){var p=Math.abs(e.data[c]-this.priorFrame.data[c])+Math.abs(e.data[c+1]-this.priorFrame.data[c+1])+Math.abs(e.data[c+2]-this.priorFrame.data[c+2]);if(p>h*Math.abs((n-100)/100)){s.data[c]=255;s.data[c+1]=0;s.data[c+2]=0;s.data[c+3]=255;f+=1;o+=c/4%s.width;a+=Math.floor(c/4/s.height)}else{s.data[c]=e.data[c];s.data[c+1]=e.data[c+1];s.data[c+2]=e.data[c+2];s.data[c+3]=e.data[c+3]}}}if(f>0){v.search({x:o,y:a,d:f});if(t.debug.state&&t.debug.context.putImageData){t.debug.canvas.width=r;t.debug.canvas.height=i;t.debug.context.putImageData(s,0,0)}}this.priorFrame=e}},v={prior:false,filteringFactor:.9,filteredTotal:0,minTotalChange:300,minDirChange:2,longDirChange:7,state:0,search:function(e){var t={x:e.x/e.d,y:e.y/e.d,d:e.d};this.filteredTotal=this.filteringFactor*this.filteredTotal+(1-this.filteringFactor)*t.d;var n=t.d-this.filteredTotal,r=n>this.minTotalChange;switch(this.state){case 0:if(r){this.state=1;v.prior=t}break;case 1:this.state=2;var i=t.x-v.prior.x,s=t.y-v.prior.y,o=Math.abs(s)this.minDirChange&&o){f({direction:"Left",left:true})}if(s>this.minDirChange&&!o){if(Math.abs(s)>this.longDirChange){f({direction:"Long down",down:true})}else{f({direction:"Down",down:true})}}else if(s<-this.minDirChange&&!o){if(Math.abs(s)>this.longDirChange){f({direction:"Long up",up:true})}else{f({direction:"Up",up:true})}}break;case 2:if(!r){this.state=0}break;default:break}}},m={htmlEvents:{onload:1,onunload:1,onblur:1,onchange:1,onfocus:1,onreset:1,onselect:1,onsubmit:1,onabort:1,onkeydown:1,onkeypress:1,onkeyup:1,onclick:1,ondblclick:1,onmousedown:1,onmousemove:1,onmouseout:1,onmouseover:1,onmouseup:1},addEventListener:function(e,t,n){if(t.addEventListener)t.addEventListener(e,n,false);else if(t.attachEvent&&this.htmlEvents["on"+e]){t.attachEvent("on"+e,n)}else{t["on"+e]=n}},removeEventListener:function(e,t,n){if(t.removeEventListener)t.removeEventListener(e,n,false);else if(t.detachEvent&&this.htmlEvents["on"+e]){t.detachEvent("on"+e,n)}else{t["on"+e]=null}},createCustomEvent:function(e,t){try{var n;if(t.createEvent){n=t.createEvent("Event");n.initEvent(e,true,true)}else if(t.createEventObject){n=t.createEventObject();n.eventType=e}n.evntName=e;n.evntElement=t;return n}catch(r){console.error(r);return false}},fireEvent:function(e){try{if(e.evntElement.dispatchEvent){e.evntElement.dispatchEvent(e)}else if(e.evntElement.fireEvent&&this.htmlEvents["on"+e.evntName]){e.evntElement.fireEvent("on"+e.eventType,e)}else if(e.evntElement[e.evntName]){e.evntElement[e.evntName]()}else if(e.evntElement["on"+e.evntName]){e.evntElement["on"+e.evntName]()}}catch(t){console.error(t)}}};a.prototype.start=function(){r=true;if(!n){return false}if(!s||!(s.paused||s.ended||s.seeking||s.readyState=100){t.sensitivity=100}else if(e<=0){t.sensitivity=0}else{t.sensitivity=e}},debug:function(e){t.debug.state=e;if(e){t.debug.canvas=document.createElement("canvas");t.debug.canvas.setAttribute("style","width: 100%; height: 100%; display: block; position: absolute; top: 0; left: 0;");document.body.appendChild(t.debug.canvas);t.debug.context=t.debug.canvas.getContext("2d")}else{t.debug.canvas.setAttribute("style","display: none;");t.debug.canvas.parentNode.removeChild(t.debug.canvas)}return t.debug},skinFilter:function(e){t.skinFilter=e}};return new a}(window) \ No newline at end of file + window.gest=function(e){"use strict";navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;var t,n,a,i,o={framerate:25,videoCompressionRate:4,sensitivity:80,skinFilter:!1,debug:{state:!1,canvas:null,context:null}},r=!1,s=!1,d=function(){function t(){return b.removeEventListener("DOMContentLoaded",document,t),b.removeEventListener("load",e,t),h()&&(r=!0),s&&r?e.gest.start():!1}return d.prototype._singletonInstance?d.prototype._singletonInstance:(d.prototype._singletonInstance=this,"complete"===document.readyState?t.call():(b.addEventListener("DOMContentLoaded",document,t),b.addEventListener("load",e,t)),!0)},c=function(e){var t=b.createCustomEvent("gest",document);t.direction=e.direction||null,t.up=e.up||!1,t.down=e.down||!1,t.left=e.left||!1,t.right=e.right||!1,t.error=e.error||null,b.fireEvent(t)},h=function(){return n=document.createElement("video"),a=document.createElement("canvas"),n.canPlayType&&a.getContext&&a.getContext("2d")&&navigator.getUserMedia?(n.width=300,n.height=225,n.setAttribute("style","visibility: hidden;"),document.body.appendChild(n),a.setAttribute("style","width: 300px; display: none;"),document.body.appendChild(a),i=a.getContext("2d"),!0):(u(0),!1)},u=function(e,t){var n;switch(e){case 0:n={code:e,message:"gest.js can't run in your browser :("};break;case 1:n={code:e,message:"gest.js could not start."};break;case 2:n={code:e,message:"gest.js has already started."};break;case 10:n={code:e,message:"DEEENIED! gest.js needs webcam access.",obj:t};break;case 11:n={code:e,message:"A constraint specified is not supported by the web-browser.",obj:t};break;case 12:n={code:e,message:"No media tracks of the type specified in the constraints are found.",obj:t};break;case 13:n={code:e,message:"Couldn't get access to webcam.",obj:t};break;default:n=null}o.debug.state&&console.error(n.message),c({error:n})},v=function(e,t){try{i.drawImage(n,0,0,e,t);var a=i.getImageData(0,0,e,t);o.skinFilter?g.get(l.apply(a),o.sensitivity,e,t):g.get(a,o.sensitivity,e,t)}catch(r){if("NS_ERROR_NOT_AVAILABLE"===r.name)return!1;throw r}},l={huemin:0,huemax:.1,satmin:.3,satmax:1,valmin:.4,valmax:1,rgb2hsv:function(e,t,n){e/=255,t/=255,n/=255;var a,i,o=Math.max(e,t,n),r=Math.min(e,t,n),s=o,d=o-r;if(i=0===o?0:d/o,o==r)a=0;else{switch(o){case e:a=(t-n)/d+(n>t?6:0);break;case t:a=(n-e)/d+2;break;case n:a=(e-t)/d+4}a/=6}return[a,i,s]},apply:function(e){for(var t=e.width*e.height,n=4*t,a=0,i=0;ithis.huemin&&h[0].59&&h[0]<1)&&h[1]>this.satmin&&h[1]this.valmin&&h[2]=0;){var l=Math.abs(e.data[u]-this.priorFrame.data[u])+Math.abs(e.data[u+1]-this.priorFrame.data[u+1])+Math.abs(e.data[u+2]-this.priorFrame.data[u+2]);l>v*Math.abs((t-100)/100)?(r.data[u]=255,r.data[u+1]=0,r.data[u+2]=0,r.data[u+3]=255,c+=1,s+=u/4%r.width,d+=Math.floor(u/4/r.height)):(r.data[u]=e.data[u],r.data[u+1]=e.data[u+1],r.data[u+2]=e.data[u+2],r.data[u+3]=e.data[u+3])}c>0&&(m.search({x:s,y:d,d:c}),o.debug.state&&o.debug.context.putImageData&&(o.debug.canvas.width=n,o.debug.canvas.height=a,o.debug.context.putImageData(r,0,0))),this.priorFrame=e}},m={prior:!1,filteringFactor:.9,filteredTotal:0,minTotalChange:300,minDirChange:2,longDirChange:7,state:0,search:function(e){var t={x:e.x/e.d,y:e.y/e.d,d:e.d};this.filteredTotal=this.filteringFactor*this.filteredTotal+(1-this.filteringFactor)*t.d;var n=t.d-this.filteredTotal,a=n>this.minTotalChange;switch(this.state){case 0:a&&(this.state=1,m.prior=t);break;case 1:this.state=2;var i=t.x-m.prior.x,o=t.y-m.prior.y,r=Math.abs(o)this.minDirChange&&r&&c({direction:"Left",left:!0}),o>this.minDirChange&&!r?c(Math.abs(o)>this.longDirChange?{direction:"Long down",down:!0}:{direction:"Down",down:!0}):o<-this.minDirChange&&!r&&c(Math.abs(o)>this.longDirChange?{direction:"Long up",up:!0}:{direction:"Up",up:!0});break;case 2:a||(this.state=0)}}},b={htmlEvents:{onload:1,onunload:1,onblur:1,onchange:1,onfocus:1,onreset:1,onselect:1,onsubmit:1,onabort:1,onkeydown:1,onkeypress:1,onkeyup:1,onclick:1,ondblclick:1,onmousedown:1,onmousemove:1,onmouseout:1,onmouseover:1,onmouseup:1},addEventListener:function(e,t,n){t.addEventListener?t.addEventListener(e,n,!1):t.attachEvent&&this.htmlEvents["on"+e]?t.attachEvent("on"+e,n):t["on"+e]=n},removeEventListener:function(e,t,n){t.removeEventListener?t.removeEventListener(e,n,!1):t.detachEvent&&this.htmlEvents["on"+e]?t.detachEvent("on"+e,n):t["on"+e]=null},createCustomEvent:function(e,t){try{var n;return t.createEvent?(n=t.createEvent("Event"),n.initEvent(e,!0,!0)):t.createEventObject&&(n=t.createEventObject(),n.eventType=e),n.evntName=e,n.evntElement=t,n}catch(a){return console.error(a),!1}},fireEvent:function(e){try{e.evntElement.dispatchEvent?e.evntElement.dispatchEvent(e):e.evntElement.fireEvent&&this.htmlEvents["on"+e.evntName]?e.evntElement.fireEvent("on"+e.eventType,e):e.evntElement[e.evntName]?e.evntElement[e.evntName]():e.evntElement["on"+e.evntName]&&e.evntElement["on"+e.evntName]()}catch(t){console.error(t)}}};return d.prototype.start=function(){return s=!0,r?n&&(n.paused||n.ended||n.seeking||n.readyState=100?o.sensitivity=100:0>=e?o.sensitivity=0:o.sensitivity=e},debug:function(e){return o.debug.state=e,e?(o.debug.canvas=document.createElement("canvas"),o.debug.canvas.setAttribute("style","width: 100%; height: 100%; display: block; position: absolute; top: 0; left: 0;"),document.body.appendChild(o.debug.canvas),o.debug.context=o.debug.canvas.getContext("2d")):(o.debug.canvas.setAttribute("style","display: none;"),o.debug.canvas.parentNode.removeChild(o.debug.canvas)),o.debug},skinFilter:function(e){o.skinFilter=e}},new d}(window); \ No newline at end of file