Advertisement
Guest User

analog clock

a guest
Apr 1st, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.45 KB | Source Code | 0 0
  1. ```dataviewjs
  2.  
  3. const container = dv.container;
  4.  
  5. container.style.width = "200px";
  6. container.style.height = "200px";
  7. container.style.position = "relative";
  8. container.style.margin = "20px auto";
  9.  
  10. // Create clock face
  11. const clockFace = container.createEl("div");
  12. clockFace.style.width = "200px";
  13. clockFace.style.height = "200px";
  14. clockFace.style.border = "2px solid #FFC3FD";
  15. clockFace.style.borderRadius = "50%";
  16. clockFace.style.position = "relative";
  17. clockFace.style.background = "#8839ef";
  18.  
  19. // Create minute/second markers
  20. for (let i = 0; i < 60; i++) {
  21.     const marker = clockFace.createEl("div");
  22.     const angle = i * 6;
  23.     const isHourMarker = i % 5 === 0;
  24.     marker.style.position = "absolute";
  25.     marker.style.width = isHourMarker ? "2px" : "1px";
  26.     marker.style.height = isHourMarker ? "10px" : "5px";
  27.     marker.style.background = "#000000";
  28.     marker.style.left = "50%";
  29.     marker.style.top = "2%";
  30.     marker.style.transformOrigin = "50% 94px"; // Radius for markers
  31.     marker.style.transform = `translateX(-50%) rotate(${angle}deg)`;
  32. }
  33.  
  34. // Create hour markers (numbers)
  35. for (let i = 1; i <= 12; i++) {
  36.     const angle = (i * 30 - 90) * (Math.PI / 180);
  37.     const marker = clockFace.createEl("div");
  38.     marker.textContent = i;
  39.     marker.style.position = "absolute";
  40.     marker.style.width = "20px";
  41.     marker.style.textAlign = "center";
  42.     marker.style.left = `${88 + 70 * Math.cos(angle)}px`;
  43.     marker.style.top = `${88 + 70 * Math.sin(angle)}px`;
  44.     marker.style.fontFamily = "Chicken Pie";
  45.     marker.style.fontSize = "17px";
  46.     marker.style.fontWeight = "bold";
  47.     marker.style.color = "#000000";
  48. }
  49.  
  50. // Create clock hands
  51. const hands = {
  52.     hour: clockFace.createEl("div"),
  53.     minute: clockFace.createEl("div"),
  54.     second: clockFace.createEl("div")
  55. };
  56.  
  57. // Style hour hand
  58. hands.hour.style.width = "4px";
  59. hands.hour.style.height = "50px";
  60. hands.hour.style.background = "#dc8a78";
  61. hands.hour.style.position = "absolute";
  62. hands.hour.style.left = "50%";
  63. hands.hour.style.top = "50%";
  64. hands.hour.style.transformOrigin = "50% 100%";
  65. hands.hour.style.borderRadius = '4px';
  66. hands.hour.style.transform = "translate(-50%, -100%)";
  67.  
  68. // Style minute hand
  69. hands.minute.style.width = "3px";
  70. hands.minute.style.height = "70px";
  71. hands.minute.style.background = "#7287fd";
  72. hands.minute.style.position = "absolute";
  73. hands.minute.style.left = "50%";
  74. hands.minute.style.top = "50%";
  75. hands.minute.style.transformOrigin = "50% 100%";
  76. hands.minute.style.borderRadius = '4px';
  77. hands.minute.style.transform = "translate(-50%, -100%)";
  78.  
  79. // Style second hand
  80. hands.second.style.width = "2px";
  81. hands.second.style.height = "80px";
  82. hands.second.style.background = "#fe640b";
  83. hands.second.style.position = "absolute";
  84. hands.second.style.left = "50%";
  85. hands.second.style.top = "50%";
  86. hands.second.style.transformOrigin = "50% 100%";
  87. hands.second.style.borderRadius = '4px';
  88. hands.second.style.transform = "translate(-50%, -100%)";
  89.  
  90. // Create center dot
  91. const centerDot = clockFace.createEl("div");
  92. centerDot.style.width = "10px";
  93. centerDot.style.height = "10px";
  94. centerDot.style.background = "#FF0000";
  95. centerDot.style.borderRadius = "50%";
  96. centerDot.style.position = "absolute";
  97. centerDot.style.left = "50%";
  98. centerDot.style.top = "50%";
  99. centerDot.style.transform = "translate(-50%, -50%)";
  100.  
  101. // Load sounds [ sound files are inside a folder of same vault]
  102. const ticktockSound= new Audio('sounds/ticking-clock_1-27477.mp3');
  103. const clockSound= new Audio('sounds/old-clock-bell-27265.mp3');
  104.  
  105. function updateClock() {
  106.     const now= new Date();
  107.     const hours= now.getHours() % 12;
  108.     const minutes= now.getMinutes();
  109.     const seconds= now.getSeconds();
  110.    
  111.     const hourDeg= (hours + minutes / 60) * 30;
  112.     const minuteDeg= (minutes + seconds / 60) * 6;
  113.     const secondDeg= seconds * 6;
  114.  
  115.     hands.hour.style.transform= `translate(-50%, -100%) rotate(${hourDeg}deg)`;
  116.     hands.minute.style.transform= `translate(-50%, -100%) rotate(${minuteDeg}deg)`;
  117.     hands.second.style.transform= `translate(-50%, -100%) rotate(${secondDeg}deg)`;
  118.  
  119.    // Play sound effects for hours and seconds
  120.    if (seconds === 0 && minutes === 0) {
  121.        clockSound.play();
  122.    } else {
  123.        ticktockSound.play();
  124.    }
  125. }
  126.  
  127. // Initial update
  128. updateClock();
  129.  
  130. // Update every second
  131. const interval= setInterval(updateClock, 1000);
  132.  
  133. // Cleanup interval when the component is removed
  134. dv.container.cleanup= () => clearInterval(interval);
  135.  
  136.  
  137. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement