Guest User

Cloud chamber simulation using Claude Sonnet 3.5

a guest
Jun 25th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Cloud Chamber Simulation (p5.js)</title>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
  8. <style>
  9. body {
  10. display: flex;
  11. flex-direction: column;
  12. align-items: center;
  13. font-family: Arial, sans-serif;
  14. background-color: #f0f0f0;
  15. }
  16. #controls {
  17. margin-top: 20px;
  18. }
  19. .slider-container {
  20. margin: 10px 0;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h1>Cloud Chamber Simulation</h1>
  26. <div id="particleCount">Particles detected: 0</div>
  27. <div id="particlesPerSecond">Particles per second: 0</div>
  28. <div id="controls">
  29. <div class="slider-container">
  30. <label for="particleSlider">Particle Rate:</label>
  31. <input type="range" id="particleSlider" min="1" max="10" value="5" step="1">
  32. </div>
  33. <div class="slider-container">
  34. <label for="speedSlider">Animation Speed:</label>
  35. <input type="range" id="speedSlider" min="1" max="100" value="50" step="1">
  36. </div>
  37. </div>
  38. <script>
  39. let particles = [];
  40. let particleCount = 0;
  41. let particlesPerSecond = 0;
  42. let lastSecondCount = 0;
  43. let particleSlider, speedSlider;
  44. let animationSpeed = 1;
  45.  
  46. function setup() {
  47. createCanvas(800, 600);
  48. particleSlider = select('#particleSlider');
  49. speedSlider = select('#speedSlider');
  50. setInterval(updateParticlesPerSecond, 1000);
  51. }
  52.  
  53. function draw() {
  54. background(0, 25);
  55. animationSpeed = map(speedSlider.value(), 1, 100, 0.1, 2);
  56.  
  57. for (let i = particles.length - 1; i >= 0; i--) {
  58. if (particles[i].update()) {
  59. particles[i].display();
  60. } else {
  61. particles.splice(i, 1);
  62. }
  63. }
  64.  
  65. for (let i = 0; i < particleSlider.value(); i++) {
  66. if (random(1) < 0.2) {
  67. createParticle();
  68. }
  69. }
  70.  
  71. updateParticleCount();
  72. }
  73.  
  74. function createParticle() {
  75. particles.push(new Particle());
  76. particleCount++;
  77. lastSecondCount++;
  78. }
  79.  
  80. function updateParticleCount() {
  81. select('#particleCount').html(`Particles detected: ${particleCount}`);
  82. }
  83.  
  84. function updateParticlesPerSecond() {
  85. particlesPerSecond = lastSecondCount;
  86. lastSecondCount = 0;
  87. select('#particlesPerSecond').html(`Particles per second: ${particlesPerSecond}`);
  88. }
  89.  
  90. class Particle {
  91. constructor() {
  92. this.x = random(width);
  93. this.y = random(height);
  94. this.length = random(50, 150);
  95. this.angle = random(TWO_PI);
  96. this.opacity = 255;
  97. this.lifespan = random(1, 2); // 1-2 seconds lifespan
  98. this.width = random(1, 3);
  99. this.curvePoints = [];
  100. for (let i = 0; i < 5; i++) {
  101. this.curvePoints.push({
  102. x: random(-20, 20),
  103. y: random(-20, 20)
  104. });
  105. }
  106. this.shiftSpeed = random(0.2, 0.5);
  107. this.shiftAngle = random(TWO_PI);
  108. this.age = 0;
  109. }
  110.  
  111. update() {
  112. this.age += deltaTime / 1000 * animationSpeed; // Convert to seconds and apply animation speed
  113. this.opacity = map(this.age, 0, this.lifespan, 255, 0);
  114.  
  115. // Shift the entire line
  116. let shiftAmount = this.shiftSpeed * animationSpeed;
  117. this.x += cos(this.shiftAngle) * shiftAmount;
  118. this.y += sin(this.shiftAngle) * shiftAmount;
  119.  
  120. return this.age < this.lifespan;
  121. }
  122.  
  123. display() {
  124. push();
  125. stroke(255, this.opacity);
  126. strokeWeight(this.width);
  127. noFill();
  128. beginShape();
  129. curveVertex(this.x, this.y);
  130. curveVertex(this.x, this.y);
  131. for (let i = 0; i < this.curvePoints.length; i++) {
  132. let px = this.x + cos(this.angle) * this.length * (i + 1) / this.curvePoints.length + this.curvePoints[i].x;
  133. let py = this.y + sin(this.angle) * this.length * (i + 1) / this.curvePoints.length + this.curvePoints[i].y;
  134. curveVertex(px, py);
  135. }
  136. let endX = this.x + cos(this.angle) * this.length;
  137. let endY = this.y + sin(this.angle) * this.length;
  138. curveVertex(endX, endY);
  139. curveVertex(endX, endY);
  140. endShape();
  141. pop();
  142. }
  143. }
  144. </script>
  145. </body>
  146. </html>
Advertisement
Add Comment
Please, Sign In to add comment