pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/cysctl/electric-sim

mous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-a33d805aa3bce2cb.css" /> GitHub - cysctl/electric-sim: Visualizing Electric Fields of Point Charges in 2D Using Canvas and JavaScript · GitHub
Skip to content

cysctl/electric-sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Electric Field Simulation

An interactive 2D electric field simulator that runs in the browser. Add point charges, drag them around, and watch the electric field lines and equipotential curves update in real time.

Electric Field Lines

Equipotential Lines


Features

  • Add and remove integer-valued point charges (in μC)
  • Drag charges around the canvas
  • Visualize electric field lines from each charge
  • Visualize equipotential curves
  • Zoom in/out and pan the viewport
  • Real-time tooltip showing E, V, and position at the cursor
  • Values displayed with SI prefixes (kN/C, MV, etc.)

Physics

Electric Field

The electric field at a point due to a charge $q$ at distance $r$ is:

$$\vec{E} = \frac{kq}{r^2}\hat{r}$$

With multiple charges, the total field is the sum of each contribution (superposition):

$$\vec{E}_{total} = \sum_{i} \frac{kq_i}{r_i^2}\hat{r}_i$$

In component form:

$$E_x = \sum_{i} \frac{kq_i}{r_i^2} \cdot \frac{\Delta x_i}{r_i}, \quad E_y = \sum_{i} \frac{kq_i}{r_i^2} \cdot \frac{\Delta y_i}{r_i}$$

where $k = 9 \times 10^9 \text{ N·m}^2\text{/C}^2$ is Coulomb's constant.

Electric Potential

The scalar potential is:

$$V = \sum_{i} \frac{kq_i}{r_i}$$

Equipotential curves are the lines where $V$ is constant. They are always perpendicular to electric field lines.

Charge Quantization

Electric charge is quantized. Only integer values are accepted:

$$q = n \cdot 1\text{ μC}, \quad n \in \mathbb{Z} \setminus {0}$$


Scaling

The simulation maps pixel coordinates to real physical units:

Parameter Value Description
Length 1 px = 1 cm = $10^{-2}$ m Pixel to meter conversion
Charge 1 unit = 1 μC = $10^{-6}$ C User-entered integer
Coulomb's constant $k = 9 \times 10^9$ N·m²/C² Vacuum

Example: A charge of 1 μC at 100 px (= 1 m) away:

$$E = \frac{9 \times 10^9 \times 10^{-6}}{1^2} = 9\text{ kN/C}$$

$$V = \frac{9 \times 10^9 \times 10^{-6}}{1} = 9\text{ kV}$$

Values are displayed with SI prefixes for readability:

Prefix Symbol Factor
Tera T $10^{12}$
Giga G $10^{9}$
Mega M $10^{6}$
Kilo k $10^{3}$
Milli m $10^{-3}$
Micro μ $10^{-6}$

Algorithms

Field Lines (Streamlines)

16 field lines are launched from each charge at equal angles. Each line is traced step by step:

  1. Compute $\vec{E}$ at the current point
  2. Step 4 px in the direction of the unit field vector
  3. Lines flow outward from positive charges and inward to negative charges
  4. Stop when leaving the visible area or reaching another charge

Line color maps to field strength on a log scale — blue for weak, yellow/red for strong.

Equipotential Curves (Marching Squares)

The visible area is divided into a 10 px grid. The potential is computed at each corner. The Marching Squares algorithm then extracts contour lines at fixed potential levels.

Levels are spaced logarithmically (5 per sign):

  • Red curves — positive potential
  • Blue curves — negative potential

Controls

Action How
Add charge Enter a value and click Add Charge
Delete charge Select a charge, then press Delete or Backspace
Drag charge Enable Draggable, then click and drag a charge
Zoom in/out Mouse scroll wheel (0.2× – 5×, toward cursor)
Pan Alt + left drag, or middle mouse drag
Show field lines Show Field checkbox
Show equipotential curves Show Potential checkbox
Show grid Show Grid checkbox

Project Structure

electric-sim/
├── index.html          # HTML layout and control panel
├── css/
│   └── styles.css      # All styles
└── js/
    ├── main.js         # Render loop and canvas setup
    ├── camera.js       # Zoom and pan camera system
    ├── controls.js     # GUI events, charge management, dragging
    ├── physics.js      # Electric field and potential calculations
    └── renderer.js     # Grid, charges, field lines, equipotential curves

Running Locally

This project uses ES Modules and cannot be opened directly as a file:// URL. You need a local server:

npx serve .
# or
python3 -m http.server 8000

Then open http://localhost:8000 in your browser.

About

Visualizing Electric Fields of Point Charges in 2D Using Canvas and JavaScript

Topics

Resources

Stars

Watchers

Forks

Contributors

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy