Skip to content

Commit

Permalink
add moving Sonic gif around a page
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoconnordev committed Oct 14, 2023
1 parent bffd592 commit 934dc6a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
Binary file added classes/sonic-speed/assets/sonic-running.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions classes/sonic-speed/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sonic Speed</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<h1>Press an arrow key to move Sonic</h1>
<img id="sonic-running" src="./assets/sonic-running.gif">

<script src="./script.js"></script>
</body>

</html>
40 changes: 40 additions & 0 deletions classes/sonic-speed/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Select sonicGif in DOM so we can do things to it with javascript
const stepSize = 100;
const sonicGif = document.getElementById('sonic-running');

// Create functions which move Sonic around page using margins
function moveSonicDown() {
sonicGif.style.marginTop = (parseInt(window.getComputedStyle(sonicGif).marginTop) + stepSize) + 'px';
}

function moveSonicUp() {
sonicGif.style.marginTop = (parseInt(window.getComputedStyle(sonicGif).marginTop) - stepSize) + 'px';
}

function moveSonicRight() {
sonicGif.style.marginLeft = (parseInt(window.getComputedStyle(sonicGif).marginLeft) + stepSize) + 'px';
}

function moveSonicLeft() {
sonicGif.style.marginLeft = (parseInt(window.getComputedStyle(sonicGif).marginLeft) - stepSize) + 'px';
}

// Add key press event listener. Only do things for arrow keys
document.onkeydown = function(e) {
if (e.key === 'ArrowDown') {
moveSonicDown();
return; // Once we've found our key let's exit the function with return
}
if (e.key === 'ArrowUp') {
moveSonicUp();
return;
}
if (e.key === 'ArrowRight') {
moveSonicRight();
return;
}
if (e.key === 'ArrowLeft') {
moveSonicLeft();
return;
}
};
4 changes: 4 additions & 0 deletions classes/sonic-speed/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#sonic-running {
height: 100px;
aspect-ratio: auto;
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h2>Navigation links</h2>
<li><a href="./classes/birds-with-hands">Birds with hands</a></li>
<li><a href="./classes/coder-dojo-homepage">CoderDojo homepage</a></li>
<li><a href="./classes/noughts-and-crosses/">X's and O's game</a></li>
<li><a href="./classes/sonic-speed/">Sonic Speed</a></li>
</ul>
</nav>

Expand Down

0 comments on commit 934dc6a

Please sign in to comment.