-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.js
More file actions
40 lines (37 loc) · 1.36 KB
/
8.js
File metadata and controls
40 lines (37 loc) · 1.36 KB
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
function letsDraw() {
const canvas = document.querySelector("#draw");
const context = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
context.strokeStyle = "red";
context.lineJoin = "round";
context.lineCap = "round";
context.lineWidth = 1;
context.globalCompositeOperation = "multiply";
let isDrawing = false;
let lastX = 0;
let lastY = 0;
let hue = 0;
let direction = true;
function draw(e) {
if (!isDrawing) return;
context.strokeStyle = `hsl(${hue}, 100%, 50%)`;
context.beginPath();
context.moveTo(lastX, lastY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
hue >= 360 ? (hue = 0) : hue++;
if (direction == true)
context.lineWidth > 30 ? (direction = false) : context.lineWidth++;
else context.lineWidth <= 1 ? (direction = true) : context.lineWidth--;
}
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("mousedown", (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener("mouseup", () => (isDrawing = false));
canvas.addEventListener("mouseout", () => (isDrawing = false));
}
window.addEventListener("DOMContentLoaded", letsDraw);