@@ -63,53 +63,122 @@ window.addEventListener("load", () => {
63
63
}
64
64
65
65
ctx .strokeStyle = " #000000" ;
66
+ ctx .fillStyle = " #000000" ;
66
67
ctx .lineWidth = 10 ;
67
68
ctx .lineCap = " round" ;
69
+ ctx .lineJoin = " round" ;
70
+ var posX;
71
+ var posY;
68
72
69
73
let shouldPaint = false ;
74
+ let shouldDrawShape = false ;
70
75
71
76
var selector = document .getElementById (" selector" );
72
77
var pencil = document .getElementById (" pencil" );
73
78
var eraser = document .getElementById (" eraser" );
74
79
var square = document .getElementById (" square" );
80
+ var roundSquare = document .getElementById (" roundSquare" );
75
81
var circle = document .getElementById (" circle" );
76
- var magic = document .getElementById (" magic" );
77
82
var text = document .getElementById (" text" );
78
83
79
- function startDrawing () {
84
+ // Allows canvas to be drawn on immediately when page is loaded
85
+ drawingLines ();
86
+ canvas .addEventListener (" mousedown" , startLine);
87
+ document .addEventListener (" mouseup" , endLine);
88
+ canvas .addEventListener (" mousemove" , continueLine);
89
+
90
+ // Pencil Drawing
91
+ function startLine () {
80
92
shouldPaint = true ;
81
93
var mousePos = getMousePos (canvas, event );
82
94
ctx .moveTo (mousePos .x , mousePos .y );
83
95
ctx .beginPath ();
84
- continueDrawing (event );
96
+ continueLine (event );
85
97
}
86
98
87
- function continueDrawing (event ) {
99
+ function continueLine (event ) {
88
100
if (shouldPaint) {
89
101
var mousePos = getMousePos (canvas, event );
90
102
ctx .lineTo (mousePos .x , mousePos .y );
91
103
ctx .stroke ();
92
104
}
93
105
}
94
106
95
- function endDrawing () {
107
+ function endLine () {
96
108
shouldPaint = false ;
97
109
}
98
110
99
- // Allows user to draw on canvas
100
- drawing ();
111
+ // Shapes
112
+ function startShape () {
113
+ shouldDrawShape = true ;
114
+ var mousePos = getMousePos (canvas, event );
115
+ ctx .moveTo (mousePos .x , mousePos .y );
116
+ posX = mousePos .x ;
117
+ posY = mousePos .y ;
118
+ ctx .beginPath ();
119
+ }
120
+
121
+ function drawRect (event ) {
122
+ if (shouldDrawShape) {
123
+ var mousePos = getMousePos (canvas, event );
124
+ ctx .fillRect (posX, posY, mousePos .x - posX, mousePos .y - posY);
125
+ }
126
+ }
127
+
128
+ function drawRoundRect (event ) {
129
+ if (shouldDrawShape) {
130
+ var mousePos = getMousePos (canvas, event );
131
+ ctx .lineJoin = " round" ;
132
+ ctx .strokeRect (posX, posY, mousePos .x - posX, mousePos .y - posY);
133
+ ctx .fillRect (posX, posY, mousePos .x - posX, mousePos .y - posY);
134
+ }
135
+ }
136
+
137
+ function drawEllipse (event ) {
138
+ if (shouldDrawShape) {
139
+ var mousePos = getMousePos (canvas, event );
140
+ var rh = (posY - mousePos .y ) / 2 ;
141
+ var rw = (posX - mousePos .x ) / 2 ;
142
+ ctx .beginPath ();
143
+ ctx .ellipse (
144
+ mousePos .x + rw,
145
+ mousePos .y + rh,
146
+ Math .abs (rw),
147
+ Math .abs (rh),
148
+ 0 ,
149
+ 0 ,
150
+ 2 * Math .PI
151
+ );
152
+ ctx .fill ();
153
+ }
154
+ }
155
+
156
+ function endShape () {
157
+ shouldDrawShape = false ;
158
+ }
159
+
160
+ // Allows user to draw lines or shapes on canvas
161
+
162
+ function drawingLines () {
163
+ haveColor = true ;
164
+ if (ctx .strokeStyle == canvas .backgroundColor ) {
165
+ ctx .strokeStyle = pencilColor;
166
+ if (! pencilColor) {
167
+ ctx .strokeStyle = " #000000" ;
168
+ }
169
+ }
170
+ }
101
171
102
- function drawing () {
172
+ function drawingShapes () {
103
173
haveColor = true ;
104
174
if (ctx .strokeStyle == canvas .backgroundColor ) {
175
+ ctx .fillStyle = pencilColor;
105
176
ctx .strokeStyle = pencilColor;
106
177
if (! pencilColor) {
178
+ ctx .fillStyle = " #000000" ;
107
179
ctx .strokeStyle = " #000000" ;
108
180
}
109
181
}
110
- canvas .addEventListener (" mousedown" , startDrawing);
111
- document .addEventListener (" mouseup" , endDrawing);
112
- canvas .addEventListener (" mousemove" , continueDrawing);
113
182
}
114
183
115
184
// Changing colors
@@ -118,6 +187,7 @@ window.addEventListener("load", () => {
118
187
if (haveColor) {
119
188
ctx .strokeStyle = this .style .backgroundColor ;
120
189
pencilColor = this .style .backgroundColor ;
190
+ ctx .fillStyle = this .style .backgroundColor ;
121
191
}
122
192
});
123
193
});
@@ -130,21 +200,59 @@ window.addEventListener("load", () => {
130
200
});
131
201
132
202
// Activates and deactivates buttons
133
- function removeDraw () {
134
- canvas .removeEventListener (" mousedown" , startDrawing);
203
+ function removeTools () {
204
+ canvas .removeEventListener (" mousedown" , startLine);
205
+ canvas .removeEventListener (" mousedown" , startShape);
206
+ canvas .removeEventListener (" mouseup" , drawRect);
207
+ canvas .removeEventListener (" mouseup" , drawRoundRect);
208
+ canvas .removeEventListener (" mouseup" , drawEllipse);
135
209
}
136
210
137
- selector .addEventListener (" click" , removeDraw);
138
- pencil .addEventListener (" click" , drawing);
211
+ selector .addEventListener (" click" , removeTools);
212
+
213
+ pencil .addEventListener (" click" , function () {
214
+ removeTools ();
215
+ drawingLines ();
216
+ canvas .addEventListener (" mousedown" , startLine);
217
+ document .addEventListener (" mouseup" , endLine);
218
+ canvas .addEventListener (" mousemove" , continueLine);
219
+ });
139
220
140
221
eraser .addEventListener (" click" , function () {
141
- drawing ();
222
+ removeTools ();
223
+ drawingLines ();
142
224
haveColor = false ;
143
225
ctx .strokeStyle = canvas .backgroundColor ;
226
+ canvas .addEventListener (" mousedown" , startLine);
227
+ document .addEventListener (" mouseup" , endLine);
228
+ canvas .addEventListener (" mousemove" , continueLine);
144
229
});
145
- square .addEventListener (" click" , removeDraw);
146
- circle .addEventListener (" click" , removeDraw);
147
- magic .addEventListener (" click" , removeDraw);
148
- text .addEventListener (" click" , removeDraw);
230
+
231
+ square .addEventListener (" click" , function () {
232
+ removeTools ();
233
+ drawingShapes ();
234
+ canvas .addEventListener (" mousedown" , startShape);
235
+ canvas .addEventListener (" mouseup" , drawRect);
236
+ document .addEventListener (" mouseup" , endShape);
237
+ });
238
+
239
+ roundSquare .addEventListener (" click" , function () {
240
+ removeTools ();
241
+ drawingShapes ();
242
+ canvas .addEventListener (" mousedown" , startShape);
243
+ canvas .addEventListener (" mouseup" , drawRoundRect);
244
+ document .addEventListener (" mouseup" , endShape);
245
+ console .log (" round" );
246
+ });
247
+
248
+ circle .addEventListener (" click" , function () {
249
+ removeTools ();
250
+ drawingShapes ();
251
+ canvas .addEventListener (" mousedown" , startShape);
252
+ canvas .addEventListener (" mouseup" , drawEllipse);
253
+ document .addEventListener (" mouseup" , endShape);
254
+ });
255
+
256
+ text .addEventListener (" click" , removeTools);
149
257
});
150
258
</script >
0 commit comments