Skip to content

Instantly share code, notes, and snippets.

@jasontwuk
Created November 7, 2019 15:37
Show Gist options
  • Save jasontwuk/433b2626ef2502e9837caf28deb32ebd to your computer and use it in GitHub Desktop.
Save jasontwuk/433b2626ef2502e9837caf28deb32ebd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Ch14-exercise-The Cat's Hat</title>
<style>body { min-height: 200px }</style>
</head>
<body>
<img src="img/cat.png" id="cat" style="position: absolute">
<img src="img/hat.png" id="hat" style="position: absolute">
<script>
let cat = document.querySelector("#cat");
let hat = document.querySelector("#hat");
let angle = 0;
let lastTime = null;
function animate(time) {
if (lastTime != null) angle += (time - lastTime) * 0.001;
lastTime = time;
cat.style.top = (Math.sin(angle) * 40 + 40) + "px";
cat.style.left = (Math.cos(angle) * 200 + 230) + "px";
// Your extensions here.
// Make the hat orbit at opposite sides of the ellipse.
hat.style.top = (Math.sin(angle) * -40 + 40) + "px";
hat.style.left = (Math.cos(angle) * -200 + 230) + "px";
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment