-
Notifications
You must be signed in to change notification settings - Fork 8
/
magic-canvas.min.js
1 lines (1 loc) · 5.92 KB
/
magic-canvas.min.js
1
jQuery.magicCanvas={reqId:0,draw:function(a){function b(){v=document.getElementById("reactive-bg-canvas"),t=$(window).width(),u=$(window).height(),v.style.position="fixed",v.style.zIndex=D.zIndex,v.style.top="0px",v.style.left="0px",v.width=t,v.height=u,y={x:t/2,y:u/2,rx:t/2,ry:u/2},w=v.getContext("2d"),q()}function c(){"ontouchstart"in window||window.addEventListener("mousemove",e),window.addEventListener("scroll",d),window.addEventListener("resize",f)}function d(){y.x=y.rx+document.body.scrollLeft+document.documentElement.scrollLeft,y.y=y.ry+document.body.scrollTop+document.documentElement.scrollTop}function e(a){a.pageX||a.pageY?(y.x=a.pageX,y.y=a.pageY):(a.clientX||a.clientY)&&(y.x=a.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,y.y=a.clientY+document.body.scrollTop+document.documentElement.scrollTop),y.rx=a.clientX,y.ry=a.clientY}function f(){t=window.innerWidth,u=window.innerHeight,v.width=t,v.height=u,this.reqId&&window.cancelAnimationFrame(this.reqId),b(),g()}function g(){if(this.reqId&&s(this.reqId),"heart-beat"==D.type)setInterval(i,D.heartBeatCD);else if("random-move"==D.type){h();for(var a=0;a<x.length;a++)j(x[a])}}function h(){if(w.clearRect(0,0,t,u),"heart-beat"==D.type){for(var a=0,b=0;b<A.length;b++){var c=A[b];c.circle.active>0?(c.circle.active-=.012,c.circle.draw()):a++}if(A.length>0&&a==A.length)return void(A=[])}else if("random-move"==D.type)for(var d=r(),b=0;b<x.length;b++)Math.abs(p(d,x[b]))<4e3?(x[b].active=.3,x[b].circle.active=.6):Math.abs(p(d,x[b]))<2e4?(x[b].active=.1,x[b].circle.active=.3):Math.abs(p(d,x[b]))<4e4?(x[b].active=.02,x[b].circle.active=.1):(x[b].active=0,x[b].circle.active=0),o(x[b]),x[b].circle.draw();this.reqId=requestAnimationFrame(h)}function i(){h();var a=k(),b=new n(a,0),c=2400,d=60*c/1e3,e=D.heartBeatRange/d,f=c/d,g=.8,i=r();A=[];var j=function(){if(b.radius<D.heartBeatRange){for(var a=0;a<x.length;a++){var c=x[a];if(p(c,b.pos)<Math.pow(b.radius,2))for(var d=0;d<c.closest.length;d++){var h=c.closest[d],k=m(c,h,b);void 0!=k&&(k.circle=new n(k,1.2,i),k.circle.active=g,g*=.999,A.push(k))}}setTimeout(j,f),b.radius+=e}};z&&j()}function j(a){TweenLite.to(a,1+Math.random(),{x:a.originX-50+100*Math.random(),y:a.originY-50+100*Math.random(),onComplete:function(){j(a)}})}function k(){for(var a={x:-100,y:-100},b=r(),c=0;c<x.length;c++){var d=x[c];a=p(b,d)<p(b,a)?d:a}return a}function l(a,b){var c=D.lineLen*Math.sin(60*Math.PI/180),d=D.lineLen*Math.cos(60*Math.PI/180),e={closest:[]};return"left"==b||"right"==b?(e.x=a.x+D.lineLen*("left"==b?-1:1),e.y=a.y):"rightTop"==b||"rightBottom"==b?(e.x=a.x+d,e.y=a.y+c*("rightTop"==b?-1:1)):("leftTop"==b||"leftBottom"==b)&&(e.x=a.x-d,e.y=a.y+c*("leftTop"==b?-1:1)),e.type=b,a.closest.push(e),e.closest.push(a),e}function m(a,b,c){var d=p(a,c.pos),e=p(b,c.pos),f=Math.sqrt(Math.max(d,e)),g=Math.sqrt(Math.min(d,e));if(g<c.radius&&f>c.radius){var h=(a.y-b.y)/(a.x-b.x),i=a.y-h*a.x,j=-c.pos.x,k=-c.pos.y,l=c.radius,m=(Math.pow(h,2)+1)*Math.pow(l,2)-Math.pow(j*h,2)+2*(j*k+i*j)*h-Math.pow(k+i,2),n=(-1*Math.sqrt(m)-h*(k+i)-j)/(Math.pow(h,2)+1),o=(Math.sqrt(m)-h*(k+i)-j)/(Math.pow(h,2)+1),q=n<Math.max(a.x,b.x)&&n>Math.min(a.x,b.x)?n:o,r=h*q+i;return{x:q,y:r}}}function n(a,b,c){this.pos=a||null,this.radius=b||null,this.centerP=c,this.draw=function(){if(this.active){w.beginPath(),w.arc(this.pos.x,this.pos.y,this.radius,0,2*Math.PI,!1);var a="function"==typeof D.rgb?D.rgb(this.pos,this.centerP||this.pos):D.rgb;a="".concat(a.r).concat(",").concat(a.g).concat(",").concat(a.b),w.fillStyle="rgba("+a+","+this.active+")",w.fill()}}}function o(a){if(a.active)for(var b=0;b<a.closest.length;b++){w.beginPath(),w.moveTo(a.x,a.y),w.lineTo(a.closest[b].x,a.closest[b].y);var c="function"==typeof D.rgb?D.rgb(y):D.rgb;c="".concat(c.r).concat(",").concat(c.g).concat(",").concat(c.b),w.strokeStyle="rgba("+c+","+a.active+")",w.stroke()}}function p(a,b){return Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2)}function q(){if("random-move"==D.type){x=[];for(var a=0;t>a;a+=t/20)for(var b=0;u>b;b+=u/20){var c=a+Math.random()*t/20,d=b+Math.random()*u/20,e={x:c,originX:c,y:d,originY:d};x.push(e)}for(var f=0;f<x.length;f++){for(var g=[],h=x[f],i=0;i<x.length;i++){var j=x[i];if(h!=j){for(var k=!1,m=0;5>m;m++)k||void 0==g[m]&&(g[m]=j,k=!0);for(var m=0;5>m;m++)k||p(h,j)<p(h,g[m])&&(g[m]=j,k=!0)}}h.closest=g}for(var f=0;f<x.length;f++)x[f].circle=new n(x[f],2+2*Math.random(),void 0)}else if("heart-beat"==D.type){var o={x:t/2,y:u/2,closest:[]},q=[l(o,"left"),l(o,"rightTop"),l(o,"rightBottom")];for(x=[o];q.length>0;){var e=q.pop();if(0<e.x&&e.x<t&&0<e.y&&e.y<u){for(var r=!1,f=0;f<x.length;f++){var s=x[f],v=p(e,s);if(v<.1*Math.pow(D.lineLen,2)){r=!0;break}}if(!r){x.push(e);var w=e.type;"leftTop"==w||"leftBottom"==w?(q.unshift(l(e,"left")),q.unshift(l(e,"leftTop"==w?"rightTop":"rightBottom"))):"rightTop"==w||"rightBottom"==w?(q.unshift(l(e,"right")),q.unshift(l(e,"rightTop"==w?"leftTop":"leftBottom"))):"left"==w?(q.unshift(l(e,"leftBottom")),q.unshift(l(e,"leftTop"))):"right"==w&&(q.unshift(l(e,"rightBottom")),q.unshift(l(e,"rightTop")))}}}for(var f=0;f<x.length;f++)x[f].circle=new n(x[f],2)}}function r(){var a=y.x-B.offset().left,b=y.y-B.offset().top;return{x:a,y:b}}function s(a){for(var b=0,c=["ms","moz","webkit","o"],d=0;d<c.length&&!window.requestAnimationFrame;++d)window.requestAnimationFrame=window[c[d]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[c[d]+"CancelAnimationFrame"]||window[c[d]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(a,c){var d=(new Date).getTime(),e=Math.max(0,16-(d-b)),f=window.setTimeout(function(){a(d+e)},e);return b=d+e,f}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)}),a&&window.cancelAnimationFrame(a)}s();var t,u,v,w,x,y,z=!0,A=[],B=$("#reactive-bg-canvas"),C={lineLen:30,heartBeatCD:3e3,heartBeatRange:300,rgb:{r:156,g:217,b:249},type:"heart-beat",zIndex:-99999},D=$.extend(C,a);$(document).mouseenter(function(){z=!0}),$(document).mouseleave(function(){z=!1}),b(),g(),c()}};