-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.js
More file actions
111 lines (82 loc) · 3.4 KB
/
datetime.js
File metadata and controls
111 lines (82 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export function updateDateTime(locale) {
const now = new Date();
const dateOptions = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' };
const formattedDate = now.toLocaleDateString(locale, dateOptions);
const timeOptions = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
const formattedTime = now.toLocaleTimeString(locale, timeOptions);
document.getElementById('currentDate').textContent = formattedDate.charAt(0).toUpperCase() + formattedDate.slice(1);
document.getElementById('currentTime').textContent = formattedTime;
}
export function showDateTime() {
document.getElementsByClassName('datetime-container')[0].classList.remove('hidden');
}
export function setFront(){
document.getElementsByClassName('datetime-container')[0].classList.add('front');
}
function toggleAnimation(element) {
if (element.classList.contains('appear-disappear-1')) {
element.classList.remove('appear-disappear-1');
element.classList.add('appear-disappear-2');
} else {
element.classList.remove('appear-disappear-2');
element.classList.add('appear-disappear-1');
}
}
function getActiveAnimations(element) {
const computedStyle = window.getComputedStyle(element);
const animations = [];
// Get all active animations
for (let i = 0; i < element.getAnimations().length; i++) {
const anim = element.getAnimations()[i];
if (anim.playState === 'running') {
animations.push(anim);
}
}
return animations.length > 0;
}
export function randomizePosition(bounds) {
let element = document.getElementsByClassName('datetime-container')[0];
// Check if animation is already running
if (getActiveAnimations(element)) {
//console.log("Animation is currently running. Skip new run!");
return; // Skip if animation is active
}
let { left, top } = getRandomPosition(bounds, 20, 400);
if (Math.random() > 0.5) {
element.classList.toggle('front');
}
element.style.top = top;
element.style.left = left;
toggleAnimation(element);
}
let lastPosition = null;
function getRandomPosition(bounds, paddingPercentage = 10, minDistance = 200) {
const paddingX = bounds.width * (paddingPercentage / 100);
const paddingY = bounds.height * (paddingPercentage / 100);
const maxWidth = Math.max(0, bounds.width - paddingX * 2);
const maxHeight = Math.max(0, bounds.height - paddingY * 2);
let left, top;
let attempts = 0;
const maxAttempts = 50; // Prevent infinite loops
do {
left = Math.floor(Math.random() * maxWidth) + paddingX;
top = Math.floor(Math.random() * maxHeight) + paddingY;
// Check distance from last position if it exists
if (lastPosition && attempts < maxAttempts) {
const distance = Math.sqrt(
Math.pow(left - lastPosition.left, 2) +
Math.pow(top - lastPosition.top, 2)
);
if (distance >= minDistance) {
break; // Acceptable distance
}
} else if (!lastPosition) {
break; // First call, always accept
}
attempts++;
} while (attempts < maxAttempts);
// Update last position
lastPosition = { left, top };
console.log(`left: ${left}, top: ${top}`)
return { left, top };
}