-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
195 lines (167 loc) · 5.36 KB
/
Copy pathscript.js
File metadata and controls
195 lines (167 loc) · 5.36 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Modern Multi-Tab Effect JavaScript
class MultiTabEffect {
constructor() {
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.width = window.innerWidth;
this.height = window.innerHeight;
this.id = Math.random().toString(36).substr(2, 9);
this.setupCanvas();
this.setupBall();
this.setupEventListeners();
this.setupInstructions();
this.animate();
this.startBroadcasting();
}
setupCanvas() {
this.canvas.width = this.width;
this.canvas.height = this.height;
}
setupBall() {
this.defaultX = this.width / 2;
this.defaultY = this.height / 2;
this.ball = {
x: this.defaultX,
y: this.defaultY,
r: 30,
color: '#00ffff',
glow: 0,
pulse: 0
};
this.targetX = this.defaultX;
this.targetY = this.defaultY;
this.otherWindow = null;
}
setupEventListeners() {
window.addEventListener('storage', (e) => this.handleStorageEvent(e));
window.addEventListener('resize', () => this.handleResize());
window.addEventListener('click', (e) => this.handleClick(e));
}
setupInstructions() {
// Hide instructions after 5 seconds
const instructions = document.querySelector('.instructions');
if (instructions) {
setTimeout(() => {
instructions.style.opacity = '0';
setTimeout(() => instructions.remove(), 300);
}, 5000);
}
}
handleResize() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.canvas.width = this.width;
this.canvas.height = this.height;
this.defaultX = this.width / 2;
this.defaultY = this.height / 2;
}
handleClick(e) {
// Reset ball to center on click
this.targetX = this.defaultX;
this.targetY = this.defaultY;
this.ball.glow = 1;
}
drawBall(x, y, r, color, glow = 0) {
this.ctx.beginPath();
this.ctx.arc(x, y, r, 0, Math.PI * 2);
// Create gradient for modern look
const gradient = this.ctx.createRadialGradient(x, y, 0, x, y, r);
gradient.addColorStop(0, color);
gradient.addColorStop(1, this.adjustColor(color, -30));
this.ctx.fillStyle = gradient;
this.ctx.fill();
// Add glow effect
if (glow > 0) {
this.ctx.shadowColor = color;
this.ctx.shadowBlur = 20 * glow;
this.ctx.beginPath();
this.ctx.arc(x, y, r + 5, 0, Math.PI * 2);
this.ctx.fillStyle = color + '40';
this.ctx.fill();
this.ctx.shadowBlur = 0;
}
}
adjustColor(color, amount) {
const hex = color.replace('#', '');
const r = Math.max(0, Math.min(255, parseInt(hex.substr(0, 2), 16) + amount));
const g = Math.max(0, Math.min(255, parseInt(hex.substr(2, 2), 16) + amount));
const b = Math.max(0, Math.min(255, parseInt(hex.substr(4, 2), 16) + amount));
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
}
animate() {
this.ctx.clearRect(0, 0, this.width, this.height);
// Smooth movement with easing
const easing = 0.08;
this.ball.x += (this.targetX - this.ball.x) * easing;
this.ball.y += (this.targetY - this.ball.y) * easing;
// Update glow effect
this.ball.glow = Math.max(0, this.ball.glow - 0.02);
this.ball.pulse = (this.ball.pulse + 0.05) % (Math.PI * 2);
// Add subtle pulse effect
const pulseSize = Math.sin(this.ball.pulse) * 2;
this.drawBall(
this.ball.x,
this.ball.y,
this.ball.r + pulseSize,
this.ball.color,
this.ball.glow
);
requestAnimationFrame(() => this.animate());
}
broadcast() {
const data = {
id: this.id,
screenX: window.screenX,
screenY: window.screenY,
outerWidth: window.outerWidth,
outerHeight: window.outerHeight,
timestamp: Date.now()
};
localStorage.setItem('window-sync', JSON.stringify(data));
}
handleStorageEvent(e) {
if (e.key === 'window-sync') {
const data = JSON.parse(e.newValue);
if (data.id !== this.id) {
this.otherWindow = data;
this.updateTargetPosition();
}
}
}
updateTargetPosition() {
const myLeft = window.screenX;
const myRight = window.screenX + window.outerWidth;
const myTop = window.screenY;
const myBottom = window.screenY + window.outerHeight;
const otherLeft = this.otherWindow.screenX;
const otherRight = this.otherWindow.screenX + this.otherWindow.outerWidth;
const otherTop = this.otherWindow.screenY;
const otherBottom = this.otherWindow.screenY + this.otherWindow.outerHeight;
const threshold = 100;
// Horizontal (X): Attract to walls
if (Math.abs(myRight - otherLeft) < threshold) {
this.targetX = this.width - this.ball.r - 30;
this.ball.glow = 0.8;
} else if (Math.abs(myLeft - otherRight) < threshold) {
this.targetX = this.ball.r + 30;
this.ball.glow = 0.8;
} else {
this.targetX = this.defaultX;
}
// Vertical (Y): Coordinate ball heights
const yDiff = otherTop - myTop;
if (Math.abs(yDiff) < 200) {
const verticalOffset = yDiff * 0.3;
this.targetY = this.defaultY + verticalOffset;
} else {
this.targetY = this.defaultY;
}
}
startBroadcasting() {
setInterval(() => this.broadcast(), 100);
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new MultiTabEffect();
});