-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
281 lines (251 loc) · 9 KB
/
index.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const canvas = document.getElementById('c')
const c = canvas.getContext('2d')
// One gridline every 50 pixels
const gridlineSpacing = 50
const gridlineThickness = 2
const chargeRadius = 10
function drawGridlines() {
c.fillStyle = 'black'
for (let i = 0; i < canvas.width; i+=gridlineSpacing) {
c.fillRect(i, 0, gridlineThickness, canvas.height)
}
for (let i = 0; i < canvas.height; i+=gridlineSpacing) {
c.fillRect(0, i, canvas.width, gridlineThickness)
}
}
let charges = []
/*
* Finds the nearest gridline value.
* For example, if gridlineSpacing were 50
* and position were 173, then the function
* would return 200, because 200 is the closest
* multiple of 50.
*/
function findNearestGridlineValue(position) {
if ((position % gridlineSpacing) > gridlineSpacing / 2) {
return position - (position % gridlineSpacing) + gridlineSpacing
} else {
return position - (position % gridlineSpacing)
}
}
const abortThreshold = 10
let ds = 1
/*
* Return a vector with a magnitude ds (small increment)
* that points in the direction of the force on a small
* positive test charge. For more information, see
* https://en.wikipedia.org/wiki/Field_line#Construction
*/
function findFieldVector(x, y, checkAbort) {
let abort = false
// x and y component of the vector
let xComponent = 0
let yComponent = 0
// Magnitude of charge of test charge
const q1 = 0.01
for (let i = 0; i < charges.length; i++) {
const q2 = charges[i].charge
// Charges with no magnitude do not affect the force on the test charge
if (q2 === 0) {
continue
}
// Distance between test charge and current charge
const deltaX = x - charges[i].x
const deltaY = y - charges[i].y
const r = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
// Abort if we have already reached the "final position" for performance
if (r < abortThreshold && checkAbort) {
abort = true
break
}
const magnitude = q1 * q2 / (r * r)
// These calculations come from the fact that the
// vector forms a similar triangle with the triangle
// connecting the two charges
xComponent += magnitude * deltaX / r
yComponent += magnitude * deltaY / r
}
// Make the magnitude of the returned vector equal ds
const magnitude = Math.sqrt(xComponent * xComponent + yComponent * yComponent)
xComponent = xComponent * ds / magnitude
yComponent = yComponent * ds / magnitude
return {xComponent, yComponent, abort}
}
// The length of the tips of the arrows
const arrowLength = 20
function renderArrows(arrows) {
c.lineWidth = 2
for (let i = 0; i < arrows.length; i++) {
let {xPos, yPos, xComponent, yComponent} = arrows[i]
// Reverse signs so that arrows point away from positive charges (instead of towards them)
const theta = Math.atan2(-yComponent, -xComponent)
c.beginPath()
c.moveTo(xPos, yPos)
c.lineTo(xPos + arrowLength * Math.cos(theta + Math.PI / 4), yPos + arrowLength * Math.sin(theta + Math.PI / 4))
c.stroke()
c.beginPath()
c.moveTo(xPos, yPos)
c.lineTo(xPos + arrowLength * Math.cos(theta - Math.PI / 4), yPos + arrowLength * Math.sin(theta - Math.PI / 4))
c.stroke()
}
}
// How often should there be an arrow on the field line?
let arrowIncrement = 200
let maxIterationsPerFieldLine = 10000
function renderFieldLine(startingX, startingY) {
let xPos = startingX
let yPos = startingY
const arrows = []
c.lineWidth = 3
c.beginPath()
for (let i = 0; i < maxIterationsPerFieldLine; i++) {
const deltaXFromStart = startingX - xPos
const deltaYFromStart = startingY - yPos
const distanceFromStart = Math.sqrt(deltaXFromStart * deltaXFromStart + deltaYFromStart * deltaYFromStart)
const {xComponent, yComponent, abort} = findFieldVector(xPos, yPos, distanceFromStart > abortThreshold)
if (abort) {
break
}
c.lineTo(xPos, yPos)
xPos += xComponent
yPos += yComponent
c.lineTo(xPos, yPos)
if (i !== 0 && i % arrowIncrement === 0) {
arrows.push({xPos, yPos, xComponent, yComponent})
}
}
c.stroke()
renderArrows(arrows)
}
// Number of field lines per charge
const numFieldLinesPerCharge = 6
function renderCharges() {
c.clearRect(0, 0, canvas.width, canvas.height)
drawGridlines()
if (charges.length > 0) {
for (let i = 0; i < charges.length; i++) {
if (charges[i].charge <= 0) {
continue
}
const numFieldLines = charges[i].charge * numFieldLinesPerCharge
const angleIncrement = Math.PI * 2 / numFieldLines
for (let j = 0; j < numFieldLines; j++) {
renderFieldLine(charges[i].x + Math.cos(j * angleIncrement), charges[i].y + Math.sin(j * angleIncrement))
}
}
}
for (let i = 0; i < charges.length; i++) {
c.beginPath()
c.arc(charges[i].x, charges[i].y, 10, 0, 2 * Math.PI)
if (charges[i].charge === 0) {
c.fillStyle = 'gray'
} else if (charges[i].charge > 0) {
c.fillStyle = 'red'
} else {
c.fillStyle = 'blue'
}
c.fill()
// Highlight the charge being edited
if (i === currentChargeEditing) {
c.lineWidth = 2
c.stroke()
}
}
}
// The index of the charge whose properties are currently being edited
let currentChargeEditing = 0
// Returns the index of the charge with the given coordinates, -1 if there is none
function indexOfChargeAt(x, y) {
for (let i = 0; i < charges.length; i++) {
if (charges[i].x === x && charges[i].y === y) {
return i
}
}
return -1
}
function onCanvasClick(event) {
// Find the nearest grid intersection and center the coordinate on the gridline
const x = findNearestGridlineValue(event.clientX) + gridlineThickness / 2
const y = findNearestGridlineValue(event.clientY) + gridlineThickness / 2
if (event.buttons === 1) {
// Do not create charges with the same coordinates
if (indexOfChargeAt(x, y) !== -1) {
return
}
charges.push({x, y, charge: 1})
currentChargeEditing = charges.length - 1
updateEditingDiv()
renderCharges()
} else {
const i = indexOfChargeAt(x, y)
if (i !== -1) {
currentChargeEditing = i
updateEditingDiv()
renderCharges()
}
}
}
const chargeInput = document.getElementById('chargeInput')
const arrowFrequencyInput = document.getElementById('arrowFrequencyInput')
const dsInput = document.getElementById('dsInput')
const maxIterationsInput = document.getElementById('maxIterationsInput')
const saveButton = document.getElementById('saveButton')
function updateEditingDiv() {
chargeInput.value = charges[currentChargeEditing].charge
}
arrowFrequencyInput.value = arrowIncrement
dsInput.value = ds
maxIterationsInput.value = maxIterationsPerFieldLine
saveButton.onclick = function() {
const newCharge = parseInt(chargeInput.value)
if (isNaN(newCharge)) {
charges[currentChargeEditing].charge = 0
chargeInput.value = '0'
} else {
charges[currentChargeEditing].charge = parseInt(chargeInput.value)
}
const newArrowFrequency = parseInt(arrowFrequencyInput.value)
if (isNaN(newArrowFrequency)) {
arrowFrequencyInput.value = arrowIncrement
} else {
arrowIncrement = newArrowFrequency
}
const newDs = parseInt(dsInput.value)
if (isNaN(newDs)) {
dsInput.value = ds
} else {
ds = newDs
}
const newMaxIterations = parseInt(maxIterationsInput.value)
if (isNaN(newMaxIterations)) {
maxIterationsInput.value = maxIterationsPerFieldLine
} else {
maxIterationsPerFieldLine = newMaxIterations
}
renderCharges()
}
canvas.onmousedown = onCanvasClick
function sizeCanvas() {
canvas.width = window.innerWidth * 0.8
canvas.height = window.innerHeight
drawGridlines()
renderCharges()
}
function preset(newCharges, newArrowIncrement, newDs, newMaxIterations) {
charges = newCharges
currentChargeEditing = charges.length - 1
updateEditingDiv()
arrowIncrement = newArrowIncrement
arrowFrequencyInput.value = arrowIncrement
ds = newDs
dsInput.value = ds
maxIterationsPerFieldLine = newMaxIterations
maxIterationsInput.value = maxIterationsPerFieldLine
renderCharges()
}
document.getElementById('presetPointCharge').onclick = () => preset([{"x":351,"y":301,"charge":1}], 200, 1, 10000)
document.getElementById('presetLines').onclick = () => preset([{"x":201,"y":301,"charge":1},{"x":251,"y":301,"charge":1},{"x":301,"y":301,"charge":1},{"x":351,"y":301,"charge":1},{"x":401,"y":301,"charge":1},{"x":451,"y":301,"charge":1},{"x":501,"y":301,"charge":1},{"x":551,"y":301,"charge":1},{"x":601,"y":301,"charge":1},{"x":651,"y":301,"charge":1},{"x":701,"y":301,"charge":1},{"x":751,"y":301,"charge":1},{"x":801,"y":301,"charge":1},{"x":851,"y":301,"charge":1},{"x":201,"y":401,"charge":-1},{"x":251,"y":401,"charge":-1},{"x":301,"y":401,"charge":-1},{"x":351,"y":401,"charge":-1},{"x":401,"y":401,"charge":-1},{"x":451,"y":401,"charge":-1},{"x":501,"y":401,"charge":-1},{"x":551,"y":401,"charge":-1},{"x":601,"y":401,"charge":-1},{"x":651,"y":401,"charge":-1},{"x":701,"y":401,"charge":-1},{"x":751,"y":401,"charge":-1},{"x":801,"y":401,"charge":-1},{"x":851,"y":401,"charge":-1}], 100, 1, 10000000)
document.getElementById('presetEqualMagnitudeOppositeSign').onclick = () => preset([{"x":151,"y":301,"charge":1},{"x":551,"y":301,"charge":-1}], 200, 1, 10000)
document.getElementById('presetEqualMagnitudeSameSign').onclick = () => preset([{"x":151,"y":301,"charge":1},{"x":551,"y":301,"charge":1}], 200, 1, 10000)
window.onresize = sizeCanvas
sizeCanvas()