-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-perlin.min.js
7 lines (7 loc) · 1.69 KB
/
jquery-perlin.min.js
1
2
3
4
5
6
7
/* jQuery Perlin
* http://www.njl.us/projects/jquery-perlin/
*
* Copyright 2011, Ned Jackson Lovely
* Released under the MIT License
*/
(function(a){a.fn.perlin=function(p){p=a.extend({},a.fn.perlin.defaults,p);var o=this,c=document.createElement("canvas");if(!c.getContext){return m()}if(typeof p.color=="string"){p.color=p.color.replace(/ /g,"");var b=/(.*?)rgb\((\d+),(\d+),(\d+)\)/.exec(p.color);p.color=[parseInt(b[2]),parseInt(b[3]),parseInt(b[4])]}function m(i){if(!i){i=p.fallback}return o.each(function(){a(this).css("background-image","url("+i+")")})}function f(i){return 3*Math.pow(i,2)-2*Math.pow(i,3)}function g(D,A){var F=(D/p.gridSpacing),t=(A/p.gridSpacing),I=~~F,v=~~t,q=[];for(var s=0;s<4;++s){var r=I+s%2,H=v+~~(s/2),E=k[(H+n[r])&255];q[s]=E[0]*(F-r)+E[1]*(t-H)}var C=F-~~F,z=t-~~t,G=f(C),u=f(z),B=q[0]+G*(q[1]-q[0]),w=q[2]+G*(q[3]-q[2]);return B+u*(w-B)}function j(i,r){var q=p.tileSize;return(g(i+q,r+q)*(q-i)*(q-r)+g(i,r+q)*i*(q-r)+g(i,r)*i*r+g(i+q,r)*(q-i)*r)/(q*q)}function l(s){var z=s.getContext("2d"),q=s.width,F=s.height,t=255*p.opacity,v=z.createImageData(q,F),u=v.data;var B=p.tileable?j:g,i=p.color[0],w=p.color[1],D=p.color[2];for(var C=0;C<q;++C){var G=C*q*4;for(var E=0;E<F;++E){var A=G+E*4;u[A]=i;u[A+1]=w;u[A+2]=D;u[A+3]=~~((B(E,C)+1/2)*t)}}z.putImageData(v,0,0)}c.height=c.width=p.tileSize;var k=[],n=[];for(var h=0;h<256;++h){var e=2*Math.PI*Math.random();k[h]=[Math.cos(e),Math.sin(e)];n[h]=~~(Math.random()*255)}l(c);var d=c.toDataURL("image/png");if(d.indexOf("data:image/png")!=0||a.browser.msie&&a.browser.version.substr(0,1)<9&&d.length>32000){d=null}return m(d)};a.fn.perlin.defaults={gridSpacing:15,tileSize:200,fallback:"",opacity:0.1,tileable:true,color:[0,0,0]}})(jQuery);