/app-showcase/mouse-speak/mouse-speak.min.js
https://github.com/apalsapure/pubnub-api · JavaScript · 11 lines · 11 code · 0 blank · 0 comment · 3 complexity · 33198f32abb15c65a4bd94670bb5727e MD5 · raw file
- (function(){function A(a){var b=0,d=0;if(!a)return[0,0];if(a.touches&&a.touches[0])PUBNUB.each(a.touches,function(c){b=c.pageX;d=c.pageY});else if(a.pageX){b=a.pageX;d=a.pageY}else try{b=a.clientX+n.scrollLeft+B.scrollLeft;d=a.clientY+n.scrollTop+B.scrollTop}catch(f){}b+=o*2;d+=o;if(b<=o*2)b=0;if(d<=o)d=0;return[b,d]}function l(a){if(i){var b=+new Date,d=p[i];if(C+m>b){clearTimeout(D);D=setTimeout(function(){l(a)},m);return 1}C=b;b=A(a);var f=L(),c={uuid:i};if(!d)return E(c);if(!(q[0]==b[0]&&q[1]==
- b[1]||!b[0]||!b[1])){q[0]=b[0];q[1]=b[1];c.pos=b}if(F!=f||!(M++%3)){F=f;c.txt=f||" ";r.set("mtxt",c.txt)}if(!(c.txt||c.pos))return 1;c.c=(p[i].last||1)+2;PUBNUB.publish({channel:w,message:c});c.force=1;s(c);return 1}}function E(a){var b=a.pos||[100,100],d=g.create({image:{url:"http://www.pubnub.com/static/mouse.png",width:350,height:30,offset:{top:36,left:0}},cell:{count:1},left:b[1],top:b[0],framerate:50});i!=a.uuid&&h("mouseover",d.node,function(){g.move(d,{opacity:0.5,top:Math.ceil(Math.random()*
- 150),left:Math.ceil(Math.random()*150)},m)});PUBNUB.css(d.node,{fontWeight:"bold",padding:"5px 0 0 20px",fontSize:"15px",textShadow:"rgba(0,0,0,0.9) 1px 1px 2px","-webkit-box-shadow":"1px 1px 5px rgba(0,0,0,0.5)","-webkit-border-radius":"5px",opacity:0,backgroundColor:"rgba(255,255,255,0.6)",color:"#"+(a.uuid||"000000").slice(-6)});PUBNUB.attr(d.node,"uuid",a.uuid);p[a.uuid]=d;s(a)}function s(a){var b=a.pos,d=a.click,f=a.txt,c=a.c,j=a.force,e=p[a.uuid];if(!e)return E(a);if(d)return N(b);if(c&&(e.last||
- 0)-c>100)e.last=c;if(!(!j&&c&&e.last&&e.last>c)){if(c)e.last=c;if(f)e.node.innerHTML=f.replace(O,"");e.timerfade&&clearTimeout(e.timerfade);e.timerfade=setTimeout(function(){PUBNUB.css(e.node,{opacity:0.4});clearTimeout(e.timerfade);e.timerfade=setTimeout(function(){PUBNUB.css(e.node,{display:"none"})},G)},G);PUBNUB.css(e.node,{display:"block",opacity:1});if(b){g.move(e,{top:b[1],left:b[0]},m-10);if(b[0]>e.left)g.setframe(e,0,{top:36});else b[0]<e.left&&g.setframe(e,0,{top:1})}}}function N(a){var b=
- PUBNUB.create("div");if(!(a[1]&&a[0]))return 1;PUBNUB.css(b,{position:"absolute",background:"transparent url(http://www.pubnub.com/static/mouse-click.png) no-repeat",top:a[1],left:a[0],width:13,height:13});n.appendChild(b);return 1}function P(a,b){setTimeout(function(){if(",13,27,".indexOf(","+b+",")!==-1)k.value=" ";l(a)},20);return 1}function Q(){return x=1}function y(a){x=0;a=A(a);var b={pos:a,click:1,uuid:i};if(a[1]&&a[0]){PUBNUB.publish({channel:w,message:b});b.force=1;s(b)}return 1}function L(){var a=
- k.value||"",b=a.length;if(b>H)k.value=a="..."+a.slice(-H);else if(a.indexOf(t)!=-1&&b>t.length)k.value=a=a.replace(t,"");return a}var u=this.localStorage,r={get:function(a){if(u)return u.getItem(a);if(document.cookie.indexOf(a)==-1)return null;return((document.cookie||"").match(RegExp(a+"=([^;]+)"))||[])[1]||null},set:function(a,b){if(u)return u.setItem(a,b);document.cookie=a+"="+b+"; expires=Thu, 1 Aug 2030 20:00:00 UTC; path=/"}},h=PUBNUB.bind,n=PUBNUB.search("body")[0],B=document.documentElement,
- p={},w="mouse-speak",G=9E3,k=PUBNUB.create("input"),x=0,q=[],F="",M=0,i=r.get("uuid")||0,m=100,H=34,o=10,D=0,C=0,O=/[<>]/g,g={create:function(a){a.intervals={animate:0,move:{}};a.cell.size=Math.floor(a.image.width/a.cell.count);a.node=PUBNUB.create("div");a.opacity=a.opacity||1;PUBNUB.css(a.node,{opacity:a.opacity,position:"absolute",top:a.top,left:a.left,width:a.cell.size,height:a.image.height,backgroundRepeat:"no-repeat",backgroundImage:"url("+a.image.url+")"});g.setframe(a,0);g.append(a.node);
- return a},ground:PUBNUB.search("body")[0],append:function(a){g.ground.appendChild(a)},setframe:function(a,b,d){d=d||{};if(typeof d.top=="number")a.image.offset.top=d.top;if(typeof d.left=="number")a.image.offset.left=d.left;PUBNUB.css(a.node,{backgroundPosition:"-"+(a.cell.size*b+a.image.offset.left)+"px -"+a.image.offset.top+"px"})},animate:function(a,b,d,f,c){if(!c){c=0;g.stop_animate(a)}if(c===b.length)if(d===0)return f&&f();else{d--;c=0}var j=b[c][0]||b[c];a.intervals.animate=setTimeout(function(){g.setframe(a,
- j);g.animate(a,b,d,f,c+1)},(b[c][1]||0.1)*1E3)},movie:function(a,b,d,f,c){if(!c){c=0;g.stop_all(a)}if(c===b.length)if(d===0)return f&&f();else{d--;c=0}if(b[c][2])g.animate(a,b[c][2],b[c][3]||0);g.move(a,b[c][0],b[c][1],function(){g.movie(a,b,d,f,c+1)})},move:function(a,b,d,f){var c=+new Date;g.stop_all(a);PUBNUB.each(b,function(j,e){var v=c,I=c+d,J=a[j]||0,R=e-J,K={},z=j+e;g.stop_move(a,z);a.intervals.move[z]=setInterval(function(){v=+new Date;a[j]=I<=v?e:R*(v-c)/d+J;K[j]=a[j];PUBNUB.css(a.node,K);
- if(I<=v&&a.intervals.move){g.stop_move(a,z);f&&f()}},Math.ceil(1E3/a.framerate))})},stop_all:function(a){clearTimeout(a.intervals.animate);PUBNUB.each(a.intervals.move,function(b){clearInterval(a.intervals.move[b])})},stop_move:function(a,b){clearInterval(a.intervals.move[b])},stop_animate:function(a){clearTimeout(a.intervals.animate)}};PUBNUB.subscribe({channel:w},s);i||PUBNUB.uuid(function(a){i=a;r.set("uuid",i);setTimeout(function(){l({})},m)});PUBNUB.css(k,{position:"absolute",top:-10000,left:0});
- var t="TYPE!!!";k.value=r.get("mtxt")||t;n.appendChild(k);h("mousemove",document,l);h("touchmove",document,l);h("touchstart",document,l);h("touchend",document,l);h("keydown",document,function(a){var b=a.keyCode;if(",18,37,38,39,40,20,17,35,36,33,34,16,9,91,".indexOf(","+b+",")!==-1)return 1;x||k.focus();P(a,b);return 1});h("mousedown",document,y);h("click",document,y);PUBNUB.each(PUBNUB.search("input"),function(a){h("focus",a,Q);h("blur",a,y)})})();