Useless Things Series: Circle 5 - Solar System Simulation

Simulates a basic solar system with circles representing planets, orbits, and direction indicators.

  1. // ==UserScript==
  2. // @name Useless Things Series: Circle 5 - Solar System Simulation
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Simulates a basic solar system with circles representing planets, orbits, and direction indicators.
  6. // @match *://*/*
  7. // @grant none
  8. // @license MIT
  9. // @namespace https://gf.qytechs.cn/users/1126616
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to generate a random color
  16. function randomColor() {
  17. return '#' + Math.floor(Math.random()*16777215).toString(16);
  18. }
  19.  
  20. // Function to generate random velocity vector
  21. function randomVelocity(speed) {
  22. const angle = Math.random() * Math.PI * 2;
  23. return {
  24. x: Math.cos(angle) * speed,
  25. y: Math.sin(angle) * speed
  26. };
  27. }
  28.  
  29. // Function to update planet positions and draw them on the canvas
  30. function updateAndDrawPlanets(ctx, planets) {
  31. planets.forEach(planet => {
  32. planet.angle += planet.angularSpeed;
  33. planet.x = planet.centerX + Math.cos(planet.angle) * planet.distance;
  34. planet.y = planet.centerY + Math.sin(planet.angle) * planet.distance;
  35.  
  36. // Draw orbit
  37. ctx.beginPath();
  38. ctx.arc(planet.centerX, planet.centerY, planet.distance, 0, Math.PI * 2);
  39. ctx.strokeStyle = 'rgba(255,69,0)';
  40. ctx.stroke();
  41. ctx.closePath();
  42.  
  43. // Draw direction indicator
  44. const directionX = planet.centerX + Math.cos(planet.angle) * (planet.distance - planet.radius - 5);
  45. const directionY = planet.centerY + Math.sin(planet.angle) * (planet.distance - planet.radius - 5);
  46. ctx.beginPath();
  47. ctx.moveTo(planet.centerX, planet.centerY);
  48. ctx.lineTo(directionX, directionY);
  49. ctx.strokeStyle = 'rgba(255,191,0)';
  50. ctx.stroke();
  51. ctx.closePath();
  52.  
  53. // Draw planet
  54. ctx.beginPath();
  55. ctx.arc(planet.x, planet.y, planet.radius, 0, Math.PI * 2);
  56. ctx.fillStyle = planet.color;
  57. ctx.fill();
  58. ctx.closePath();
  59. });
  60. }
  61.  
  62. // Main function to initialize and run the solar system simulation
  63. function runSolarSystem(numPlanets, sunRadius, planetDistances, planetSpeeds, planetRadii) {
  64. const canvas = document.createElement('canvas');
  65. canvas.width = window.innerWidth;
  66. canvas.height = window.innerHeight;
  67. canvas.style.position = 'fixed';
  68. canvas.style.top = '0';
  69. canvas.style.left = '0';
  70. canvas.style.pointerEvents = 'none';
  71. canvas.style.zIndex = '9999';
  72. document.body.appendChild(canvas);
  73.  
  74. const ctx = canvas.getContext('2d');
  75. const planets = [];
  76.  
  77. // Generate sun
  78. const sunX = canvas.width / 2;
  79. const sunY = canvas.height / 2;
  80. planets.push({ x: sunX, y: sunY, centerX: sunX, centerY: sunY, distance: 0, angle: 0, angularSpeed: 0, color: '#ffcc00', radius: sunRadius });
  81.  
  82. // Generate planets
  83. for (let i = 0; i < numPlanets; i++) {
  84. const distance = planetDistances[i] * 50 + 100; // Scale distances for better visualization
  85. const angularSpeed = planetSpeeds[i] / distance; // Orbital speed decreases with distance
  86. const color = randomColor();
  87. const radius = planetRadii[i] / 10; // Scale radii for better visualization
  88. planets.push({ x: sunX + distance, y: sunY, centerX: sunX, centerY: sunY, distance: distance, angle: Math.random() * Math.PI * 2, angularSpeed: angularSpeed, color: color, radius: radius });
  89. }
  90.  
  91. // Update and draw planets and grid
  92. setInterval(() => {
  93. ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
  94. updateAndDrawPlanets(ctx, planets); // Update and draw planets
  95. }, 1000 / 60); // Update at 60 frames per second
  96. }
  97.  
  98. // Define parameters for the solar system
  99. const numPlanets = 8; // Number of planets (including dwarf planets like Pluto)
  100. const sunRadius = 30; // Radius of the sun
  101. const planetDistances = [0.4, 0.7, 1.0, 1.5, 2.2, 3.0, 4.0, 5.0]; // Relative distances of planets from the sun
  102. const planetSpeeds = [0.8, 0.6, 0.5, 0.4, 0.3, 0.2, 0.15, 0.1]; // Angular speeds of planets (radians per frame)
  103. const planetRadii = [20, 40, 60, 80, 100, 120, 140, 160]; // Relative radii of planets
  104.  
  105. // Run the solar system simulation
  106. runSolarSystem(numPlanets, sunRadius, planetDistances, planetSpeeds, planetRadii);
  107.  
  108. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址