-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
338 lines (277 loc) · 10.6 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
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
let blinkAnime, stareAnime;
let blushImage,
smileImage,
poutImage,
rightEyeImage,
leftEyeImage,
bothEyesCloseImage,
rightEyeClosed,
leftEyeClosed,
surprisedImage,
currentImage;
let baseSprite, smileSprite, poutSprite, blushSprite, rightEyeSprite, leftEyeSprite, facialExpression;
let faceTrigger, leftEyeTrigger, rightEyeTrigger, headTrigger, surprisedTrigger;
let screenWidth;
let screenHeight;
let imageWidth;
let imageHeight;
let frameRates;
const maxRightEyeMovementX = 15;
const maxRightEyeMovementY = 7;
const maxLeftEyeMovementX = 5;
const maxLeftEyeMovementY = 7;
let movementSpeed = 2.5;
const easingFactor = 0.1; /* You could leave this as is or try to play with this if you want to. */
const snapThreshold = 3;
const hoverDelay = 2000;
let hoverExitTime = 0;
let havePressedNatsuki = false;
const blinkDuration = 45;
let lastBlinkFrame = 0;
let haveSentError = false;
async function loadConfig() {
try {
const response = await fetch("./config.json");
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const config = await response.json();
frameRates = config.fps;
} catch (error) {
console.error("Failed to load config:", error);
}
}
// Call the function to load the config
loadConfig();
/**
* Similar to Unity'd Awake Method
*/
function preload() {
blinkAnime = loadAnimation(
"./public/1.webp",
"./public/2.webp",
"./public/3.webp",
"./public/3.webp",
"./public/2.webp",
"./public/1.webp"
);
stareAnime = loadAnimation("./public/1.webp");
bothEyesCloseImage = loadImage("./public/3.webp");
rightEyeClosed = loadImage("./public/RightEyeClosed.webp");
leftEyeClosed = loadImage("./public/LeftEyeClosed.webp");
smileImage = loadImage("./public/Smile.webp");
blushImage = loadImage("./public/Blush.webp");
poutImage = loadImage("./public/Pout.webp");
rightEyeImage = loadImage("./public/EyeRight.webp");
leftEyeImage = loadImage("./public/EyeLeft.webp");
surprisedImage = loadImage("./public/surprised.webp");
}
/**
* Similar to Unity's Start Method
*/
function setup() {
screenWidth = windowWidth;
screenHeight = windowHeight;
imageWidth = smileImage.width;
imageHeight = smileImage.height;
createCanvas(screenWidth, screenHeight);
let screenAspectRatio = windowWidth / windowHeight;
let imageAspectRatio = imageWidth / imageHeight;
let scaleFactor;
if (screenAspectRatio > imageAspectRatio) {
scaleFactor = windowHeight / imageHeight;
} else {
scaleFactor = windowWidth / imageWidth;
}
poutImage.resize(poutImage.width * scaleFactor, poutImage.height * scaleFactor);
smileImage.resize(smileImage.width * scaleFactor, smileImage.height * scaleFactor);
blushImage.resize(blushImage.width * scaleFactor, blushImage.height * scaleFactor);
leftEyeImage.resize(leftEyeImage.width * scaleFactor, leftEyeImage.height * scaleFactor);
rightEyeImage.resize(rightEyeImage.width * scaleFactor, rightEyeImage.height * scaleFactor);
leftEyeClosed.resize(leftEyeClosed.width * scaleFactor, leftEyeClosed.height * scaleFactor);
rightEyeClosed.resize(rightEyeClosed.width * scaleFactor, rightEyeClosed.height * scaleFactor);
surprisedImage.resize(surprisedImage.width * scaleFactor, surprisedImage.height * scaleFactor);
bothEyesCloseImage.resize(bothEyesCloseImage.width * scaleFactor, bothEyesCloseImage.height * scaleFactor);
leftEyeSprite = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
leftEyeSprite.addImage(leftEyeImage);
rightEyeSprite = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
rightEyeSprite.addImage(rightEyeImage);
baseSprite = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
baseSprite.addAnimation("stare", stareAnime);
baseSprite.scale = scaleFactor;
smileSprite = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
smileSprite.addImage(smileImage);
currentImage = smileImage;
facialExpression = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
facialExpression.visible = false;
surprisedTrigger = createSprite(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
surprisedTrigger.mouseActive = true;
surprisedTrigger.visible = false;
faceTrigger = createSprite(screenWidth * (935 / 1366), screenHeight * (440 / 768), 250, 235);
faceTrigger.mouseActive = true;
faceTrigger.rotation = -45;
faceTrigger.visible = false;
leftEyeTrigger = createSprite(screenWidth * (800 / 1366), screenHeight * (445 / 768), 60, 60);
leftEyeTrigger.scale = Math.min(screenWidth / 1366, screenHeight / 768);
leftEyeTrigger.mouseActive = true;
leftEyeTrigger.rotation = -45;
leftEyeTrigger.visible = false;
rightEyeTrigger = createSprite(screenWidth * (970 / 1366), screenHeight * (355 / 768), 60, 60);
rightEyeTrigger.scale = Math.min(screenWidth / 1366, screenHeight / 768);
rightEyeTrigger.mouseActive = true;
rightEyeTrigger.rotation = -45;
rightEyeTrigger.visible = false;
headTrigger = createSprite(screenWidth * (800 / 1366), screenHeight * (214 / 768), 450, 235);
headTrigger.scale = Math.min(screenWidth / 1366, screenHeight / 768);
headTrigger.mouseActive = true;
headTrigger.rotation = -45;
headTrigger.visible = false;
screenCenter = createSprite(screenWidth / 2, screenHeight / 2, 50, 50);
screenCenter.mouseActive = true;
screenCenter.visible = false;
}
/**
* Similar to Unity's Update Method
*/
function draw() {
EyeBlinking();
HandleEyeAnimation();
HandleMouthAnimation();
HandleLeftEyeMovement();
HandleRightEyeMovement();
if (frameRates === 30) {
frameRate(30);
movementSpeed = 5;
} else if (frameRates === 60) {
frameRate(60);
movementSpeed = 2.5;
} else if (frameRates === 120) {
frameRate(120);
movementSpeed = 1.25;
} else if ((frameRates > 120 || frameRates < 30) && !haveSentError) {
console.error(
`${frameRates}fps is not officially supported.\n And why would anyone even want a ${frameRates}fps wallpaper?`
);
haveSentError = true;
frameRate(60);
movementSpeed = 2.5;
}
drawSprites();
}
function HandleEyeAnimation() {
if (headTrigger.mouseIsPressed) {
facialExpression.visible = true;
facialExpression.addImage(bothEyesCloseImage);
} else if (rightEyeTrigger.mouseIsOver) {
facialExpression.visible = true;
facialExpression.addImage(rightEyeClosed);
} else if (leftEyeTrigger.mouseIsOver) {
facialExpression.visible = true;
facialExpression.addImage(leftEyeClosed);
} else {
facialExpression.visible = false;
}
}
function HandleMouthAnimation() {
if (faceTrigger.mouseIsOver) {
if (havePressedNatsuki) {
currentImage = poutImage;
hoverExitTime = millis();
} else {
if (millis() - hoverExitTime > hoverDelay) {
currentImage = blushImage;
}
}
} else {
if (millis() - hoverExitTime > hoverDelay) {
currentImage = smileImage;
havePressedNatsuki = false;
}
if (surprisedTrigger.mouseIsPressed && !havePressedNatsuki) {
currentImage = surprisedImage;
hoverExitTime = millis();
}
}
smileSprite.addImage(currentImage);
if (faceTrigger.mouseIsPressed && !(leftEyeTrigger.mouseIsOver || rightEyeTrigger.mouseIsOver)) {
if (havePressedNatsuki) return;
havePressedNatsuki = true;
}
}
function EyeBlinking() {
if (frameCount % 360 === 0 && frameCount !== lastBlinkFrame) {
baseSprite.addAnimation("stare", blinkAnime);
lastBlinkFrame = frameCount;
}
if (frameCount >= lastBlinkFrame + blinkDuration) {
baseSprite.addAnimation("stare", stareAnime);
}
}
function HandleRightEyeMovement() {
let mouseXRel = mouseX - rightEyeSprite.position.x;
let mouseYRel = mouseY - rightEyeSprite.position.y;
let distance = dist(0, 0, mouseXRel, mouseYRel);
let maxDistance = Math.sqrt(maxRightEyeMovementX ** 2 + maxRightEyeMovementY ** 2);
if (screenCenter.mouseIsOver || havePressedNatsuki || faceTrigger.mouseIsOver || leftEyeTrigger.mouseIsOver) {
rightEyeSprite.position.x += (screenWidth / 2 - rightEyeSprite.position.x) * movementSpeed * easingFactor;
rightEyeSprite.position.y += (screenHeight / 2 - rightEyeSprite.position.y) * movementSpeed * easingFactor;
if (
dist(rightEyeSprite.position.x, rightEyeSprite.position.y, screenWidth / 2, screenHeight / 2) <= snapThreshold
) {
rightEyeSprite.position.x = screenWidth / 2;
rightEyeSprite.position.y = screenHeight / 2;
}
return;
}
if (distance > 0.1) {
if (distance > maxDistance) {
mouseXRel = (mouseXRel / distance) * maxDistance;
mouseYRel = (mouseYRel / distance) * maxDistance;
}
rightEyeSprite.position.x += mouseXRel * movementSpeed;
rightEyeSprite.position.y += mouseYRel * movementSpeed;
rightEyeSprite.position.x = constrain(
rightEyeSprite.position.x,
screenWidth / 2 - maxRightEyeMovementX,
screenWidth / 2 + maxRightEyeMovementX
);
rightEyeSprite.position.y = constrain(
rightEyeSprite.position.y,
screenHeight / 2 - maxRightEyeMovementY,
screenHeight / 2 + maxRightEyeMovementY
);
}
}
function HandleLeftEyeMovement() {
let mouseXRel = mouseX - leftEyeSprite.position.x;
let mouseYRel = mouseY - leftEyeSprite.position.y;
let distance = dist(0, 0, mouseXRel, mouseYRel);
let maxDistance = Math.sqrt(maxLeftEyeMovementX ** 2 + maxLeftEyeMovementY ** 2);
if (screenCenter.mouseIsOver || havePressedNatsuki || faceTrigger.mouseIsOver || rightEyeTrigger.mouseIsOver) {
leftEyeSprite.position.x += (screenWidth / 2 - leftEyeSprite.position.x) * movementSpeed * easingFactor;
leftEyeSprite.position.y += (screenHeight / 2 - leftEyeSprite.position.y) * movementSpeed * easingFactor;
if (dist(leftEyeSprite.position.x, leftEyeSprite.position.y, screenWidth / 2, screenHeight / 2) <= snapThreshold) {
leftEyeSprite.position.x = screenWidth / 2;
leftEyeSprite.position.y = screenHeight / 2;
}
return;
}
if (distance > 0.1 && !havePressedNatsuki) {
if (distance > maxDistance) {
mouseXRel = (mouseXRel / distance) * maxDistance;
mouseYRel = (mouseYRel / distance) * maxDistance;
}
leftEyeSprite.position.x += mouseXRel * movementSpeed;
leftEyeSprite.position.y += mouseYRel * movementSpeed;
leftEyeSprite.position.x = constrain(
leftEyeSprite.position.x,
screenWidth / 2 - maxLeftEyeMovementX - 18,
screenWidth / 2 + maxLeftEyeMovementX
);
leftEyeSprite.position.y = constrain(
leftEyeSprite.position.y,
screenHeight / 2 - maxLeftEyeMovementY,
screenHeight / 2 + maxLeftEyeMovementY
);
}
}