-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS object
68 lines (55 loc) · 1.5 KB
/
JS object
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
function robot(headcolor, armcolor, legcolor, bodycolor) {
this.headcolor = headcolor;
this.armcolor = armcolor;
this.legcolor = legcolor;
this.bodycolor = bodycolor;
}
var color = {
head: "#ADD8E6",
body: "#ADD8E6",
legs: "#ADD8E6",
arms: "#ADD8E6",
getColor: function (head, body, arms, legs) {
this.head = head;
this.arms = arms;
this.legs = legs;
this.body = body;
}
};
function drawRobot(color) {
// head
ctx.fillStyle = color.head;
ctx.fillRect(100, 0, 110, 70);
// body
ctx.fillStyle = color.body;
ctx.fillRect(125, 70, 60, 90);
// legs
ctx.fillStyle = color.legs;
ctx.fillRect(125, 160, 15, 40);
ctx.fillRect(170, 160, 15, 40);
//arms
ctx.fillStyle = color.arms;
ctx.fillRect(75, 85, 50, 8);
ctx.fillRect(185, 85, 50, 8);
// Eyes
ctx.beginPath();
ctx.arc(130, 30, 10, 0, 2 * Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(170, 30, 10, 0, 2 * Math.PI);
ctx.stroke();
}
function setColors() {
color.getColor(document.colors.head.value, document.colors.body.value,
document.colors.arm.value, document.colors.leg.value);
drawRobot(color);
}
var btn = document.createElement("BUTTON");
btn.onclick = setColors;
document.body.appendChild(btn).appendChild(document.createTextNode("Set Colors"));
var c = document.createElement("CANVAS");
c.width = 300;
c.height = 300;
document.body.appendChild(c);
var ctx = c.getContext("2d");
drawRobot(color);