trial-js
Version:
simple library to monitoring mouse position and predicting user input
8 lines (7 loc) • 3.96 kB
JavaScript
/**
* trial-js - lightweight library for monitoring mouse position and predicting user input
* @author Marko Cen
* @version v0.1.0
* @link https://github.com/MarkoCen/trial-js
* @license MIT
*/!function(t){function e(t){t.Trial=n,t.document&&t.document.addEventListener("mousemove",t.Trial.throttle(function(e){for(var n=t.Trial.getPointerPos(e),r=0;r<t.Trial.queue.length;r++)t.Trial.invoke(n,t.Trial.queue[r],e)},50)),(t.$||t.jQuery||t.Zepto)&&t.$.fn.extend({within:function(e,n){var r=t.Trial(this.get());return r.within(e,n),this},enter:function(e,n){var r=t.Trial(this.get());return r.enter(e,n),this},leave:function(e,n){var r=t.Trial(this.get());return r.leave(e,n),this}})}var n=function(t){var e=n.checkDup(t);if(e)return e;var o=new r(t);return n.queue.push(o),o};n.queue=[],n.checkSame=function(t,e){if(t.length!==e.length)return!1;for(var n=t.length;n--;)if(t[n]!==e[n])return!1;return!0},n.getDefaultOpts=function(){return{distance:50,cord:"center"}},n.getEleCord=function(t,e){var r=n.getEleRect(t);switch(e.toLowerCase()){case"center":return{top:r.top+r.height/2,left:r.left+r.width/2};case"topleft":return{top:r.top,left:r.left};case"topcenter":return{top:r.top,left:r.left+r.width/2};case"topright":return{top:r.top,left:r.left+r.width};case"bottomleft":return{top:r.top+r.height,left:r.left};case"bottomcenter":return{top:r.top+r.height,left:r.left+r.width/2};case"bottomright":return{top:r.top+r.height,left:r.left+r.width};case"centerleft":return{top:r.top+r.height/2,left:r.left};case"centerright":return{top:r.top+r.height/2,left:r.left+r.width};default:return{top:r.top+r.height/2,left:r.left+r.width/2}}},n.getEleRect=function(t){var e=t.getBoundingClientRect();return{top:e.top||0,left:e.left||0,width:e.width||0,height:e.height||0}},n.getEles=function(t){if("string"==typeof t)return document.querySelectorAll(t);if("object"==typeof t){if(t.constructor==Array||t.constructor==NodeList){for(var e=[],n=0;n<t.length;n++)!t[n].nodeType||1!=t[n].nodeType&&9!=t[n].nodeType&&11!=t[n].nodeType||e.push(t[n]);return e}return t.nodeType?1==t.nodeType||9==t.nodeType||11==t.nodeType?[t]:[]:window.$&&t instanceof window.$?t.get():[]}},n.getPointerPos=function(t){return{top:t.clientY,left:t.clientX}},n.checkDup=function(t){for(var e=n.getEles(t),r=0;r<n.queue.length;r++){var o=n.queue[r];if(n.checkSame(o,e))return o}},n.invoke=function(t,e,n){for(var r=Object.keys(e.listeners||{}),o=0;o<r.length;o++)e.listeners[r[o]].call(e,t,n)},n.throttle=function(t,e){var n=!1;return function(r){n||(t.call(this,r),n=!0,setTimeout(function(){n=!1},e))}};var r=function(t){var e=n.getEles(t);this.length=e.length,this.listeners={};for(var r=0;r<e.length;r++)this[r]=e[r]};r.prototype.off=function(t){return delete this.listeners[t],this},r.prototype.within=function(t,e){var r,o;return"function"==typeof t?(o=t,r=n.getDefaultOpts()):(r=t,o=e),this.listeners.within=function(t,e){for(var i=0;i<this.length;i++){var h=n.getEleCord(this[i],r.cord),l=Math.sqrt(Math.pow(Math.abs(t.top-h.top),2)+Math.pow(Math.abs(t.left-h.left),2));l<r.distance&&o.call(this,l,this[i],e)}},this},r.prototype.leave=function(t,e){for(var r,o,i={},h=0;h<this.length;h++)i[h]=!1;return"function"==typeof t?(o=t,r=n.getDefaultOpts()):(r=t,o=e),this.listeners.leave=function(t,e){for(var h=0;h<this.length;h++){var l=n.getEleCord(this[h],r.cord),f=Math.sqrt(Math.pow(Math.abs(t.top-l.top),2)+Math.pow(Math.abs(t.left-l.left),2));f>r.distance?i[h]||(i[h]=!0,o.call(this,f,this[h],e)):i[h]=!1}},this},r.prototype.enter=function(t,e){for(var r,o,i={},h=0;h<this.length;h++)i[h]=!1;return"function"==typeof t?(o=t,r=n.getDefaultOpts()):(r=t,o=e),this.listeners.enter=function(t,e){for(var h=0;h<this.length;h++){var l=n.getEleCord(this[h],r.cord),f=Math.sqrt(Math.pow(Math.abs(t.top-l.top),2)+Math.pow(Math.abs(t.left-l.left),2));f<r.distance?i[h]||(i[h]=!0,o.call(this,f,this[h],e)):i[h]=!1}},this},"object"==typeof module&&module.exports?module.exports=e(t):e(t)}(this);