-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsaveashtml.js
More file actions
14 lines (14 loc) · 7.51 KB
/
saveashtml.js
File metadata and controls
14 lines (14 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function saveAsHtml(file, im) {
var s = '<!DOCTYPE html><html><title>esp8266gameEngine</title><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"><style>#screen{width: 100%;height: 30em;image-rendering: optimizeSpeed;image-rendering: -moz-crisp-edges;image-rendering: -webkit-optimize-contrast;image-rendering: -o-crisp-edges;image-rendering: pixelated;-ms-interpolation-mode: nearest-neighbor;}@media screen and (orientation: portrait) { #screen { height: 40em; }}@media all and (display-mode: fullscreen) {#screen { height: 100%; }}</style></head><body><div id="cont" style="max-width:30em;height:40em;margin: 0 auto;background-color:#fff;"><canvas id="screen" width="512" height="512" onclick="start()"></canvas><div id="viewKeyboard">print on keyboard</div><div>key A - z, key B - x or space. Created width <a href="https://corax89.github.io/esp8266Game/index.html">ESP lge</a><br>click on screen for start </div><input type="checkbox" onclick="sound.rtttl.globalSound=this.checked" id="soundCheckbox" checked>sound</label></div><script>';
s += 'var code=[' + file.join(',') + '];';
s += 'function decompress(r){for(var e,f,n,o=[],s=0;s<r.length;)if(0==(128&r[s])){e=((127&r[s])<<8)+r[s+1],s+=2;for(var h=0;h<e;h++)o.push(r[s]),s++}else{e=(127&r[s])>>1,f=((1&r[s])<<8)+r[s+1],s+=2,n=o.length-f;for(h=0;h<e;h++)o.push(o[n+h])}return o}code=decompress(code);\n';
s += 'var font=[' + font.join(',') + '];\n';
s += (Sound + '\n' + Cpu + '\nvar timers = [];var cpu = new Cpu;cpu.init();').replace(/([^\/])\/\/[^\n]*/g, '').replace(/[\t ]+/g, ' ').replace(/\r/g, '').replace(/\n /g, '\n').replace(/,\n/g, ',').replace(/;\n/g, ';').replace(/\n\n/g, '\n').replace(/\}\n/g, '}').replace(/([\d)=\+\-\*<>%,\/;\]]) /g, function (s, d) {
return d;
});
s += 'var timerId,timeForRedraw=48,display=new Display,sound=new Sound,cpuLostCycle=0,isRedraw=!0,isRun=!1,bpalette=["#000000","#EDE3C7","#BE3746","#7FB8B5","#4A3E4F","#6EA76C","#273F68","#DEBB59","#B48D6C","#42595A","#C0624D","#333333","#777777","#8FAB62","#3ABFD1","#bbbbbb"],globalJKey=0,globalKey=0,palette=[],sprtpalette=[];function keyPressHandler(e){13==(globalKey=e.keyCode)&&(globalKey=10),e.preventDefault()}function keyDownHandler(e){switch(e.keyCode){case 38:case 87:globalJKey|=1;break;case 40:case 83:globalJKey|=2;break;case 37:case 65:globalJKey|=4;break;case 39:case 68:globalJKey|=8;break;case 88:case 32:globalJKey|=32;break;case 90:globalJKey|=16}e.preventDefault()}function keyUpHandler(e){switch(e.keyCode){case 38:case 87:globalJKey&=-2;break;case 40:case 83:globalJKey&=-3;break;case 37:case 65:globalJKey&=-5;break;case 39:case 68:globalJKey&=-9;break;case 88:case 32:globalJKey&=-33;break;case 90:globalJKey&=-17}e.preventDefault()}function Display(){var e,t,n,i=[],r=[],a=[],l=[],o=4,c=4,u=document.getElementById("screen"),d=!1,s=!1,f=0,y=128,p=0,g=128;function m(){e.textAlign="start",e.textBaseline="hanging",e.font=8*c+"px monospace",e.fillStyle="black",e.fillRect(0,0,2*t,2*n);for(var r=0;r<20480;r++)i[r]=0,a[r]=0,l[r]=0;cpuLostCycle+=2e3,e.fillStyle="black",e.fillRect(0,128*o,128*c,16*o),e.fillStyle="white";for(r=0;r<16;r++)palette[r]=bpalette[r],sprtpalette[r]=bpalette[r]}function w(e,t,n){cpuLostCycle+=.1,t>=f&&t<y&&n>=p&&n<g&&(a[128*t+n]=e)}function b(e,t,n,r){var a,l;for(a=0;a<r;a++)for(l=0;l<r;l++)w(e,t+a,n+l),i[128*(t+a)+n+l]=15&e}function v(){var t=[8,143,24,130,24,156,40,143,90,150,110,135];e.beginPath();e.strokeStyle="#fff";for(var n=0;n<8;n++)e.rect(t[2*n]*o,t[2*n+1]*c,12*o,12*c);e.stroke()}function h(e){var t=Number(e).toString(16);return t.length<2&&(t="0"+t),t}function k(e,t,n){return"#"+h(e)+h(t)+h(n)}return{init:function(){t=u.getBoundingClientRect().width,n=u.getBoundingClientRect().height,(e=u.getContext("2d")).imageSmoothingEnabled=!1,m()},reset:m,clearScreen:function(e){null==e&&(e=0);for(var t=0;t<20480;t++)i[t]=e,a[t]=e},drawLed:function(t){var n=527*(t>>11&31)+23>>6,i=259*(t>>5&63)+33>>6,r=527*(31&t)+23>>6;e.fillStyle=k(n,i,r),e.fillRect(0,0,128*pixelSize,16*pixelSize),e.fillRect(0,144*c,128*o,16*c)},char:function(e,t,n,i,r){for(var a=e.charCodeAt(0),l=0;l<5;l++)for(var o=font[5*a+l],c=0;c<8;c++,o>>=1)w(1&o?i:r,t+l,n+c)},updatePixel:function(e,t){a[128*e+t]=i[128*e+t]},drawPixel:w,drawSpritePixel:function(e,t,n){t>=0&&t<128&&n>=0&&n<128&&(r[128*t+n]=e)},plot:function(e,t,n){t>=f&&t<y&&n>=p&&n<g&&(w(e,t,n),i[128*t+n]=15&e)},largeplot:b,getPixel:function(e,t){return e>=0&&e<=127&&t>=0&&t<=127?i[128*e+t]:0},setClip:function(e,t,n,i){e>32767&&(e-=65536),t>32767&&(t-=65536),f=e>=0&&e<127?e:0,p=t>=0&&t<127?t:0,y=e+n>0&&e+n<=128?e+n:128,g=t+i>0&&t+i<=128?t+i:128},viewKeyboard:function(e){d=!0},redraw:function(){var i,f,y,p;for(d?(document.getElementById("viewKeyboard").style.display="block",d=0):document.getElementById("viewKeyboard").style.display="none",u.clientWidth+10<u.clientHeight?(c=3,v()):c=4,p=!1,t==u.getBoundingClientRect().width&&n==u.getBoundingClientRect().height||(t=u.getBoundingClientRect().width,n=u.getBoundingClientRect().height,v(),p=!0),f=0;f<128;f++)for(y=0;y<128;y++)r[128*f+y]>0?(i=r[128*f+y],l[128*f+y]=i,e.fillStyle=sprtpalette[15&i],e.fillRect(f*o,y*c,o,c)):(a[128*f+y]!=l[128*f+y]||s||p)&&(l[128*f+y]=a[128*f+y],i=a[128*f+y],e.fillStyle=palette[15&i],e.fillRect(f*o,y*c,o,c));s=!1},changePalette:function(e,t){var n=527*(t>>11&31)+23>>6,i=259*(t>>5&63)+33>>6,r=527*(31&t)+23>>6;s=!0,e<16?palette[e]=k(n,i,r):e<32&&(sprtpalette[e-16]=k(n,i,r))},clearSprite:function(){for(var e=0;e<20480;e++)r[e]=0},drawIco:function(e){for(var t=0,n=0;n<16;n++)for(var i=0;i<24;i++)if(b((240&e[t])>>4,16+4*i,32+4*n,4),i++,b(15&e[t],16+4*i,32+4*n,4),++t>=e.length)return}}}function redraw(){setTimeout(function(){requestAnimationFrame(redraw),cpu.redrawParticle(),display.redraw(),cpu.setRedraw(),isRedraw=!0},timeForRedraw)}function run(){for(var e=0;e<8;e++)timers[e]-=16,timers[e]<=0&&(timers[e]=0);soundTimer-=16,soundTimer<=30&&(soundTimer=sound.playRtttl()),soundTimer>2e3&&(soundTimer=2e3);for(e=0;e<8e3;e++)cpu.step(),e+=cpuLostCycle,cpuLostCycle=0;isRedraw&&(display.clearSprite(),cpu.redrawSprite(),cpu.testSpriteCollision(0),isRedraw=!1),timertime+=16;var t=(new Date).getTime()-timerstart-timertime;clearTimeout(timerId),timerId=setTimeout(function(){run()},16-t)}function fullScr(){var e=document.getElementById("cont");e.webkitRequestFullScreen?e.webkitRequestFullScreen():e.mozRequestFullScreen(),display.redraw()}function start(){isRun||(isRun=!0,sound.initAudio(),timerstart=(new Date).getTime(),timertime=0,soundTimer=0,run())}window.addEventListener("load",function(){var e=document.getElementById("screen"),t=(!!("ontouchstart"in window)||!!("ontouchstart"in document.documentElement)||!!window.ontouchstart||!!window.Touch||!!window.onmsgesturechange||window.DocumentTouch&&(window.document,window.DocumentTouch),[24,130,24,156,8,143,40,143,90,150,110,135]);function n(n){var i=n.target.getBoundingClientRect(),r=n.targetTouches;start(),globalJKey=0;for(var a=0;a<r.length;a++)for(var l=parseInt(128*(r[a].clientX-i.left)/e.clientWidth),o=parseInt(160*(r[a].clientY-i.left)/e.clientHeight),c=0;c<8;c++)l>t[2*c]&&l<t[2*c]+10&&o>t[2*c+1]-10&&o<t[2*c+1]&&(globalJKey|=1<<c),o<100&&fullScr();n.preventDefault()}e.addEventListener("touchstart",n,!1),e.addEventListener("touchend",n,!1)},!1),document.addEventListener("keydown",keyDownHandler,!1),document.addEventListener("keyup",keyUpHandler,!1),document.addEventListener("keypress",keyPressHandler,!1),display.init(),display.reset(),cpu.init(),cpu.load(code),redraw();';
if (im.length > 1)
s += 'var im=[' + im.join(',') + '];display.drawIco(im);';
s += '</script></body></html>';
return s;
}