-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball_cannon.html
147 lines (126 loc) · 3.1 KB
/
ball_cannon.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
input[type="range"] {
-webkit-appearance: none;
width: 100%;
margin: 4.15px 0;
}
input[type="range"]:focus {
outline: none;
}
input[type="range"]::-webkit-slider-runnable-track {
width: 100%;
height: 6.7px;
cursor: pointer;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
background: #ffffff;
border-radius: 1.3px;
border: 0.2px solid #010101;
}
input[type="range"]::-webkit-slider-thumb {
box-shadow: 0.9px 0.9px 1px #000000, 0px 0px 0.9px #0d0d0d;
border: 1.1px solid #000000;
height: 15px;
width: 40px;
border-radius: 50px;
background: red;
cursor: pointer;
-webkit-appearance: none;
margin-top: -4.35px;
}
input[type="range"]:focus::-webkit-slider-runnable-track {
background: #ffffff;
}
</style>
</head>
<body>
<script>
let balls = [];
let path = [];
let numBalls = 1;
var slider;
var angle = 45;
var fire = false;
function setup() {
createCanvas(500, 400);
slider = createSlider(0, 90, 45, 5);
slider.position(25, 30);
slider.style("width", "250px");
button = createButton("fire");
button.position(300, 25);
button.mousePressed(function () {
fire = true;
});
for (let i = 0; i < numBalls; i++) {
balls[i] = new Ball(20, height - 20, 20);
}
}
function draw() {
background(0);
angle = radians(slider.value());
for (let b of balls) {
b.update();
b.show();
}
for (let i = 0; i < path.length; i++) {
if (path.length > 1500) {
path.splice(0, 1);
}
noFill();
stroke(255);
point(path[i].x, path[i].y);
}
}
class Ball {
constructor(x, y, r) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.g = createVector(0, 0);
this.r = r;
}
update() {
this.acc = p5.Vector.fromAngle(angle * -1);
this.acc.setMag(1);
if (fire) {
this.g.y += 0.056;
this.g.x -= 0.004;
this.acc.add(this.g);
this.vel.add(this.acc);
this.pos.add(this.vel);
//console.log(this.acc);
this.acc.mult(0);
if (this.pos.x <= this.r || this.pos.x >= width - this.r) {
this.vel.x = -this.vel.x;
}
if (this.pos.y <= this.r || this.pos.y >= height - this.r) {
this.vel.y = -this.vel.y;
}
if (this.pos.y > height - this.r) {
this.pos.x = 20;
this.pos.y = height - 20;
this.g.mult(0);
this.acc.mult(0);
this.vel.mult(0);
fire = false;
}
path.push({ x: this.pos.x, y: this.pos.y });
}
}
show() {
stroke(255);
strokeWeight(2);
fill(color(200, 200, 200, 80));
ellipse(this.pos.x, this.pos.y, this.r);
}
}
</script>
</body>
</html>