data-lines-canvas.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const canvas = document.getElementById('dataLinesCanvas');
  2. const ctx = canvas.getContext('2d');
  3. let lines = [];
  4. const numberOfLines = 9;
  5. // Resize canvas to match 100% CSS size
  6. function resizeCanvas() {
  7. canvas.width = canvas.offsetWidth; // Match visible width
  8. canvas.height = canvas.offsetHeight; // Match visible height
  9. updateLinePositions();
  10. }
  11. // Create vertical line positions
  12. function updateLinePositions() {
  13. lines = [];
  14. let spacing = canvas.width / numberOfLines;
  15. for (let i = 0; i < numberOfLines; i++) {
  16. let xPosition = spacing * i + spacing / 2;
  17. lines.push({
  18. x: xPosition,
  19. currentDrop: createDrop(),
  20. });
  21. }
  22. }
  23. // Create drop properties
  24. function createDrop() {
  25. return {
  26. y: -10,
  27. speed: Math.random() * 0.5 + 0.3,
  28. radius: Math.random() * 1.1 + 1.0,
  29. pauseTime: Math.random() * 3000 + 1000,
  30. isPaused: false,
  31. pauseCounter: 0
  32. };
  33. }
  34. // Draw animation
  35. function draw() {
  36. ctx.clearRect(0, 0, canvas.width, canvas.height);
  37. // Draw vertical lines
  38. ctx.strokeStyle = '#1E293B';
  39. ctx.lineWidth = 0.2;
  40. lines.forEach(line => {
  41. ctx.beginPath();
  42. ctx.moveTo(line.x, 0);
  43. ctx.lineTo(line.x, canvas.height);
  44. ctx.stroke();
  45. });
  46. // Draw drops
  47. lines.forEach(line => {
  48. let drop = line.currentDrop;
  49. ctx.beginPath();
  50. ctx.arc(line.x, drop.y, drop.radius, 0, Math.PI * 2);
  51. ctx.fillStyle = '#38BDF8';
  52. ctx.fill();
  53. // Pause handling
  54. if (drop.isPaused) {
  55. drop.pauseCounter -= 16;
  56. if (drop.pauseCounter <= 0) {
  57. drop.isPaused = false;
  58. }
  59. return;
  60. }
  61. drop.y += drop.speed;
  62. // Random pause
  63. if (Math.random() > 0.9985) {
  64. drop.isPaused = true;
  65. drop.pauseCounter = drop.pauseTime;
  66. }
  67. // Reset when leaving screen
  68. if (drop.y - drop.radius > canvas.height) {
  69. line.currentDrop = createDrop();
  70. }
  71. });
  72. requestAnimationFrame(draw);
  73. }
  74. // Init
  75. resizeCanvas();
  76. draw();
  77. // Resize event
  78. window.addEventListener('resize', resizeCanvas);