0
\$\begingroup\$

Which is the most efficient way to animate an HTML <img> element in vanilla javascript so that it jumps and falls back with acceleration. Edit: Im adding source code index.html

<!doctype html>
<html>
<head>
    <title>T-rex Runner by Gyan Prakash</title>
</head>
<body onclick="jump()">
<img src="./assets/dino.png" height="70px" width="90px" id="dino">
<img src="./assets/cacti1.png" height="40px" width="30px" id="cacti1">
<img src="./assets/cacti2.png" height="40px" width="30px" id="cacti2">
<img src="./assets/volcano.gif" height="60px" width="50px" id="volcano">
<img src="./assets/shield.png" height="22px" width="20px" id="shield">
<img src="./assets/wings.png" height="30px" width="50px" id="wings"><br>
<img src="./assets/level.png" height="20px" width="400px" id="level">
<script type="text/javascript" src="./main.js"></script>
<noscript><h3>Ah, Can you tell me who in this universe can expect to play a web based game with a browser not running javascript?</h3></noscript>
</body>
</html>

main.js

var dino = document.getElementById("dino");
var level = document.getElementById("level");
var cac1 = document.getElementById("cacti1");
var cac2 = document.getElementById("cacti2");
var vol = document.getElementById("volcano");
var shield = document.getElementById("shield");
var wings = document.getElementById("wings");
dino.style.position = "absolute";
level.style.position = "absolute";
cac1.style.position = "absolute";
cac2.style.position = "absolute";
vol.style.position = "absolute";
shield.style.position = "absolute";
wings.style.position = "absolute";
level.style.top = "160px";
dino.style.top = "100px";
var pos = 100;
function jump() {
function up() {
if (pos < 50) {
    clearInterval(upt);
    } else {
pos--;
dino.style.top = pos + "px";
}
}
var upt = setInterval(up, 10);
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ What did you try so far? There are lots of tutorials about how to move and animate DOM elements in JavaScript. Which guide are you currently following, and where did to get stuck? \$\endgroup\$ Commented May 29, 2020 at 10:32
  • \$\begingroup\$ None of them can jump like a real object with acceleration!!! \$\endgroup\$ Commented May 29, 2020 at 10:53
  • \$\begingroup\$ Please demonstrate the code you've used so far, and we can help you add acceleration to it. \$\endgroup\$ Commented May 29, 2020 at 10:54
  • \$\begingroup\$ its just a simple DOM element to get the image up with 1px/10ms till it reach 60px up. \$\endgroup\$ Commented May 29, 2020 at 13:19
  • \$\begingroup\$ Great. Now edit your question to show that code. \$\endgroup\$ Commented May 29, 2020 at 13:24

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.