(function(){vare;e=function(){functione(e){vart,n;this.socket=void0,this.ws=void0,this.canvas=void0,n="decoder.js",this.scale=1,e.ws&&(this.ws=e.ws),this.canvas=e.element,"string"==typeofthis.canvas&&(this.canvas=document.getElementById(this.canvas)),e.worker&&(n=e.worker),this.decoder=newWorker(n),(t=this).mouseMask=0,this.decoder.onmessage=function(e){returnt.process(e.data)}}returne.prototype.init=function(){varn;returnn=this,newPromise(function(e,t){returnn.canvas?(n.initInputEvent(),e()):t("Canvas is not set")})},e.prototype.initInputEvent=function(){vare,n,s,o;if((s=this).canvas&&(n=function(e){vart;returnt=s.canvas.getBoundingClientRect(),{x:Math.floor((e.clientX-t.left)/s.scale),y:Math.floor((e.clientY-t.top)/s.scale)}},o=function(e){vart;returnt=n(e),s.sendPointEvent(t.x,t.y,s.mouseMask)},s.canvas))returns.canvas.oncontextmenu=function(e){returne.preventDefault(),!1},s.canvas.onmousemove=function(e){returno(e)},s.canvas.onmousedown=function(e){vart;returnt=1<<e.button,s.mouseMask=s.mouseMask|t,o(e)},s.canvas.onmouseup=function(e){vart;returnt=1<<e.button,s.mouseMask=s.mouseMask&~t,o(e)},s.canvas.onkeydown=s.canvas.onkeyup=function(e){vart;switch(e.keyCode){case8:t=65288;break;case9:t=65417;break;case13:t=65293;break;case27:t=65307;break;case46:t=65535;break;case38:t=65362;break;case40:t=65364;break;case37:t=65361;break;case39:t=65363;break;case91:t=65511;break;case93:t=65512;break;case16:t=65505;break;case17:t=65507;break;case18:t=65513;break;case20:t=65509;break;case113:t=65471;break;case112:t=65470;break;case114:t=65472;break;case115:t=65473;break;case116:t=65474;break;case117:t=65475;break;case118:t=65476;break;case119:t=65477;break;case120:t=65478;break;case121:t=65479;break;case122:t=65480;break;case123:t=65481;break;default:t=e.key.charCodeAt(0)}if(e.preventDefault(),t)return"keydown"===e.type?s.sendKeyEvent(t,1):"keyup"===e.type?s.sendKeyEvent(t,0):void0},this.canvas.addEventListener("wheel",function(e){vart;returnt=n(e),e.preventDefault(),e.deltaY<0?(s.sendPointEvent(t.x,t.y,8),voids.sendPointEvent(t.x,t.y,0)):(s.sendPointEvent(t.x,t.y,16),s.sendPointEvent(t.x,t.y,0))}),this.canvas.onpaste=function(e){vart;returnt=void0,window.clipboardData&&window.clipboardData.getData?t=window.clipboardData.getData("Text"):e.clipboardData&&e.clipboardData.getData&&(t=e.clipboardData.getData("text/plain")),!!t&&(e.preventDefault(),this.sendTextAsClipboard(t))},e=function(e){if(console.log("unload"),s.socket)returns.socket.close()},window.addEventListener("unload",e),window.addEventListener("beforeunload",e)},e.prototype.initCanvas=function(e,t,n){returnthis.depth=n,this.canvas.width=e,this.canvas.height=t,this.resolution={w:e,h:t,depth:this.depth},this.decoder.postMessage(this.resolution),this.canvas.style.cursor="none",this.setScale(this.scale)},e.prototype.process=function(e){vart,n,s;if(this.socket)returnn=newUint8Array(e.pixels),(s=(t=this.canvas.getContext("2d",{alpha:!1})).createImageData(e.w,e.h)).data.set(n),t.putImageData(s,e.x,e.y)},e.prototype.setScale=function(e){if(this.scale=e,this.canvas)returnthis.canvas.style.transformOrigin="0 0",this.canvas.style.transform="scale("+e+")"},e.prototype.connect=function(e,t){varn;if((n=this).socket&&this.socket.close(),this.ws)returnthis.socket=newWebSocket(this.ws),this.socket.binaryType="arraybuffer",this.socket.onopen=function(){returnconsole.log("socket opened"),n.initConnection(e,t)},this.socket.onmessage=function(e){returnn.consume(e)},this.socket.onclose=function(){returnn.socket=null,n.canvas.style.cursor="auto",n.canvas&&n.canvas.getContext("2d").clearRect(0,0,n.resolution.w,n.resolution.h),console.log("socket closed")}},e.prototype.disconnect=function(){returnthis.socket.close()},e.prototype.initConnection=function(e,t){varn;return(n=newUint8Array(e.length+3))[0]=32,n[1]=2,n[2]=50,t&&(t.bbp&&(n[0]=t.bbp),t.flag&&(n[1]=t.flag),t.quality&&(n[2]=t.quality)),n.set((newTextEncoder).encode(e),3),this.socket.send(this.buildCommand(1,n))},e.prototype.sendPointEvent=function(e,t,n){vars;if(th