-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
208 lines (188 loc) · 5.7 KB
/
script.js
File metadata and controls
208 lines (188 loc) · 5.7 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const settings = {
airFriction:0.99//normal friction, always active
,slideFriction:0.8//friction along edges perpendicular to edges
,absorption:0.8//loses when bounce off
,gravity:2// self explaintory
,complicatedGravity: true
,complicatedBounce: true
,slowdown: false
,vx:0//starting velocities
,vy:0
}
class SettingsInput{
constructor(setting,isNum){
this.input=document.createElement("input")
this.input.setting = setting
if (isNum){
this.input.type="text"
this.input.value=settings[setting]
this.input.oninput = function() {
if (this.value.match(/^\-?\d+\.?\d*$/)) settings[this.setting] = parseFloat(this.value);
}
} else{
this.input.type="checkbox"
this.input.checked=settings[setting];
this.input.oninput = function() {
settings[this.setting] = this.checked;
console.log(settings[this.setting])
}
}
this.label = document.createElement("label")
this.label.appendChild(document.createTextNode(setting+": "));
this.label.appendChild(this.input)
settingsDiv.appendChild(this.label);
settingsDiv.appendChild(document.createElement("br"));
}
}
class Dot {
constructor(x,y,radius,color){
this.x=x
this.y=y
this.vx=settings.vx //velocities
this.vy=settings.vy
this.radius=radius
this.color=color
this.draw()
}
updateVelocities(){
if (!(this.y+this.vy+this.radius>canvas.height)){// only gravity if it isn't going to crash into the wall
this.vy+=settings.gravity
} else {//if it is going to crash
if (settings.complicatedGravity) {
//distance to travel to wall divided by how fast it's traveling t=d/r r>d
var dist=canvas.height-(this.y+this.radius)
if (dist>1){
let time=dist/this.vy
this.vy+=settings.gravity*time
}
}
}//problem: doesn't account for air resistance(unders) or increasing velocity(overs), so will slightly over/underestimate depending on that
//problem:
this.vx*=settings.airFriction//airfric
this.vy*=settings.airFriction
this.slidefric()
}
slidefric() {
if (this.y + this.vy + this.radius > canvas.height || this.y - this.radius - this.vy < 0) { //slidefric, calculates will be inside wall?
this.vx *= settings.slideFriction
}
if (this.x + this.vx + this.radius > canvas.width || this.x - this.radius - this.vx < 0) {
this.vy *= settings.slideFriction
}
}
updateLocation(){
this.x+=this.vx
this.y+=this.vy
}
bounce(){
if (settings.complicatedBounce) {
this.complicatedBounce()
} else {
if (this.x+this.radius>canvas.width||this.x-this.radius<0){
this.vx*=-1
this.x+=this.vx
this.vx*=settings.absorption
}
if (this.y+this.radius>canvas.height||this.y-this.radius<0){
this.vy*=-1
this.y+=this.vy
this.vy*=settings.absorption
}
}
}
complicatedBounce() {
if (this.x + this.radius > canvas.width || this.x - this.radius < 0) { // is inside wall?
if (this.x + this.radius > canvas.width) {
this.x -= 2 * (this.x + this.radius - canvas.width)
}
if (this.x - this.radius < 0) {
this.x += 2 * (0 - this.x + this.radius)
}
this.vx *= -1
this.vx *= settings.absorption
}
if (this.y + this.radius > canvas.height || this.y - this.radius < 0) {
if (this.y + this.radius > canvas.height) {
this.y -= 2 * (this.y + this.radius - canvas.height)
}
if (this.y - this.radius < 0) {
this.y += 2 * (0 - this.y + this.radius)
}
this.vy *= -1
this.vy *= settings.absorption
}
}
draw(){
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
ctx.fill();
}
update(){
this.updateVelocities()
this.updateLocation()
this.bounce()
this.draw()
}
}
const canvas = document.getElementById("canvas")
const ctx = canvas.getContext("2d")
const observer = new ResizeObserver((entries) => {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
});
observer.observe(canvas)
const settingsDiv = document.getElementById("settings")
var play=true
const dots = []
var dot = new Dot(10,10,10,"red")
const numInputs = ["airFriction","slideFriction","absorption","gravity","vx","vy"]
numInputs.forEach((value,index,array) =>{array[index]=new SettingsInput(value,true)})
const checkInputs = ["complicatedGravity","complicatedBounce","slowdown"]
checkInputs.forEach((value,index,array) =>{array[index]=new SettingsInput(value,false)})
function render() {
if (play){
ctx.clearRect(0, 0, canvas.width, canvas.height);
dot.update();
}
if (settings.slowdown) {
setTimeout(() => {
requestAnimationFrame(render);
}, 500);
} else {
requestAnimationFrame(render)
}
}
requestAnimationFrame(render);
document.addEventListener("keydown",(event) => {
if(event.key==' ') {
if (event.repeat) return;
play=!play
}
if (event.key=='r') {
dot = new Dot(10,10,10,"red")
}
if (event.key=='s'){
if (settingsDiv.style.display === "none") {
settingsDiv.style.display = "block";
} else {
settingsDiv.style.display = "none";
}
}
})
document.addEventListener('mousedown', function(e) {
if (!play){
ctx.clearRect(0, 0, canvas.width, canvas.height);
const rect = canvas.getBoundingClientRect()
const x = e.clientX - rect.left
const y = e.clientY - rect.top
dot.vx=-(dot.x-x)/2
dot.vy=-(dot.y-y)/2
ctx.lineWidth=2
ctx.beginPath();
ctx.moveTo(dot.x,dot.y);
ctx.lineTo(x, y);
ctx.stroke();
dot.draw()
}
})