Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved #1988- Magnetic field lines stimulation #2031

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Magnetic Field Lines Stimulation/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magnetic Field Lines Simulation</title>
<link rel="stylesheet" href="styles2.css">
</head>
<body>
<div class="controls">
<button id="startBtn">Start</button>
<button id="stopBtn">Stop</button>
<button id="resetBtn">Reset</button>
</div>
<div class="container">
<canvas id="simulationCanvas"></canvas>
</div>
<script src="script2.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions Magnetic Field Lines Stimulation/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "Magnetic Field Lines Simulation",
"version": "1.0.0",
"description": "A simulation to visualize magnetic field lines around a magnet or a set of magnets.",
"main": "index.html",
"scripts": {
"start": "live-server",
"build": "webpack --mode production"
},
"repository": {
"type": "git",
"url": "https://github.com/yourusername/magnetic-field-lines-simulation.git"
},
"keywords": [
"magnetism",
"magnetic field",
"simulation",
"physics",
"visualization"
],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"three": "^0.130.0",
"dat.gui": "^0.7.6"
},
"devDependencies": {
"webpack": "^5.38.1",
"webpack-cli": "^4.7.2",
"live-server": "^1.2.1"
},
"browserslist": [
"defaults"
]
}
100 changes: 100 additions & 0 deletions Magnetic Field Lines Stimulation/script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

// script2.js

const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const resetBtn = document.getElementById('resetBtn');

canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;

let animationId;
let particles = [];

const magnet = {
x: canvas.width / 2,
y: canvas.height / 2,
width: 20,
height: 100
};

function Particle(x, y) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
}

Particle.prototype.update = function() {
const dx = this.x - magnet.x;
const dy = this.y - magnet.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const force = 100 / (dist * dist);

const fx = force * dx / dist;
const fy = force * dy / dist;

this.vx -= fx;
this.vy -= fy;

this.x += this.vx;
this.y += this.vy;
};

Particle.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, 2, 0, Math.PI * 2);
ctx.fillStyle = '#1abc9c';
ctx.fill();
};

function initParticles() {
particles = [];
for (let i = 0; i < 100; i++) {
const angle = Math.PI * 2 * Math.random();
const radius = Math.random() * 150 + 50;
const x = magnet.x + Math.cos(angle) * radius;
const y = magnet.y + Math.sin(angle) * radius;
particles.push(new Particle(x, y));
}
}

function drawMagnet() {
ctx.fillStyle = '#e74c3c';
ctx.fillRect(magnet.x - magnet.width / 2, magnet.y - magnet.height / 2, magnet.width, magnet.height);
}

function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawMagnet();
particles.forEach(particle => {
particle.update();
particle.draw();
});
animationId = requestAnimationFrame(animate);
}

function startSimulation() {
if (!animationId) {
animate();
}
}

function stopSimulation() {
cancelAnimationFrame(animationId);
animationId = null;
}

function resetSimulation() {
stopSimulation();
initParticles();
animate();
}

startBtn.addEventListener('click', startSimulation);
stopBtn.addEventListener('click', stopSimulation);
resetBtn.addEventListener('click', resetSimulation);

resetSimulation();
52 changes: 52 additions & 0 deletions Magnetic Field Lines Stimulation/styles2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* styles2.css */

body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #2c3e50;
color: #ecf0f1;
font-family: Arial, sans-serif;
}

.controls {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}

button {
margin: 0 10px;
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #34495e;
color: #ecf0f1;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #1abc9c;
}

.container {
position: relative;
width: 800px;
height: 600px;
border: 1px solid #ecf0f1;
background-color: #34495e;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
}

canvas {
display: block;
width: 100%;
height: 100%;
border-radius: 10px;
}
Loading