@@ -34,14 +34,8 @@ ctx.stroke();
34
34
35
35
// Draw on the canvas
36
36
function draw ( { key } ) {
37
- if ( rainbowMode ) {
38
- // Increment the hue
39
- hue += 10 ;
40
- ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)` ;
41
- }
42
- else {
43
- ctx . strokeStyle = DEFAULT_STROKE_COLOR ;
44
- }
37
+ setStrokeColor ( ) ;
38
+
45
39
// Start the path
46
40
ctx . beginPath ( ) ;
47
41
ctx . moveTo ( x , y ) ;
@@ -84,6 +78,29 @@ function draw({ key }) {
84
78
ctx . stroke ( ) ;
85
79
}
86
80
81
+ // Draw on the canvas with the mouse cursor
82
+ function drawByCursor ( { x, y} ) {
83
+ setStrokeColor ( ) ;
84
+
85
+ // Start the path
86
+ ctx . beginPath ( ) ;
87
+ ctx . moveTo ( x , y ) ;
88
+ ctx . lineTo ( x , y ) ;
89
+ ctx . closePath ( ) ;
90
+ ctx . stroke ( ) ;
91
+ }
92
+
93
+ function setStrokeColor ( ) {
94
+ if ( rainbowMode ) {
95
+ // Increment the hue
96
+ hue += 10 ;
97
+ ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)` ;
98
+ }
99
+ else {
100
+ ctx . strokeStyle = DEFAULT_STROKE_COLOR ;
101
+ }
102
+ }
103
+
87
104
// Used to shrink the line dot after enlarging it with the slider
88
105
function vigorousStroke ( ) {
89
106
ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ; ctx . stroke ( ) ;
@@ -192,16 +209,6 @@ function rotateKnob(side, direction) {
192
209
) ;
193
210
}
194
211
195
- // Listen for arrow keys
196
- window . addEventListener ( 'keydown' , handleKey ) ;
197
- shakebutton . addEventListener ( 'click' , clearCanvas ) ;
198
-
199
- // Listen for changes to the line width slider
200
- slider . addEventListener ( 'input' , handleSlider ) ;
201
-
202
- // List for click on rainbow button
203
- rainbowButton . addEventListener ( 'click' , handleRainbowButton ) ;
204
-
205
212
function drawWes ( ) {
206
213
const image = new Image ( 800 , 800 ) ; // Stretching a 400x file out
207
214
image . src = 'wes-a-sketch.png' ; // to 800x800
@@ -211,4 +218,34 @@ function drawWes() {
211
218
212
219
// Draw when image has loaded
213
220
image . onload = ( ) => ctx . drawImage ( image , dx , dy , image . width , image . height ) ;
214
- }
221
+ }
222
+
223
+ function getMousePos ( canvas , e ) {
224
+ var rect = canvas . getBoundingClientRect ( ) , // abs. size of element
225
+ scaleX = canvas . width / rect . width , // relationship bitmap vs. element for X
226
+ scaleY = canvas . height / rect . height ; // relationship bitmap vs. element for Y
227
+
228
+ return {
229
+ x : ( e . clientX - rect . left ) * scaleX , // scale mouse coordinates after they have
230
+ y : ( e . clientY - rect . top ) * scaleY // been adjusted to be relative to element
231
+ }
232
+ }
233
+
234
+ function handleMouseMovement ( e ) {
235
+ var mousePos = getMousePos ( canvas , e ) ;
236
+ drawByCursor ( mousePos ) ;
237
+ }
238
+
239
+
240
+ // Listen for arrow keys
241
+ window . addEventListener ( 'keydown' , handleKey ) ;
242
+ shakebutton . addEventListener ( 'click' , clearCanvas ) ;
243
+
244
+ // Listen for changes to the line width slider
245
+ slider . addEventListener ( 'input' , handleSlider ) ;
246
+
247
+ // Listen for click on rainbow button
248
+ rainbowButton . addEventListener ( 'click' , handleRainbowButton ) ;
249
+
250
+ // Listen for mouse movement on canvas
251
+ canvas . addEventListener ( 'mousemove' , handleMouseMovement ) ;
0 commit comments