-
Notifications
You must be signed in to change notification settings - Fork 1
/
monkeylib.js
56 lines (47 loc) · 1.63 KB
/
monkeylib.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
var monkeySrcs = [ "1f435.svg", "1f648.svg", "1f649.svg" ];
function Monkey(canvas, pos) {
this.pos = pos;
this.weight = .75 * (1 + Math.random())/2;
this.canvas = canvas;
this.img = new Image();
this.img.src = monkeySrcs[pos];
this.img.className = "monkey";
this.canvas.appendChild(this.img);
}
Monkey.prototype.maxSize = function() {
return this.canvas.height / MONKEY_COUNT - 10;
}
Monkey.prototype.size = function() {
return this.maxSize() * this.weight ;
}
Monkey.prototype.redraw = function() {
this.img.style.top = (this.canvas.height / MONKEY_COUNT * this.pos + (this.maxSize() - this.size()) / 2) + "px";
this.img.style.width = this.size() + "px";
this.img.style.height = this.size() + "px";
}
function Banana(canvas) {
this.canvas = canvas;
this.img = new Image();
this.img.src = "1f34c.svg";
this.img.className = "banana";
this.img.style.transform = "rotate("+((Math.random()-.5)*50)+"deg)";
this.relX = Math.random();
this.relY = Math.random();
this.canvas.appendChild(this.img);
}
Banana.prototype.x = function() {
var maxMonkeyWidth = this.canvas.height / MONKEY_COUNT - 10;
return maxMonkeyWidth + this.relX * (this.canvas.width - maxMonkeyWidth);
}
Banana.prototype.y = function() {
return this.relY * this.canvas.height;
}
Banana.prototype.size = function() {
return Math.min(this.canvas.height / 10, this.canvas.width / 10);
}
Banana.prototype.redraw = function() {
this.img.style.top = this.y() + "px";
this.img.style.left = this.x() + "px";
this.img.style.width = this.size() + "px";
this.img.style.height = this.size() + "px";
}