-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphysics.js
More file actions
96 lines (90 loc) · 2.86 KB
/
physics.js
File metadata and controls
96 lines (90 loc) · 2.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var physicsObjects = [];
function enablePhysics() {
var header = document.getElementById("header-text");
header.removeAttribute("onclick");
init(header, true);
var code = document.getElementsByTagName("p");
for(var i = 0; i < code.length; i++) {
init(code[i], false);
}
window.setInterval(calc, 16);
}
function init(element, pad) {
var id = getID();
var s = "";
var skip = false;
for(var i = 0; i < element.innerHTML.length; i++) {
if(element.innerHTML.charAt(i) == "<") {
skip = true;
}
if(element.innerHTML.charAt(i) == ">") {
skip = false;
s += ">";
continue;
}
if(skip) {
s += element.innerHTML.charAt(i);
continue;
}
var eid = id + "-" + i;
s += (pad ? " " : "") + "<span id=\"" + eid + "\">" + element.innerHTML.charAt(i) + "</span>";
physicsObjects.push({
id: eid,
x: 0,
y: 0,
width: 0,
height: 0,
vx: 0,
vy: 0
});
}
element.innerHTML = s;
for(var i = 0; i < physicsObjects.length; i++) {
var bounds = document.getElementById(physicsObjects[i].id).getBoundingClientRect();
physicsObjects[i].x = bounds.left;
physicsObjects[i].y = bounds.top;
physicsObjects[i].width = bounds.width;
physicsObjects[i].height = bounds.height;
physicsObjects[i].vx = (Math.random() - 0.5) * 10;
physicsObjects[i].vy = (Math.random() - 0.5) * 10;
}
for(var i = 0; i < physicsObjects.length; i++) {
var e = document.getElementById(physicsObjects[i].id);
e.style.position = "absolute";
e.style.left = physicsObjects[i].x + "px";
e.style.top = physicsObjects[i].y + "px";
}
}
function calc() {
for(var i = 0; i < physicsObjects.length; i++) {
var obj = physicsObjects[i];
if(obj.y + obj.height + obj.vy > screen.height - 200) {
obj.vy = -obj.vy * 0.5;
/*if(Math.random() > 0.1) {
obj.vx = Math.random() * 10;
} else {
obj.vx = -Math.random() * 10;
}*/
obj.vx *= 0.9;
if(Math.abs(obj.vy) < 4) {
obj.vy *= Math.random() * 10;
}
} else {
obj.vy += 0.981;
}
if((obj.vx < 0 && outOfBoundsX(obj.x + obj.vx)) || (obj.vx > 0 && outOfBoundsX(obj.x + obj.vx + obj.width))) {
obj.vx = -10 * Math.sign(obj.vx);
}
obj.x += obj.vx;
obj.y += obj.vy;
var e = document.getElementById(obj.id);
e.style.left = obj.x + "px";
e.style.top = obj.y + "px";
}
}
function outOfBoundsX(x) {
return x < 0 || (x + 100) >= screen.width;
}
function getID() {
return Math.round(Math.random() * 100000);
}