-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (47 loc) · 1.35 KB
/
index.js
File metadata and controls
58 lines (47 loc) · 1.35 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const STEP_SIZE_PX = 10;
const STEP_INTERVAL_MS = 50;
const DANCE_TIME_MS = 5000;
const WALKING_CAT_URL = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
const DANCING_CAT_URL = 'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif';
function walk(img, startPos, stopPos) {
return new Promise((resolve) => {
let currentPos = startPos;
img.style.left = `${currentPos}px`;
img.src = WALKING_CAT_URL;
const intervalId = setInterval(() => {
currentPos += STEP_SIZE_PX;
img.style.left = `${currentPos}px`;
if (currentPos >= stopPos) {
clearInterval(intervalId);
resolve();
}
}, STEP_INTERVAL_MS);
});
}
function dance(img) {
return new Promise((resolve) => {
img.src = DANCING_CAT_URL;
setTimeout(() => {
img.src = WALKING_CAT_URL;
resolve();
}, DANCE_TIME_MS);
});
}
function catWalk() {
const img = document.querySelector('img');
if (!img) {
return;
}
const startPos = -img.width;
const centerPos = (window.innerWidth - img.width) / 2;
const stopPos = window.innerWidth;
function performCatWalk() {
walk(img, startPos, centerPos)
.then(() => dance(img))
.then(() => walk(img, centerPos, stopPos))
.then(() => performCatWalk());
}
performCatWalk();
}
window.addEventListener('load', catWalk);