-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
167 lines (149 loc) · 4.57 KB
/
script.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
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
'use strict'
import Ball from './ball.js'
import { dialog } from './dialog.js'
let running = false
const h1 = document.querySelector('body > h1')
const clearButton = document.querySelector('button.clear')
const canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
const ctx = canvas.getContext('2d')
document.body.appendChild(canvas)
const balls = []
load()
function load() {
if (!localStorage.getItem('balls')) return
const ballsFromStorage = JSON.parse(localStorage.getItem('balls'))
for (const ball of ballsFromStorage) {
const newBall = new Ball(
ball.pos.x,
ball.pos.y,
ball.initialVelocity.x,
ball.initialVelocity.y,
ball.mass,
ball.radius,
ball.isStatic
)
balls.push(newBall)
newBall.draw(ctx)
}
if (balls.length > 0) h1.style.display = 'none'
}
clearButton.onclick = () => {
if (running) return
balls.length = 0
h1.style = ''
ctx.clearRect(0, 0, canvas.width, canvas.height)
save()
}
let draggingBall
let open = true
function save() {
localStorage.setItem('balls', JSON.stringify(balls))
}
canvas.onmousemove = ({ clientX, clientY }) => {
if (running || !draggingBall) return
open = false
draggingBall.pos.x = clientX
draggingBall.pos.y = clientY
draggingBall.initialPos.x = clientX
draggingBall.initialPos.y = clientY
ctx.clearRect(0, 0, canvas.width, canvas.height)
for (const ball of balls) ball.draw(ctx)
save()
}
canvas.onmousedown = ({ clientX, clientY }) => {
if (running) return
const ball = clickedOnABall(clientX, clientY)
if (ball) draggingBall = ball
save()
}
canvas.onmouseup = () => (draggingBall = null)
canvas.onclick = async ({ clientX, clientY }) => {
if (running) return
const ball = clickedOnABall(clientX, clientY)
if (ball) {
if (!open) return (open = true)
const defaults = {
title: 'Edit Ball',
posX: ball.pos.x,
posY: ball.pos.y,
velX: ball.initialVelocity.x * 10,
velY: ball.initialVelocity.y * 10,
radius: ball.radius,
mass: ball.mass / 1000,
isStatic: ball.isStatic,
}
const results = await dialog(clientX, clientY, defaults)
if (!results) return
const index = balls.indexOf(ball)
if (results.delete) balls.splice(index, 1)
else {
balls[index].pos.x = results.posX
balls[index].pos.y = results.posY
balls[index].velocity.x = results.velX
balls[index].velocity.y = results.velY
balls[index].initialVelocity.x = results.velX
balls[index].initialVelocity.y = results.velY
balls[index].radius = results.radius
balls[index].mass = results.mass
balls[index].isStatic = results.isStatic
}
ctx.clearRect(0, 0, canvas.width, canvas.height)
for (const ball of balls) ball.draw(ctx)
} else {
const results = await dialog(clientX, clientY, { title: 'Add Ball' })
if (!results) return
const ball = new Ball(
results.posX,
results.posY,
results.velX,
results.velY,
results.mass,
results.radius,
results.isStatic
)
balls.push(ball)
ball.draw(ctx)
h1.style.display = 'none'
}
if (balls.length === 0) h1.style = ''
save()
}
function clickedOnABall(x, y) {
for (const ball of balls)
if (
ball.pos.x + ball.radius > x &&
ball.pos.x - ball.radius < x &&
ball.pos.y + ball.radius > y &&
ball.pos.y - ball.radius < y
)
return ball
}
let lastTime = null
function loop(time) {
if (lastTime !== null) {
const delta = time - lastTime
if (running) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
for (const ball of balls) ball.update(ctx, balls, delta)
}
}
lastTime = time
requestAnimationFrame(loop)
}
requestAnimationFrame(loop)
const runButton = document.querySelector('button.run')
runButton.onclick = () => {
if (balls.length < 2)
return alert('You need at least 2 balls to run the simulation')
if (running) {
running = false
runButton.innerText = 'Run'
ctx.clearRect(0, 0, canvas.width, canvas.height)
for (const ball of balls) ball.reset(ctx)
return
}
runButton.textContent = 'Stop'
running = true
}