forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_3_the_cats_hat.html
More file actions
27 lines (22 loc) · 832 Bytes
/
14_3_the_cats_hat.html
File metadata and controls
27 lines (22 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!doctype html>
<meta charset="utf8">
<base href="https://eloquentjavascript.net/">
<style>body { min-height: 200px }</style>
<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";
hat.style.top = (Math.sin(angle + Math.PI) * 40 + 40) + "px";
hat.style.left = (Math.cos(angle + Math.PI) * 200 + 230) + "px";
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>