const canvas = document.getElementById('drawing-board'); const ctx = canvas.getContext('2d'); const reset = document.getElementById("reset"); robot = document.getElementById('robot'); cross = document.getElementById('cross'); const canvasOffsetX = canvas.offsetLeft; const canvasOffsetY = canvas.offsetTop; let scale = 2 canvas.width = window.innerWidth - canvasOffsetX * 2; canvas.height = window.innerHeight - canvasOffsetY * 2; //robot.style.top = canvas.height/2 - robot.height/2 - canvasOffsetY; //robot.style.left = canvas.width/2 - robot.width/2 - canvasOffsetX; const tr = {x: 0, y: 0, a: Math.PI} ctx.translate(canvas.width/2 + canvasOffsetX, canvas.height/2 + canvasOffsetY) reset.style.top = canvasOffsetY * 2 + "px"; reset.style.right = canvasOffsetX * 2 + "px"; let lineWidth = 2; let strokeColor = "red" let drawings = []; ctx.lineWidth = lineWidth; ctx.lineCap = 'square'; ctx.strokeStyle = strokeColor; ctx.fillStyle = "red"; const wall = (element) => { ctx.fillRect(element.x * canvas.width - canvasOffsetX - robot.offsetWidth/2, element.y * canvas.height - canvasOffsetY, robot.offsetWidth, 5); ctx.stroke(); ctx.beginPath(); } canvas.addEventListener('mousedown', (e) => { cross.style.visibility = "visible"; cross.style.left = e.layerX - canvasOffsetX + "px" cross.style.top = e.layerY - canvasOffsetY + "px" moveTo(-(e.layerX - canvasOffsetX - canvas.width/2) * scale, -(e.layerY - canvasOffsetY - canvas.height/2) * scale) tr.a = Math.atan((e.layerX - document.querySelector('body').clientWidth/2)/(e.layerY - document.querySelector('body').clientHeight/2)) if ( e.layerY < canvas.height/2 ) { tr.a += Math.PI } robot.style.transform = 'translate(-50%, -50%) rotate('+ -(tr.a+Math.PI)+'rad)'; tr.x -= 120 * Math.sin(tr.a) tr.y -= 120 * Math.cos(tr.a) // (-(e.layerX - canvasOffsetX - canvas.width/2) * scale, -(e.layerY - canvasOffsetY - canvas.height/2) * scale) update() }); client.on('message', function (topic, message) { let val = message.toString(); var n = parseInt(val); if (!isNaN(n)){ if (n < 150){ drawings.push({x: ((n - 30) * Math.sin(tr.a) - tr.x)/canvas.width, y: ((n - 30) * Math.cos(tr.a) - tr.y)/canvas.height}) // add a wall } update(); } }); /* canvas.addEventListener('touchstart', (e) => { drawCross(e.targetTouches[0].layerX, e.targetTouches[0].layerY); // moveTo((e.targetTouches[0].layerX - canvasOffsetX - canvas.width/2) * scale, (e.targetTouches[0].layerY - canvasOffsetY - canvas.height/2) * scale) tr.a = Math.atan((-e.targetTouches[0].layerX + canvasOffsetX + canvas.width/2)/(e.targetTouches[0].layerY - canvasOffsetY - canvas.height/2)) if ( e.targetTouches[0].layerY - canvasOffsetY > canvas.height/2 ) { tr.a += Math.PI } // if wall detected then // drawings.push({x: (canvasOffsetX - robot.width/2)/canvas.width, y: -(20 * scale + canvasOffsetY + robot.height)/canvas.height, tr: {x: tr.x, y: tr.y, a: tr.a}}) // add a wall update() }); */ reset.onclick = () => { drawings = []; tr.x = 0 tr.y = 0 tr.a = 0 robot.style.transform = 'translate(-50%, -50%) rotate(0deg)'; update(); } function resizeCanvas() { canvas.width = window.innerWidth - canvasOffsetX * 2; canvas.height = window.innerHeight - canvasOffsetY * 2; ctx.strokeStyle = strokeColor; ctx.lineWidth = lineWidth; ctx.lineCap = 'round'; ctx.fillStyle = "red"; robot.style.top = canvas.height/2 - robot.height/2 - canvasOffsetY; robot.style.left = canvas.width/2 - robot.width/2 - canvasOffsetX; ctx.setTransform(1, 0, 0, 1, 0, 0); } function redrawCanvas(){ ctx.translate(canvas.width/2 + canvasOffsetX, canvas.height/2 + canvasOffsetY) drawings.forEach(element => { //ctx.translate(tr.x, tr.y) // ctx.translate(element.tr.x, element.tr.y) wall(element); // element.tr.x = tr.x // element.tr.y = tr.y }); } function update(){ resizeCanvas(); redrawCanvas(); } const updateOnFrame = () => { update(); requestAnimationFrame(updateOnFrame) } //updateOnFrame(); window.onresize = update;