-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbouncingBall.html
More file actions
104 lines (91 loc) · 2.05 KB
/
bouncingBall.html
File metadata and controls
104 lines (91 loc) · 2.05 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
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
<head>
<title></title>
<!--https://cdnjs.com/libraries/p5.js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.sound.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.dom.min.js" type="text/javascript"></script>
</head>
<style type="text/css">
html, body{
margin: 0;
overflow: hidden;
}
</style>
<body>
</body>
<script type="text/javascript">
//https://p5js.org/reference/
"use strict";
var MAX_NUM = 200;
var circles= [];
function setup(){
createCanvas(window.innerWidth, window.innerHeight);
background(0);
noStroke();
}
function draw(){
background(0, 50);
for(var i = 0; i < circles.length; i++){
circles[i].checkBoundary();
circles[i].update();
circles[i].display();
}
}
function mouseClicked(){
if(circles.length < MAX_NUM){
for(var i = 0; i < 5; i++){
circles.push(new Circle(mouseX, mouseY));
}
}
}
function keyPressed(){
if(key == " "){
for(var i = 0; i < circles.length; i++){
circles[i].speedX = random(-5, 5) * 2;
circles[i].speedY = random(-5, 5) * 2;
}
}
}
function Circle(x, y){
this.x = x;
this.y = y;
this.speedX = random(-5, 5);
this.speedY = random(-5, 5);
this.size = random(25, 50);
this.color = color(random(255), random(255), random(255), 100);
this.checkBoundary = function(){
if(this.x < 0){
this.x = 0;
this.speedX *= -1;
}
if(this.x > width){
this.x = width;
this.speedX *= -1;
}
if(this.y < 0){
this.y = 0;
this.speedY *= -1;
}
if(this.y > height){
this.y = height;
this.speedY *= -1;
}
}
this.update = function(){
//restitution
this.speedX *= 0.98;
this.speedY *= 0.98;
this.x += this.speedX;
this.y += this.speedY;
}
this.display = function(){
push();
fill(this.color);
ellipse(this.x, this.y, this.size, this.size);
pop();
}
}
</script>
</html>