Skip to content
Permalink
ac9b814517
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
79 lines (66 sloc) 1.54 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" tabindex="0">
</canvas>
<script>
const canvas = document.querySelector('canvas');
const 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('keydown', event =>{
if(event.keyCode == 65) {
// Move ('left');
x -= 1;
console.log(x);
}
else if(event.keyCode == 68) {
// Move ('right');
x += 1;
console.log(x);
}
else if(event.keyCode == 87) {
// Move ('up');
y -= 1;
console.log(y);
}
else if(event.keyCode == 83) {
// Move ('down');
y += 1;
console.log(y);
}
drawCircle(x, y, size);
});
</script>
</body>
</html>