forked from pupilfirst/wd101-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.js
77 lines (66 loc) · 1.8 KB
/
final.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
var c = document.getElementById("my-canvas");
var ctx = c.getContext("2d");
let drawCircle = (
centerX,
centerY,
radius,
startAngle,
endAngle,
fillColor
) => {
ctx.beginPath();
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
ctx.stroke();
ctx.fillStyle = fillColor;
ctx.fill();
};
drawCircle(250, 250, 150, 0, 2 * Math.PI, "transparent");
let drawLine = (startX, startY, endX, endY) => {
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.stroke();
};
// Eyes
let drawEyes = (radius, color) => {
// Left eye
drawCircle(300, 200, radius, 0, 2 * Math.PI, color);
// Right eye
drawCircle(200, 200, radius, 0, 2 * Math.PI, color);
};
// Nose
let drawNose = (type) => {
// Nose type is either "crooked", or "button".
if (type === "crooked") {
drawLine(250, 225, 200, 275);
drawLine(200, 275, 250, 275);
} else {
drawCircle(250, 250, 20, 0, Math.PI, "transparent");
drawLine(230, 250, 250, 225);
drawLine(270, 250, 250, 225);
}
};
// Mouth
let drawMouth = (expression) => {
// Expression is either an 'surprise', 'happy' & 'flat'.
if (expression === "surprise") {
drawCircle(250, 325, 40, 0, 2 * Math.PI, "transparent");
} else if (expression === "happy") {
drawCircle(250, 300, 40, 0, Math.PI, "transparent");
} else {
drawLine(200, 325, 300, 325);
}
};
let getRndInteger = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
let drawRandomFace = () => {
let eyeRadius = getRndInteger(1, 20);
let eyeColor = ["brown", "blue", "green"][getRndInteger(0, 2)];
let noseType = ["crooked", "button"][getRndInteger(0, 1)];
let expression = ["surprise", "happy", "flat"][getRndInteger(0, 2)];
drawEyes(eyeRadius, eyeColor);
drawNose(noseType);
drawMouth(expression);
};
drawRandomFace();