-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ca2d.min.js
1 lines (1 loc) · 2.19 KB
/
jquery.ca2d.min.js
1
(function(b){var a={init:function(d){var e=b.extend({},b.fn.ca2d.defaults,d);return this.filter("canvas").each(function(){$this=b(this);if(!$this[0].getContext){return true}$this.data("ca2d");if(!$this.data("ca2d")){var f=$this.width();var i=$this.height();var h=new c(f,i,e.cellSize);var g=this.getContext("2d");b(this).data("ca2d",{grid:h,context:g,settings:e})}var j=$this.data("ca2d")});function c(n,m,f){this.x=n;this.y=m;this.cellSize=f;this.rows=Math.floor(m/f);this.cols=Math.floor(n/f);var o=new Array(this.rows);for(var l=0;l<this.rows;l++){var p=new Array(this.cols);for(var k=0;k<this.cols;k++){var g=Math.round(Math.random());p[k]=new h(l,k,g)}o[l]=p}this.cells=o;function h(i,q,j){this.x=i;this.y=q;this.state=j;this.nextstate=j}}},step:function(){return this.filter("canvas").each(function(){$this=b(this);if(!$this.data("ca2d")){console.log("need to initialize ca2d");return}var e=$this.data("ca2d");c(e.grid,e.settings);d(e.context,e.grid,e.settings)});function c(f,g){for(var k=0;k<f.rows;k++){var s=f.cells[k];for(var h=0;h<f.cols;h++){var p=s[h];var e=f.cells[k-1<0?f.rows-1:k-1][h-1<0?f.cols-1:h-1];var t=f.cells[k-1<0?f.rows-1:k-1][h];var r=f.cells[k-1<0?f.rows-1:k-1][h+1>=f.cols?0:h+1];var q=f.cells[k][h-1<0?f.cols-1:h-1];var o=f.cells[k][h+1>=f.cols?0:h+1];var n=f.cells[k+1>=f.rows?0:k+1][h-1<0?f.cols-1:h-1];var m=f.cells[k+1>=f.rows?0:k+1][h];var l=f.cells[k+1>=f.rows?0:k+1][h+1>=f.cols?0:h+1];p.nextstate=g.rule(p.state,[e.state,t.state,r.state,q.state,o.state,n.state,m.state,l.state])}}}function d(n,e,f){for(var h=0;h<e.rows;h++){var o=e.cells[h];var k=h*e.cellSize;for(var g=0;g<e.cols;g++){var l=g*e.cellSize;var m=o[g];n.fillStyle=f.color(m.nextstate);n.fillRect(l,k,e.cellSize,e.cellSize);m.state=m.nextstate}}}},update:function(c){console.log("update")}};b.fn.ca2d=function(c){if(a[c]){return a[c].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof c==="object"||!c){return a.init.apply(this,arguments)}else{b.error("Method "+c+" does not exist on jQuery.ca2d")}}};b.fn.ca2d.defaults={cellSize:10,color:function(c){return c==1?"#aaaaaa":"#ffffff"},rule:function(f,e){var c=0;jQuery.each(e,function(g,h){c+=h});var d=f;if(f==1){if(c<=1||c>=4){d=0}}else{if(c==3){d=1}}return d},}})(jQuery);