Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (68 sloc) 1.61 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Events: Task 2</title>
<style>
p {
color: purple;
margin: 0.5em 0;
}
* {
box-sizing: border-box;
}
canvas {
border: 1px solid black;
}
</style>
<link rel="stylesheet" href="../styles.css" />
</head>
<body>
<section class="preview">
</section>
<canvas width="480" height="320">
</canvas>
</body>
<script>
let canvas = document.querySelector('canvas');
let ctx = canvas.getContext('2d');
function drawCircle(x, y, size) {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = 'black';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
let x = 50;
let y = 50;
const size = 30;
drawCircle(x, y, size);
// Add your code here
document.addEventListener('keypress', moveCircle);
function moveCircle(e) {
let keyPressed = e.key.toLowerCase()
console.log(keyPressed);
switch (keyPressed) {
case 'w':
y -= 10
drawCircle(x, y, size)
break;
case 's':
y += 10
drawCircle(x, y, size)
break;
case 'a':
x -= 10
drawCircle(x, y, size)
break;
case 'd':
x += 10
drawCircle(x, y, size)
break;
default:
break;
}
}
</script>
</html>
You can’t perform that action at this time.