-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
47 lines (39 loc) · 1.13 KB
/
client.js
File metadata and controls
47 lines (39 loc) · 1.13 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
// client.js
const apiUrl = 'http://localhost:3000/emotion-color';
async function getColorForEmotion(emotion) {
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ emotion }),
});
const data = await response.json();
return data.color;
} catch (err) {
console.error('Error fetching color:', err);
return '#FFFFFF'; // Default color if there's an error
}
}
const sketch = (p) => {
let colorToShow = '#FFFFFF';
p.setup = function () {
p.createCanvas(400, 400);
const emotionInput = document.getElementById('emotion-input');
emotionInput.addEventListener('keydown', async function (event) {
if (event.key === 'Enter') {
const emotion = emotionInput.value;
const color = await getColorForEmotion(emotion);
colorToShow = color;
}
});
};
p.draw = function () {
p.background(p.color(colorToShow));
p.fill(0);
p.textSize(20);
p.text('Emotion Color Display', 50, 50);
};
};
new p5(sketch);