var canvas = document.querySelector('canvas');
// returning a drawing context to a variable 'c'
// allows you to draw 2d elements
var c = canvas.getContext('2d');
canvas.width = 1600;
canvas.height = 1400;
canvas.style.width = 1600;
canvas.style.height = 1400;
///----------------------------------------------------
var canvasPos = getPosition(canvas);
var mouseX = 0;
var mouseY = 0;
var mouseWidth = 30;
var mouseHeight = 30;
canvas.addEventListener("mousemove", setMousePosition);
function setMousePosition(e) {
mouseX = e.clientX - canvasPos.x;
mouseY = e.clientY - canvasPos.y;
}
function update() {
c.clearRect(0, 0, canvas.width, canvas.height);
//1. create landscapes
// a. Maze (blue region)
c.beginPath();
c.moveTo(150, 1300); //start
c.lineTo(150, 400);
c.lineTo(1350, 400);
c.lineTo(1350, 450);
c.lineTo(700, 450);
c.lineTo(700, 1300);
c.lineTo(150, 1300);
c.fillStyle = "#C1EEFF";
c.fill();
//Red region
c.beginPath();
c.moveTo(1350, 400);
c.lineTo(1350, 450);
c.lineTo(1300, 450);
c.lineTo(1300, 400);
c.moveTo(1350, 400);
c.fillStyle = "#FF4000";
c.fill();
//2. create cursor
c.beginPath();
c.rect(mouseX, mouseY, mouseWidth, mouseHeight);
c.fillStyle = "#928C6F";
c.fill();
// console.log(mouseX, mouseY);
requestAnimationFrame(update);
}
canvas.addEventListener("mousemove", update());
function getPosition(el) {
var xPosition = 0;
var yPosition = 0;
while (el) {
xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
el = el.offsetParent;
}
return {
x: xPosition,
y: yPosition
};
}
h1 {
text-align: center;
}
canvas {
border: 1px solid black;
background-color: black;
/* HERE'S WHERE THE CHANGE IS BEING MADE */
/* width: 100%; */
/* height: auto; */
margin: 0;
display: inline;
/* cursor: none; */
}
.container {
text-align: center;
margin: 0;
/* display: flex;
justify-content: center; */
}
body {
margin: 0;
max-width: 1000px;
background-color: #6d72c3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="maze.css">
<title>Maze Game</title>
</head>
<body>
<div class="container">
<canvas></canvas>
</div>
<script src = "maze.js"></script>
</body>
</html>