-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (103 loc) · 3.45 KB
/
script.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
(function () {
const canvaStickFigure = document.getElementById("stickFigure");
console.log("Canva Stick Figure", canvaStickFigure);
const ctx = canvaStickFigure.getContext("2d");
let color = "hsl(239, 100%, 50%)";
function randmColor() {
let color = Math.floor(Math.random() * 360);
return `hsl(${color}, 100%, 50%)`;
}
function drawStickyFigure(color) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvaStickFigure.width, canvaStickFigure.height);
/* ------------------------
Face
---------------------------*/
const faceX = 200;
const faceY = 100;
const faceRadius = 50;
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.arc(faceX, faceY, faceRadius, 0, Math.PI * 2);
ctx.stroke();
/* ---------------------------
Body
--------------------------- */
const bodyLength = 200;
const bodyLengthY = faceY + faceRadius + bodyLength;
ctx.beginPath();
// Me want to move to the center of the face
ctx.moveTo(faceX, faceY + faceRadius);
ctx.lineTo(faceX, bodyLengthY);
ctx.stroke();
ctx.closePath();
/* ---------------------------
LEGS
--------------------------- */
const legLength = 70;
ctx.beginPath();
ctx.moveTo(faceX, bodyLengthY);
ctx.lineTo(faceX - legLength, bodyLengthY + legLength);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(faceX, bodyLengthY);
ctx.lineTo(faceX + legLength, bodyLengthY + legLength);
ctx.stroke();
ctx.closePath();
/* ---------------------------
ARMS
--------------------------- */
const armsY = bodyLength / 2 + faceY;
ctx.beginPath();
ctx.moveTo(faceX, armsY);
ctx.lineTo(faceX - legLength, armsY - legLength);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(faceX, armsY);
ctx.lineTo(faceX + legLength, armsY - legLength);
ctx.stroke();
ctx.closePath();
}
/* Biger canva */
const canvas = document.getElementById("canvas");
console.log("Canvas", canvas);
const context = canvas.getContext("2d");
drawStickyFigure(color);
// draw (image, x, y)
let positionX = 0;
let positionY = 0;
context.drawImage(canvaStickFigure, positionX, positionY);
document.addEventListener("keydown", function (event) {
console.log("event: ", event);
switch (event.key) {
case "ArrowLeft":
// Left pressed
positionX--;
break;
case "ArrowRight":
// Right pressed
positionX++;
break;
case "ArrowUp":
// Up pressed
positionY--;
break;
case "ArrowDown":
// Down pressed
positionY++;
break;
case "Shift":
console.log("Shift pressed");
color = randmColor();
console.log("Color: ", color);
break;
}
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
drawStickyFigure(color);
context.drawImage(canvaStickFigure, positionX, positionY);
});
})();