-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathface.js
136 lines (115 loc) · 2.4 KB
/
face.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
'use strict';
let state = "unknown";
let graphics;
let matrix;
let render;
let isBlinking = false;
export const FACE_TYPES = {
UNKOWN: "unknown",
SMILE: "smile",
FROWN: "frown",
NEUTRAL: "neutral"
};
export function init(context, renderFunction) {
graphics = context;
render = renderFunction;
startAnimations();
}
function startAnimations() {
setInterval(renderFace, 1000 / 16);
setTimeout(blink, nextBlinkTime());
}
function blink() {
isBlinking = true;
setTimeout(openEyes, eyeCloseTime());
}
function openEyes() {
isBlinking = false;
setTimeout(blink, nextBlinkTime());
}
function nextBlinkTime() {
if (Math.floor(Math.random() * 3) === 1) {
return eyeCloseTime();
} else {
return 5000 + ((Math.random() - 0.5) * 2000);
}
}
function eyeCloseTime() {
return 250 + ((Math.random() - 0.5) * 200);
}
function eyes() {
//eyes
if (!isBlinking) {
graphics.setPixel(2, 2);
graphics.setPixel(5, 2);
}
}
function smile() {
//mouth
graphics.drawLine(2, 4, 4, 5);
graphics.drawLine(5, 4, 3, 5);
}
function frown() {
//mouth
graphics.drawLine(3, 4, 2, 5);
graphics.drawLine(4, 4, 5, 5);
}
function neutralMouth() {
//mouth
graphics.drawLine(2, 4, 5, 4);
}
function roundFace() {
//face top
graphics.drawLine(0, 2, 2, 0);
graphics.drawLine(5, 0, 7, 2);
graphics.drawLine(3, 0, 5, 0);
//face left
graphics.drawLine(0, 2, 0, 4);
//face right
graphics.drawLine(7, 2, 7, 4);
//face bottom
graphics.drawLine(0, 5, 2, 7);
graphics.drawLine(5, 7, 7, 5);
graphics.drawLine(3, 7, 5, 7);
}
function smilyFace() {
graphics.clear();
roundFace();
smile();
eyes();
}
function frownyFace() {
graphics.clear();
roundFace();
frown();
eyes();
}
function neutralFace() {
graphics.clear();
roundFace();
neutralMouth();
eyes();
}
function renderFace() {
switch (state) {
case FACE_TYPES.UNKOWN:
roundFace();
break;
case FACE_TYPES.SMILE:
smilyFace();
break;
case FACE_TYPES.FROWN:
frownyFace();
break;
case FACE_TYPES.NEUTRAL:
neutralFace();
break;
default:
smilyFace();
break;
}
render();
}
export function setState(newState) {
state = newState;
}