golicons
Version:
1 lines • 5.77 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.golicons=e():t.golicons=e()}(window,function(){return function(t){var e={};function s(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,s),i.l=!0,i.exports}return s.m=t,s.c=e,s.d=function(t,e,n){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(s.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)s.d(n,i,function(e){return t[e]}.bind(null,i));return n},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e,s){"use strict";s.r(e);const n="http://www.w3.org/2000/svg",i={};function r(t,e){!function(t,e){const s="goli-"+t;if(i[s])throw"golicons name '"+t+"' already in use";i[s]=e}(t,e)}function o(t,e){r(t,t=>{t.setPatternFunc(()=>(function(t){const e=[];for(let s of t.split("\n")){if(""===(s=s.trim()))continue;const t=[];e.push(t);for(const e of s){let s;if("."===e)s=0;else{if("O"!==e)throw"Error parsing pattern char: "+e;s=1}t.push(s)}}return e})(e))})}function l(){!function(){const t=document.createElement("style");t.type="text/css";let e="";e+=".goli { width: 32px; height: 32px; }",e+=".goli-live { fill: #000; }",e+=".goli-dead { fill: #fff; stroke: #000; }";const s=document.createTextNode(e);t.appendChild(s);const n=document.getElementsByTagName("head")[0];n.insertBefore(t,n.firstChild)}();const t=document.getElementsByClassName("goli");for(const e of t){new h(e).start()}}class h{constructor(t){this._el=t,this._tickDelayMs=1e3,this._parseClassOptions(),this._state=this._patternFunc(),this._newState=this._patternFunc(),this._prevState=this._patternFunc(),this._cells=[],this._numRows=this._state.length,this._numCols=this._state[0].length;const e=t.getBoundingClientRect(),s=e.width/this._numCols,i=e.height/this._numRows,r=document.createElementNS(n,"svg");r.style.width="100%",r.style.height="100%",t.appendChild(r);for(let t=0;t<this._state.length;t++){const e=document.createElementNS(n,"g");this._cells[t]=[],e.classList.add("goli-row"),e.setAttribute("transform","translate(0, "+t*i+")"),r.appendChild(e);for(let r=0;r<this._state[0].length;r++){const o=document.createElementNS(n,"rect");o.setAttribute("width",s),o.setAttribute("height",i),o.setAttribute("x",r*s),e.appendChild(o),this._cells[t][r]=o}}this.initRender()}_parseClassOptions(){const t=this._el.classList;for(const e of t)if(e.startsWith("goli-tick-ms"))this._tickDelayMs=Number(e.slice(13));else if(e.startsWith("goli-start-delay-ms"))this._startDelayMs=Number(e.slice(20));else if(i[e])i[e](this);else if(e.startsWith("goli-"))throw"Unrecognized golicons class: "+e}setPatternFunc(t){this._patternFunc=t}start(){const t=()=>{setInterval(()=>{this.tick(),this.render()},this._tickDelayMs)};this._startDelayMs?setTimeout(t,this._startDelayMs):t()}printState(){for(let t=0;t<this._state.length;t++){const e=this._state[t];console.log(JSON.stringify(e),t)}console.log()}tick(){a(this._state,this._newState),a(this._state,this._prevState);for(let t=0;t<this._state.length;t++)for(let e=0;e<this._state[0].length;e++){const s=this.neighbors(t,e);let n=0;for(const t of s)1===t&&n++;const i=this._state[t][e];let r=i;1===i?r=n<2?0:n>3?0:1:3===n&&(r=1),this._newState[t][e]=r}a(this._newState,this._state)}neighbors(t,e){const s=[];return s.push(this.topLeft(t,e)),s.push(this.top(t,e)),s.push(this.topRight(t,e)),s.push(this.left(t,e)),s.push(this.right(t,e)),s.push(this.bottomLeft(t,e)),s.push(this.bottom(t,e)),s.push(this.bottomRight(t,e)),s}wrapTop(t){return 0===t?this._state.length-1:t-1}wrapLeft(t){return 0===t?this._state[0].length-1:t-1}wrapRight(t){return t===this._state[0].length-1?0:t+1}wrapBottom(t){return t===this._state.length-1?0:t+1}topLeft(t,e){return this._state[this.wrapTop(t)][this.wrapLeft(e)]}top(t,e){return this._state[this.wrapTop(t)][e]}topRight(t,e){return this._state[this.wrapTop(t)][this.wrapRight(e)]}left(t,e){return this._state[t][this.wrapLeft(e)]}right(t,e){return this._state[t][this.wrapRight(e)]}bottomLeft(t,e){return this._state[this.wrapBottom(t)][this.wrapLeft(e)]}bottom(t,e){return this._state[this.wrapBottom(t)][e]}bottomRight(t,e){return this._state[this.wrapBottom(t)][this.wrapRight(e)]}initRender(){for(let t=0;t<this._numRows;t++)for(let e=0;e<this._numCols;e++)this.renderCell(t,e,this._state[t][e])}render(){for(let t=0;t<this._numRows;t++)for(let e=0;e<this._numCols;e++)this._state[t][e]!==this._prevState[t][e]&&this.renderCell(t,e,this._state[t][e])}renderCell(t,e,s){1===s?(this._cells[t][e].classList.remove("goli-dead"),this._cells[t][e].classList.add("goli-live")):(this._cells[t][e].classList.remove("goli-live"),this._cells[t][e].classList.add("goli-dead"))}}function a(t,e){for(let s=0;s<t.length;s++)for(let n=0;n<t[0].length;n++)e[s][n]=t[s][n]}s.d(e,"start",function(){return l}),s.d(e,"registerPattern",function(){return o}),s.d(e,"registerPlugin",function(){return r}),o("blinker","\n .....\n .....\n .OOO.\n .....\n .....\n"),o("beacon","\n ......\n .OO...\n .OO...\n ...OO.\n ...OO.\n ......\n"),o("toad","\n ......\n ......\n ..OOO.\n .OOO..\n ......\n ......\n"),o("glider","\n ......\n ..O...\n ...O..\n .OOO..\n ......\n ......\n")}])});