-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
340 lines (288 loc) · 9.85 KB
/
script.js
File metadata and controls
340 lines (288 loc) · 9.85 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
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
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var processingCanvas = document.getElementById("processingCanvas");
var processingCtx = processingCanvas.getContext("2d");
function resizeCanvas(target = canvas) {
target.width = window.innerWidth;
target.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
////////////////////////////////////////////////
// Caremarein Info //
////////////////////////////////////////////////
var timeSinceBlinked = 0;
var blinking = false;
var blinkFrames = 0;
// Eyes
const eyePaths = {
"simple": "assets/eyes/eye_simple.svg",
"man": "assets/eyes/eye_man.svg",
"circle": "assets/eyes/eye_circle.svg",
"squid": "assets/eyes/eye_squid.svg",
"sad": "assets/eyes/eye_sad.svg",
"angry": "assets/eyes/eye_angry.svg",
"nano": "assets/eyes/eye_nano.svg",
};
var eyes = {
"simple": { img: null, flip: true },
"man": { img: null, flip: true },
"circle": { img: null, flip: true },
"squid": { img: null, flip: true },
"sad": { img: null, flip: true },
"angry": { img: null, flip: true },
"nano": { img: null, flip: false },
};
function loadEyes() {
for (var eye in eyePaths) {
var img = new Image();
img.src = eyePaths[eye];
eyes[eye].img = img;
}
}
loadEyes();
// Mouths
const mouthPaths = {
"uwu": "assets/mouths/mouth_uwu.svg",
"man": "assets/mouths/mouth_man.svg",
"tongue": "assets/mouths/mouth_tongue.svg",
"squid": "assets/mouths/mouth_squid.svg",
"meh": "assets/mouths/mouth_meh.svg",
"blep": "assets/mouths/mouth_blep.svg",
"jagged": "assets/mouths/mouth_jagged.svg",
};
var mouths = {};
function loadMouths() {
for (var mouth in mouthPaths) {
var img = new Image();
img.src = mouthPaths[mouth];
mouths[mouth] = img;
}
}
loadMouths();
// Accessories
const accessoryPaths = {
"glasses": "assets/accessories/acc_glasses.svg"
};
var accessories = {
"glasses": {
"img": null,
"x": "cw/2",
"y": "ch/3",
"w": "1.05*(ch+eyegap)",
"h": "1.05*(ch+eyegap)"
}
};
function loadAccessories() {
for (var accessory in accessoryPaths) {
var img = new Image();
img.src = accessoryPaths[accessory];
accessories[accessory].img = img;
}
}
loadAccessories();
// Info
var careimarinInfo = {
eye: {
typeLeft: "simple",
typeRight: "simple",
gap: 50,
colour: "#00ff00",
blinkLength: 0.2
},
screen: {
colour: "#002010"
},
mouth: {
type: "uwu",
colour: "#00ff00"
},
accessory: {
type: "glasses",
colour: "#00ff00"
}
};
// Drawing Utilities
function drawImageColour(ctx, img, x, y, width, height, colour, drawToCanvas = true) {
processingCtx.clearRect(0, 0, processingCanvas.width, processingCanvas.height);
processingCanvas.width = width;
processingCanvas.height = height;
processingCtx.fillStyle = colour;
processingCtx.fillRect(0, 0, width, height);
processingCtx.globalCompositeOperation = "destination-in";
processingCtx.drawImage(img, 0, 0, width, height);
processingCtx.globalCompositeOperation = "source-over";
if (drawToCanvas) {
ctx.drawImage(processingCanvas, x, y, width, height);
}
}
function drawImageScale(ctx, img, x, y, width, height, scale = [1, 1], scaleCenter = [0.5, 0.5], drawToCanvas = true) {
let scaledCtx
if (drawToCanvas) {
scaledCtx = ctx;
} else {
scaledCtx = processingCtx;
scaledCtx.clearRect(0, 0, processingCanvas.width, processingCanvas.height);
}
scaledCtx.save(); // Save the current state
scaledCtx.translate(x + width * scaleCenter[0], y + height * scaleCenter[1]); // Set translate to set the scale center
scaledCtx.scale(scale[0], scale[1]); // Set scale to flip the image
scaledCtx.drawImage(img, -width * scaleCenter[0] * scale[0], -height * scaleCenter[1] * scale[1], width * scale[0], height * scale[1]); // draw the image with correct positioning
if (debug && debugMode == 3) {
scaledCtx.strokeStyle = "blue";
scaledCtx.strokeRect(-width * scaleCenter[0], -height * scaleCenter[1], width, height);
ctx.beginPath();
ctx.moveTo(-width * scaleCenter[0], -height * scaleCenter[1]);
ctx.lineTo(-width * scaleCenter[0] + width, -height * scaleCenter[1] + height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-width * scaleCenter[0] + width, -height * scaleCenter[1]);
ctx.lineTo(-width * scaleCenter[0], -height * scaleCenter[1] + height);
ctx.stroke();
}
scaledCtx.restore(); // Restore the last saved state
}
function drawImage(ctx, img, x, y, width, height, colour = false, scale = [1, 1], scaleCenter = [0.5, 0.5]) {
var basicArgs = [x, y, width, height];
if (colour) {
drawImageColour(ctx, img, ...basicArgs, colour, false);
drawImageScale(ctx, processingCanvas, ...basicArgs, scale, scaleCenter);
} else {
drawImageScale(ctx, img, ...basicArgs, scale, scaleCenter);
}
if (debug && debugMode == 2) {
ctx.strokeStyle = "red";
ctx.strokeRect(x, y, width, height);
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + width, y + height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x + width, y);
ctx.lineTo(x, y + height);
ctx.stroke();
}
}
// Drawing
function draw() {
ctx.fillStyle = careimarinInfo.screen.colour;
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawEyes();
drawMouth();
//drawAccessories();
}
function drawEyes() {
if (eyes[careimarinInfo.eye.typeLeft].img && eyes[careimarinInfo.eye.typeRight].img) {
var imageLeft = eyes[careimarinInfo.eye.typeLeft].img;
var imageRight = eyes[careimarinInfo.eye.typeRight].img;
var eyeWidth = canvas.height / 2;
var eyeHeight = eyeWidth;
var leftEyeX = canvas.width / 2 - (eyeWidth + careimarinInfo.eye.gap / 2);
var rightEyeX = canvas.width / 2 + (careimarinInfo.eye.gap / 2);
var eyeY = canvas.height / 3 - eyeHeight / 2;
var blinkScale = blinking ? Math.abs(Math.cos(Math.PI * blinkFrames / careimarinInfo.eye.blinkLength)) : 1;
//var lookOffsetXLeft = (mousePos[0] - (leftEyeX+eyeWidth/2))*0.02;
//var lookOffsetXRight = (mousePos[0] - (rightEyeX+eyeWidth/2))*0.02;
//var lookOffsetY = (mousePos[1] - eyeHeight)*0.02;
drawImage(ctx, imageLeft, leftEyeX, eyeY, eyeWidth, eyeHeight, colour = careimarinInfo.eye.colour, scale = [1, blinkScale], scaleCenter = [0.5, 0.5]);
drawImage(ctx, imageRight, rightEyeX, eyeY, eyeWidth, eyeHeight, colour = careimarinInfo.eye.colour, scale = [eyes[careimarinInfo.eye.typeRight].flip ? -1 : 1, blinkScale], scaleCenter = [0.5, 0.5]);
}
}
function drawMouth() {
if (mouths[careimarinInfo.mouth.type]) {
var mouth = mouths[careimarinInfo.mouth.type];
var mouthWidth = canvas.height / 2;
var mouthHeight = mouthWidth;
var mouthX = canvas.width / 2 - (mouthWidth / 2);
var mouthY = canvas.height / 3 + mouthHeight / 2;
drawImage(ctx, mouth, mouthX, mouthY, mouthWidth, mouthHeight, colour = careimarinInfo.mouth.colour, scale = [1, 1 + (0.025 * Math.sin(timeElapsed))], scaleCenter = [1, 0.25]);
}
}
function drawAccessories() {
if (accessories[careimarinInfo.accessory.type].img) {
var accessoryInfo = accessories[careimarinInfo.accessory.type];
var scope = { cw: canvas.width, ch: canvas.height, eyegap: careimarinInfo.eye.gap };
var accessory = accessoryInfo.img;
var accessoryWidth = math.evaluate(accessoryInfo.w, scope);
var accessoryHeight = accessoryWidth;
var accessoryX = math.evaluate(accessoryInfo.x, scope) - (accessoryWidth / 2);
var accessoryY = math.evaluate(accessoryInfo.y, scope) - accessoryHeight / 2;
drawImage(ctx, accessory, accessoryX, accessoryY, accessoryWidth, accessoryHeight, colour = careimarinInfo.accessory.colour);
}
}
// Game Loop
var mousePos = [0, 0];
let lastTime = 0;
var timeElapsed = 0;
function gameLoop() {
//Delta time handling
const time = performance.now();
const deltaTime = (time - lastTime) / 1000;
lastTime = time;
timeElapsed += deltaTime;
//Blink handling
if (!blinking) {
timeSinceBlinked += deltaTime;
} else {
timeSinceBlinked = 0;
blinkFrames += deltaTime;
}
if (Math.random() < timeSinceBlinked / 180) {
blinking = true;
blinkFrames = 0;
}
if (blinkFrames > careimarinInfo.eye.blinkLength) {
blinking = false;
}
window.requestAnimationFrame(gameLoop);
draw();
debugHandler();
}
gameLoop();
document.addEventListener('mousemove', function(event) {
mousePos = [event.clientX, event.clientY];
});
// Debug
var debug = false;
var debugMode = 1;
function debugHandler() {
// Debug Mode 1
if (debug && debugMode == 1) {
canvas.style.display = 'none';
processingCanvas.style.display = 'block';
document.getElementById("debugText").style.display = 'block';
} else {
canvas.style.display = 'block';
processingCanvas.style.display = 'none';
document.getElementById("debugText").style.display = 'none';
}
// Debug Mode 2 is handled in drawImage()
// Debug Mode 3 is handled in drawImageScale()
}
document.addEventListener('keydown', function(event) {
if (event.key === '/') {
debug = !debug;
} else if (event.key === '1') {
debugMode = 1;
} else if (event.key === '2') {
debugMode = 2;
} else if (event.key === '3') {
debugMode = 3;
} else if (event.key == 'r') {
// Randomize Eyes and Mouth
careimarinInfo.eye.typeLeft = Object.keys(eyePaths)[Math.floor(Math.random() * Object.keys(eyePaths).length)];
careimarinInfo.eye.typeRight = careimarinInfo.eye.typeLeft;
careimarinInfo.mouth.type = Object.keys(mouthPaths)[Math.floor(Math.random() * Object.keys(mouthPaths).length)];
careimarinInfo.screen.colour = randomHexColour(0, 50);
var brightColour = randomHexColour(175, 255);
careimarinInfo.eye.colour = brightColour;
careimarinInfo.mouth.colour = brightColour;
careimarinInfo.accessory.colour = brightColour;
}
});
function randomHexColour(minDimness = 0, maxDimness = 255) {
var r = Math.floor(Math.random() * (maxDimness - minDimness + 1)) + minDimness;
var g = Math.floor(Math.random() * (maxDimness - minDimness + 1)) + minDimness;
var b = Math.floor(Math.random() * (maxDimness - minDimness + 1)) + minDimness;
return "#" + r.toString(16).padStart(2, '0') + g.toString(16).padStart(2, '0') + b.toString(16).padStart(2, '0');
}