-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.js
80 lines (63 loc) · 2.18 KB
/
cat.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// https://stackoverflow.com/questions/13778439/how-to-get-the-css-left-property-value-of-a-div-using-javascript
function getCssProperty(elem, property){
return window.getComputedStyle(elem,null).getPropertyValue(property);
}
class Cat {
constructor(name, elem) {
this.name = name;
this.elem = elem;
this.left = parseInt(getCssProperty(elem, "left").slice(0, -2));
this.top = parseInt(getCssProperty(elem, "top").slice(0, -2));
this.leftHome = parseInt(getCssProperty(elem, "left").slice(0, -2));
this.topHome = parseInt(getCssProperty(elem, "top").slice(0, -2));
}
setLeft(num) {
let px = num + "px";
this.left = num;
this.elem.style.left = px;
}
setTop(num) {
let px = num + "px";
this.top = num;
this.elem.style.top = px;
}
}
// make cats
let catTop = new Cat("Peeking", document.getElementById("cat1"));
let catRight = new Cat("Grumpy", document.getElementById("cat2"));
let catLeft = new Cat ("Cranky", document.getElementById("cat3"));
let catBottom = new Cat ("Droopy", document.getElementById("cat4"));
// make array of cats
let allCats = [catTop, catRight, catLeft, catBottom];
// center of container for future calculations
// let originX = window.innerWidth/2;
// let originY = window.innerHeight/2;
// Add cats
document.addEventListener("mousemove", function(e) {
let x = e.clientX;
let y = e.clientY;
let percentX = x/(window.innerWidth);
let percentY = y/(window.innerHeight);
// let percentX = x/(window.innerWidth);
// let percentY = y/(window.innerHeight);
// console.log('mousemove left: ' + x + " top: " + y);
// console.log('percent left: ' + percentX + " top: " + percentY);
for (let kitty of allCats) {
let homeX = kitty.leftHome;
let homeY = kitty.topHome;
let newX;
let newY;
if (homeX > 0) {
newX = homeX*percentX;
} else {
newX = homeX-homeX*percentX;
}
if (homeY > 0) {
newY = homeY*percentY;
} else {
newY = homeY-homeY*percentY;
}
kitty.setLeft(newX);
kitty.setTop(newY);
}
})