-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (132 loc) · 5.08 KB
/
index.html
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drawing App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Welcome to the Drawing App!</h1>
<div class= "container">
<div class= "btn-container">
<button class= "btn btn-red" type="button"></button>
<button class= "btn btn-orange" type="button"></button>
<button class= "btn btn-yellow" type="button"></button>
<button class= "btn btn-green" type="button"></button>
<button class= "btn btn-blue" type="button"></button>
<button class= "btn btn-purple" type="button"></button>
<button class= "btn btn-pink" type="button"></button>
<button class= "btn btn-brown" type="button"></button>
<button class= "btn btn-grey" type="button"></button>
<button class= "btn btn-black" type="button"></button>
</div>
<canvas id="myCanvas" width="700" height="500" style="border:4px solid lime;
display: block;"/>
</div>
<div class="toolbar-bottom">
<div class="shapes">
<button class= "btn btn-circle" type="button"></button>
<button class= "btn btn-square" type="button"></button>
</div>
<div class="actions">
<button class= "btn-clear" type="button">Clear</button>
</div>
</div>
<script>
var shapeColor = "black";
var c = document.getElementById("myCanvas");
var circleBtn = document.getElementsByClassName("btn-circle")[0];
var squareBtn = document.getElementsByClassName("btn-square")[0];
var clearBtn = document.getElementsByClassName("btn-clear")[0];
var shapeToDraw = "";
var redBtn = document.getElementsByClassName("btn-red")[0];
var orangeBtn = document.getElementsByClassName("btn-orange")[0];
var yellowBtn = document.getElementsByClassName("btn-yellow")[0];
var greenBtn = document.getElementsByClassName("btn-green")[0];
var blueBtn = document.getElementsByClassName("btn-blue")[0];
var purpleBtn = document.getElementsByClassName("btn-purple")[0];
var pinkBtn = document.getElementsByClassName("btn-pink")[0];
var brownBtn = document.getElementsByClassName("btn-brown")[0];
var greyBtn = document.getElementsByClassName("btn-grey")[0];
var blackBtn = document.getElementsByClassName("btn-black")[0];
redBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
orangeBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
yellowBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
greenBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
blueBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
purpleBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
pinkBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
brownBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
greyBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
blackBtn.addEventListener("click",(evt)=> {
shapeColor = getComputedStyle(evt.target).backgroundColor
});
c.addEventListener("click",(evt)=>{
if (shapeToDraw === "circle"){
drawCircle(evt);
} else if (shapeToDraw === "square"){
drawSquare(evt);
} else {
alert("Choose a shape to draw with!")
}
});
circleBtn.addEventListener("click", () => {
shapeToDraw = "circle";
circleBtn.classList.add("chosenShape");
squareBtn.classList.remove("chosenShape");
});
squareBtn.addEventListener("click", () => {
shapeToDraw = "square";
squareBtn.classList.add("chosenShape");
circleBtn.classList.remove("chosenShape");
});
clearBtn.addEventListener("click", () => window.location.reload());
function drawCircle(evt){
const{x,y}=getMousePos(c,evt);
var ctx = c.getContext("2d");
ctx.strokeStyle = shapeColor;
ctx.beginPath();
ctx.arc(x,y,100,0,2*Math.PI);
ctx.stroke();
}
function drawSquare(evt){
const{x,y}=getMousePos(c,evt);
var ctx = c.getContext("2d");
ctx.strokeStyle = shapeColor;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(x+100,y);
ctx.lineTo(x+100,y+100);
ctx.lineTo(x,y+100);
ctx.lineTo(x,y);
ctx.stroke();
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
</script>
</body>
</html>