-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile.js
137 lines (113 loc) · 3.77 KB
/
mobile.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
let highestZ = 1;
class Paper {
holdingPaper = false;
touchStartX = 0;
touchStartY = 0;
touchMoveX = 0;
touchMoveY = 0;
touchEndX = 0;
touchEndY = 0;
prevTouchX = 0;
prevTouchY = 0;
velX = 0;
velY = 0;
rotation = Math.random() * 30 - 15;
currentPaperX = 0;
currentPaperY = 0;
rotating = false;
init(paper) {
paper.addEventListener('touchmove', (e) => {
e.preventDefault();
if(!this.rotating) {
this.touchMoveX = e.touches[0].clientX;
this.touchMoveY = e.touches[0].clientY;
this.velX = this.touchMoveX - this.prevTouchX;
this.velY = this.touchMoveY - this.prevTouchY;
}
const dirX = e.touches[0].clientX - this.touchStartX;
const dirY = e.touches[0].clientY - this.touchStartY;
const dirLength = Math.sqrt(dirX*dirX+dirY*dirY);
const dirNormalizedX = dirX / dirLength;
const dirNormalizedY = dirY / dirLength;
const angle = Math.atan2(dirNormalizedY, dirNormalizedX);
let degrees = 180 * angle / Math.PI;
degrees = (360 + Math.round(degrees)) % 360;
if(this.rotating) {
this.rotation = degrees;
}
if(this.holdingPaper) {
if(!this.rotating) {
this.currentPaperX += this.velX;
this.currentPaperY += this.velY;
}
this.prevTouchX = this.touchMoveX;
this.prevTouchY = this.touchMoveY;
paper.style.transform = `translateX(${this.currentPaperX}px) translateY(${this.currentPaperY}px) rotateZ(${this.rotation}deg)`;
}
})
paper.addEventListener('touchstart', (e) => {
if(this.holdingPaper) return;
this.holdingPaper = true;
paper.style.zIndex = highestZ;
highestZ += 1;
this.touchStartX = e.touches[0].clientX;
this.touchStartY = e.touches[0].clientY;
this.prevTouchX = this.touchStartX;
this.prevTouchY = this.touchStartY;
});
paper.addEventListener('touchend', () => {
this.holdingPaper = false;
this.rotating = false;
});
// For two-finger rotation on touch screens
paper.addEventListener('gesturestart', (e) => {
e.preventDefault();
this.rotating = true;
});
paper.addEventListener('gestureend', () => {
this.rotating = false;
});
}
}
const papers = Array.from(document.querySelectorAll('.paper'));
papers.forEach(paper => {
const p = new Paper();
p.init(paper);
});
function createEmojis() {
const emojiCount = 10;
const emojis = ['🎉', '❤️', '🥰', '🎂', '🎈', '✨','🎊','🥳'];
for (let i = 0; i < emojiCount; i++) {
const emoji = document.createElement('span');
emoji.textContent = emojis[Math.floor(Math.random() * emojis.length)];
emoji.style.position = 'absolute';
emoji.style.fontSize = '30px';
emoji.style.pointerEvents = 'none';
const x = Math.random() * window.innerWidth;
emoji.style.left = `${x}px`;
emoji.style.bottom = '0px';
document.body.appendChild(emoji);
setTimeout(() => {
emoji.style.transition = 'transform 3s linear, opacity 3s linear';
emoji.style.transform = `translateY(-${window.innerHeight}px)`;
emoji.style.opacity = '0';
}, 50);
setTimeout(() => {
document.body.removeChild(emoji);
}, 3050);
}
}
setInterval(createEmojis, 1000);
window.onload = function() {
const audio = document.getElementById('background-music');
audio.volume = 0.5;
audio.play().catch(error => {
console.log('Audio playback failed:', error);
});
};
// document.addEventListener('click', function() {
// const audio = document.getElementById('background-music');
// audio.play().catch(error => {
// console.log('Audio playback failed:', error);
// });
// });