512byt.es

's codegolf playground.

Libraries

A Template-Engine mixing up HTML and pure JavaScript in one template

function T(t,c,r,_){try{return eval("with(c||{}){_='';"+t[r="replace"](/(?:\n|^)\s*(<.*)/g,function(p,a){return'_+="'+a[r](/"/g,'\\"')[r](/<%(.*?)%>|\$(\w+(?:\.*[a-z0-9_[]]*)*)\|*([|\w]*)/gi,'"+ ("$3" ? T["$3"]($1$2) : ($1$2)) +"')+'";'})+"}")}catch(e){console.log(e)}}Demo

A cross-browser css, javascript- and module-loader.

/*! minc.js 0.7.0 build on 2015-09-10 */ !function(w){function $(a,b,k){return b=b||[],w=k||w,function _(j){var r,i,L,g,m=Minc.m,M=[],C=w.console;return _.done=function(a){r=a},!function l(s,d,o,t,I){with(j=0,(w.define=function(a,b,c){if(j=1,a.call?(c=a,a="",b=[]):a.pop?(c=b,b=a,a=""):b.call&&(c=b,b=[]),m.push({i:a,d:b,m:c}),L=b.length)for(d=L,o=[];d--;){for(g=m.length;g--;)m[g]&&b[d]==m[g].i&&o.push(m[g].m());L!=o.length&&C&&C.log("Missing",b),M.push(c.apply(w,o))}else M.push(c())}).amd=1,document)(d=createElement(t=".css"==s.substr(-4)?"link":"script"))["link"==t?"href":"src"]=s.replace(/https*:/,""),"link"==t?(I=setInterval(function(b){try{if(d.sheet||d[b="styleSheet"]&&d[b].rules&&d[b].rules[0]){try{a[++i]?l(a[i]):r&&r.apply(_,M)}catch(c){}clearInterval(I)}}catch(c){try{a[++i]?l(a[i]):r&&r.apply(_,M)}catch(c){}clearInterval(I)}},5))&&(d.rel="stylesheet"):d.onload=d.onreadystatechange=d.onerror=function(c){"error"==(c=c||this).type&&b[i]?l(b[i]):"load"==c.type||"loaded"==c[g="readyState"]||"complete"==c[g]?a[++i]?l(a[i]):r&&r.apply(_,M):0},getElementsByTagName("head")[0].appendChild(d)}(a[i=0]),_}()}$.m=[],Minc=$}(window);Demo

A lightweight modular JavaScript framework

/*! framewreck 1.4.5 build on 2014-11-10 */ F=function(a){var b,c,d,e,f,g,h,i,j=this;return F.d=i=document,F.L=L="length",F.Q="querySelectorAll",F.H="innerHTML",F.C="checked",h="createElement",j.__proto__.constructor!=F?new F(a):(j.each=function(a){for(d=c;d--;)a(F(j[d]),d);return j},j.y=function(){for(d=99;d--;)b[d]?j[d]=b[d]:delete j[d];return j[L]=c=b[L],j.x=b,j},j.find=function(a){if(a&&!a.big)return b=[a],j.y();try{f=a.split(" "),a="";for(d in f)a+=" "+(g=f[d].split(":"))[0]+(isNaN(e=g[1])?e?":"+e:"":":nth-of-type("+(+e+1)+")");for(b||(c=1,b=[i]),f=[],d=c;d--;)f[d]=b[d][F.Q](a);for(b=[],d=0;d<f[L];d++)if((g=f[d])[L]&&!g.options)for(e=0;e<g[L];e++)b.push(g[e]);else b.push(g);return b.toString().match(/No/)&&(b=[]),j.y()}catch(h){}},j.get=function(a){return b=[b[a]],j.y()},j.prev=function(){return b=[b[0].previousElementSibling],j.y()},j.next=function(){return b=[b[0].nextElementSibling],j.y()},j.parent=function(){return b=[b[0].parentElement],j.y()},j.require=function(a,b,c,d,e){for(c=0,e=a[F.L];(d=i[h]("script")).src=a.shift();i.head.appendChild(d))d.onload=function(){c++,b&&c==e&&b(j)};return j},a&&"<"==a[0]?((g=i[h]("div"))[F.H]=a,b=g.childNodes,j.y()):j.find(a))},F.ext=function(a){for(i in a)F.prototype[i]=a[i]},F.ext({ajax:function(v,D,C,P,X){with(X=new XMLHttpRequest)return onreadystatechange=function(){4^readyState||C(this)},open(v,D,1),send(P),X}}),F.ext({css:function(a,b,c,d,e){if(c=this.x,a&&a.big&&b==[]._)return getComputedStyle(c[0],null).getPropertyValue(a);for(d=c[F.L];d--;)if(b!=[]._)c[d].style[a]=b;else for(e in a)c[d].style[e]=a[e]+(isNaN(a[e])||"opacity"==e?"":"px");return this}}),F.ext({data:function(a,b,c,d){for(c=this.x,d=c[F.L];d--;)b&&(!c[d].D&&(c[d].D={}),c[d].D[a]=b);return b?this:c[0].D?c[0].D[a]:null}}),F.ext({html:function(a){for(var b,c,d=[],e=this.x,f=[],g=0;g<e[F.L];g++)b=e[g],c=F.H,b.tagName.match(/INP|SEL|TEX/)&&(c="value"),b.multiple?F(b).find(":"+F.C).each(function(a){f.push(a.val())})&&d.push(f):d.push(b[c]),a!=[]._&&(b[c]=a);return a&&this||d.join("").trim()},append:function(a,b,c){b=this.x;try{for(c=0;c<b[F.L];c++)a.big?"<"==a[0]?b[c][F.H]+=a:b[c].appendChild(F.d[F.Q](a)[0]):b[c].appendChild(a instanceof F?a[0]:a)}catch(d){console.warn(d)}return this},appendTo:function(a,b,c){b=this.x;try{for(c=0;c<b[F.L];c++)a.big?"<"==a[0]?b[c][F.H]+=a:F.d[F.Q](a)[0].appendChild(b[c]):a.appendChild(b[c])}catch(d){console.warn(d)}return this},remove:function(a,b){for(a=this.x,b=a[F.L];b--;)a[b].parentNode.removeChild(a[b]);return this},checked:function(a,b,c,d,e){for(b=this.x,c=[],e=b[F.L];e--;)(d=b[e]).type.match(/ch|rad/)&&(a!=[]._?d[F.C]=a:c.push(d[F.C]));return c[F.L]?1^c[F.L]?c:c[0]:this},serialize:function(a,b,c,d){for(d=this.x,a={},this.find("input,select,textarea"),b=this[F.L];b--;)c=F(this[b]),a[c[0].name]=c[0].type.match(/ch|rad/)?c[F.C]():c.val();return a},attr:function(a,b,c){for(c=this.x,i=c[F.L];i--;)b&&c[i].setAttribute(a,b);return b?this:c[0].getAttribute(a)},addClass:function(a,b,c){for(c=this.x,b=c[F.L];b--;)c[b].classList.add(a);return this},removeClass:function(a,b,c){for(c=this.x,b=c[F.L];b--;)c[b].classList.remove(a);return this}}),F.ext({val:F.prototype.html}),F.ext({on:function(a,b,c,d){for(c=this.x,d=c[F.L];d--;)c[d].addEventListener(a,b);return this},off:function(a,b,c,d){for(c=this.x,d=c[F.L];d--;)c[d].removeEventListener(a,b);return this},fire:function(a,b,c,d){for(c=this.x,d=c[F.L];d--;)c[d].dispatchEvent(new CustomEvent(a,{detail:b}));return this}});Demo

Games

a competitive 2-player chess-game in ~1k.

var a="bcdfedcbaaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAABCDFEDCB".split(""),F,P=0;document.body[H="innerHTML"]="<x id=p />",p.onclick=function(b,c,e){if(f=b.target,t=f?f[i="getAttribute"]("c"):0,j=f?+f[i]("i"):-1,F&&(N=F.tagName,e=f.tagName,m=0,J=+F[i]("i"),T=+F[i]("c"),s=J^j&&t^T,D=J-j,d=Math.abs(D),s)){if("A"==N)m="X"==e?!T&&-8==D||J>47&&16==d||T&&8==D||J<16&&16==d:!T&&(-9==D||-7==D)||T&&(9==D||7==D);else{if(("D"==N||"E"==N)&&(M=d%7?d%9?0:9:7))for(m=1,b=J>j?j+M:J+M;b<(J>j?J:j);b+=M)"x"!=a[b]&&(m=0);if(("B"==N||"E"==N)&&(M=~~(J/8)==~~(j/8)?1:d%8?0:8))for(m=1,b=J<j?J+M:J-M;J<j?b<j:b>j;J<j?b+=M:b-=M)"x"!=a[b]&&(m=0);("F"==N&&(1==d||d>6&&d<10)||"C"==N&&(17==d||15==d||10==d||6==d))&&(m=1)}c="X"!=e&&T^t,m&&(a[j]=T?N:N.toLowerCase(),a[J]="x",F=0,p.className=P=P?0:1)}for(p[H]="",i=0;i<a.length,b=a[i];i++)p[H]+="<"+b+" c="+("x"==b?2:b.charCodeAt(0)<97?1:0)+" i="+i+" a="+(t==P&&i==j)+"></"+b+">";F="X"==e||t!=P||c?F:f},p.onclick({});Demo

tetris clone in ~1K

B=[[[[0,1,1],[1,1]],[[1,0],[1,1],[0,1]]],[[[1,1],[0,1,1]],[[0,1],[1,1],[1]]],[[[1,1],[1,1]]],[[[1,1],[0,1],[0,1]],[[0,0,1],[1,1,1]],[[1],[1],[1,1]],[[1,1,1],[1]]],[[[1,1],[1],[1]],[[1,1,1],[0,0,1]],[[0,1],[0,1],[1,1]],[[1],[1,1,1]]],[[[1],[1],[1],[1]],[[1,1,1,1]]],[[[0,1],[1,1,1]],[[1],[1,1],[1]],[[1,1,1],[0,1]],[[0,1],[1,1],[0,1]]]];b=B[(M=Math)[f="floor"](M[f](7*M.random()))],P={x:8,y:0},i=0,g=[],G=[],L="length",I=setInterval(function(a){for(F=b[i],y=0;y<30;y++)for(g[y]=[],G[y]||(G[y]=[]),x=0;x<17;x++)g[y][x]=G[y][x];for(H={},y=0;y<30;y++)for(H[y]=1,x=0;x<17;x++){if(x==P.x&&y==P.y)for(j=0;j<F[L];j++)for(k=0;k<F[j][L];k++)F[j][k]&&(g[y+j][x+k]=F[j][k],G[y+j+1]&&G[y+j+1][x+k]&&(a=1));g[y][x]||delete H[y]}for(o="",y=0;y<30;y++){for(x=0;x<17;x++)o+=g[y][x]?"<b></b>":"<i></i>";o+="\n"}p.innerHTML=o,P.y++,(P.y+F[L]==31||a)&&(b=B[M[f](M[f](7*M.random()))],P.x=8,P.y=i=0,G=g.slice(0));for(r in H){for(y=r;y>0;y--)g[y]=g[y-1]?g[y-1]:[];G=g.slice(0)}},100),onkeydown=function(a,c){c=a.keyCode,39==c?P.x++:37==c?P.x--:38==c&&i++,b[i]||(i=0)};Demo

A tiny ascii side-scrolling platformer.

var k=[],j=J=c=0,X=setInterval(function(a,b,f,g,h,i,l){for(b="",a="indexOf",k[39]&&!~P[d.y][a](d.x+1)?d.x++&&(c=d.x<26?0:c+1):k[37]&&!~P[d.y][a](d.x-1)&&d.x--&&(c=d.x<26?0:c-1),d.y>24&&clearInterval(X),j&&(d.y-=j,j++,d.y<0&&(d.y=0),j=j>4?0:j),i=0;i<P.length;i++){for(h=c;h<c+100;h++){for(l=1,g=0;g<e.length;g++)if(f=e[g],h!=c||i||(~P[f.y][a](f.x+1)?f.a=0:~P[f.y][a](f.x-1)&&(f.a=1),f.x=f.a?f.x+.5:f.x-.5),f.x==h&&f.y==i){f.x==d.x&&f.y==d.y&&clearInterval(X),b+="<i>▄</i>",l=0;break}G.x==d.x&&G.y==d.y&&clearInterval(X),l&&(b+=d.x==h&&d.y==i?"<b>▄</b>":G.x==h&&G.y==i?"<u>▄</u>":~P[i][a](h)?"▄":" ")}b+="\n"}P[d.y+1]&&!~P[d.y+1][a](d.x)&&d.y++||(J=0),p.innerHTML=b},50);onkeydown=onkeyup=function(a,b){(37==a[b="keyCode"]||39==a[b])&&(k[a[b]]="keydown"==a.type),"keydown"==a.type&&38==a[b]&&!j&&J<2&&(j=1,J++)};Demo

Space Invaders in 502 bytes

for(p=389,L=l=m=t=0,c=" _ ",onkeydown=function(e){39==(k=e.which)?p++:37==k?p--:!l&(l=p)},setInterval('_="<pre>";l&(l-=20)<0&&(l=0);m=(m+=20)>p?b[+new Date%6]:m;for(i=0;i<400;i++){if(i%20==0)_+="\\n";if(~b[n="indexOf"](l))b.splice(b[n](l),1),L+=5,l=0;if(~b[n](p)||p==m)p=n,b=[],c="xxx";_+=i==p?"oIo":~b[n](i)?".#.":i==m&&m?" * ":i==l&&l?" | ":c}document.body.innerHTML=_+="\\nP "+L;t+=o;for(i in b)b[i]+=t%5e3==0?20:t%2e3==0?1:t%1e3==0&&-1',o=50),b=[],j=2;136>j;j+=14==j||94==j?29:55==j?27:2)b.push(j);Demo

Tron in 757 bytes

a=A=0;with(c.getContext("2d"))onkeyup=function(e){d=(k=e.which)==39?2:k==37?4:k==38?1:k==40?3:d;D=k==87?1:k==68?2:k==83?3:k==65?4:D;k==32&&X&S()},(S=function(){X=0;w=[{x:795,y:400}];d=1;W=[{x:5,y:0}];D=3;v=setInterval('c.width=c.width;p1[H="innerHTML"]=A+=z(w,d,W,"blue",0);p2[H]=a+=z(W,D,w,"red",1);if(X)clearInterval(v)',60)})(),z=function(f,g,F,B,b){h={x:f[l=f[m="length"]-1].x,y:f[l].y};beginPath();L=lineWidth=10;h.x+=g==2?L:g==4&&-L;h.y+=g==3?L:g==1&&-L;if(F[I="filter"](t=function(o){return o.x==(T=this).x&o.y==T.y},h)[m]||f[I](t,h)[m])return X=1;strokeStyle=_=createLinearGradient(0,0,800,0);for($ in _);_[$](b,"magenta");_[$](.3,"#AFD2E6");_[$](.6,"#FF1493");_[$](!b,B);for(i=f.push({x:h.x,y:h.y})-1;i--;){lineTo(f[i].x,f[i].y)};stroke();return 0}Demo

Collect under water bubble to stay alive

!function(_,o,n,T,S,a,q,x,k,p,b,e,w){function r(){return~~(Math.random()*(e+1))}function d(){for(s="<pre style=border:solid;width:350px>",0>q&&(q=e),0>p&&(p=e),p>e&&(p=e-b),i=0;e>i;i++){0^i&&i%b==0&&(s+="\n"),R=0;for(y in w)w[y]==i&&(s+="~|~",R++);s+=i==p?" > ":q==i?" o ":R?"":" "}document.body.innerHTML=s+"\n P: "+x+", O2: "+~~(100*(1-a/T))+"%"}function m(){for(q=w[0];~w[_](q);)q=r();d()}for(i=0,P=r();24>i;i++){for(;~w[_](P)||P==q||!P;)P=r();w[i]=P}onkeyup=function(e){h=e.which,g=39==h?p+1:37==h?p-1:40==h?p+b:38==h?p-b:0,~w[_](g)||(p=g),q==g&&(x+=5,a-=S,m(),n(k),k=o(function(){m(),d()},3e3)),d()},o(function(){if(a+=S,a>=T)n(k),onkeyup=0,a=T,d();else{for(i in w)w[i]=w[i]-1<0?e:w[i]-1,w[i]==q&&q--,w[i]==p&&p--;d()}},S)}("indexOf",setInterval,clearInterval,6e4,600,0,5,0,0,0,15,255,[]);Demo