/// CONFIG var FPS=30; //How often tick() is called each second //Width and height of the canvas. -1 = maximum possible (whole window) var WIDTH = -1; var HEIGHT = -1; var HIDDEN = false; var LEFT = 0.5; var TOP = 0.5; var BORDER = 0; var BORDER_COLOR = "black"; var FULLSCREEN = false; var PAGE_TITLE = "Empty.js"; var PAUSED = false; //set to true to pause calling tick(). var CURSOR = "default" //set cursor on canvas //EMPTYJS var cv, c; var empty_keys = []; var empty_mouse_x = -1; var empty_mouse_y = -1; var empty_mouse_keys= [false, false, false]; var empty_new_mouse_x = -1; var empty_new_mouse_y = -1; document.addEventListener("DOMContentLoaded",function() { //ONLOAD cv = document.getElementById("cv"); if (cv == null) { cv = document.createElement("canvas"); cv.id = "cv"; document.body.appendChild(cv); } c = cv.getContext("2d"); document.body.style.margin="0"; window.addEventListener("resize",empty_resize); document.addEventListener("fullscreenchange",function() { cv.focus(); empty_fullscreen(); }); document.addEventListener("mousemove",function(e) { //MOUSE MOVEMENT var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = e.clientX - boxX; var y = e.clientY - boxY; empty_new_mouse_x = e.clientX; empty_new_mouse_y = e.clientY; if (typeof onMouseMove == "function") { onMouseMove(x,y); } }); document.addEventListener("keydown", function(e) { //KEY DOWN if (e.isComposing || e.keyCode === 229) { return; } empty_setKey(e.code, true); if (typeof onKeyDown == "function") { onKeyDown(e); } empty_fullscreen(); }); document.addEventListener("keyup", function(e) { //KEY UP if (e.isComposing || e.keyCode === 229) {return;} empty_setKey(e.code, false); if (typeof onKeyUp == "function") { onKeyUp(e); } }); document.addEventListener("click", function(e) { //CLICK empty_fullscreen(); if (!HIDDEN && !PAUSED) { cv.focus(); } }); document.addEventListener("mousedown", function(e) { //MOUSEDOWN if (e.button==0) { empty_fullscreen(); } if (!HIDDEN && !PAUSED) { cv.focus(); } empty_new_mouse_x = e.clientX; empty_new_mouse_y = e.clientY; empty_mouse_keys[e.button]=true; if (typeof onMouseDown == "function") { onMouseDown(getMousePos().x, getMousePos().y, e.button); } }); document.addEventListener("mouseup",function(e) { //MOUSEUP if (e.button==0) { empty_fullscreen(); } empty_new_mouse_x = e.clientX; empty_new_mouse_y = e.clientY; empty_mouse_keys[e.button]=false; if (typeof onMouseUp == "function") { onMouseUp(getMousePos().x, getMousePos().y, e.button); } }); document.addEventListener("contextmenu",function(e) { //CONTEXTMENU if (!HIDDEN && !PAUSED) { cv.focus(); e.preventDefault(); } cv.focus(); }); document.addEventListener("touchmove",function(e) { empty_new_mouse_x = e.touches[0].clientX; empty_new_mouse_y = e.touches[0].clientY; var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; if (typeof onMouseMove == "function") { onMouseMove(x,y); } }) document.addEventListener("touchstart",function(e) { empty_new_mouse_x = e.touches[0].clientX; empty_new_mouse_y = e.touches[0].clientY; var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; if (typeof onMouseDown == "function") { onMouseDown(x,y,0); } }) document.addEventListener("touchstart",function(e) { empty_new_mouse_x = e.touches[0].clientX; empty_new_mouse_y = e.touches[0].clientY; var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; empty_mouse_keys[0] = true; if (typeof onMouseDown == "function") { onMouseDown(x,y,0); } }) document.addEventListener("touchend",function(e) { var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; empty_mouse_keys[0] = false; if (typeof onMouseUp == "function") { onMouseUp(x,y,0); } }) empty_resize(); if (typeof init == "function") { init(); } setTimeout(empty_tick,1000/FPS); }); function empty_tick() { //TICK window.document.title=PAGE_TITLE; if (HIDDEN && cv.style.display!="hidden") { cv.style.display="none"; } if (!HIDDEN && cv.style.display!="inline-block") { cv.style.display="inline-block"; } if (BORDER>0) { cv.style.borderStyle="solid"; } else { cv.style.borderStyle="none"; } cv.style.borderWidth=BORDER; cv.style.borderColor=BORDER_COLOR; if (typeof tick == "function" && !PAUSED) { tick(); } document.body.style.cursor = CURSOR; empty_mouse_x = empty_new_mouse_x; empty_mouse_y = empty_new_mouse_y; setTimeout(empty_tick,1000/FPS); } function empty_fullscreen() { if (FULLSCREEN) { FULLSCREEN=false; if(document.body.requestFullscreen) { document.body.requestFullscreen(); } else if(document.body.mozRequestFullScreen) { document.body.mozRequestFullScreen(); } else if(document.body.msRequestFullscreen) { document.body.msRequestFullscreen(); } else if(document.body.webkitRequestFullscreen) { document.body.webkitRequestFullscreen(); } else { FULLSCREEN=true; } } } function empty_resize() { //WINDOW RESIZE if (typeof onresize=="function") { onresize(window.innerWidth, window.innerHeight); } } function empty_setKey(code, value) { //SET KEYSTATE empty_keys[code] = value; } // DATA FUNCTIONS function getMouseKey(key) { //GET MOUSE KEY return empty_mouse_keys[key]; } function getKey(code) { //GET KEYSTATE if (code in empty_keys) return empty_keys[code]; return false; } function getMousePosGobal() { //MOUSE POSITION return {x: empty_mouse_x, y: empty_mouse_y}; } function getMousePos() { var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_new_mouse_x - box.top; var y = empty_new_mouse_y - box.left; return {x: x, y: y}; } function getLastMousePos() { var box = cv.getBoundingClientRect(); var boxX = box.top; var boxY = box.left; var x = empty_mouse_x - box.top; var y = empty_mouse_y - box.left; return {x: x, y: y}; } function getRelativeMouseMovement() { var m1=getMousePos(); var m2=getLastMousePos(); var ret={x: m1.x - m2.x, y: m1.y - m2.y}; return ret; } // DRAWING FUNCTIONS function fillBackground() { c.fillRect(0,0,cv.width,cv.height); } function fillCircle(x,y,r) { c.beginPath(); c.moveTo(x,y); c.arc(x,y,r,0,Math.PI*2); c.fill(); } function strokeCircle(x,y,r) { c.beginPath(); c.moveTo(x+r,y); c.arc(x,y,r,0,Math.PI*2); c.stroke(); } function line(x1,y1,x2,y2) { c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.stroke(); } function drawRotatedImage(image,x,y,angle,width,height) { var origin = {x: width/2, y: height/2}; c.save(); c.translate(x,y); c.rotate(angle); c.drawImage(image,-origin.x,-origin.y,width,height); c.restore(); } // CALCULATION FUNCTIONS function distance(x1,y1,x2,y2) { //DISTANCE return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2)); } function randomRange(min,max) { //RANDOM RANGE return Math.floor(Math.random() * (max-min+1) + min); } function range(j) { var arr=[]; for (var i=0; imax) return max; if (num=rx && py>=ry && px<=rx+rw && py<=ry+rh; } function pointInCircle(px,py,cx,cy,cr) { return distance(px,py,cx,cy)<=cr; } function rectsCollide(x1,y1,dx1,dy1,x2,y2,dx2,dy2) { return (empty_1dcollide(x1,dx1,x2,dx2) && empty_1dcollide(y1,dy1,y2,dy2)) } function empty_1dcollide(x1,dx1,x2,dx2) { if (x2 >= x1 && x2 < x1+dx1) { return true; } if (x1 >= x2 && x1 < x2+dx2) { return true; } return false; }