@@ -63,53 +63,122 @@ window.addEventListener("load", () => {
6363 }
6464
6565 ctx .strokeStyle = " #000000" ;
66+ ctx .fillStyle = " #000000" ;
6667 ctx .lineWidth = 10 ;
6768 ctx .lineCap = " round" ;
69+ ctx .lineJoin = " round" ;
70+ var posX;
71+ var posY;
6872
6973 let shouldPaint = false ;
74+ let shouldDrawShape = false ;
7075
7176 var selector = document .getElementById (" selector" );
7277 var pencil = document .getElementById (" pencil" );
7378 var eraser = document .getElementById (" eraser" );
7479 var square = document .getElementById (" square" );
80+ var roundSquare = document .getElementById (" roundSquare" );
7581 var circle = document .getElementById (" circle" );
76- var magic = document .getElementById (" magic" );
7782 var text = document .getElementById (" text" );
7883
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 () {
8092 shouldPaint = true ;
8193 var mousePos = getMousePos (canvas, event );
8294 ctx .moveTo (mousePos .x , mousePos .y );
8395 ctx .beginPath ();
84- continueDrawing (event );
96+ continueLine (event );
8597 }
8698
87- function continueDrawing (event ) {
99+ function continueLine (event ) {
88100 if (shouldPaint) {
89101 var mousePos = getMousePos (canvas, event );
90102 ctx .lineTo (mousePos .x , mousePos .y );
91103 ctx .stroke ();
92104 }
93105 }
94106
95- function endDrawing () {
107+ function endLine () {
96108 shouldPaint = false ;
97109 }
98110
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+ }
101171
102- function drawing () {
172+ function drawingShapes () {
103173 haveColor = true ;
104174 if (ctx .strokeStyle == canvas .backgroundColor ) {
175+ ctx .fillStyle = pencilColor;
105176 ctx .strokeStyle = pencilColor;
106177 if (! pencilColor) {
178+ ctx .fillStyle = " #000000" ;
107179 ctx .strokeStyle = " #000000" ;
108180 }
109181 }
110- canvas .addEventListener (" mousedown" , startDrawing);
111- document .addEventListener (" mouseup" , endDrawing);
112- canvas .addEventListener (" mousemove" , continueDrawing);
113182 }
114183
115184 // Changing colors
@@ -118,6 +187,7 @@ window.addEventListener("load", () => {
118187 if (haveColor) {
119188 ctx .strokeStyle = this .style .backgroundColor ;
120189 pencilColor = this .style .backgroundColor ;
190+ ctx .fillStyle = this .style .backgroundColor ;
121191 }
122192 });
123193 });
@@ -130,21 +200,59 @@ window.addEventListener("load", () => {
130200 });
131201
132202 // 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);
135209 }
136210
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+ });
139220
140221 eraser .addEventListener (" click" , function () {
141- drawing ();
222+ removeTools ();
223+ drawingLines ();
142224 haveColor = false ;
143225 ctx .strokeStyle = canvas .backgroundColor ;
226+ canvas .addEventListener (" mousedown" , startLine);
227+ document .addEventListener (" mouseup" , endLine);
228+ canvas .addEventListener (" mousemove" , continueLine);
144229 });
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);
149257});
150258 </script >
0 commit comments