forked from MaxLaumeister/bitlisten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floatable.js
75 lines (63 loc) · 2.07 KB
/
floatable.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
function Floatable() {
this.velocity = {
x : 0,
y : -1
};
this.div = document.createElement("div");
this.div.className = "floatableDiv";
document.getElementById(pageDivId).appendChild(this.div);
this.innerDiv = document.createElement("div");
this.div.appendChild(this.innerDiv);
this.innerDiv.className = "innerDiv";
// Add this object to the update array
updateTargets.push(this);
}
Floatable.prototype.update = function() {
this.x += this.velocity.x;
this.y += this.velocity.y;
this.velocity.x += Math.random() * 0.1 - 0.05;
if (this.velocity.x > 2)
this.velocity.x = 2;
if (this.velocity.x < -2)
this.velocity.x = -2;
if (this.x < 0)
this.velocity.x += 0.02;
if (this.x > window.innerWidth - this.width)
this.velocity.x -= 0.02;
this.updateDiv();
if (this.y < -this.height)
this.removeSelf();
}
Floatable.prototype.updateDiv = function() {
this.div.style.top = this.y + "px";
this.div.style.left = this.x + "px";
}
Floatable.prototype.removeSelf = function() {
document.getElementById(pageDivId).removeChild(this.div);
// Remove self from update array
updateTargets.splice(updateTargets.indexOf(this), 1);
}
Floatable.prototype.addImage = function(image, width, height) {
this.canvas = document.createElement('canvas');
this.image = image;
this.canvas.height = height;
this.canvas.width = width;
this.canvas.style.position = "absolute";
this.canvas.style.top = "0px";
this.canvas.style.left = "0px";
var ctx = this.canvas.getContext("2d");
ctx.drawImage(this.image, 0, 0, width-1, height-1);
this.div.appendChild(this.canvas);
}
Floatable.prototype.addText = function(text) {
this.innerDiv.innerHTML += text;
}
Floatable.prototype.initPosition = function() {
this.x = Math.random() * (window.innerWidth - this.width);
this.y = window.innerHeight;
this.updateDiv();
this.div.style.width = this.width + "px";
this.div.style.height = this.height + "px";
this.innerDiv.style.top = (this.height / 2 - this.innerDiv.offsetHeight / 2) + 'px';
// Centers the text within the bubble
}