Skip to content

Commit 44e6bf1

Browse files
committed
Implemented drawing via mouse
1 parent f130ab8 commit 44e6bf1

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

33 - Etch-a-Sketch/etch-a-sketch.js

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ ctx.stroke();
3434

3535
// Draw on the canvas
3636
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+
4539
// Start the path
4640
ctx.beginPath();
4741
ctx.moveTo(x, y);
@@ -84,6 +78,29 @@ function draw({ key }) {
8478
ctx.stroke();
8579
}
8680

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+
87104
// Used to shrink the line dot after enlarging it with the slider
88105
function vigorousStroke() {
89106
ctx.stroke();ctx.stroke();ctx.stroke();ctx.stroke();ctx.stroke();ctx.stroke();ctx.stroke();ctx.stroke();
@@ -192,16 +209,6 @@ function rotateKnob(side, direction) {
192209
);
193210
}
194211

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-
205212
function drawWes() {
206213
const image = new Image(800, 800); // Stretching a 400x file out
207214
image.src = 'wes-a-sketch.png'; // to 800x800
@@ -211,4 +218,34 @@ function drawWes() {
211218

212219
// Draw when image has loaded
213220
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

Comments
 (0)