-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
91 lines (76 loc) · 2.87 KB
/
main.js
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
let container = document.querySelector(".main");
let positionX = 0;
let velocity = 0; // Inertia effect
let friction = 0.9; // Slowdown factor
let sensitivity = .5; // Adjust movement speed
// Calculate movement limits dynamically
function getScrollLimits() {
let containerWidth = container.scrollWidth;
let viewportWidth = window.innerWidth;
return {
min: 0, // Stop at the first image
max: Math.max(0, containerWidth - viewportWidth), // Stop at the last image
};
}
function handleMotion(event) {
if (!event.accelerationIncludingGravity) return;
let accelX = event.accelerationIncludingGravity.x || 0; // Left/Right tilt
// Apply sensitivity for smooth scrolling
velocity += accelX * sensitivity;
updateFieldIfNotNull("Accelerometer_gx", accelX);
updateFieldIfNotNull("Accelerometer_i", event.interval, 2);
incrementEventCount();
}
function updatePosition() {
positionX -= velocity; // Invert direction to match scrolling behavior
velocity *= friction; // Apply inertia effect
// Get scroll limits dynamically
let { min, max } = getScrollLimits();
// Restrict position within limits
positionX = Math.max(min, Math.min(max, positionX));
container.style.transform = `translateX(${-positionX}px)`; // Move container
requestAnimationFrame(updatePosition);
}
function updateFieldIfNotNull(fieldName, value, precision = 4) {
if (value != null) {
document.getElementById(fieldName).innerHTML = value.toFixed(precision);
}
}
function incrementEventCount() {
let counterElement = document.getElementById("num-observed-events");
let eventCount = parseInt(counterElement.innerHTML);
counterElement.innerHTML = eventCount + 1;
}
let is_running = false;
let demo_button = document.getElementById("start_demo");
demo_button.onclick = function (e) {
e.preventDefault();
// Request permission for iOS 13+ devices
if (
DeviceMotionEvent &&
typeof DeviceMotionEvent.requestPermission === "function"
) {
DeviceMotionEvent.requestPermission()
.then(response => {
if (response !== "granted") {
alert("Permission denied for motion sensors.");
}
})
.catch(console.error);
}
if (is_running) {
window.removeEventListener("devicemotion", handleMotion);
demo_button.innerHTML = "Start demo";
demo_button.classList.add("btn-success");
demo_button.classList.remove("btn-danger");
is_running = false;
} else {
window.addEventListener("devicemotion", handleMotion);
updatePosition(); // Start animation loop
demo_button.innerHTML = "Stop demo";
demo_button.classList.remove("btn-success");
demo_button.classList.add("btn-danger");
container.style.overflow = "visible"
is_running = true;
}
};