-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-eye-animation.js
436 lines (383 loc) · 15 KB
/
web-eye-animation.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
(function() {
// CSSを動的に生成して追加する関数
function addCSS() {
const style = document.createElement("style");
style.textContent = `
.eye-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100vw; /* 横幅を50%に設定 */
height: 100vh; /* 縦幅を50%に設定 */
background-color: black;
}
.eye {
position: absolute;
width: 10vw;
height: 25vh;
background-color: white;
border-radius: 50%;
top: 50%; /* 縦方向の中央に配置 */
transform: translateY(-50%);
}
#leftEye {
left: 20%; /* eye-container内の左側に配置 */
}
#rightEye {
right: 20%; /* eye-container内の右側に配置 */
}
`;
document.head.appendChild(style);
}
// 動的にCSSを追加
addCSS();
const eyeContainer = document.querySelector(".eye-container");
if (!eyeContainer) return;
const leftEye = document.createElement("div");
leftEye.id = "leftEye";
leftEye.className = "eye";
const rightEye = document.createElement("div");
rightEye.id = "rightEye";
rightEye.className = "eye";
eyeContainer.appendChild(leftEye);
eyeContainer.appendChild(rightEye);
const eyeElements = [leftEye, rightEye];
let isAnimating = false;
let blinkTimeoutId = null;
let ws;
let reconnectInterval = 5000; // 再接続の間隔(ミリ秒)
let reconnectIntervalId;
// GSAPの読み込み
function loadGSAP(callback) {
if (typeof gsap !== "undefined") {
callback(); // すでに読み込まれている場合はコールバックを実行
return;
}
const script = document.createElement("script");
script.src = "https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js";
script.onload = callback;
document.head.appendChild(script);
}
// GSAPの読み込みを確認してからアニメーションを実行するPromiseを返す関数
function ensureGSAP() {
return new Promise((resolve) => {
if (typeof gsap !== "undefined") {
resolve(); // GSAPがすでに読み込まれている場合は即座にresolve
} else {
loadGSAP(resolve); // GSAPが未ロードならロードしてからresolveを実行
}
});
}
// 感情を表現するための基礎関数
function expressEmotion(animationFunction) {
if (isAnimating) return;
isAnimating = true;
const timeline = animationFunction();
timeline.then(() => {
isAnimating = false;
scheduleBlink();
});
}
function expressJoy() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { borderRadius: "0%", rotate: 45, scaleY: 0.1, duration: 0.2 })
.to(eyeElements, { y: "-=10", duration: 0.1, yoyo: true, repeat: 3 })
.to(eyeElements, { borderRadius: "50%", rotate: 0, scaleY: 1, duration: 0.2 });
});
}
// expressSadness
function expressSadness() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 0.1, duration: 0.5 })
.to(eyeElements, { y: "+=10", duration: 0.5 })
.to(eyeElements, { scaleX: 1.5, duration: 0.5 })
.to(eyeElements, { scaleX: 1, scaleY: 1, duration: 0.5 })
.to(eyeElements, { y: "-=10", duration: 0.5 });
});
}
// expressSurprise
function expressSurprise() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleX: 1.5, scaleY: 1.5, duration: 0.2 })
.to(eyeElements, { scaleX: 1, scaleY: 1, duration: 0.3, delay: 0.5 });
});
}
// expressAnger
function expressAnger() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 0.5, rotate: -10, duration: 0.2 })
.to(eyeElements, { x: "+=10", duration: 0.1, yoyo: true, repeat: 3 })
.to(eyeElements, { scaleY: 1, rotate: 0, duration: 0.2 });
});
}
// expressFear
function expressFear() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 1.5, duration: 0.2 })
.to(eyeElements, { y: "-=10", duration: 0.2 })
.to(eyeElements, { scaleX: 0.8, duration: 0.1, yoyo: true, repeat: 5 })
.to(eyeElements, { scaleY: 1, scaleX: 1, duration: 0.2 })
.to(eyeElements, { y: "+=10", duration: 0.2 });
});
}
// expressDisgust
function expressDisgust() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 0.3, rotate: -5, duration: 0.3 })
.to(eyeElements, { y: "+=10", duration: 0.3 })
.to(eyeElements, { scaleY: 1, rotate: 0, duration: 0.3, delay: 0.5 })
.to(eyeElements, { y: "-=10", duration: 0.3 });
});
}
// expressConfusion
function expressConfusion() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(leftEye, { rotate: -20, duration: 0.2 })
.to(rightEye, { rotate: 20, duration: 0.2 }, "<")
.to(eyeElements, { y: "+=10", duration: 0.1, yoyo: true, repeat: 3 })
.to(eyeElements, { rotate: 0, duration: 0.2 });
});
}
function expressLove() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { borderRadius: "0 0 50% 50%", scaleY: 0.5, duration: 0.2 })
.to(eyeElements, { scaleX: 1.2, scaleY: 0.4, duration: 0.2 })
.to(eyeElements, { scaleX: 1, scaleY: 1, borderRadius: "50%", duration: 0.2, delay: 0.5 });
});
}
function expressSleepy() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 0.3, duration: 0.5 })
.to(eyeElements, { y: "+=10", duration: 0.5 })
.to(eyeElements, { scaleY: 0.1, duration: 0.2, delay: 0.5 })
.to(eyeElements, { scaleY: 0.3, duration: 0.2 })
.to(eyeElements, { scaleY: 1, duration: 0.5 })
.to(eyeElements, { y: "-=10", duration: 0.5 });
});
}
function expressExcitement() {
return ensureGSAP().then(() => {
gsap.timeline()
.to(eyeElements, { scaleY: 1.2, scaleX: 1.2, duration: 0.2 })
.to(eyeElements, { y: "-=10", duration: 0.1, yoyo: true, repeat: 5 })
.to(eyeElements, { rotate: 360, duration: 0.5 })
.to(eyeElements, { scaleY: 1, scaleX: 1, duration: 0.2 })
.to(eyeElements, { rotate: 0, duration: 0.1 });
});
}
function singleBlink() {
return ensureGSAP().then(() => {
return gsap.timeline()
.to(eyeElements, { scaleY: 0.1, duration: 0.1 })
.to(eyeElements, { scaleY: 1, duration: 0.1 });
});
}
function doubleBlink() {
return ensureGSAP().then(() => {
return gsap.timeline()
.to(eyeElements, { scaleY: 0.1, duration: 0.1 })
.to(eyeElements, { scaleY: 1, duration: 0.1 })
.to(eyeElements, { scaleY: 0.1, duration: 0.1, delay: 0.1 })
.to(eyeElements, { scaleY: 1, duration: 0.1 });
});
}
function blink() {
if (isAnimating) {
scheduleBlink();
return;
}
const blinkType = Math.random();
let timeline;
if (blinkType < 0.6) {
timeline = singleBlink();
} else {
timeline = doubleBlink();
}
timeline.then(scheduleBlink);
}
function scheduleBlink() {
if (blinkTimeoutId) {
clearTimeout(blinkTimeoutId);
}
blinkTimeoutId = setTimeout(blink, Math.random() * 5000 + 1000);
}
function moveEyes(x, y, reg = false) {
return ensureGSAP().then(() => {
const rect = eyeContainer.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
// 正規化された座標が与えられた場合、ピクセル単位に変換
if (reg) {
x = x * window.innerWidth;
y = y * window.innerHeight;
}
const deltaX = (x - centerX);
const deltaY = (y - centerY);
gsap.to(eyeElements, {
x: deltaX,
y: deltaY,
duration: 0.3
});
});
}
function moveEyesTarget(x, y, z, focalLength = 1000) {
return ensureGSAP().then(() => {
// 3D空間のベクトルを2Dスクリーン平面に投影
const projectedX = (x / (z + focalLength)) * window.innerWidth / 2;
const projectedY = (y / (z + focalLength)) * window.innerHeight / 2;
// スクリーン平面での動きの大きさを調整
const deltaX = (projectedX);
const deltaY = (projectedY);
// 目の動きを反映
gsap.to(eyeElements, {
x: deltaX,
y: deltaY,
duration: 0.3
});
});
}
function resetEyes() {
return ensureGSAP().then(() => {
gsap.to(eyeElements, {
x: 0,
y: 0,
duration: 0.5
});
});
}
// マウスムーブイベントをリッスンして目を動かす
document.addEventListener("mousemove", (event) => {
const rect = eyeContainer.getBoundingClientRect();
const isInContainer = (
event.clientX >= rect.left &&
event.clientX <= rect.right &&
event.clientY >= rect.top &&
event.clientY <= rect.bottom
);
if (isInContainer) {
moveEyes(event.clientX, event.clientY);
} else {
resetEyes();
}
});
// 初期化時にブリンクのスケジュールを開始
scheduleBlink();
// WebSocketクライアントの設定
function startWebSocket(ip_address) {
// 既存のWebSocket接続が存在する場合、それをクローズする
if (ws && ws.readyState !== WebSocket.CLOSED) {
ws.close();
}
ws = new WebSocket(`wss://${ip_address}:8765`);
ws.onopen = function(event) {
console.log("WebSocket connection established");
clearInterval(reconnectIntervalId); // 接続が確立されたら再接続タイマーを停止
};
ws.onmessage = function(event) {
const message = event.data;
// Parse the received message
console.log(message);
const parts = message.split(" ");
if (parts[0] === "emotion") {
runEmotion(parts[1]);
} else if (parts[0] === "eye" && parts[1] === "target" && parts.length === 6) {
// parts[2] = x, parts[3] = y, parts[4] = z, parts[5] = focalLength
const x = parseFloat(parts[2]);
const y = parseFloat(parts[3]);
const z = parseFloat(parts[4]);
const focalLength = parseFloat(parts[5]);
if (!isNaN(x) && !isNaN(y) && !isNaN(z) && !isNaN(focalLength)) {
moveEyesTarget(x, y, z, focalLength);
} else {
console.log("Invalid eye target coordinates or focalLength:", parts);
}
} else if (parts[0] === "eye" && parts.length === 3) {
const x = parseFloat(parts[1]);
const y = parseFloat(parts[2]);
const rect = eyeContainer.getBoundingClientRect();
if (!isNaN(x) && !isNaN(y)) {
moveEyes(x * rect.width, y * rect.height);
} else {
console.log("Invalid eye coordinates:", parts[1], parts[2]);
}
}
};
ws.onclose = function(event) {
console.log("WebSocket connection closed");
scheduleReconnect(ip_address); // 接続が閉じられたら再接続をスケジュール
};
ws.onerror = function(event) {
console.error("WebSocket error:", event);
scheduleReconnect(ip_address); // エラーが発生したら再接続をスケジュール
};
}
function scheduleReconnect(ip_address) {
// 既に再接続がスケジュールされている場合は何もしない
if (reconnectIntervalId) {
return;
}
console.log(`Attempting to reconnect in ${reconnectInterval / 1000} seconds...`);
reconnectIntervalId = setInterval(() => {
if (ws.readyState === WebSocket.CLOSED) {
startWebSocket(ip_address);
} else {
clearInterval(reconnectIntervalId); // 接続が確立されたら再接続タイマーを停止
reconnectIntervalId = null;
}
}, reconnectInterval);
}
// runEmotion関数をグローバル変数として定義
function runEmotion(emotion) {
switch (emotion) {
case "joy":
expressEmotion(expressJoy);
break;
case "sadness":
expressEmotion(expressSadness);
break;
case "surprise":
expressEmotion(expressSurprise);
break;
case "anger":
expressEmotion(expressAnger);
break;
case "fear":
expressEmotion(expressFear);
break;
case "disgust":
expressEmotion(expressDisgust);
break;
case "confusion":
expressEmotion(expressConfusion);
break;
case "love":
expressEmotion(expressLove);
break;
case "sleepy":
expressEmotion(expressSleepy);
break;
case "excitement":
expressEmotion(expressExcitement);
break;
default:
console.log("Unknown emotion:", emotion);
}
}
// WebSocket接続を開始するためのグローバル関数を設定
window.eyes = {
websocket: startWebSocket,
emotion: runEmotion,
move: moveEyes,
target: moveEyesTarget,
};
})();