Changed around line 1
+ class Particle {
+ constructor(x, y) {
+ this.x = x;
+ this.y = y;
+ this.vx = (Math.random() - 0.5) * 2;
+ this.vy = (Math.random() - 0.5) * 2;
+ this.radius = 3;
+ }
+
+ update(canvas) {
+ this.x += this.vx;
+ this.y += this.vy;
+
+ if (this.x < this.radius || this.x > canvas.width - this.radius) this.vx *= -1;
+ if (this.y < this.radius || this.y > canvas.height - this.radius) this.vy *= -1;
+ }
+
+ draw(ctx) {
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
+ ctx.fillStyle = '#3498db';
+ ctx.fill();
+ ctx.closePath();
+ }
+ }
+
+ class CellSimulator {
+ constructor() {
+ this.canvas = document.getElementById('particleCanvas');
+ this.ctx = this.canvas.getContext('2d');
+ this.particles = [];
+ this.showMembrane = true;
+ this.speed = 1;
+ this.temperature = 37;
+
+ this.resize();
+ this.init();
+ this.setupEventListeners();
+ this.animate();
+ }
+
+ resize() {
+ this.canvas.width = this.canvas.offsetWidth;
+ this.canvas.height = this.canvas.offsetHeight;
+ }
+
+ init() {
+ this.particles = [];
+ for (let i = 0; i < 100; i++) {
+ this.particles.push(new Particle(
+ Math.random() * this.canvas.width,
+ Math.random() * this.canvas.height
+ ));
+ }
+ }
+
+ setupEventListeners() {
+ window.addEventListener('resize', () => this.resize());
+ document.getElementById('resetBtn').addEventListener('click', () => this.init());
+ document.getElementById('membraneBtn').addEventListener('click', () => this.showMembrane = !this.showMembrane);
+ document.getElementById('speedSlider').addEventListener('input', (e) => this.speed = e.target.value / 50);
+ document.getElementById('tempSlider').addEventListener('input', (e) => this.temperature = e.target.value);
+ }
+
+ drawMembrane() {
+ if (!this.showMembrane) return;
+ this.ctx.beginPath();
+ this.ctx.ellipse(
+ this.canvas.width/2,
+ this.canvas.height/2,
+ this.canvas.width/3,
+ this.canvas.height/3,
+ 0, 0, Math.PI * 2
+ );
+ this.ctx.strokeStyle = 'rgba(52, 152, 219, 0.5)';
+ this.ctx.lineWidth = 2;
+ this.ctx.stroke();
+ }
+
+ animate() {
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
+
+ this.drawMembrane();
+
+ this.particles.forEach(particle => {
+ particle.update(this.canvas);
+ particle.draw(this.ctx);
+ });
+
+ requestAnimationFrame(() => this.animate());
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new CellSimulator();
+ });