-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (185 loc) · 7.49 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Electric Field</title>
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
}
canvas {
background-color: black;
position: absolute;
right: 0;
}
div#settings {
width: 350px;
left: 0;
position: absolute
}
.uc-unit::after {
content: '\0003BCC';
}
</style>
<script src="vector.js"></script>
<script src="metric_prefix.js"></script>
<script src="charge.js"></script>
<script src="arrow.js"></script>
</head>
<body>
<div id="settings">
<p>This is a simulation of an electric field. Each arrow represents the field at its base. The relative
lightness of the arrows is proportional to the relative strength of the field at that point. The direction
of the arrow is the direction of the field at that point.</p>
<p>You can create a charge by entering a number in the field bellow and clicking on the 'Create Charge'
button. You can move charges by left-clicking on them and moving the mouse around; To leave the charge
in-place just left-click again. Right-clicking on a charge will delete it. Scrolling while a charge is
picked-up or while hovering over a charge will change the charge's charge. Shift-scrolling while a charge is
picked-up or while hovering over a charge will change the charge's visual radius, but will keep its physical
properties.</p>
<div>
<label class="uc-unit">
Charge:
<input type="number" id="charge-input">
</label><br>
<button onclick="createCharge()">Create Charge</button>
</div>
</div>
<canvas id="render"></canvas>
</body>
<script>
/** @type {HTMLCanvasElement}*/
const canvas = document.querySelector("canvas#render")
canvas.width = window.innerWidth - 350
canvas.height = window.innerHeight
const ctx = canvas.getContext("2d")
const chargeRadius = Math.min(canvas.width, canvas.height) / 20
const arrowLength = chargeRadius * 2 / 5, arrowSpace = chargeRadius / 2
const arrowsCount = [Math.floor(canvas.width / (arrowSpace + 1)), Math.floor(canvas.height / (arrowSpace + 1))]
/** @type {Charge[]} */
let charges = []
/** @type {?Charge} */
let mouseCharge = null
/**
* @returns {Charge[]}
*/
function allCharges() {
if (mouseCharge === null) return charges;
return charges.concat([mouseCharge])
}
function createCharge() {
charges.push(new Charge(document.querySelector("#charge-input").value * 1e-6,
new Vector(canvas.width / 2, canvas.height / 2), chargeRadius))
}
setInterval(function () {
ctx.clearRect(0, 0, canvas.width, canvas.height)
/** @type {Arrow[]} */
let arrows = []
for (let x = 0; x < arrowsCount[0]; x++) {
for (let y = 0; y < arrowsCount[1]; y++) {
const p = new Vector(canvas.width * (x + 1) / arrowsCount[0] + 1, canvas.height * (y + 1) / arrowsCount[1] + 1)
let positiveField = new Vector(), negativeField = new Vector()
for (const charge of allCharges()) {
const currentField = charge.fieldAt(p)
if (charge.charge < 0) {
negativeField = negativeField.add(currentField)
} else {
positiveField = positiveField.add(currentField)
}
}
const field = positiveField.add(negativeField)
const arrow = new Arrow(p, field, positiveField.length / field.length, arrowLength)
let doesIntersectCharge = false
for (const charge of allCharges()) {
if (arrow.pos.sub(charge.pos).length < charge.radius * 1.1 ||
arrow.tip.sub(charge.pos).length < charge.radius * 1.1) {
doesIntersectCharge = true
break
}
}
if (doesIntersectCharge) continue
arrows.push(arrow)
}
}
let hasNonZeroCharges = false
let fieldMax = 0
for (const charge of allCharges()) {
let field = Math.abs(charge.fieldAt(charge.pos.add(new Vector(charge.radius, 0))).length)
if (field > fieldMax) fieldMax = field
if (charge.charge !== 0) hasNonZeroCharges = true
}
for (const charge of charges) charge.render(ctx)
if (mouseCharge !== null) mouseCharge.render(ctx);
if (!hasNonZeroCharges) return
for (const arrow of arrows) arrow.render(ctx, fieldMax)
}, 1000 / 60)
document.addEventListener("mousedown", function (evt) {
if (mouseCharge !== null) {
charges.push(mouseCharge)
mouseCharge = null
} else {
let rect = canvas.getBoundingClientRect()
let scaleX = canvas.width / rect.width, scaleY = canvas.height / rect.height
let pos = new Vector((evt.clientX - rect.left) * scaleX, (evt.clientY - rect.top) * scaleY)
for (let i = 0; i < charges.length; i++) {
if (pos.sub(charges[i].pos).length < charges[i].radius) {
mouseCharge = charges[i]
charges.splice(i, 1)
break
}
}
}
return false
})
document.addEventListener("contextmenu", function (evt) {
evt.preventDefault()
if (mouseCharge !== null) {
mouseCharge = null
} else {
let rect = canvas.getBoundingClientRect()
let scaleX = canvas.width / rect.width, scaleY = canvas.height / rect.height
let pos = new Vector((evt.clientX - rect.left) * scaleX, (evt.clientY - rect.top) * scaleY)
for (let i = 0; i < charges.length; i++) {
if (pos.sub(charges[i].pos).length < charges[i].radius) {
charges.splice(i, 1)
break
}
}
}
return false
})
document.addEventListener("wheel", function (evt) {
/** @type {?Charge} */
let charge = null
if (mouseCharge !== null) {
charge = mouseCharge
} else {
let rect = canvas.getBoundingClientRect()
let scaleX = canvas.width / rect.width, scaleY = canvas.height / rect.height
let pos = new Vector((evt.clientX - rect.left) * scaleX, (evt.clientY - rect.top) * scaleY)
for (let i = 0; i < charges.length; i++) {
if (pos.sub(charges[i].pos).length < charges[i].radius) {
charge = charges[i]
break
}
}
}
if (charge === null) return
if (evt.shiftKey) {
charge.radius += evt.deltaY
} else {
charge.charge += evt.deltaY * 1e-9
}
})
document.addEventListener("mousemove", function (evt) {
if (mouseCharge !== null) {
let rect = canvas.getBoundingClientRect()
let scaleX = canvas.width / rect.width, scaleY = canvas.height / rect.height
mouseCharge.pos = new Vector((evt.clientX - rect.left) * scaleX, (evt.clientY - rect.top) * scaleY)
}
return false
})
</script>
</html>